Sprankelprachtig aan/afmeldsysteem

authentication_helper.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. end
  35. end
  36. end
  37. # Determine whether the user is logged in, and if so, disable the Session, then flush session cookies.
  38. def log_out
  39. if is_logged_in? and @user_session
  40. get_user_session
  41. @user_session.update!(active: false)
  42. end
  43. cookies.delete(:user_id)
  44. cookies.delete(:remember_token)
  45. cookies.delete(:session_id)
  46. reset_session
  47. end
  48. # Determine whether the current request is from a user with a non-expired session.
  49. # Makes @user_session available as a side effect if the user is not.
  50. def is_logged_in?
  51. # Case 1: User has an active session inside the cookie.
  52. # We verify that the session hasn't expired yet.
  53. if session[:user_id] && session[:expires].to_time > DateTime.now
  54. return true
  55. else
  56. # Case 2: User is returning and has a remember token saved.
  57. # We get the Session, check the token and expiry, and log the user in.
  58. if cookies.signed.permanent[:remember_token] && cookies.signed.permanent[:user_id] &&
  59. cookies.signed.permanent[:session_id]
  60. get_user_session
  61. if @user_session.nil? || @user_session.remember_digest.nil?
  62. return false
  63. end
  64. session_password = BCrypt::Password.new @user_session.remember_digest
  65. if @user_session.expires > DateTime.now &&
  66. session_password == cookies.signed.permanent[:remember_token]
  67. log_in @user_session.user, false, false
  68. return true
  69. end
  70. return false
  71. end
  72. return false
  73. end
  74. end
  75. # Get the Session object representing the current user's session.
  76. def get_user_session
  77. if @user_session
  78. @user_session
  79. else
  80. @user_session ||= Session.find(
  81. cookies.signed.permanent[:session_id]
  82. )
  83. end
  84. # Edge case if a session no longer exists in the database
  85. if not @user_session
  86. log_out
  87. redirect_to login_path # FIXME!
  88. end
  89. end
  90. def current_user
  91. get_user_session
  92. @user_session.user
  93. end
  94. def current_person
  95. current_user.person
  96. end
  97. def require_login!
  98. if !is_logged_in?
  99. flash_message(:warning, I18n.t('authentication.login_required'))
  100. redirect_to controller: 'authentication', action: 'login_form'
  101. end
  102. end
  103. def require_admin!
  104. if !current_person.is_admin?
  105. flash_message(:danger, I18n.t('authentication.admin_required'))
  106. redirect_to '/dashboard'
  107. end
  108. end
  109. end