20220704024901_migrate_settings_to_user_roles.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # frozen_string_literal: true
  2. class MigrateSettingsToUserRoles < ActiveRecord::Migration[6.1]
  3. disable_ddl_transaction!
  4. class UserRole < ApplicationRecord; end
  5. def up
  6. owner_role = UserRole.find_by(name: 'Owner')
  7. admin_role = UserRole.find_by(name: 'Admin')
  8. moderator_role = UserRole.find_by(name: 'Moderator')
  9. everyone_role = UserRole.find_by(id: -99)
  10. min_invite_role = Setting.min_invite_role
  11. show_staff_badge = Setting.show_staff_badge
  12. if everyone_role
  13. everyone_role.permissions &= ~::UserRole::FLAGS[:invite_users] unless min_invite_role == 'user'
  14. everyone_role.save
  15. end
  16. if owner_role
  17. owner_role.highlighted = show_staff_badge
  18. owner_role.save
  19. end
  20. if admin_role
  21. admin_role.permissions |= ::UserRole::FLAGS[:invite_users] if %w(admin moderator).include?(min_invite_role)
  22. admin_role.highlighted = show_staff_badge
  23. admin_role.save
  24. end
  25. if moderator_role
  26. moderator_role.permissions |= ::UserRole::FLAGS[:invite_users] if %w(moderator).include?(min_invite_role)
  27. moderator_role.highlighted = show_staff_badge
  28. moderator_role.save
  29. end
  30. end
  31. def down; end
  32. end