ticket_articles_controller.rb 6.9 KB

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