settings_controller_policy.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Controllers::SettingsControllerPolicy < Controllers::ApplicationControllerPolicy
  3. default_permit!('admin.*')
  4. def show?
  5. user.permissions!('admin.*')
  6. authorized_for_setting?(:show?)
  7. rescue Exceptions::Forbidden => e
  8. not_authorized(e)
  9. end
  10. def update?
  11. updateable?
  12. end
  13. def update_image?
  14. updateable?
  15. end
  16. def reset?
  17. updateable?
  18. end
  19. private
  20. def setting
  21. @setting ||= Setting.lookup(id: record.params[:id])
  22. end
  23. def authorized_for_setting?(query)
  24. Pundit.authorize(user, setting, query)
  25. true
  26. rescue Pundit::NotAuthorizedError
  27. not_authorized("required #{setting.preferences[:permission].inspect}")
  28. end
  29. def updateable?
  30. return false if !user.permissions?('admin.*')
  31. return false if !authorized_for_setting?(:update?)
  32. service_enabled?
  33. end
  34. def service_enabled?
  35. return true if !Setting.get('system_online_service')
  36. return true if !setting.preferences[:online_service_disable]
  37. not_authorized('service disabled')
  38. end
  39. end