2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 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
|
2017-02-15 12:29:25 +00:00
|
|
|
prepend_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
|
2016-06-30 09:57:07 +00:00
|
|
|
per_page = 500
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-09-14 07:21:17 +00:00
|
|
|
if per_page > 500
|
|
|
|
per_page = 500
|
|
|
|
end
|
|
|
|
|
2013-07-19 14:21:44 +00:00
|
|
|
# only allow customer to fetch his own organization
|
|
|
|
organizations = []
|
2017-04-11 06:33:08 +00:00
|
|
|
if !current_user.permissions?(['admin.organization', 'ticket.agent'])
|
2013-07-19 14:21:44 +00:00
|
|
|
if current_user.organization_id
|
2016-09-21 17:42:47 +00:00
|
|
|
organizations = Organization.where(id: current_user.organization_id).order(id: 'ASC').offset(offset).limit(per_page)
|
2013-07-19 14:21:44 +00:00
|
|
|
end
|
|
|
|
else
|
2016-09-21 17:42:47 +00:00
|
|
|
organizations = Organization.all.order(id: 'ASC').offset(offset).limit(per_page)
|
2016-06-06 15:26:37 +00:00
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2016-06-08 04:56:05 +00:00
|
|
|
list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
organizations.each do |organization|
|
2017-01-31 17:13:45 +00:00
|
|
|
list.push organization.attributes_with_association_names
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-08 04:56:05 +00:00
|
|
|
render json: list, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
2016-06-06 15:26:37 +00:00
|
|
|
assets = {}
|
|
|
|
item_ids = []
|
2017-10-01 12:25:52 +00:00
|
|
|
organizations.each do |item|
|
2016-06-06 15:26:37 +00:00
|
|
|
item_ids.push item.id
|
|
|
|
assets = item.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-06 15:26:37 +00:00
|
|
|
render json: {
|
|
|
|
record_ids: item_ids,
|
|
|
|
assets: assets,
|
|
|
|
}, status: :ok
|
|
|
|
return
|
2013-07-19 14:21:44 +00:00
|
|
|
end
|
2017-12-14 13:19:24 +00:00
|
|
|
|
2017-04-10 11:32:16 +00:00
|
|
|
list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
organizations.each do |organization|
|
2017-04-10 11:32:16 +00:00
|
|
|
list.push organization.attributes_with_association_ids
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-10 11:32:16 +00:00
|
|
|
render json: list
|
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
|
2017-04-11 06:33:08 +00:00
|
|
|
if !current_user.permissions?(['admin.organization', 'ticket.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
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::NotAuthorized if params[:id].to_i != current_user.organization_id
|
2013-07-19 14:21:44 +00:00
|
|
|
end
|
2016-06-08 04:56:05 +00:00
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2017-01-31 17:13:45 +00:00
|
|
|
organization = Organization.find(params[:id]).attributes_with_association_names
|
2016-06-08 04:56:05 +00:00
|
|
|
render json: organization, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
2016-06-07 19:22:08 +00:00
|
|
|
full = Organization.full(params[:id])
|
2017-12-14 13:19:24 +00:00
|
|
|
render json: full, status: :ok
|
2014-08-13 00:12:38 +00:00
|
|
|
return
|
|
|
|
end
|
2016-06-08 04:56:05 +00:00
|
|
|
|
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
|
2017-04-11 06:33:08 +00:00
|
|
|
permission_check(['admin.organization', 'ticket.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
|
2017-04-11 06:33:08 +00:00
|
|
|
permission_check(['admin.organization', 'ticket.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
|
2017-04-11 06:33:08 +00:00
|
|
|
permission_check(['admin.organization', 'ticket.agent'])
|
2016-06-30 08:24:03 +00:00
|
|
|
model_references_check(Organization, params)
|
2016-11-30 10:30:03 +00:00
|
|
|
model_destroy_render(Organization, params)
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2014-07-22 09:00:29 +00:00
|
|
|
|
2016-06-08 04:56:05 +00:00
|
|
|
# GET /api/v1/organizations/search
|
2016-06-06 15:26:37 +00:00
|
|
|
def search
|
|
|
|
|
2017-04-11 06:33:08 +00:00
|
|
|
if !current_user.permissions?(['admin.organization', 'ticket.agent'])
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::NotAuthorized
|
2016-06-06 15:26:37 +00:00
|
|
|
end
|
|
|
|
|
2018-04-13 07:22:55 +00:00
|
|
|
per_page = params[:per_page] || params[:limit] || 100
|
|
|
|
per_page = per_page.to_i
|
|
|
|
if per_page > 500
|
|
|
|
per_page = 500
|
2016-09-14 07:21:17 +00:00
|
|
|
end
|
2018-04-13 07:22:55 +00:00
|
|
|
page = params[:page] || 1
|
|
|
|
page = page.to_i
|
|
|
|
offset = (page - 1) * per_page
|
2016-09-14 07:21:17 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
query = params[:query]
|
|
|
|
if query.respond_to?(:permit!)
|
|
|
|
query = query.permit!.to_h
|
|
|
|
end
|
2016-06-06 15:26:37 +00:00
|
|
|
query_params = {
|
2017-11-23 08:09:44 +00:00
|
|
|
query: query,
|
2018-04-13 07:22:55 +00:00
|
|
|
limit: per_page,
|
|
|
|
offset: offset,
|
2016-06-06 15:26:37 +00:00
|
|
|
current_user: current_user,
|
|
|
|
}
|
2017-11-23 08:09:44 +00:00
|
|
|
if params[:role_ids].present?
|
2016-06-06 15:26:37 +00:00
|
|
|
query_params[:role_ids] = params[:role_ids]
|
|
|
|
end
|
|
|
|
|
|
|
|
# do query
|
|
|
|
organization_all = Organization.search(query_params)
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2016-06-08 04:56:05 +00:00
|
|
|
list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
organization_all.each do |organization|
|
2017-01-31 17:13:45 +00:00
|
|
|
list.push organization.attributes_with_association_names
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-08 04:56:05 +00:00
|
|
|
render json: list, status: :ok
|
2016-06-06 15:26:37 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# build result list
|
2016-09-11 13:24:10 +00:00
|
|
|
if params[:label]
|
2016-06-06 15:26:37 +00:00
|
|
|
organizations = []
|
2017-10-01 12:25:52 +00:00
|
|
|
organization_all.each do |organization|
|
2016-09-11 13:24:10 +00:00
|
|
|
a = { id: organization.id, label: organization.name, value: organization.name }
|
2016-06-06 15:26:37 +00:00
|
|
|
organizations.push a
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
# return result
|
|
|
|
render json: organizations
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
2016-09-11 13:24:10 +00:00
|
|
|
organization_ids = []
|
|
|
|
assets = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
organization_all.each do |organization|
|
2016-09-11 13:24:10 +00:00
|
|
|
assets = organization.assets(assets)
|
|
|
|
organization_ids.push organization.id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-06 15:26:37 +00:00
|
|
|
|
2016-09-11 13:24:10 +00:00
|
|
|
# return result
|
|
|
|
render json: {
|
|
|
|
assets: assets,
|
|
|
|
organization_ids: organization_ids.uniq,
|
|
|
|
}
|
2016-09-11 14:02:15 +00:00
|
|
|
return
|
2016-09-11 13:24:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
organization_all.each do |organization|
|
2017-04-10 11:32:16 +00:00
|
|
|
list.push organization.attributes_with_association_ids
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-09-11 13:24:10 +00:00
|
|
|
render json: list, status: :ok
|
2016-06-06 15:26:37 +00:00
|
|
|
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
|
2017-04-11 06:33:08 +00:00
|
|
|
if !current_user.permissions?(['admin.organization', 'ticket.agent'])
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::NotAuthorized
|
2014-07-22 09:00:29 +00:00
|
|
|
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
|
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
# @path [GET] /organizations/import_example
|
|
|
|
#
|
|
|
|
# @summary Download of example CSV file.
|
|
|
|
# @notes The requester have 'admin.organization' permissions to be able to download it.
|
|
|
|
# @example curl -u 'me@example.com:test' http://localhost:3000/api/v1/organizations/import_example
|
|
|
|
#
|
|
|
|
# @response_message 200 File download.
|
|
|
|
# @response_message 401 Invalid session.
|
|
|
|
def import_example
|
|
|
|
permission_check('admin.organization')
|
|
|
|
send_data(
|
|
|
|
Organization.csv_example,
|
|
|
|
filename: 'organization-example.csv',
|
|
|
|
type: 'text/csv',
|
|
|
|
disposition: 'attachment'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# @path [POST] /organizations/import
|
|
|
|
#
|
|
|
|
# @summary Starts import.
|
|
|
|
# @notes The requester have 'admin.text_module' permissions to be create a new import.
|
|
|
|
# @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/organizations.csv' 'https://your.zammad/api/v1/organizations/import?try=true'
|
|
|
|
# @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/organizations.csv' 'https://your.zammad/api/v1/organizations/import'
|
|
|
|
#
|
|
|
|
# @response_message 201 Import started.
|
|
|
|
# @response_message 401 Invalid session.
|
|
|
|
def import_start
|
|
|
|
permission_check('admin.user')
|
2018-06-06 01:30:17 +00:00
|
|
|
string = params[:data] || params[:file].read.force_encoding('utf-8')
|
2018-02-20 04:29:30 +00:00
|
|
|
result = Organization.csv_import(
|
2018-06-06 01:30:17 +00:00
|
|
|
string: string,
|
2018-02-20 04:29:30 +00:00
|
|
|
parse_params: {
|
2018-06-06 01:30:17 +00:00
|
|
|
col_sep: params[:col_sep] || ',',
|
2018-02-20 04:29:30 +00:00
|
|
|
},
|
|
|
|
try: params[:try],
|
2018-06-12 20:58:59 +00:00
|
|
|
delete: params[:delete],
|
2018-02-20 04:29:30 +00:00
|
|
|
)
|
|
|
|
render json: result, status: :ok
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|