2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-12-20 13:02:30 +00:00
|
|
|
|
|
|
|
class MacroPolicy < ApplicationPolicy
|
|
|
|
class Scope < ApplicationPolicy::Scope
|
|
|
|
|
|
|
|
def resolve
|
|
|
|
if user.permissions?('admin.macro')
|
|
|
|
scope.all
|
|
|
|
elsif user.permissions?('ticket.agent')
|
|
|
|
scope
|
|
|
|
.left_joins(:groups)
|
|
|
|
.group('macros.id')
|
|
|
|
.having(agent_having_groups)
|
|
|
|
else
|
|
|
|
scope.none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def agent_having_groups
|
|
|
|
base_query = 'SELECT Count(*) FROM groups_macros WHERE groups_macros.macro_id = macros.id'
|
|
|
|
|
|
|
|
having = "((#{base_query}) = 0)"
|
|
|
|
|
|
|
|
groups = user.groups.access(:change, :create)
|
|
|
|
|
|
|
|
if groups.any?
|
|
|
|
groups_matcher = groups.map(&:id).join(',')
|
|
|
|
having += " OR ((#{base_query} AND groups_macros.group_id IN (#{groups_matcher})) > 0)"
|
|
|
|
end
|
|
|
|
|
|
|
|
having
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|