tickets_shared_draft_starts_controller.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class TicketsSharedDraftStartsController < ApplicationController
  3. prepend_before_action :authorize!
  4. prepend_before_action :authentication_check
  5. def index
  6. drafts = scope
  7. render json: {
  8. shared_draft_ids: drafts.map(&:id),
  9. assets: ApplicationModel::CanAssets.reduce(drafts),
  10. }
  11. end
  12. def show
  13. object = scope.find params[:id]
  14. render json: {
  15. shared_draft_id: object.id,
  16. shared_draft_content: object.content,
  17. assets: object.assets,
  18. }
  19. end
  20. def create
  21. object = scope.create! safe_params
  22. object.attach_upload_cache params[:form_id]
  23. render json: {
  24. shared_draft_id: object.id,
  25. assets: object.assets,
  26. }
  27. end
  28. def update
  29. object = scope.find params[:id]
  30. object.update! safe_params
  31. object.attach_upload_cache params[:form_id]
  32. render json: {
  33. shared_draft_id: object.id,
  34. assets: object.assets,
  35. }
  36. end
  37. def destroy
  38. object = scope.find params[:id]
  39. object.destroy!
  40. render json: {
  41. shared_draft_id: object.id
  42. }
  43. end
  44. def import_attachments
  45. object = scope.find params[:id]
  46. new_attachments = object.clone_attachments 'UploadCache', params[:form_id]
  47. render json: {
  48. attachments: new_attachments
  49. }
  50. end
  51. private
  52. def scope
  53. Ticket::SharedDraftStartPolicy::Scope
  54. .new(current_user, Ticket::SharedDraftStart)
  55. .resolve
  56. end
  57. def safe_params
  58. safe_params = params.permit :name, :group_id, content: {}
  59. safe_params[:content].delete :group_id
  60. allowed_groups = current_user.groups_access('create').map(&:id).map(&:to_s)
  61. group_id = safe_params[:group_id]&.to_s
  62. if allowed_groups.exclude? group_id
  63. raise Exceptions::UnprocessableEntity, __("User does not have access to one of given group IDs: #{group_id}")
  64. end
  65. safe_params
  66. end
  67. end