2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
class TicketPolicy < ApplicationPolicy
|
|
|
|
|
|
|
|
def show?
|
|
|
|
access?('read')
|
|
|
|
end
|
|
|
|
|
|
|
|
def create?
|
2020-11-13 14:10:24 +00:00
|
|
|
ensure_group!
|
2020-03-19 09:39:51 +00:00
|
|
|
access?('create')
|
|
|
|
end
|
|
|
|
|
|
|
|
def update?
|
|
|
|
access?('change')
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy?
|
|
|
|
return true if user.permissions?('admin')
|
|
|
|
|
|
|
|
# This might look like a bug is actually just defining
|
|
|
|
# what exception is being raised and shown to the user.
|
|
|
|
return false if !access?('delete')
|
|
|
|
|
|
|
|
not_authorized('admin permission required')
|
|
|
|
end
|
|
|
|
|
|
|
|
def full?
|
|
|
|
access?('full')
|
|
|
|
end
|
|
|
|
|
2020-11-13 14:10:24 +00:00
|
|
|
def ensure_group!
|
|
|
|
return if record.group_id
|
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __("Group can't be blank")
|
2020-11-13 14:10:24 +00:00
|
|
|
end
|
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
def follow_up?
|
|
|
|
return true if user.permissions?('ticket.agent') # agents can always reopen tickets, regardless of group configuration
|
|
|
|
return true if record.group.follow_up_possible != 'new_ticket' # check if the setting for follow_up_possible is disabled
|
|
|
|
return true if record.state.name != 'closed' # check if the ticket state is already closed
|
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('Cannot follow-up on a closed ticket. Please create a new ticket.')
|
2020-03-19 09:39:51 +00:00
|
|
|
end
|
|
|
|
|
2020-10-29 14:43:14 +00:00
|
|
|
def agent_read_access?
|
|
|
|
agent_access?('read')
|
|
|
|
end
|
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def access?(access)
|
2020-09-08 12:51:34 +00:00
|
|
|
return true if agent_access?(access)
|
2020-03-19 09:39:51 +00:00
|
|
|
|
2020-09-08 12:51:34 +00:00
|
|
|
customer_access?
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent_access?(access)
|
|
|
|
return false if !user.permissions?('ticket.agent')
|
|
|
|
return true if owner?
|
|
|
|
|
|
|
|
user.group_access?(record.group.id, access)
|
|
|
|
end
|
2020-03-19 09:39:51 +00:00
|
|
|
|
2020-09-08 12:51:34 +00:00
|
|
|
def owner?
|
|
|
|
record.owner_id == user.id
|
|
|
|
end
|
2020-03-19 09:39:51 +00:00
|
|
|
|
2020-09-08 12:51:34 +00:00
|
|
|
def customer_access?
|
2020-08-20 07:10:08 +00:00
|
|
|
return false if !user.permissions?('ticket.customer')
|
2020-09-08 12:51:34 +00:00
|
|
|
return true if customer?
|
2020-03-19 09:39:51 +00:00
|
|
|
|
2020-09-08 12:51:34 +00:00
|
|
|
shared_organization?
|
|
|
|
end
|
2020-03-19 09:39:51 +00:00
|
|
|
|
2020-09-08 12:51:34 +00:00
|
|
|
def customer?
|
|
|
|
record.customer_id == user.id
|
2020-08-20 07:10:08 +00:00
|
|
|
end
|
2020-03-19 09:39:51 +00:00
|
|
|
|
2020-09-08 12:51:34 +00:00
|
|
|
def shared_organization?
|
2020-08-20 07:10:08 +00:00
|
|
|
return false if record.organization_id.blank?
|
|
|
|
return false if user.organization_id.blank?
|
|
|
|
return false if record.organization_id != user.organization_id
|
2020-03-19 09:39:51 +00:00
|
|
|
|
2020-08-20 07:10:08 +00:00
|
|
|
record.organization.shared?
|
2020-03-19 09:39:51 +00:00
|
|
|
end
|
|
|
|
end
|