checks_user_attributes_by_current_user_permission.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. module ChecksUserAttributesByCurrentUserPermission
  3. extend ActiveSupport::Concern
  4. private
  5. def check_attributes_by_current_user_permission(params)
  6. authorize!
  7. # admins can do whatever they want
  8. return true if current_user.permissions?('admin.user')
  9. # regular agents are not allowed to set Groups and Roles
  10. suffixes = %w[_ids s]
  11. %w[Role Group].each do |model|
  12. suffixes.each do |suffix|
  13. attribute = "#{model.downcase}#{suffix}"
  14. values = params[attribute]
  15. next if values.nil?
  16. logger.warn "#{model} assignment is only allowed by admin! User with ID #{current_user.id} tried to assign #{values.inspect}"
  17. params.delete(attribute)
  18. end
  19. end
  20. # check for create requests and set
  21. # signup roles if no other roles are given
  22. return true if params[:id].present?
  23. return true if params[:role_ids]
  24. return true if params[:roles]
  25. params[:role_ids] = Role.signup_role_ids
  26. true
  27. end
  28. end