2014-02-03 19:24:49 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
class OrganizationsController < ApplicationController
|
2015-05-07 11:23:55 +00:00
|
|
|
before_action :authentication_check
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Format:
|
|
|
|
JSON
|
|
|
|
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
"id":1,
|
|
|
|
"name":"Znuny GmbH",
|
|
|
|
"note":"",
|
|
|
|
"active":true,
|
|
|
|
"shared":true,
|
|
|
|
"updated_at":"2012-09-14T17:51:53Z",
|
|
|
|
"created_at":"2012-09-14T17:51:53Z",
|
|
|
|
"created_by_id":2,
|
|
|
|
}
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
Resource:
|
2016-06-06 06:34:15 +00:00
|
|
|
GET /api/v1/organizations
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
Response:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"name": "some_name1",
|
|
|
|
...
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"name": "some_name2",
|
|
|
|
...
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
Test:
|
2016-06-06 06:34:15 +00:00
|
|
|
curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
=end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
def index
|
2016-06-06 15:26:37 +00:00
|
|
|
offset = 0
|
|
|
|
per_page = 1000
|
|
|
|
|
|
|
|
if params[:page] && params[:per_page]
|
|
|
|
offset = (params[:page].to_i - 1) * params[:per_page].to_i
|
|
|
|
per_page = params[:per_page].to_i
|
|
|
|
end
|
2013-07-19 14:21:44 +00:00
|
|
|
|
|
|
|
# only allow customer to fetch his own organization
|
|
|
|
organizations = []
|
2015-05-08 08:15:45 +00:00
|
|
|
if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
|
2013-07-19 14:21:44 +00:00
|
|
|
if current_user.organization_id
|
2016-06-06 15:26:37 +00:00
|
|
|
organizations = Organization.where(id: current_user.organization_id).offset(offset).limit(per_page)
|
2013-07-19 14:21:44 +00:00
|
|
|
end
|
|
|
|
else
|
2016-06-06 15:26:37 +00:00
|
|
|
organizations = Organization.all.offset(offset).limit(per_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:full]
|
|
|
|
assets = {}
|
|
|
|
item_ids = []
|
|
|
|
organizations.each {|item|
|
|
|
|
item_ids.push item.id
|
|
|
|
assets = item.assets(assets)
|
|
|
|
}
|
|
|
|
render json: {
|
|
|
|
record_ids: item_ids,
|
|
|
|
assets: assets,
|
|
|
|
}, status: :ok
|
|
|
|
return
|
2013-07-19 14:21:44 +00:00
|
|
|
end
|
2016-06-06 15:26:37 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: organizations
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Resource:
|
2016-06-06 06:34:15 +00:00
|
|
|
GET /api/v1/organizations/#{id}
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
Response:
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"name": "name_1",
|
|
|
|
...
|
|
|
|
}
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
Test:
|
2016-06-06 06:34:15 +00:00
|
|
|
curl http://localhost/api/v1/organizations/#{id} -v -u #{login}:#{password}
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=end
|
|
|
|
|
|
|
|
def show
|
2013-07-19 14:21:44 +00:00
|
|
|
|
|
|
|
# only allow customer to fetch his own organization
|
2015-05-08 08:15:45 +00:00
|
|
|
if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
|
2013-07-19 14:21:44 +00:00
|
|
|
if !current_user.organization_id
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {}
|
2013-07-19 14:21:44 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if params[:id].to_i != current_user.organization_id
|
|
|
|
response_access_deny
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2014-08-13 00:12:38 +00:00
|
|
|
if params[:full]
|
2016-06-07 19:22:08 +00:00
|
|
|
full = Organization.full(params[:id])
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: full
|
2014-08-13 00:12:38 +00:00
|
|
|
return
|
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
model_show_render(Organization, params)
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Resource:
|
2016-06-06 06:34:15 +00:00
|
|
|
POST /api/v1/organizations
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
Payload:
|
|
|
|
{
|
|
|
|
"name": "some_name",
|
|
|
|
"active": true,
|
|
|
|
"note": "some note",
|
|
|
|
"shared": true
|
|
|
|
}
|
|
|
|
|
|
|
|
Response:
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"name": "some_name",
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
Test:
|
2016-06-06 06:34:15 +00:00
|
|
|
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"}'
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def create
|
2015-02-15 10:01:12 +00:00
|
|
|
return if deny_if_not_role(Z_ROLENAME_AGENT)
|
2012-09-20 12:08:02 +00:00
|
|
|
model_create_render(Organization, params)
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Resource:
|
2016-06-06 06:34:15 +00:00
|
|
|
PUT /api/v1/organizations/{id}
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
Payload:
|
|
|
|
{
|
|
|
|
"id": 1
|
|
|
|
"name": "some_name",
|
|
|
|
"active": true,
|
|
|
|
"note": "some note",
|
|
|
|
"shared": true
|
|
|
|
}
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
Response:
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"name": "some_name",
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
Test:
|
2016-06-06 06:34:15 +00:00
|
|
|
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"}'
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def update
|
2015-02-15 10:01:12 +00:00
|
|
|
return if deny_if_not_role(Z_ROLENAME_AGENT)
|
2012-09-20 12:08:02 +00:00
|
|
|
model_update_render(Organization, params)
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Resource:
|
2016-06-06 06:34:15 +00:00
|
|
|
DELETE /api/v1/organization/{id}
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
Response:
|
2016-06-06 06:34:15 +00:00
|
|
|
{}
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
Test:
|
2016-06-06 06:34:15 +00:00
|
|
|
curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE -d '{}'
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def destroy
|
2016-06-06 06:34:15 +00:00
|
|
|
return if deny_if_not_role(Z_ROLENAME_AGENT)
|
|
|
|
return if model_references_check(Organization, params)
|
2012-09-20 12:08:02 +00:00
|
|
|
model_destory_render(Organization, params)
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2014-07-22 09:00:29 +00:00
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
def search
|
|
|
|
|
|
|
|
if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
|
|
|
|
response_access_deny
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# set limit for pagination if needed
|
|
|
|
if params[:page] && params[:per_page]
|
|
|
|
params[:limit] = params[:page].to_i * params[:per_page].to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
query_params = {
|
|
|
|
query: params[:term],
|
|
|
|
limit: params[:limit],
|
|
|
|
current_user: current_user,
|
|
|
|
}
|
|
|
|
if params[:role_ids] && !params[:role_ids].empty?
|
|
|
|
query_params[:role_ids] = params[:role_ids]
|
|
|
|
end
|
|
|
|
|
|
|
|
# do query
|
|
|
|
organization_all = Organization.search(query_params)
|
|
|
|
|
|
|
|
# do pagination if needed
|
|
|
|
if params[:page] && params[:per_page]
|
|
|
|
offset = (params[:page].to_i - 1) * params[:per_page].to_i
|
|
|
|
organization_all = organization_all.slice(offset, params[:per_page].to_i) || []
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:expand]
|
|
|
|
render json: organization_all
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# build result list
|
|
|
|
if !params[:full]
|
|
|
|
organizations = []
|
|
|
|
organization_all.each { |organization|
|
|
|
|
a = { id: organization.id, label: organization.name }
|
|
|
|
organizations.push a
|
|
|
|
}
|
|
|
|
|
|
|
|
# return result
|
|
|
|
render json: organizations
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
organization_ids = []
|
|
|
|
assets = {}
|
|
|
|
organization_all.each { |organization|
|
|
|
|
assets = organization.assets(assets)
|
|
|
|
organization_ids.push organization.id
|
|
|
|
}
|
|
|
|
|
|
|
|
# return result
|
|
|
|
render json: {
|
|
|
|
assets: assets,
|
|
|
|
organization_ids: organization_ids.uniq,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-07-22 09:00:29 +00:00
|
|
|
# GET /api/v1/organizations/history/1
|
|
|
|
def history
|
|
|
|
|
2016-01-27 18:26:10 +00:00
|
|
|
# permission check
|
2015-05-08 08:15:45 +00:00
|
|
|
if !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
|
2014-07-22 09:00:29 +00:00
|
|
|
response_access_deny
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# get organization data
|
2016-06-06 06:34:15 +00:00
|
|
|
organization = Organization.find(params[:id])
|
2014-07-22 09:00:29 +00:00
|
|
|
|
|
|
|
# get history of organization
|
|
|
|
history = organization.history_get(true)
|
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: history
|
2014-07-22 09:00:29 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|