organizations_controller.rb 6.3 KB

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