From c115bf7d0b57f762324349bbc96d4450fe4a9dcd Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Mon, 16 Dec 2024 12:59:47 +0300 Subject: [PATCH] ensured system user's community users get seeded with everyone, mod, and unrestricted abilities --- db/seeds.rb | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 2b17968cd..11cb350a0 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -16,7 +16,7 @@ end # Prioritize the following models (in this order) such that models depending on them get created after -priority = [PostType, CloseReason, License, TagSet, PostHistoryType, User, CommunityUser, Filter] +priority = [PostType, CloseReason, License, TagSet, PostHistoryType, User, Ability, CommunityUser, Filter] sorted = files.zip(types).to_h.sort do |a, b| (priority.index(a.second) || 999) <=> (priority.index(b.second) || 999) end.to_h @@ -64,6 +64,27 @@ def create_objects(type, seed) [created, skipped] end +def ensure_system_user_abilities + system_users = CommunityUser.unscoped.where(user_id: -1) + + system_users.each do |su| + abilities = Ability.unscoped + .where(internal_id: ['everyone', 'mod', 'unrestricted']) + .where(community_id: su.community_id) + + user_abilities = UserAbility.unscoped.where(community_user_id: su.id) + + abilities.each do |ab| + unless user_abilities.any? { |ua| ua.ability_id == ab.id } + UserAbility.create community_user_id: su.id, ability: ab + end + rescue => e + puts "#{type}: failed to add \"#{ab.name}\" to system user \"#{su.id}\" on \"#{su.community.name}\"" + puts e + end + end +end + sorted.each do |f, type| begin processed = ERB.new(File.read(f)).result(binding) @@ -129,6 +150,10 @@ def create_objects(type, seed) new_created, new_skipped = create_objects(type, seed) created += new_created skipped += new_skipped + + if type == CommunityUser + ensure_system_user_abilities + end end end unless Rails.env.test?