has_user.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. module ApplicationController::HasUser
  2. extend ActiveSupport::Concern
  3. included do
  4. before_action :set_user, :session_update
  5. end
  6. private
  7. def current_user
  8. user_on_behalf = current_user_on_behalf
  9. return user_on_behalf if user_on_behalf
  10. current_user_real
  11. end
  12. # Finds the User with the ID stored in the session with the key
  13. # :current_user_id This is a common way to handle user login in
  14. # a Rails application; logging in sets the session value and
  15. # logging out removes it.
  16. def current_user_real
  17. return @_current_user if @_current_user
  18. return if !session[:user_id]
  19. @_current_user = User.lookup(id: session[:user_id])
  20. end
  21. # Finds the user based on the id, login or email which is given
  22. # in the headers. If it is found then all api activities are done
  23. # with the behalf of user. With this functionality it is possible
  24. # to do changes with a user which is different from the admin user.
  25. # E.g. create a ticket as a customer user based on a user with admin rights.
  26. def current_user_on_behalf
  27. # check header
  28. return if request.headers['X-On-Behalf-Of'].blank?
  29. # return user if set
  30. return @_user_on_behalf if @_user_on_behalf
  31. # get current user
  32. user_real = current_user_real
  33. return if !user_real
  34. # check if the user has admin rights
  35. raise Exceptions::NotAuthorized, "Current user has no permission to use 'X-On-Behalf-Of'!" if !user_real.permissions?('admin.user')
  36. # find user for execution based on the header
  37. %i[id login email].each do |field|
  38. search_attributes = {}
  39. search_attributes[field] = request.headers['X-On-Behalf-Of']
  40. @_user_on_behalf = User.find_by(search_attributes)
  41. next if !@_user_on_behalf
  42. return @_user_on_behalf
  43. end
  44. # no behalf of user found
  45. raise Exceptions::NotAuthorized, "No such user '#{request.headers['X-On-Behalf-Of']}'"
  46. end
  47. def current_user_set(user, auth_type = 'session')
  48. session[:user_id] = user.id
  49. @_auth_type = auth_type
  50. @_current_user = user
  51. set_user
  52. end
  53. # Sets the current user into a named Thread location so that it can be accessed
  54. # by models and observers
  55. def set_user
  56. if !current_user
  57. UserInfo.current_user_id = 1
  58. return
  59. end
  60. UserInfo.current_user_id = current_user.id
  61. end
  62. # update session updated_at
  63. def session_update
  64. #sleep 0.6
  65. session[:ping] = Time.zone.now.iso8601
  66. # check if remote ip need to be updated
  67. if session[:user_id]
  68. if !session[:remote_ip] || session[:remote_ip] != request.remote_ip
  69. session[:remote_ip] = request.remote_ip
  70. session[:geo] = Service::GeoIp.location(request.remote_ip)
  71. end
  72. end
  73. # fill user agent
  74. return if session[:user_agent]
  75. session[:user_agent] = request.env['HTTP_USER_AGENT']
  76. end
  77. def valid_session_with_user
  78. return true if current_user
  79. raise Exceptions::UnprocessableEntity, 'No session user!'
  80. end
  81. end