tickets_shared_draft_starts_controller.rb 1.9 KB

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