organizations_controller.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class OrganizationsController < ApplicationController
  3. before_filter :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.json
  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.json -v -u #{login}:#{password}
  37. =end
  38. def index
  39. # only allow customer to fetch his own organization
  40. organizations = []
  41. if is_role('Customer') && !is_role('Admin') && !is_role('Agent')
  42. if current_user.organization_id
  43. organizations = Organization.where( :id => current_user.organization_id )
  44. end
  45. else
  46. organizations = Organization.all
  47. end
  48. render :json => organizations
  49. end
  50. =begin
  51. Resource:
  52. GET /api/v1/organizations/#{id}.json
  53. Response:
  54. {
  55. "id": 1,
  56. "name": "name_1",
  57. ...
  58. }
  59. Test:
  60. curl http://localhost/api/v1/organizations/#{id}.json -v -u #{login}:#{password}
  61. =end
  62. def show
  63. # only allow customer to fetch his own organization
  64. if is_role('Customer') && !is_role('Admin') && !is_role('Agent')
  65. if !current_user.organization_id
  66. render :json => {}
  67. return
  68. end
  69. if params[:id].to_i != current_user.organization_id
  70. response_access_deny
  71. return
  72. end
  73. end
  74. if params[:full]
  75. full = Organization.full( params[:id] )
  76. render :json => full
  77. return
  78. end
  79. model_show_render(Organization, params)
  80. end
  81. =begin
  82. Resource:
  83. POST /api/v1/organizations.json
  84. Payload:
  85. {
  86. "name": "some_name",
  87. "active": true,
  88. "note": "some note",
  89. "shared": true
  90. }
  91. Response:
  92. {
  93. "id": 1,
  94. "name": "some_name",
  95. ...
  96. }
  97. Test:
  98. curl http://localhost/api/v1/organizations.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"name": "some_name","active": true,"shared": true,"note": "some note"}'
  99. =end
  100. def create
  101. return if deny_if_not_role('Agent')
  102. model_create_render(Organization, params)
  103. end
  104. =begin
  105. Resource:
  106. PUT /api/v1/organizations/{id}.json
  107. Payload:
  108. {
  109. "id": 1
  110. "name": "some_name",
  111. "active": true,
  112. "note": "some note",
  113. "shared": true
  114. }
  115. Response:
  116. {
  117. "id": 1,
  118. "name": "some_name",
  119. ...
  120. }
  121. Test:
  122. curl http://localhost/api/v1/organizations.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"id": 1,"name": "some_name","active": true,"shared": true,"note": "some note"}'
  123. =end
  124. def update
  125. return if deny_if_not_role('Agent')
  126. model_update_render(Organization, params)
  127. end
  128. =begin
  129. Resource:
  130. Response:
  131. Test:
  132. =end
  133. def destroy
  134. return if deny_if_not_role('Agent')
  135. model_destory_render(Organization, params)
  136. end
  137. # GET /api/v1/organizations/history/1
  138. def history
  139. # permissin check
  140. if !is_role('Admin') && !is_role('Agent')
  141. response_access_deny
  142. return
  143. end
  144. # get organization data
  145. organization = Organization.find( params[:id] )
  146. # get history of organization
  147. history = organization.history_get(true)
  148. # return result
  149. render :json => history
  150. end
  151. end