ticket_articles_controller.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class TicketArticlesController < ApplicationController
  3. include CreatesTicketArticles
  4. include ClonesTicketArticleAttachments
  5. prepend_before_action -> { authorize! }, only: %i[index import_example import_start]
  6. prepend_before_action :authentication_check
  7. # GET /articles
  8. def index
  9. model_index_render(Ticket::Article, params)
  10. end
  11. # GET /articles/1
  12. def show
  13. article = Ticket::Article.find(params[:id])
  14. authorize!(article)
  15. if response_expand?
  16. result = article.attributes_with_association_names
  17. render json: result, status: :ok
  18. return
  19. end
  20. if response_full?
  21. full = Ticket::Article.full(params[:id])
  22. render json: full
  23. return
  24. end
  25. render json: article.attributes_with_association_names
  26. end
  27. # GET /ticket_articles/by_ticket/1
  28. def index_by_ticket
  29. ticket = Ticket.find(params[:id])
  30. authorize!(ticket, :show?)
  31. articles = []
  32. if response_expand?
  33. ticket.articles.each do |article|
  34. next if !authorized?(article, :show?)
  35. result = article.attributes_with_association_names
  36. articles.push result
  37. end
  38. render json: articles, status: :ok
  39. return
  40. end
  41. if response_full?
  42. assets = {}
  43. record_ids = []
  44. ticket.articles.each do |article|
  45. next if !authorized?(article, :show?)
  46. record_ids.push article.id
  47. assets = article.assets({})
  48. end
  49. render json: {
  50. record_ids: record_ids,
  51. assets: assets,
  52. }, status: :ok
  53. return
  54. end
  55. ticket.articles.each do |article|
  56. next if !authorized?(article, :show?)
  57. articles.push article.attributes_with_association_names
  58. end
  59. render json: articles, status: :ok
  60. end
  61. # POST /articles
  62. def create
  63. ticket = Ticket.find(params[:ticket_id])
  64. authorize!(ticket)
  65. article = article_create(ticket, params)
  66. if response_expand?
  67. result = article.attributes_with_association_names
  68. render json: result, status: :created
  69. return
  70. end
  71. if response_full?
  72. full = Ticket::Article.full(params[:id])
  73. render json: full, status: :created
  74. return
  75. end
  76. render json: article.attributes_with_association_names, status: :created
  77. end
  78. # PUT /articles/1
  79. def update
  80. article = Ticket::Article.find(params[:id])
  81. authorize!(article)
  82. clean_params = Ticket::Article.association_name_to_id_convert(params)
  83. clean_params = Ticket::Article.param_cleanup(clean_params, true)
  84. # only apply preferences changes (keep not updated keys/values)
  85. clean_params = article.param_preferences_merge(clean_params)
  86. article.update!(clean_params)
  87. if response_expand?
  88. result = article.attributes_with_association_names
  89. render json: result, status: :ok
  90. return
  91. end
  92. if response_full?
  93. full = Ticket::Article.full(params[:id])
  94. render json: full, status: :ok
  95. return
  96. end
  97. render json: article.attributes_with_association_names, status: :ok
  98. end
  99. # DELETE /api/v1/ticket_articles/:id
  100. def destroy
  101. article = Ticket::Article.find(params[:id])
  102. authorize!(article)
  103. article.destroy!
  104. render json: {}, status: :ok
  105. end
  106. # POST /ticket_attachment_upload_clone_by_article
  107. def ticket_attachment_upload_clone_by_article
  108. article = Ticket::Article.find(params[:article_id])
  109. authorize!(article.ticket, :show?)
  110. render json: {
  111. attachments: article_attachments_clone(article),
  112. }
  113. end
  114. # GET /ticket_attachment/:ticket_id/:article_id/:id
  115. def attachment
  116. ticket = Ticket.lookup(id: params[:ticket_id])
  117. authorize!(ticket, :show?)
  118. article = Ticket::Article.find(params[:article_id])
  119. if ticket.id != article.ticket_id
  120. # check if requested ticket got merged
  121. if ticket.state.state_type.name != 'merged'
  122. raise Exceptions::NotAuthorized, 'No access, article_id/ticket_id is not matching.'
  123. end
  124. ticket = article.ticket
  125. authorize!(ticket, :show?)
  126. end
  127. list = article.attachments || []
  128. access = false
  129. list.each do |item|
  130. if item.id.to_i == params[:id].to_i
  131. access = true
  132. end
  133. end
  134. raise Exceptions::NotAuthorized, 'Requested file id is not linked with article_id.' if !access
  135. # find file
  136. file = Store.find(params[:id])
  137. disposition = sanitized_disposition
  138. content = nil
  139. if params[:view].present? && file.preferences[:resizable] == true
  140. if file.preferences[:content_inline] == true && params[:view] == 'inline'
  141. content = file.content_inline
  142. elsif file.preferences[:content_preview] == true && params[:view] == 'preview'
  143. content = file.content_preview
  144. end
  145. end
  146. if content.blank?
  147. content = file.content
  148. end
  149. send_data(
  150. content,
  151. filename: file.filename,
  152. type: file.preferences['Content-Type'] || file.preferences['Mime-Type'] || 'application/octet-stream',
  153. disposition: disposition
  154. )
  155. end
  156. # GET /ticket_article_plain/1
  157. def article_plain
  158. article = Ticket::Article.find(params[:id])
  159. authorize!(article, :show?)
  160. file = article.as_raw
  161. # find file
  162. return if !file
  163. send_data(
  164. file.content,
  165. filename: file.filename,
  166. type: 'message/rfc822',
  167. disposition: 'inline'
  168. )
  169. end
  170. # @path [GET] /ticket_articles/import_example
  171. #
  172. # @summary Download of example CSV file.
  173. # @notes The requester have 'admin' permissions to be able to download it.
  174. # @example curl -u 'me@example.com:test' http://localhost:3000/api/v1/ticket_articles/import_example
  175. #
  176. # @response_message 200 File download.
  177. # @response_message 401 Invalid session.
  178. def import_example
  179. csv_string = Ticket::Article.csv_example(
  180. col_sep: ',',
  181. )
  182. send_data(
  183. csv_string,
  184. filename: 'example.csv',
  185. type: 'text/csv',
  186. disposition: 'attachment'
  187. )
  188. end
  189. # @path [POST] /ticket_articles/import
  190. #
  191. # @summary Starts import.
  192. # @notes The requester have 'admin' permissions to be create a new import.
  193. # @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/ticket_articles.csv' 'https://your.zammad/api/v1/ticket_articles/import?try=true'
  194. # @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/ticket_articles.csv' 'https://your.zammad/api/v1/ticket_articles/import'
  195. #
  196. # @response_message 201 Import started.
  197. # @response_message 401 Invalid session.
  198. def import_start
  199. if Setting.get('import_mode') != true
  200. raise 'Only can import tickets if system is in import mode.'
  201. end
  202. string = params[:data]
  203. if string.blank? && params[:file].present?
  204. string = params[:file].read.force_encoding('utf-8')
  205. end
  206. raise Exceptions::UnprocessableEntity, 'No source data submitted!' if string.blank?
  207. result = Ticket::Article.csv_import(
  208. string: string,
  209. parse_params: {
  210. col_sep: ';',
  211. },
  212. try: params[:try],
  213. )
  214. render json: result, status: :ok
  215. end
  216. private
  217. def sanitized_disposition
  218. disposition = params.fetch(:disposition, 'inline')
  219. valid_disposition = %w[inline attachment]
  220. return disposition if valid_disposition.include?(disposition)
  221. raise Exceptions::NotAuthorized, "Invalid disposition #{disposition} requested. Only #{valid_disposition.join(', ')} are valid."
  222. end
  223. end