organizations_controller.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class OrganizationsController < ApplicationController
  3. prepend_before_action -> { authorize! }, except: %i[index show]
  4. prepend_before_action { authentication_check }
  5. =begin
  6. Format:
  7. JSON
  8. Example:
  9. {
  10. "id":1,
  11. "name":"Znuny GmbH",
  12. "note":"",
  13. "active":true,
  14. "shared":true,
  15. "updated_at":"2012-09-14T17:51:53Z",
  16. "created_at":"2012-09-14T17:51:53Z",
  17. "created_by_id":2,
  18. }
  19. =end
  20. =begin
  21. Resource:
  22. GET /api/v1/organizations
  23. Response:
  24. [
  25. {
  26. "id": 1,
  27. "name": "some_name1",
  28. ...
  29. },
  30. {
  31. "id": 2,
  32. "name": "some_name2",
  33. ...
  34. }
  35. ]
  36. Test:
  37. curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
  38. =end
  39. def index
  40. model_index_render(policy_scope(Organization), params)
  41. end
  42. =begin
  43. Resource:
  44. GET /api/v1/organizations/#{id}
  45. Response:
  46. {
  47. "id": 1,
  48. "name": "name_1",
  49. ...
  50. }
  51. Test:
  52. curl http://localhost/api/v1/organizations/#{id} -v -u #{login}:#{password}
  53. =end
  54. def show
  55. begin
  56. authorize!
  57. rescue Pundit::NotAuthorizedError
  58. # we have a special case here where Users that have no
  59. # organization can request any organization_id but get
  60. # an empty response. However, users with an organization_id
  61. # get that error
  62. raise if current_user.organization_id
  63. render json: {}
  64. return
  65. end
  66. if response_expand?
  67. organization = Organization.find(params[:id]).attributes_with_association_names
  68. render json: organization, status: :ok
  69. return
  70. end
  71. if response_full?
  72. full = Organization.full(params[:id])
  73. render json: full, status: :ok
  74. return
  75. end
  76. model_show_render(Organization, params)
  77. end
  78. =begin
  79. Resource:
  80. POST /api/v1/organizations
  81. Payload:
  82. {
  83. "name": "some_name",
  84. "active": true,
  85. "note": "some note",
  86. "shared": true
  87. }
  88. Response:
  89. {
  90. "id": 1,
  91. "name": "some_name",
  92. ...
  93. }
  94. Test:
  95. curl http://localhost/api/v1/organizations -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"name": "some_name","active": true,"shared": true,"note": "some note"}'
  96. =end
  97. def create
  98. model_create_render(Organization, params)
  99. end
  100. =begin
  101. Resource:
  102. PUT /api/v1/organizations/{id}
  103. Payload:
  104. {
  105. "id": 1
  106. "name": "some_name",
  107. "active": true,
  108. "note": "some note",
  109. "shared": true
  110. }
  111. Response:
  112. {
  113. "id": 1,
  114. "name": "some_name",
  115. ...
  116. }
  117. Test:
  118. curl http://localhost/api/v1/organizations -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"id": 1,"name": "some_name","active": true,"shared": true,"note": "some note"}'
  119. =end
  120. def update
  121. model_update_render(Organization, params)
  122. end
  123. =begin
  124. Resource:
  125. DELETE /api/v1/organization/{id}
  126. Response:
  127. {}
  128. Test:
  129. curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE -d '{}'
  130. =end
  131. def destroy
  132. model_references_check(Organization, params)
  133. model_destroy_render(Organization, params)
  134. end
  135. # GET /api/v1/organizations/search
  136. def search
  137. per_page = params[:per_page] || params[:limit] || 100
  138. per_page = per_page.to_i
  139. if per_page > 500
  140. per_page = 500
  141. end
  142. page = params[:page] || 1
  143. page = page.to_i
  144. offset = (page - 1) * per_page
  145. query = params[:query]
  146. if query.respond_to?(:permit!)
  147. query = query.permit!.to_h
  148. end
  149. query_params = {
  150. query: query,
  151. limit: per_page,
  152. offset: offset,
  153. sort_by: params[:sort_by],
  154. order_by: params[:order_by],
  155. current_user: current_user,
  156. }
  157. if params[:role_ids].present?
  158. query_params[:role_ids] = params[:role_ids]
  159. end
  160. # do query
  161. organization_all = Organization.search(query_params)
  162. if response_expand?
  163. list = []
  164. organization_all.each do |organization|
  165. list.push organization.attributes_with_association_names
  166. end
  167. render json: list, status: :ok
  168. return
  169. end
  170. # build result list
  171. if params[:label]
  172. organizations = []
  173. organization_all.each do |organization|
  174. a = { id: organization.id, label: organization.name, value: organization.name }
  175. organizations.push a
  176. end
  177. # return result
  178. render json: organizations
  179. return
  180. end
  181. if response_full?
  182. organization_ids = []
  183. assets = {}
  184. organization_all.each do |organization|
  185. assets = organization.assets(assets)
  186. organization_ids.push organization.id
  187. end
  188. # return result
  189. render json: {
  190. assets: assets,
  191. organization_ids: organization_ids.uniq,
  192. }
  193. return
  194. end
  195. list = []
  196. organization_all.each do |organization|
  197. list.push organization.attributes_with_association_ids
  198. end
  199. render json: list, status: :ok
  200. end
  201. # GET /api/v1/organizations/history/1
  202. def history
  203. # get organization data
  204. organization = Organization.find(params[:id])
  205. # get history of organization
  206. render json: organization.history_get(true)
  207. end
  208. # @path [GET] /organizations/import_example
  209. #
  210. # @summary Download of example CSV file.
  211. # @notes The requester have 'admin.organization' permissions to be able to download it.
  212. # @example curl -u 'me@example.com:test' http://localhost:3000/api/v1/organizations/import_example
  213. #
  214. # @response_message 200 File download.
  215. # @response_message 403 Forbidden / Invalid session.
  216. def import_example
  217. send_data(
  218. Organization.csv_example,
  219. filename: 'organization-example.csv',
  220. type: 'text/csv',
  221. disposition: 'attachment'
  222. )
  223. end
  224. # @path [POST] /organizations/import
  225. #
  226. # @summary Starts import.
  227. # @notes The requester have 'admin.text_module' permissions to be create a new import.
  228. # @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/organizations.csv' 'https://your.zammad/api/v1/organizations/import?try=true'
  229. # @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/organizations.csv' 'https://your.zammad/api/v1/organizations/import'
  230. #
  231. # @response_message 201 Import started.
  232. # @response_message 403 Forbidden / Invalid session.
  233. def import_start
  234. string = params[:data]
  235. if string.blank? && params[:file].present?
  236. string = params[:file].read.force_encoding('utf-8')
  237. end
  238. raise Exceptions::UnprocessableEntity, 'No source data submitted!' if string.blank?
  239. result = Organization.csv_import(
  240. string: string,
  241. parse_params: {
  242. col_sep: params[:col_sep] || ',',
  243. },
  244. try: params[:try],
  245. delete: params[:delete],
  246. )
  247. render json: result, status: :ok
  248. end
  249. end