organizations_controller.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class OrganizationsController < ApplicationController
  3. prepend_before_action :authentication_check
  4. =begin
  5. Format:
  6. JSON
  7. Example:
  8. {
  9. "id":1,
  10. "name":"Znuny GmbH",
  11. "note":"",
  12. "active":true,
  13. "shared":true,
  14. "updated_at":"2012-09-14T17:51:53Z",
  15. "created_at":"2012-09-14T17:51:53Z",
  16. "created_by_id":2,
  17. }
  18. =end
  19. =begin
  20. Resource:
  21. GET /api/v1/organizations
  22. Response:
  23. [
  24. {
  25. "id": 1,
  26. "name": "some_name1",
  27. ...
  28. },
  29. {
  30. "id": 2,
  31. "name": "some_name2",
  32. ...
  33. }
  34. ]
  35. Test:
  36. curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
  37. =end
  38. def index
  39. offset = 0
  40. per_page = 500
  41. if params[:page] && params[:per_page]
  42. offset = (params[:page].to_i - 1) * params[:per_page].to_i
  43. per_page = params[:per_page].to_i
  44. end
  45. if per_page > 500
  46. per_page = 500
  47. end
  48. # only allow customer to fetch his own organization
  49. organizations = []
  50. if !current_user.permissions?('admin.organization') && !current_user.permissions?('ticket.agent')
  51. if current_user.organization_id
  52. organizations = Organization.where(id: current_user.organization_id).order(id: 'ASC').offset(offset).limit(per_page)
  53. end
  54. else
  55. organizations = Organization.all.order(id: 'ASC').offset(offset).limit(per_page)
  56. end
  57. if params[:expand]
  58. list = []
  59. organizations.each { |organization|
  60. list.push organization.attributes_with_association_names
  61. }
  62. render json: list, status: :ok
  63. return
  64. end
  65. if params[:full]
  66. assets = {}
  67. item_ids = []
  68. organizations.each { |item|
  69. item_ids.push item.id
  70. assets = item.assets(assets)
  71. }
  72. render json: {
  73. record_ids: item_ids,
  74. assets: assets,
  75. }, status: :ok
  76. return
  77. end
  78. render json: organizations
  79. end
  80. =begin
  81. Resource:
  82. GET /api/v1/organizations/#{id}
  83. Response:
  84. {
  85. "id": 1,
  86. "name": "name_1",
  87. ...
  88. }
  89. Test:
  90. curl http://localhost/api/v1/organizations/#{id} -v -u #{login}:#{password}
  91. =end
  92. def show
  93. # only allow customer to fetch his own organization
  94. if !current_user.permissions?('admin.organization') && !current_user.permissions?('ticket.agent')
  95. if !current_user.organization_id
  96. render json: {}
  97. return
  98. end
  99. raise Exceptions::NotAuthorized if params[:id].to_i != current_user.organization_id
  100. end
  101. if params[:expand]
  102. organization = Organization.find(params[:id]).attributes_with_association_names
  103. render json: organization, status: :ok
  104. return
  105. end
  106. if params[:full]
  107. full = Organization.full(params[:id])
  108. render json: full
  109. return
  110. end
  111. model_show_render(Organization, params)
  112. end
  113. =begin
  114. Resource:
  115. POST /api/v1/organizations
  116. Payload:
  117. {
  118. "name": "some_name",
  119. "active": true,
  120. "note": "some note",
  121. "shared": true
  122. }
  123. Response:
  124. {
  125. "id": 1,
  126. "name": "some_name",
  127. ...
  128. }
  129. Test:
  130. 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"}'
  131. =end
  132. def create
  133. permission_check('ticket.agent')
  134. #permission_check('admin.organization')
  135. model_create_render(Organization, params)
  136. end
  137. =begin
  138. Resource:
  139. PUT /api/v1/organizations/{id}
  140. Payload:
  141. {
  142. "id": 1
  143. "name": "some_name",
  144. "active": true,
  145. "note": "some note",
  146. "shared": true
  147. }
  148. Response:
  149. {
  150. "id": 1,
  151. "name": "some_name",
  152. ...
  153. }
  154. Test:
  155. 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"}'
  156. =end
  157. def update
  158. permission_check('ticket.agent')
  159. model_update_render(Organization, params)
  160. end
  161. =begin
  162. Resource:
  163. DELETE /api/v1/organization/{id}
  164. Response:
  165. {}
  166. Test:
  167. curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE -d '{}'
  168. =end
  169. def destroy
  170. permission_check('ticket.agent')
  171. model_references_check(Organization, params)
  172. model_destroy_render(Organization, params)
  173. end
  174. # GET /api/v1/organizations/search
  175. def search
  176. if !current_user.permissions?('admin.organization') && !current_user.permissions?('ticket.agent')
  177. raise Exceptions::NotAuthorized
  178. end
  179. # set limit for pagination if needed
  180. if params[:page] && params[:per_page]
  181. params[:limit] = params[:page].to_i * params[:per_page].to_i
  182. end
  183. if params[:limit] && params[:limit].to_i > 500
  184. params[:limit].to_i = 500
  185. end
  186. query_params = {
  187. query: params[:query],
  188. limit: params[:limit],
  189. current_user: current_user,
  190. }
  191. if params[:role_ids] && !params[:role_ids].empty?
  192. query_params[:role_ids] = params[:role_ids]
  193. end
  194. # do query
  195. organization_all = Organization.search(query_params)
  196. # do pagination if needed
  197. if params[:page] && params[:per_page]
  198. offset = (params[:page].to_i - 1) * params[:per_page].to_i
  199. organization_all = organization_all.slice(offset, params[:per_page].to_i) || []
  200. end
  201. if params[:expand]
  202. list = []
  203. organization_all.each { |organization|
  204. list.push organization.attributes_with_association_names
  205. }
  206. render json: list, status: :ok
  207. return
  208. end
  209. # build result list
  210. if params[:label]
  211. organizations = []
  212. organization_all.each { |organization|
  213. a = { id: organization.id, label: organization.name, value: organization.name }
  214. organizations.push a
  215. }
  216. # return result
  217. render json: organizations
  218. return
  219. end
  220. if params[:full]
  221. organization_ids = []
  222. assets = {}
  223. organization_all.each { |organization|
  224. assets = organization.assets(assets)
  225. organization_ids.push organization.id
  226. }
  227. # return result
  228. render json: {
  229. assets: assets,
  230. organization_ids: organization_ids.uniq,
  231. }
  232. return
  233. end
  234. list = []
  235. organization_all.each { |organization|
  236. list.push organization.attributes
  237. }
  238. render json: list, status: :ok
  239. end
  240. # GET /api/v1/organizations/history/1
  241. def history
  242. # permission check
  243. if !current_user.permissions?('admin.organization') && !current_user.permissions?('ticket.agent')
  244. raise Exceptions::NotAuthorized
  245. end
  246. # get organization data
  247. organization = Organization.find(params[:id])
  248. # get history of organization
  249. history = organization.history_get(true)
  250. # return result
  251. render json: history
  252. end
  253. end