2020-03-19 09:39:51 +00:00
|
|
|
class Ticket::ArticlePolicy < ApplicationPolicy
|
|
|
|
|
|
|
|
def show?
|
|
|
|
access?(__method__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create?
|
|
|
|
access?(__method__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update?
|
|
|
|
return false if !access?(__method__)
|
|
|
|
return true if user.permissions?(['ticket.agent', 'admin'])
|
|
|
|
|
|
|
|
not_authorized('ticket.agent or admin permission required')
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy?
|
2020-07-13 09:14:15 +00:00
|
|
|
return false if !access?('show?')
|
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
# agents can destroy articles of type 'note'
|
2020-07-13 09:14:15 +00:00
|
|
|
# which were created by themselves within the last x minutes
|
|
|
|
|
|
|
|
if !user.permissions?('ticket.agent')
|
2020-09-30 09:07:01 +00:00
|
|
|
return not_authorized('agent permission required')
|
2020-07-13 09:14:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if record.created_by_id != user.id
|
|
|
|
return not_authorized('you can only delete your own notes')
|
|
|
|
end
|
|
|
|
|
2020-11-19 16:01:07 +00:00
|
|
|
if record.type.communication? && !record.internal?
|
2020-07-13 09:14:15 +00:00
|
|
|
return not_authorized('communication articles cannot be deleted')
|
|
|
|
end
|
|
|
|
|
|
|
|
if deletable_timeframe? && record.created_at <= deletable_timeframe.ago
|
|
|
|
return not_authorized('note is too old to be deleted')
|
|
|
|
end
|
2020-03-19 09:39:51 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-13 20:26:09 +00:00
|
|
|
def deletable_timeframe_setting
|
|
|
|
Setting.get('ui_ticket_zoom_article_delete_timeframe')
|
|
|
|
end
|
|
|
|
|
|
|
|
def deletable_timeframe?
|
|
|
|
deletable_timeframe_setting&.positive?
|
|
|
|
end
|
|
|
|
|
|
|
|
def deletable_timeframe
|
|
|
|
deletable_timeframe_setting.seconds
|
|
|
|
end
|
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
def access?(query)
|
|
|
|
ticket = Ticket.lookup(id: record.ticket_id)
|
2020-10-29 14:43:14 +00:00
|
|
|
return false if record.internal == true && !TicketPolicy.new(user, ticket).agent_read_access?
|
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
Pundit.authorize(user, ticket, query)
|
|
|
|
end
|
|
|
|
end
|