1
  6   3ˆÀ�þI$«8àYŒd(HÐÙþÔƒdO·Ó¿a–Ü4ý(Ô¾R( 	ÅýÆCp)‹FÿÒ ß    3# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

class TicketPolicy < ApplicationPolicy

  def show?
    access?('read')
  end

  def create?
    return false if !ensure_group?

    access?('create')
  end

  def update?
    change_access?
  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

  def ensure_group?
    return true if record.group_id

    not_authorized Exceptions::UnprocessableContent.new __("The required value 'group_id' is missing.")
  end

  def follow_up?
    # This method is used to check if a follow-up is possible (mostly based on the configuration).
    # Agents are always allowed to reopen tickets, configuration does not matter.

    return update? if Ticket::StateType.lookup(id: record.state.state_type_id).name != 'closed' # check if the ticket state is already closed
    return true if agent_update_access?

    # Check follow_up_possible configuration, based on the group.
    return true if follow_up_possible? && update?

    not_authorized Exceptions::UnprocessableContent.new __('Cannot follow-up on a closed ticket. Please create a new ticket.')
  end

  def agent_read_access?
    agent_access?('read')
  end

  def agent_update_access?
    agent_access?('change')
  end

  def agent_create_access?
    agent_access?('create')
  end

  def create_mentions?
    return true if agent_read_access?

    not_authorized __('You have insufficient permissions to mention other users.')
  end

  private

  def follow_up_possible?
    case record.group.follow_up_possible
    when 'yes'
      true
    when 'new_ticket_after_certain_time'
      record.reopen_after_certain_time?
    when 'new_ticket'
      false
    end
  end

  def access?(access)
    return true if agent_access?(access)

    customer_access?
  end

  def change_access?
    # Update permission needs an special handling related to ticket.agent+ticket.customer
    # situation, because agenr read permission should win over the general customer permission.
    return true if agent_update_access?
    return false if agent_read_access?

    customer_access?
  end

  def agent_access?(access)
    return false if !user.permissions?('ticket.agent')

    user.group_access?(record.group.id, access)
  end

  def customer_access?
    return false if !user.permissions?('ticket.customer')
    return customer_field_scope if customer?

    shared_organization?
  end

  def customer?
    record.customer_id == user.id
  end

  def shared_organization?
    return false if record.organization_id.blank?
    return false if user.organization_id.blank?
    return false if !user.organization_id?(record.organization_id)
    return false if !record.organization.shared?

    customer_field_scope
  end

  def customer_field_scope
    @customer_field_scope ||= ApplicationPolicy::FieldScope.new(deny: %i[ai_agent_running ai_summary_enabled ai_stored_summary first_article last_article time_unit time_units_per_type accounted_time_type checklist referencing_checklist_tickets ai_stored_results])
  end
end
  6   5ˆl–Ãa¾”ªi?u8 ¸ÛW˜´o�þ]p«8àYŽ1
R9,ŸÏÖƒàÕË¿Ó n    5xÚ­VsÛDî0L�R†ážN-Un”!ja$YqTÒ6Í£¯ñ(òÙºD–„$'¸ÿþ&»w§·RÊ@&#¯nw¿}ïéÓÏß|æô>ùî¶2_…^Æ¢P�«[„\¸	9eáŒ<!gªdIW$¡Ù*	KFå(ÔÜ8ÖÊ’ªÄM«%³t “ü«€Ký3£u\ÏgÁL%±›€0˜'ó(!
zpN×„…›ÅwÓgIkžŠ`©(9Žôèw€/XüŒæˆ—E	FI2Ÿ¥š…i–¬ðt8 Jã»'Qeë˜hå‘.dk2!½”è9w:MW1M¦ÓN™´Ü¨È�®6¼ì_†øN“l­ß	#Žµ‘›ú§‘›Ì27KA´,·gnšŠBæ	Wê:*©r\BêZJ³Œ…‹ßÝ%­Vë’Í4ËÁIDo–kOSTŸöÈ²š‡VGç:›n{,=b);>Æ&ÁŠÂ9[hÀVÚ9©zŸƒÈ·O ÚÒ;â†ëOZ>Lµ4ð†+}ŽÑ¨¢½Š iòed­l”Ã”&ÿÉ~ÙSt¥Ô'­nfÿpþ:×+G¹ázd…öÝ»DQ@¼F§0 ÍÁ(<ù$\ùa5h¾%KÓÒzåd@¶ÉEÄfäþ 4¿ÉÉÍ@áñ42ZÌw=µ•¼q·¡/V1çs³8=¸è”n®(cáF£lÅxW×ˆÜ€¨WÛ�ôB©ó.K¢  ‰‰¾õ›Ã•ÐÓ,�¾z\ž›Îf+6Pg2®o¸NSyÃ©¤ŸJ‚ËÑàÃ½µ«�7¶Jèßò Z8…ý	—SI@áb
"¸ì öZ­z¢·òß4O2ö§�˜Z