|
@@ -10,10 +10,7 @@ module ApplicationController::HasUser
|
|
private
|
|
private
|
|
|
|
|
|
def current_user
|
|
def current_user
|
|
- user_on_behalf = current_user_on_behalf
|
|
|
|
- return user_on_behalf if user_on_behalf
|
|
|
|
-
|
|
|
|
- current_user_real
|
|
|
|
|
|
+ current_user_on_behalf || current_user_real
|
|
end
|
|
end
|
|
|
|
|
|
# Finds the User with the ID stored in the session with the key
|
|
# Finds the User with the ID stored in the session with the key
|
|
@@ -21,10 +18,7 @@ module ApplicationController::HasUser
|
|
# a Rails application; logging in sets the session value and
|
|
# a Rails application; logging in sets the session value and
|
|
# logging out removes it.
|
|
# logging out removes it.
|
|
def current_user_real
|
|
def current_user_real
|
|
- return @_current_user if @_current_user
|
|
|
|
- return if !session[:user_id]
|
|
|
|
-
|
|
|
|
- @_current_user = User.lookup(id: session[:user_id])
|
|
|
|
|
|
+ @_current_user ||= User.lookup(id: session[:user_id]) # rubocop:disable Naming/MemoizedInstanceVariableName
|
|
end
|
|
end
|
|
|
|
|
|
# Finds the user based on the id, login or email which is given
|
|
# Finds the user based on the id, login or email which is given
|
|
@@ -33,42 +27,24 @@ module ApplicationController::HasUser
|
|
# to do changes with a user which is different from the admin user.
|
|
# to do changes with a user which is different from the admin user.
|
|
# E.g. create a ticket as a customer user based on a user with admin rights.
|
|
# E.g. create a ticket as a customer user based on a user with admin rights.
|
|
def current_user_on_behalf
|
|
def current_user_on_behalf
|
|
-
|
|
|
|
- # check header
|
|
|
|
- return if request.headers['X-On-Behalf-Of'].blank?
|
|
|
|
-
|
|
|
|
- # return user if set
|
|
|
|
- return @_user_on_behalf if @_user_on_behalf
|
|
|
|
-
|
|
|
|
- # get current user
|
|
|
|
- user_real = current_user_real
|
|
|
|
- return if !user_real
|
|
|
|
-
|
|
|
|
- # check if the user has admin rights
|
|
|
|
- raise Exceptions::Forbidden, "Current user has no permission to use 'X-On-Behalf-Of'!" if !user_real.permissions?('admin.user')
|
|
|
|
|
|
+ return if request.headers['X-On-Behalf-Of'].blank? # require header
|
|
|
|
+ return @_user_on_behalf if @_user_on_behalf # return memoized user
|
|
|
|
+ return if !current_user_real # require session user
|
|
|
|
+ if !SessionsPolicy.new(current_user_real, Sessions).impersonate?
|
|
|
|
+ raise Exceptions::Forbidden, "Current user has no permission to use 'X-On-Behalf-Of'!"
|
|
|
|
+ end
|
|
|
|
|
|
# find user for execution based on the header
|
|
# find user for execution based on the header
|
|
%i[id login email].each do |field|
|
|
%i[id login email].each do |field|
|
|
- search_attributes = search_attributes(field)
|
|
|
|
- @_user_on_behalf = User.find_by(search_attributes)
|
|
|
|
- next if !@_user_on_behalf
|
|
|
|
|
|
+ @_user_on_behalf = User.find_by(field => request.headers['X-On-Behalf-Of'].to_s.downcase.strip)
|
|
|
|
|
|
- return @_user_on_behalf
|
|
|
|
|
|
+ return @_user_on_behalf if @_user_on_behalf
|
|
end
|
|
end
|
|
|
|
|
|
# no behalf of user found
|
|
# no behalf of user found
|
|
raise Exceptions::Forbidden, "No such user '#{request.headers['X-On-Behalf-Of']}'"
|
|
raise Exceptions::Forbidden, "No such user '#{request.headers['X-On-Behalf-Of']}'"
|
|
end
|
|
end
|
|
|
|
|
|
- def search_attributes(field)
|
|
|
|
- search_attributes = {}
|
|
|
|
- search_attributes[field] = request.headers['X-On-Behalf-Of']
|
|
|
|
- if %i[login email].include?(field)
|
|
|
|
- search_attributes[field] = search_attributes[field].to_s.downcase.strip
|
|
|
|
- end
|
|
|
|
- search_attributes
|
|
|
|
- end
|
|
|
|
-
|
|
|
|
def current_user_set(user, auth_type = 'session')
|
|
def current_user_set(user, auth_type = 'session')
|
|
session[:user_id] = user.id
|
|
session[:user_id] = user.id
|
|
@_auth_type = auth_type
|
|
@_auth_type = auth_type
|
|
@@ -79,11 +55,7 @@ module ApplicationController::HasUser
|
|
# Sets the current user into a named Thread location so that it can be accessed
|
|
# Sets the current user into a named Thread location so that it can be accessed
|
|
# by models and observers
|
|
# by models and observers
|
|
def set_user
|
|
def set_user
|
|
- if !current_user
|
|
|
|
- UserInfo.current_user_id = 1
|
|
|
|
- return
|
|
|
|
- end
|
|
|
|
- UserInfo.current_user_id = current_user.id
|
|
|
|
|
|
+ UserInfo.current_user_id = current_user&.id || 1
|
|
end
|
|
end
|
|
|
|
|
|
# update session updated_at
|
|
# update session updated_at
|