scope.rb 858 B

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class MacroPolicy < ApplicationPolicy
  3. class Scope < ApplicationPolicy::Scope
  4. def resolve
  5. if user.permissions?('admin.macro')
  6. scope.all
  7. elsif user.permissions?('ticket.agent')
  8. scope
  9. .joins('LEFT OUTER JOIN groups_macros ON groups_macros.macro_id = macros.id')
  10. .distinct
  11. .where(agent_having_groups)
  12. else
  13. scope.none
  14. end
  15. end
  16. private
  17. def agent_having_groups
  18. no_assigned_groups = 'groups_macros.group_id IS NULL'
  19. groups = user.groups.access(:change, :create)
  20. if groups.any?
  21. groups_matcher = groups.map(&:id).join(',')
  22. return "#{no_assigned_groups} OR groups_macros.group_id IN (#{groups_matcher})"
  23. end
  24. no_assigned_groups
  25. end
  26. end
  27. end