20220704024901_migrate_settings_to_user_roles.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. class MigrateSettingsToUserRoles < ActiveRecord::Migration[6.1]
  3. disable_ddl_transaction!
  4. class UserRole < ApplicationRecord
  5. EVERYONE_ROLE_ID = -99
  6. end
  7. def up
  8. process_role_everyone
  9. process_role_owner
  10. process_role_admin
  11. process_role_moderator
  12. end
  13. def down; end
  14. private
  15. def process_role_everyone
  16. everyone_role = UserRole.find_by(id: UserRole::EVERYONE_ROLE_ID)
  17. return unless everyone_role
  18. everyone_role.permissions &= ~::UserRole::FLAGS[:invite_users] unless min_invite_role == 'user'
  19. everyone_role.save
  20. end
  21. def process_role_owner
  22. owner_role = UserRole.find_by(name: 'Owner')
  23. return unless owner_role
  24. owner_role.highlighted = show_staff_badge
  25. owner_role.save
  26. end
  27. def process_role_admin
  28. admin_role = UserRole.find_by(name: 'Admin')
  29. return unless admin_role
  30. admin_role.permissions |= ::UserRole::FLAGS[:invite_users] if %w(admin moderator).include?(min_invite_role)
  31. admin_role.highlighted = show_staff_badge
  32. admin_role.save
  33. end
  34. def process_role_moderator
  35. moderator_role = UserRole.find_by(name: 'Moderator')
  36. return unless moderator_role
  37. moderator_role.permissions |= ::UserRole::FLAGS[:invite_users] if %w(moderator).include?(min_invite_role)
  38. moderator_role.highlighted = show_staff_badge
  39. moderator_role.save
  40. end
  41. def min_invite_role
  42. Setting.min_invite_role
  43. end
  44. def show_staff_badge
  45. Setting.show_staff_badge
  46. end
  47. end