checks_user_attributes_by_current_user_permission.rb 1.1 KB

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