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 13:31:21 +00:00
|
|
|
class ApplicationController < ActionController::Base
|
2013-06-12 15:59:58 +00:00
|
|
|
# http_basic_authenticate_with :name => "test", :password => "ttt"
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
helper_method :current_user,
|
2015-02-22 18:10:54 +00:00
|
|
|
:authentication_check,
|
|
|
|
|
:config_frontend,
|
2015-05-08 08:15:45 +00:00
|
|
|
:role?,
|
2015-02-22 18:10:54 +00:00
|
|
|
:model_create_render,
|
|
|
|
|
:model_update_render,
|
|
|
|
|
:model_restory_render,
|
|
|
|
|
:mode_show_rendeder,
|
|
|
|
|
:model_index_render
|
2012-04-19 09:51:24 +00:00
|
|
|
|
2015-05-07 11:23:55 +00:00
|
|
|
skip_before_action :verify_authenticity_token
|
2015-08-18 22:36:58 +00:00
|
|
|
before_action :set_user, :session_update
|
2015-05-07 11:23:55 +00:00
|
|
|
before_action :cors_preflight_check
|
2012-04-19 09:51:24 +00:00
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
after_action :user_device_update, :set_access_control_headers
|
2015-05-07 11:23:55 +00:00
|
|
|
after_action :trigger_events
|
2012-04-10 14:06:46 +00:00
|
|
|
|
|
|
|
|
# For all responses in this controller, return the CORS access control headers.
|
2013-06-12 15:59:58 +00:00
|
|
|
def set_access_control_headers
|
2012-04-19 09:51:24 +00:00
|
|
|
headers['Access-Control-Allow-Origin'] = '*'
|
|
|
|
|
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
|
|
|
|
|
headers['Access-Control-Max-Age'] = '1728000'
|
2013-08-11 15:45:38 +00:00
|
|
|
headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Accept-Language'
|
2012-04-10 14:06:46 +00:00
|
|
|
headers['Access-Control-Allow-Credentials'] = 'true'
|
|
|
|
|
end
|
2012-07-29 18:55:51 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
# If this is a preflight OPTIONS request, then short-circuit the
|
|
|
|
|
# request, return only the necessary headers and return an empty
|
|
|
|
|
# text/plain.
|
2012-07-29 18:55:51 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def cors_preflight_check
|
2015-04-30 15:25:04 +00:00
|
|
|
|
|
|
|
|
return if request.method != 'OPTIONS'
|
|
|
|
|
|
|
|
|
|
headers['Access-Control-Allow-Origin'] = '*'
|
|
|
|
|
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
|
|
|
|
|
headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Accept-Language'
|
|
|
|
|
headers['Access-Control-Max-Age'] = '1728000'
|
|
|
|
|
headers['Access-Control-Allow-Credentials'] = 'true'
|
|
|
|
|
render text: '', content_type: 'text/plain'
|
|
|
|
|
|
|
|
|
|
false
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-07-29 18:55:51 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
private
|
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# execute events
|
2012-04-16 14:35:10 +00:00
|
|
|
def trigger_events
|
2012-11-18 11:02:42 +00:00
|
|
|
Observer::Ticket::Notification.transaction
|
2012-04-16 14:35:10 +00:00
|
|
|
end
|
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
# Finds the User with the ID stored in the session with the key
|
|
|
|
|
# :current_user_id This is a common way to handle user login in
|
|
|
|
|
# a Rails application; logging in sets the session value and
|
|
|
|
|
# logging out removes it.
|
|
|
|
|
def current_user
|
2013-04-20 08:52:17 +00:00
|
|
|
return @_current_user if @_current_user
|
|
|
|
|
return if !session[:user_id]
|
2015-11-30 10:50:02 +00:00
|
|
|
@_current_user = User.lookup(id: session[:user_id])
|
2012-04-19 12:32:12 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2012-04-19 12:32:12 +00:00
|
|
|
def current_user_set(user)
|
2013-09-30 23:37:45 +00:00
|
|
|
session[:user_id] = user.id
|
2012-04-19 12:32:12 +00:00
|
|
|
@_current_user = user
|
|
|
|
|
set_user
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
2013-04-20 08:52:17 +00:00
|
|
|
# Sets the current user into a named Thread location so that it can be accessed
|
|
|
|
|
# by models and observers
|
|
|
|
|
def set_user
|
|
|
|
|
return if !current_user
|
|
|
|
|
UserInfo.current_user_id = current_user.id
|
|
|
|
|
end
|
|
|
|
|
|
2013-07-26 21:45:16 +00:00
|
|
|
# update session updated_at
|
|
|
|
|
def session_update
|
2014-08-13 00:12:38 +00:00
|
|
|
#sleep 0.6
|
2013-08-25 21:34:54 +00:00
|
|
|
|
2015-05-08 10:20:33 +00:00
|
|
|
session[:ping] = Time.zone.now.iso8601
|
2013-07-26 21:45:16 +00:00
|
|
|
|
2015-03-22 11:59:39 +00:00
|
|
|
# check if remote ip need to be updated
|
2016-01-05 12:46:43 +00:00
|
|
|
if !session[:remote_ip] || session[:remote_ip] != request.remote_ip
|
|
|
|
|
session[:remote_ip] = request.remote_ip
|
2015-11-30 10:50:02 +00:00
|
|
|
session[:geo] = Service::GeoIp.location(request.remote_ip)
|
2013-07-26 21:45:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# fill user agent
|
2015-04-30 15:25:04 +00:00
|
|
|
return if session[:user_agent]
|
|
|
|
|
|
|
|
|
|
session[:user_agent] = request.env['HTTP_USER_AGENT']
|
2013-07-26 21:45:16 +00:00
|
|
|
end
|
|
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
# user device recent action update
|
|
|
|
|
def user_device_update
|
2015-08-17 13:25:41 +00:00
|
|
|
|
2015-08-17 16:14:44 +00:00
|
|
|
# return if we are in switch to user mode
|
|
|
|
|
return if session[:switched_from_user_id]
|
|
|
|
|
|
2015-08-17 13:25:41 +00:00
|
|
|
# only if user_id exists
|
|
|
|
|
return if !session[:user_id]
|
|
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
# only with user device
|
|
|
|
|
if !session[:user_device_id]
|
|
|
|
|
if params[:fingerprint]
|
|
|
|
|
return false if !user_device_log(current_user, 'session')
|
|
|
|
|
end
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# check if entry exists / only if write action
|
2015-08-17 13:25:41 +00:00
|
|
|
return if request.method == 'GET' || request.method == 'OPTIONS'
|
|
|
|
|
|
|
|
|
|
# only update if needed
|
2015-08-18 22:36:58 +00:00
|
|
|
return if session[:user_device_update_at] && session[:user_device_update_at] > Time.zone.now - 5.minutes
|
|
|
|
|
session[:user_device_update_at] = Time.zone.now
|
2015-08-17 13:25:41 +00:00
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
UserDevice.action(
|
|
|
|
|
session[:user_device_id],
|
2015-08-17 13:25:41 +00:00
|
|
|
session[:user_agent],
|
2016-01-05 12:46:43 +00:00
|
|
|
session[:remote_ip],
|
2015-08-17 13:25:41 +00:00
|
|
|
session[:user_id],
|
|
|
|
|
)
|
2015-08-18 22:36:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user_device_log(user, type)
|
|
|
|
|
|
2015-08-19 00:13:38 +00:00
|
|
|
# return if we are in switch to user mode
|
2015-08-19 00:16:57 +00:00
|
|
|
return true if session[:switched_from_user_id]
|
2015-08-19 00:13:38 +00:00
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
# for sessions we need the fingperprint
|
|
|
|
|
if !params[:fingerprint] && type == 'session'
|
|
|
|
|
render json: { error: 'Need fingerprint param!' }, status: :unprocessable_entity
|
|
|
|
|
return false
|
2015-08-17 16:14:44 +00:00
|
|
|
end
|
2015-08-18 22:36:58 +00:00
|
|
|
|
|
|
|
|
# add defice if needed
|
|
|
|
|
user_device = UserDevice.add(
|
|
|
|
|
request.env['HTTP_USER_AGENT'],
|
|
|
|
|
request.remote_ip,
|
|
|
|
|
user.id,
|
|
|
|
|
params[:fingerprint],
|
|
|
|
|
type,
|
|
|
|
|
)
|
|
|
|
|
session[:user_device_id] = user_device.id
|
2015-08-17 13:25:41 +00:00
|
|
|
end
|
|
|
|
|
|
2015-06-22 11:19:56 +00:00
|
|
|
def authentication_check_only(auth_param)
|
2013-06-25 11:54:13 +00:00
|
|
|
|
2015-10-20 08:48:43 +00:00
|
|
|
#logger.debug 'authentication_check'
|
2015-05-04 18:58:28 +00:00
|
|
|
#logger.debug params.inspect
|
|
|
|
|
#logger.debug session.inspect
|
|
|
|
|
#logger.debug cookies.inspect
|
2013-06-25 11:54:13 +00:00
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
# already logged in, early exit
|
|
|
|
|
if session.id && session[:user_id]
|
2015-08-18 22:36:58 +00:00
|
|
|
|
2015-11-30 10:50:02 +00:00
|
|
|
userdata = User.lookup(id: session[:user_id])
|
2015-06-24 08:48:48 +00:00
|
|
|
current_user_set(userdata)
|
2013-07-26 21:45:16 +00:00
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
return {
|
|
|
|
|
auth: true
|
|
|
|
|
}
|
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
error_message = 'authentication failed'
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2015-08-23 20:15:10 +00:00
|
|
|
# check sso based authentication
|
2015-06-24 08:48:48 +00:00
|
|
|
sso_userdata = User.sso(params)
|
|
|
|
|
if sso_userdata
|
2015-08-18 22:36:58 +00:00
|
|
|
session[:persistent] = true
|
2012-04-20 06:45:22 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
return {
|
2015-04-27 13:42:53 +00:00
|
|
|
auth: true
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
2012-04-20 06:45:22 +00:00
|
|
|
end
|
|
|
|
|
|
2015-08-23 20:15:10 +00:00
|
|
|
# check http basic based authentication
|
2015-06-24 08:48:48 +00:00
|
|
|
authenticate_with_http_basic do |username, password|
|
|
|
|
|
logger.debug "http basic auth check '#{username}'"
|
|
|
|
|
|
2015-11-30 10:50:02 +00:00
|
|
|
userdata = User.authenticate(username, password)
|
2013-02-17 18:28:32 +00:00
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
next if !userdata
|
2013-02-17 18:28:32 +00:00
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
current_user_set(userdata)
|
2015-08-18 22:36:58 +00:00
|
|
|
user_device_log(userdata, 'basic_auth')
|
|
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
return {
|
|
|
|
|
auth: true
|
|
|
|
|
}
|
2013-02-17 18:28:32 +00:00
|
|
|
end
|
|
|
|
|
|
2015-08-23 20:15:10 +00:00
|
|
|
# check http token based authentication
|
2015-06-22 11:19:56 +00:00
|
|
|
if auth_param[:token_action]
|
2015-06-24 08:48:48 +00:00
|
|
|
authenticate_with_http_token do |token, _options|
|
2015-08-23 20:15:10 +00:00
|
|
|
logger.debug "token auth check '#{token}'"
|
2015-06-22 11:19:56 +00:00
|
|
|
|
|
|
|
|
userdata = Token.check(
|
|
|
|
|
action: auth_param[:token_action],
|
|
|
|
|
name: token,
|
|
|
|
|
)
|
|
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
next if !userdata
|
2015-06-22 11:19:56 +00:00
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
current_user_set(userdata)
|
2015-08-18 22:36:58 +00:00
|
|
|
user_device_log(userdata, 'token_auth')
|
2015-06-22 11:19:56 +00:00
|
|
|
|
|
|
|
|
return {
|
2015-06-24 08:48:48 +00:00
|
|
|
auth: true
|
2015-06-22 11:19:56 +00:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-24 08:48:48 +00:00
|
|
|
logger.debug error_message
|
2015-04-27 20:49:17 +00:00
|
|
|
{
|
2015-06-24 08:48:48 +00:00
|
|
|
auth: false,
|
|
|
|
|
message: error_message,
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-30 10:50:02 +00:00
|
|
|
def authentication_check(auth_param = {} )
|
2015-06-22 11:19:56 +00:00
|
|
|
result = authentication_check_only(auth_param)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2015-06-22 10:56:09 +00:00
|
|
|
# check if basic_auth fallback is possible
|
2015-06-22 11:19:56 +00:00
|
|
|
if auth_param[:basic_auth_promt] && result[:auth] == false
|
2015-06-22 10:56:09 +00:00
|
|
|
|
|
|
|
|
return request_http_basic_authentication
|
|
|
|
|
end
|
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# return auth not ok
|
|
|
|
|
if result[:auth] == false
|
2012-04-11 06:37:54 +00:00
|
|
|
render(
|
2015-04-27 13:42:53 +00:00
|
|
|
json: {
|
|
|
|
|
error: result[:message],
|
2012-04-11 06:37:54 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
status: :unauthorized
|
2012-04-11 06:37:54 +00:00
|
|
|
)
|
|
|
|
|
return false
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
2012-04-19 09:51:24 +00:00
|
|
|
# return auth ok
|
2014-12-01 07:32:35 +00:00
|
|
|
true
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
2015-11-30 10:50:02 +00:00
|
|
|
def role?(role_name)
|
2012-09-04 16:39:23 +00:00
|
|
|
return false if !current_user
|
2015-11-30 10:50:02 +00:00
|
|
|
current_user.role?(role_name)
|
2012-09-04 21:28:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def ticket_permission(ticket)
|
2015-11-30 10:50:02 +00:00
|
|
|
return true if ticket.permission(current_user: current_user)
|
2012-11-12 09:34:22 +00:00
|
|
|
response_access_deny
|
2014-12-01 07:32:35 +00:00
|
|
|
false
|
2012-11-12 09:34:22 +00:00
|
|
|
end
|
|
|
|
|
|
2015-11-30 10:50:02 +00:00
|
|
|
def deny_if_not_role(role_name)
|
|
|
|
|
return false if role?(role_name)
|
2013-07-19 14:21:44 +00:00
|
|
|
response_access_deny
|
2014-12-01 07:32:35 +00:00
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def valid_session_with_user
|
|
|
|
|
return true if current_user
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: { message: 'No session user!' }, status: :unprocessable_entity
|
2014-12-01 07:32:35 +00:00
|
|
|
false
|
2012-12-27 20:17:33 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-12 09:34:22 +00:00
|
|
|
def response_access_deny
|
2012-09-04 21:28:49 +00:00
|
|
|
render(
|
2015-04-27 13:42:53 +00:00
|
|
|
json: {},
|
|
|
|
|
status: :unauthorized
|
2012-09-04 21:28:49 +00:00
|
|
|
)
|
2014-12-01 07:32:35 +00:00
|
|
|
false
|
2012-09-04 16:39:23 +00:00
|
|
|
end
|
|
|
|
|
|
2012-04-11 06:37:54 +00:00
|
|
|
def config_frontend
|
2013-06-06 07:34:09 +00:00
|
|
|
|
2012-04-11 06:37:54 +00:00
|
|
|
# config
|
|
|
|
|
config = {}
|
2015-11-30 10:50:02 +00:00
|
|
|
Setting.select('name').where(frontend: true ).each { |setting|
|
2012-04-13 13:51:10 +00:00
|
|
|
config[setting.name] = Setting.get(setting.name)
|
2012-04-11 06:37:54 +00:00
|
|
|
}
|
2013-06-06 07:34:09 +00:00
|
|
|
|
2015-08-18 08:50:12 +00:00
|
|
|
# remember if we can to swich back to user
|
2014-09-15 20:55:06 +00:00
|
|
|
if session[:switched_from_user_id]
|
|
|
|
|
config['switch_back_to_possible'] = true
|
|
|
|
|
end
|
|
|
|
|
|
2015-08-18 08:50:12 +00:00
|
|
|
# remember session_id for websocket logon
|
|
|
|
|
config['session_id'] = session.id
|
|
|
|
|
|
2014-09-15 20:55:06 +00:00
|
|
|
config
|
2012-04-11 06:37:54 +00:00
|
|
|
end
|
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# model helper
|
|
|
|
|
def model_create_render (object, params)
|
|
|
|
|
|
2015-05-07 10:20:52 +00:00
|
|
|
# create object
|
2015-11-30 10:50:02 +00:00
|
|
|
generic_object = object.new(object.param_cleanup(params[object.to_app_model_url], true ))
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2015-05-07 10:20:52 +00:00
|
|
|
# save object
|
|
|
|
|
generic_object.save!
|
2013-05-29 16:09:02 +00:00
|
|
|
|
2015-05-07 10:20:52 +00:00
|
|
|
# set relations
|
2015-11-30 10:50:02 +00:00
|
|
|
generic_object.param_set_associations(params)
|
2013-09-30 23:37:45 +00:00
|
|
|
|
2015-05-07 10:20:52 +00:00
|
|
|
model_create_render_item(generic_object)
|
2015-05-08 14:09:24 +00:00
|
|
|
rescue => e
|
2015-05-07 10:20:52 +00:00
|
|
|
logger.error e.message
|
|
|
|
|
logger.error e.backtrace.inspect
|
2016-02-16 15:41:33 +00:00
|
|
|
render json: model_match_error(e.message), status: :unprocessable_entity
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2012-11-12 09:34:22 +00:00
|
|
|
def model_create_render_item (generic_object)
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: generic_object.attributes_with_associations, status: :created
|
2012-11-12 09:34:22 +00:00
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
|
def model_update_render (object, params)
|
|
|
|
|
|
2015-05-07 10:20:52 +00:00
|
|
|
# find object
|
2015-11-30 10:50:02 +00:00
|
|
|
generic_object = object.find(params[:id])
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2015-05-07 10:20:52 +00:00
|
|
|
# save object
|
2015-11-30 10:50:02 +00:00
|
|
|
generic_object.update_attributes!(object.param_cleanup(params[object.to_app_model_url]))
|
2013-09-30 23:37:45 +00:00
|
|
|
|
2015-05-07 10:20:52 +00:00
|
|
|
# set relations
|
2015-11-30 10:50:02 +00:00
|
|
|
generic_object.param_set_associations(params)
|
2013-09-30 23:37:45 +00:00
|
|
|
|
2015-11-30 10:50:02 +00:00
|
|
|
model_update_render_item(generic_object)
|
2015-05-08 14:09:24 +00:00
|
|
|
rescue => e
|
2015-05-07 10:20:52 +00:00
|
|
|
logger.error e.message
|
|
|
|
|
logger.error e.backtrace.inspect
|
2016-02-16 15:41:33 +00:00
|
|
|
render json: model_match_error(e.message), status: :unprocessable_entity
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2012-11-12 09:34:22 +00:00
|
|
|
def model_update_render_item (generic_object)
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: generic_object.attributes_with_associations, status: :ok
|
2012-11-12 09:34:22 +00:00
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
|
def model_destory_render (object, params)
|
2015-11-30 10:50:02 +00:00
|
|
|
generic_object = object.find(params[:id])
|
2015-05-07 10:20:52 +00:00
|
|
|
generic_object.destroy
|
|
|
|
|
model_destory_render_item()
|
2015-05-08 14:09:24 +00:00
|
|
|
rescue => e
|
2015-05-07 10:20:52 +00:00
|
|
|
logger.error e.message
|
|
|
|
|
logger.error e.backtrace.inspect
|
2016-02-16 15:41:33 +00:00
|
|
|
render json: model_match_error(e.message), status: :unprocessable_entity
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2012-11-12 09:34:22 +00:00
|
|
|
def model_destory_render_item ()
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {}, status: :ok
|
2012-11-12 09:34:22 +00:00
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
|
def model_show_render (object, params)
|
2014-08-13 00:12:38 +00:00
|
|
|
|
2015-05-07 10:20:52 +00:00
|
|
|
if params[:full]
|
2015-11-30 10:50:02 +00:00
|
|
|
generic_object_full = object.full(params[:id])
|
2015-05-07 10:20:52 +00:00
|
|
|
render json: generic_object_full, status: :ok
|
|
|
|
|
return
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
2015-05-07 10:20:52 +00:00
|
|
|
|
2015-11-30 10:50:02 +00:00
|
|
|
generic_object = object.find(params[:id])
|
2015-05-07 10:20:52 +00:00
|
|
|
model_show_render_item(generic_object)
|
2015-05-08 14:09:24 +00:00
|
|
|
rescue => e
|
2015-05-07 10:20:52 +00:00
|
|
|
logger.error e.message
|
|
|
|
|
logger.error e.backtrace.inspect
|
2016-02-16 15:41:33 +00:00
|
|
|
render json: model_match_error(e.message), status: :unprocessable_entity
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2012-11-12 09:34:22 +00:00
|
|
|
def model_show_render_item (generic_object)
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: generic_object.attributes_with_associations, status: :ok
|
2012-11-12 09:34:22 +00:00
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2015-05-07 08:01:35 +00:00
|
|
|
def model_index_render (object, _params)
|
2015-05-07 10:20:52 +00:00
|
|
|
generic_objects = object.all
|
2015-11-30 10:50:02 +00:00
|
|
|
model_index_render_result(generic_objects)
|
2015-05-08 14:09:24 +00:00
|
|
|
rescue => e
|
2015-05-07 10:20:52 +00:00
|
|
|
logger.error e.message
|
|
|
|
|
logger.error e.backtrace.inspect
|
2016-02-16 15:41:33 +00:00
|
|
|
render json: model_match_error(e.message), status: :unprocessable_entity
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2012-11-12 09:34:22 +00:00
|
|
|
def model_index_render_result (generic_objects)
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: generic_objects, status: :ok
|
2012-11-12 09:34:22 +00:00
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2016-02-16 15:41:33 +00:00
|
|
|
def model_match_error (error)
|
|
|
|
|
data = {
|
|
|
|
|
error: error
|
|
|
|
|
}
|
2016-02-17 14:15:26 +00:00
|
|
|
if error =~ /(already exists|duplicate key|duplicate entry)/i
|
2016-02-16 15:41:33 +00:00
|
|
|
data[:error_human] = 'Object already exists!'
|
|
|
|
|
end
|
|
|
|
|
data
|
|
|
|
|
end
|
2012-04-10 13:31:21 +00:00
|
|
|
end
|