links_controller_policy.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Controllers::LinksControllerPolicy < Controllers::ApplicationControllerPolicy
  3. default_permit!('admin.tag')
  4. def add?
  5. object_target_update? && object_source_show?
  6. end
  7. def remove?
  8. object_target_update?
  9. end
  10. private
  11. def object_target_update?
  12. object_policy(record.params[:link_object_target], id: record.params[:link_object_target_value])
  13. .update?
  14. end
  15. def object_source_show?
  16. object_policy(record.params[:link_object_source], number: record.params[:link_object_source_number])
  17. .show?
  18. end
  19. def object_policy(object_name, id: nil, number: nil)
  20. case [object_name, id, number]
  21. in ['Ticket', id, nil]
  22. ticket_policy(:id, id)
  23. in ['Ticket', nil, number]
  24. ticket_policy(:number, number)
  25. in ['KnowledgeBase::Answer::Translation', *_]
  26. kb_answer_policy(id.presence || number.presence)
  27. end
  28. end
  29. def kb_answer_policy(id)
  30. answer_id = KnowledgeBase::Answer::Translation.find(id).answer_id
  31. answer = KnowledgeBase::Answer.find(answer_id)
  32. KnowledgeBase::AnswerPolicy.new(user, answer)
  33. end
  34. def ticket_policy(key, id)
  35. ticket = Ticket.find_by!(key => id)
  36. TicketPolicy.new(user, ticket)
  37. end
  38. end