organizations_controller.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright (C) 2012-2013 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. model_show_render(Organization, params)
  75. end
  76. =begin
  77. Resource:
  78. POST /api/v1/organizations.json
  79. Payload:
  80. {
  81. "name": "some_name",
  82. "active": true,
  83. "note": "some note",
  84. "shared": true
  85. }
  86. Response:
  87. {
  88. "id": 1,
  89. "name": "some_name",
  90. ...
  91. }
  92. Test:
  93. 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"}'
  94. =end
  95. def create
  96. return if deny_if_not_role('Agent')
  97. model_create_render(Organization, params)
  98. end
  99. =begin
  100. Resource:
  101. PUT /api/v1/organizations/{id}.json
  102. Payload:
  103. {
  104. "id": 1
  105. "name": "some_name",
  106. "active": true,
  107. "note": "some note",
  108. "shared": true
  109. }
  110. Response:
  111. {
  112. "id": 1,
  113. "name": "some_name",
  114. ...
  115. }
  116. Test:
  117. 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"}'
  118. =end
  119. def update
  120. return if deny_if_not_role('Agent')
  121. model_update_render(Organization, params)
  122. end
  123. =begin
  124. Resource:
  125. Response:
  126. Test:
  127. =end
  128. def destroy
  129. return if deny_if_not_role('Agent')
  130. model_destory_render(Organization, params)
  131. end
  132. end