Sprankelprachtig aan/afmeldsysteem

authentication_helper.rb 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. module AuthenticationHelper
  2. # Create a new Session and set the relevant cookies.
  3. def log_in(user, remember, new = true)
  4. reset_session
  5. expiry = 6.hours.since
  6. session[:user_id] = user.id
  7. session[:expires] = expiry
  8. if new
  9. if remember == 1
  10. token = Session.new_token
  11. expiry = 1.years.since
  12. cookies.signed.permanent[:remember_token] = {
  13. value: token,
  14. httponly: true
  15. }
  16. cookies.signed.permanent[:user_id] = {
  17. value: user.id,
  18. httponly: true
  19. }
  20. else
  21. token = nil
  22. end
  23. s = Session.create!(
  24. user: user,
  25. ip: request.remote_ip,
  26. expires: expiry,
  27. remember_digest: token ? Session.digest(token) : nil
  28. )
  29. if remember
  30. cookies.signed.permanent[:session_id] = {
  31. value: s.id,
  32. httponly: true
  33. }
  34. else
  35. session[:session_id] = s.id
  36. end
  37. end
  38. end
  39. # Determine whether the user is logged in, and if so, disable the Session, then flush session cookies.
  40. def log_out(session_broken = false)
  41. if !session_broken && is_logged_in? && @user_session
  42. get_user_session
  43. @user_session.update!(active: false)
  44. end
  45. cookies.delete(:user_id)
  46. cookies.delete(:remember_token)
  47. cookies.delete(:session_id)
  48. reset_session
  49. end
  50. # Determine whether the current request is from a user with a non-expired session.
  51. # Makes @user_session available as a side effect if the user is not.
  52. def is_logged_in?
  53. # Case 1: User has an active session inside the cookie.
  54. # We verify that the session hasn't expired yet.
  55. if session[:user_id] && session[:expires].to_time > DateTime.now
  56. get_user_session
  57. return false if !@user_session.active || @user_session.expires < Time.now
  58. return true
  59. else
  60. # Case 2: User is returning and has a remember token saved.
  61. # We get the Session, check the token and expiry, and log the user in.
  62. if cookies.signed.permanent[:remember_token] && cookies.signed.permanent[:user_id] &&
  63. cookies.signed.permanent[:session_id]
  64. get_user_session
  65. if @user_session.nil? || @user_session.remember_digest.nil?
  66. return false
  67. end
  68. session_password = BCrypt::Password.new @user_session.remember_digest
  69. if @user_session.expires > DateTime.now &&
  70. session_password == cookies.signed.permanent[:remember_token]
  71. log_in @user_session.user, false, false
  72. return true
  73. end
  74. return false
  75. end
  76. return false
  77. end
  78. end
  79. # Get the Session object representing the current user's session.
  80. def get_user_session
  81. if @user_session
  82. @user_session
  83. else
  84. id = cookies.signed.permanent[:session_id] || session[:session_id]
  85. @user_session ||= Session.find_by(id: id)
  86. end
  87. # Edge case if a session no longer exists in the database
  88. if not @user_session
  89. log_out(session_broken = true)
  90. end
  91. end
  92. def current_user
  93. get_user_session
  94. @user_session&.user
  95. end
  96. def current_person
  97. current_user&.person
  98. end
  99. def require_login!
  100. if !is_logged_in?
  101. flash_message(:warning, I18n.t('authentication.login_required'))
  102. redirect_to controller: 'authentication', action: 'login_form'
  103. return false
  104. end
  105. Raven.user_context(
  106. user_firstname: current_person.first_name
  107. )
  108. return true
  109. end
  110. def require_admin!
  111. if !current_person.is_admin?
  112. flash_message(:danger, I18n.t('authentication.admin_required'))
  113. redirect_to '/dashboard'
  114. end
  115. end
  116. end