ticket_articles_controller.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class TicketArticlesController < ApplicationController
  3. prepend_before_action :authentication_check
  4. # GET /articles
  5. def index
  6. permission_check('admin')
  7. model_index_render(Ticket::Article, params)
  8. end
  9. # GET /articles/1
  10. def show
  11. # permission check
  12. article = Ticket::Article.find(params[:id])
  13. article_permission(article)
  14. if params[:expand]
  15. result = article.attributes_with_association_names
  16. result[:attachments] = article.attachments
  17. render json: result, status: :ok
  18. return
  19. end
  20. if params[: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. # permission check
  30. ticket = Ticket.find(params[:id])
  31. ticket_permission(ticket)
  32. articles = []
  33. if params[:expand]
  34. ticket.articles.each { |article|
  35. # ignore internal article if customer is requesting
  36. next if article.internal == true && current_user.permissions?('ticket.customer')
  37. result = article.attributes_with_association_names
  38. # add attachments
  39. result[:attachments] = article.attachments
  40. articles.push result
  41. }
  42. render json: articles, status: :ok
  43. return
  44. end
  45. if params[:full]
  46. assets = {}
  47. record_ids = []
  48. ticket.articles.each { |article|
  49. # ignore internal article if customer is requesting
  50. next if article.internal == true && current_user.permissions?('ticket.customer')
  51. record_ids.push article.id
  52. assets = article.assets({})
  53. }
  54. render json: {
  55. record_ids: record_ids,
  56. assets: assets,
  57. }
  58. return
  59. end
  60. ticket.articles.each { |article|
  61. # ignore internal article if customer is requesting
  62. next if article.internal == true && current_user.permissions?('ticket.customer')
  63. articles.push article.attributes_with_association_names
  64. }
  65. render json: articles
  66. end
  67. # POST /articles
  68. def create
  69. ticket = Ticket.find(params[:ticket_id])
  70. ticket_permission(ticket)
  71. article = article_create(ticket, params)
  72. if params[:expand]
  73. result = article.attributes_with_association_names
  74. result[:attachments] = article.attachments
  75. render json: result, status: :created
  76. return
  77. end
  78. if params[:full]
  79. full = Ticket::Article.full(params[:id])
  80. render json: full, status: :created
  81. return
  82. end
  83. render json: article.attributes_with_association_names, status: :created
  84. end
  85. # PUT /articles/1
  86. def update
  87. # permission check
  88. article = Ticket::Article.find(params[:id])
  89. article_permission(article)
  90. if !current_user.permissions?('ticket.agent') && !current_user.permissions?('admin')
  91. raise Exceptions::NotAuthorized, 'Not authorized (ticket.agent or admin permission required)!'
  92. end
  93. clean_params = Ticket::Article.association_name_to_id_convert(params)
  94. clean_params = Ticket::Article.param_cleanup(clean_params, true)
  95. article.update_attributes!(clean_params)
  96. if params[:expand]
  97. result = article.attributes_with_association_names
  98. result[:attachments] = article.attachments
  99. render json: result, status: :ok
  100. return
  101. end
  102. if params[:full]
  103. full = Ticket::Article.full(params[:id])
  104. render json: full, status: :ok
  105. return
  106. end
  107. render json: article.attributes_with_association_names, status: :ok
  108. end
  109. # DELETE /articles/1
  110. def destroy
  111. article = Ticket::Article.find(params[:id])
  112. article_permission(article)
  113. if current_user.permissions?('admin')
  114. article.destroy!
  115. head :ok
  116. return
  117. end
  118. if current_user.permissions?('ticket.agent') && article.created_by_id == current_user.id && article.type.name == 'note'
  119. article.destroy!
  120. head :ok
  121. return
  122. end
  123. raise Exceptions::NotAuthorized, 'Not authorized (admin permission required)!'
  124. end
  125. # DELETE /ticket_attachment_upload
  126. def ticket_attachment_upload_delete
  127. if params[:store_id]
  128. Store.remove_item(params[:store_id])
  129. render json: {
  130. success: true,
  131. }
  132. return
  133. elsif params[:form_id]
  134. Store.remove(
  135. object: 'UploadCache',
  136. o_id: params[:form_id],
  137. )
  138. render json: {
  139. success: true,
  140. }
  141. return
  142. end
  143. render json: { message: 'No such store_id or form_id!' }, status: :unprocessable_entity
  144. end
  145. # POST /ticket_attachment_upload
  146. def ticket_attachment_upload_add
  147. # store file
  148. file = params[:File]
  149. content_type = file.content_type
  150. if !content_type || content_type == 'application/octet-stream'
  151. content_type = if MIME::Types.type_for(file.original_filename).first
  152. MIME::Types.type_for(file.original_filename).first.content_type
  153. else
  154. 'application/octet-stream'
  155. end
  156. end
  157. headers_store = {
  158. 'Content-Type' => content_type
  159. }
  160. store = Store.add(
  161. object: 'UploadCache',
  162. o_id: params[:form_id],
  163. data: file.read,
  164. filename: file.original_filename,
  165. preferences: headers_store
  166. )
  167. # return result
  168. render json: {
  169. success: true,
  170. data: {
  171. store_id: store.id,
  172. filename: file.original_filename,
  173. size: store.size,
  174. }
  175. }
  176. end
  177. # GET /ticket_attachment/:ticket_id/:article_id/:id
  178. def attachment
  179. # permission check
  180. ticket = Ticket.lookup(id: params[:ticket_id])
  181. if !ticket_permission(ticket)
  182. raise Exceptions::NotAuthorized, 'No such ticket.'
  183. end
  184. article = Ticket::Article.find(params[:article_id])
  185. if ticket.id != article.ticket_id
  186. raise Exceptions::NotAuthorized, 'No access, article_id/ticket_id is not matching.'
  187. end
  188. list = article.attachments || []
  189. access = false
  190. list.each { |item|
  191. if item.id.to_i == params[:id].to_i
  192. access = true
  193. end
  194. }
  195. raise Exceptions::NotAuthorized, 'Requested file id is not linked with article_id.' if !access
  196. # find file
  197. file = Store.find(params[:id])
  198. disposition = sanitized_disposition
  199. send_data(
  200. file.content,
  201. filename: file.filename,
  202. type: file.preferences['Content-Type'] || file.preferences['Mime-Type'],
  203. disposition: disposition
  204. )
  205. end
  206. # GET /ticket_article_plain/1
  207. def article_plain
  208. # permission check
  209. article = Ticket::Article.find(params[:id])
  210. article_permission(article)
  211. file = article.as_raw
  212. # find file
  213. return if !file
  214. send_data(
  215. file.content,
  216. filename: file.filename,
  217. type: 'message/rfc822',
  218. disposition: 'inline'
  219. )
  220. end
  221. private
  222. def sanitized_disposition
  223. disposition = params.fetch(:disposition, 'inline')
  224. valid_disposition = %w(inline attachment)
  225. return disposition if valid_disposition.include?(disposition)
  226. raise Exceptions::NotAuthorized, "Invalid disposition #{disposition} requested. Only #{valid_disposition.join(', ')} are valid."
  227. end
  228. end