ticket_articles_controller.rb 8.4 KB

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