organizations_controller.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class OrganizationsController < ApplicationController
  3. 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. # only allow customer to fetch his own organization
  46. organizations = []
  47. if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
  48. if current_user.organization_id
  49. organizations = Organization.where(id: current_user.organization_id).offset(offset).limit(per_page)
  50. end
  51. else
  52. organizations = Organization.all.offset(offset).limit(per_page)
  53. end
  54. if params[:expand]
  55. list = []
  56. organizations.each {|organization|
  57. list.push organization.attributes_with_relation_names
  58. }
  59. render json: list, status: :ok
  60. return
  61. end
  62. if params[:full]
  63. assets = {}
  64. item_ids = []
  65. organizations.each {|item|
  66. item_ids.push item.id
  67. assets = item.assets(assets)
  68. }
  69. render json: {
  70. record_ids: item_ids,
  71. assets: assets,
  72. }, status: :ok
  73. return
  74. end
  75. render json: organizations
  76. end
  77. =begin
  78. Resource:
  79. GET /api/v1/organizations/#{id}
  80. Response:
  81. {
  82. "id": 1,
  83. "name": "name_1",
  84. ...
  85. }
  86. Test:
  87. curl http://localhost/api/v1/organizations/#{id} -v -u #{login}:#{password}
  88. =end
  89. def show
  90. # only allow customer to fetch his own organization
  91. if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
  92. if !current_user.organization_id
  93. render json: {}
  94. return
  95. end
  96. raise Exceptions::NotAuthorized if params[:id].to_i != current_user.organization_id
  97. end
  98. if params[:expand]
  99. organization = Organization.find(params[:id]).attributes_with_relation_names
  100. render json: organization, status: :ok
  101. return
  102. end
  103. if params[:full]
  104. full = Organization.full(params[:id])
  105. render json: full
  106. return
  107. end
  108. model_show_render(Organization, params)
  109. end
  110. =begin
  111. Resource:
  112. POST /api/v1/organizations
  113. Payload:
  114. {
  115. "name": "some_name",
  116. "active": true,
  117. "note": "some note",
  118. "shared": true
  119. }
  120. Response:
  121. {
  122. "id": 1,
  123. "name": "some_name",
  124. ...
  125. }
  126. Test:
  127. 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"}'
  128. =end
  129. def create
  130. deny_if_not_role(Z_ROLENAME_AGENT)
  131. model_create_render(Organization, params)
  132. end
  133. =begin
  134. Resource:
  135. PUT /api/v1/organizations/{id}
  136. Payload:
  137. {
  138. "id": 1
  139. "name": "some_name",
  140. "active": true,
  141. "note": "some note",
  142. "shared": true
  143. }
  144. Response:
  145. {
  146. "id": 1,
  147. "name": "some_name",
  148. ...
  149. }
  150. Test:
  151. 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"}'
  152. =end
  153. def update
  154. deny_if_not_role(Z_ROLENAME_AGENT)
  155. model_update_render(Organization, params)
  156. end
  157. =begin
  158. Resource:
  159. DELETE /api/v1/organization/{id}
  160. Response:
  161. {}
  162. Test:
  163. curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE -d '{}'
  164. =end
  165. def destroy
  166. deny_if_not_role(Z_ROLENAME_AGENT)
  167. model_references_check(Organization, params)
  168. model_destory_render(Organization, params)
  169. end
  170. # GET /api/v1/organizations/search
  171. def search
  172. if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
  173. raise Exceptions::NotAuthorized
  174. end
  175. # set limit for pagination if needed
  176. if params[:page] && params[:per_page]
  177. params[:limit] = params[:page].to_i * params[:per_page].to_i
  178. end
  179. query_params = {
  180. query: params[:term],
  181. limit: params[:limit],
  182. current_user: current_user,
  183. }
  184. if params[:role_ids] && !params[:role_ids].empty?
  185. query_params[:role_ids] = params[:role_ids]
  186. end
  187. # do query
  188. organization_all = Organization.search(query_params)
  189. # do pagination if needed
  190. if params[:page] && params[:per_page]
  191. offset = (params[:page].to_i - 1) * params[:per_page].to_i
  192. organization_all = organization_all.slice(offset, params[:per_page].to_i) || []
  193. end
  194. if params[:expand]
  195. list = []
  196. organization_all.each {|organization|
  197. list.push organization.attributes_with_relation_names
  198. }
  199. render json: list, status: :ok
  200. return
  201. end
  202. # build result list
  203. if !params[:full]
  204. organizations = []
  205. organization_all.each { |organization|
  206. a = { id: organization.id, label: organization.name }
  207. organizations.push a
  208. }
  209. # return result
  210. render json: organizations
  211. return
  212. end
  213. organization_ids = []
  214. assets = {}
  215. organization_all.each { |organization|
  216. assets = organization.assets(assets)
  217. organization_ids.push organization.id
  218. }
  219. # return result
  220. render json: {
  221. assets: assets,
  222. organization_ids: organization_ids.uniq,
  223. }
  224. end
  225. # GET /api/v1/organizations/history/1
  226. def history
  227. # permission check
  228. if !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
  229. raise Exceptions::NotAuthorized
  230. end
  231. # get organization data
  232. organization = Organization.find(params[:id])
  233. # get history of organization
  234. history = organization.history_get(true)
  235. # return result
  236. render json: history
  237. end
  238. end