trabajo-afectivo/app/controllers/sessions_controller.rb

250 lines
5.9 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2012-04-10 14:06:46 +00:00
class SessionsController < ApplicationController
prepend_before_action -> { authentication_check && authorize! }, only: %i[switch_to_user list delete]
2019-09-05 14:02:31 +00:00
skip_before_action :verify_csrf_token, only: %i[show destroy create_omniauth failure_omniauth]
skip_before_action :user_device_check, only: %i[create_sso]
2012-04-10 14:06:46 +00:00
# "Create" a login, aka "log the user in"
def create
2019-09-05 14:02:31 +00:00
user = authenticate_with_password
initiate_session_for(user)
2012-04-11 06:37:54 +00:00
# return new session data
2019-09-05 14:02:31 +00:00
render status: :created,
json: SessionHelper.json_hash(user).merge(config: config_frontend)
2012-04-10 14:06:46 +00:00
end
def create_sso
raise Exceptions::NotAuthorized, 'SSO authentication disabled!' if !Setting.get('auth_sso')
user = begin
login = request.env['REMOTE_USER'] ||
request.env['HTTP_REMOTE_USER'] ||
request.headers['X-Forwarded-User']
User.lookup(login: login&.downcase)
end
raise Exceptions::NotAuthorized, 'Missing SSO ENV REMOTE_USER or X-Forwarded-User header' if login.blank?
raise Exceptions::NotAuthorized, "No such user '#{login}' found!" if user.blank?
session.delete(:switched_from_user_id)
authentication_check_prerequesits(user, 'SSO', {})
redirect_to '/#'
end
2012-04-10 14:06:46 +00:00
def show
user = authentication_check_only
raise Exceptions::NotAuthorized, 'no valid session' if user.blank?
2019-09-05 14:02:31 +00:00
initiate_session_for(user)
2014-09-09 23:42:20 +00:00
2012-04-11 06:37:54 +00:00
# return current session
2019-09-05 14:02:31 +00:00
render json: SessionHelper.json_hash(user).merge(config: config_frontend)
rescue Exceptions::NotAuthorized => e
render json: {
2019-09-05 14:02:31 +00:00
error: e.message,
config: config_frontend,
2019-09-05 14:02:31 +00:00
models: SessionHelper.models,
collections: { Locale.to_app_model => Locale.where(active: true) }
2012-04-11 06:37:54 +00:00
}
2012-04-10 14:06:46 +00:00
end
# "Delete" a login, aka "log the user out"
def destroy
reset_session
2012-04-10 14:06:46 +00:00
# Remove the user id from the session
@_current_user = nil
2012-04-11 06:37:54 +00:00
# reset session
request.env['rack.session.options'][:expire_after] = nil
render json: {}
2012-04-10 14:06:46 +00:00
end
2012-04-10 14:06:46 +00:00
def create_omniauth
# in case, remove switched_from_user_id
session[:switched_from_user_id] = nil
2012-04-10 14:06:46 +00:00
auth = request.env['omniauth.auth']
if !auth
logger.info('AUTH IS NULL, SERVICE NOT LINKED TO ACCOUNT')
2012-04-18 08:33:42 +00:00
# redirect to app
2013-02-17 18:28:32 +00:00
redirect_to '/'
2012-04-10 14:06:46 +00:00
end
2012-04-18 08:33:42 +00:00
# Create a new user or add an auth to existing user, depending on
# whether there is already a user signed in.
authorization = Authorization.find_from_hash(auth)
if !authorization
2012-04-18 08:33:42 +00:00
authorization = Authorization.create_from_hash(auth, current_user)
2012-04-10 14:06:46 +00:00
end
2012-04-18 08:33:42 +00:00
2019-09-05 14:02:31 +00:00
if in_maintenance_mode?(authorization.user)
redirect_to '/#'
return
end
2013-09-30 01:41:45 +00:00
# set current session user
current_user_set(authorization.user)
# log new session
authorization.user.activity_stream_log('session started', authorization.user.id, true)
2013-09-30 01:41:45 +00:00
2012-10-18 08:10:12 +00:00
# remember last login date
2013-02-12 22:49:52 +00:00
authorization.user.update_last_login
2012-10-18 08:10:12 +00:00
2012-04-10 14:06:46 +00:00
# redirect to app
2013-02-17 18:28:32 +00:00
redirect_to '/'
end
def failure_omniauth
raise Exceptions::UnprocessableEntity, "Message from #{params[:strategy]}: #{params[:message]}"
end
2013-10-07 03:38:07 +00:00
# "switch" to user
def switch_to_user
# check user
if !params[:id]
render(
json: { message: 'no user given' },
status: :not_found
2013-10-07 03:38:07 +00:00
)
return false
end
user = User.find(params[:id])
2013-10-07 03:38:07 +00:00
if !user
render(
json: {},
status: :not_found
2013-10-07 03:38:07 +00:00
)
return false
end
# remember original user
session[:switched_from_user_id] ||= current_user.id
2013-10-07 03:38:07 +00:00
# log new session
user.activity_stream_log('switch to', current_user.id, true)
2013-10-07 03:38:07 +00:00
# set session user
current_user_set(user)
render(
json: {
success: true,
location: '',
},
)
2013-10-07 03:38:07 +00:00
end
# "switch" back to user
def switch_back_to_user
# check if it's a switch back
raise Exceptions::NotAuthorized if !session[:switched_from_user_id]
user = User.lookup(id: session[:switched_from_user_id])
if !user
render(
json: {},
status: :not_found
)
return false
end
# remember current user
2014-09-21 13:19:28 +00:00
current_session_user = current_user
# remove switched_from_user_id
session[:switched_from_user_id] = nil
# set old session user again
current_user_set(user)
2014-09-21 13:19:28 +00:00
# log end session
current_session_user.activity_stream_log('ended switch to', user.id, true)
2014-09-21 13:19:28 +00:00
render(
json: {
success: true,
location: '',
},
)
end
def available
render json: {
app_version: AppVersion.get
}
end
2013-07-26 21:45:16 +00:00
def list
2013-10-01 18:31:03 +00:00
assets = {}
2013-07-26 21:45:16 +00:00
sessions_clean = []
SessionHelper.list.each do |session|
2017-07-24 07:06:15 +00:00
next if session.data['user_id'].blank?
2013-07-26 21:45:16 +00:00
sessions_clean.push session
2017-07-24 07:06:15 +00:00
next if session.data['user_id']
2017-07-24 07:06:15 +00:00
user = User.lookup(id: session.data['user_id'])
next if !user
2017-07-24 07:06:15 +00:00
assets = user.assets(assets)
end
render json: {
sessions: sessions_clean,
assets: assets,
2013-07-26 21:45:16 +00:00
}
end
def delete
SessionHelper.destroy(params[:id])
render json: {}
2013-07-26 21:45:16 +00:00
end
private
2019-09-05 14:02:31 +00:00
def initiate_session_for(user)
request.env['rack.session.options'][:expire_after] = 1.year if params[:remember_me]
session[:persistent] = true
user.activity_stream_log('session started', user.id, true)
end
def config_frontend
# config
config = {}
Setting.select('name, preferences').where(frontend: true).each do |setting|
next if setting.preferences[:authentication] == true && !current_user
value = Setting.get(setting.name)
next if !current_user && (value == false || value.nil?)
config[setting.name] = value
end
# remember if we can switch back to user
if session[:switched_from_user_id]
config['switch_back_to_possible'] = true
end
# remember session_id for websocket logon
if current_user
config['session_id'] = session.id
end
config
end
2019-09-05 14:02:31 +00:00
end