20240423091057_add_2fa_permission.rb 806 B

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Add2faPermission < ActiveRecord::Migration[7.0]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. add_permission
  7. modify_roles
  8. end
  9. private
  10. def add_permission
  11. Permission.create_if_not_exists(
  12. name: 'user_preferences.two_factor_authentication',
  13. note: 'Change %s',
  14. preferences: {
  15. translations: ['Two-factor Authentication']
  16. },
  17. allow_signup: true,
  18. )
  19. end
  20. def modify_roles
  21. Role
  22. .joins(:permissions)
  23. .where(permissions: { name: 'user_preferences.password' })
  24. .each do |role|
  25. role.permission_grant('user_preferences.two_factor_authentication')
  26. end
  27. end
  28. end