Skip to content

Commit

Permalink
ensured system user's community users get seeded with everyone, mod, …
Browse files Browse the repository at this point in the history
…and unrestricted abilities
  • Loading branch information
Oaphi committed Dec 16, 2024
1 parent 3122f7b commit c115bf7
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit c115bf7

Please sign in to comment.