Sprankelprachtig aan/afmeldsysteem

authentication_controller.rb 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. class AuthenticationController < ApplicationController
  2. before_action :require_login!, only: [:logout_confirm, :logout]
  3. def login_form
  4. render layout: 'void'
  5. end
  6. def login
  7. if params[:session][:email].blank? || params[:session][:password].blank?
  8. flash_message(:warning, I18n.t(:value_required))
  9. redirect_to action: 'login_form'
  10. else
  11. u = User.find_by(email: params[:session][:email])
  12. if u && u.confirmed && u.authenticate(params[:session][:password])
  13. log_in(u, params[:session][:remember_me].to_i)
  14. flash_message(:success, I18n.t(:greeting, name: u.person.first_name))
  15. redirect_to root_path
  16. elsif u && !u.confirmed
  17. flash_message(:warning, I18n.t('authentication.activation_required'))
  18. redirect_to action: 'login_form'
  19. else
  20. flash_message(:danger, I18n.t('authentication.invalid_user_or_pass'))
  21. redirect_to action: 'login_form'
  22. end
  23. end
  24. end
  25. def logout_confirm
  26. render layout: 'void'
  27. end
  28. def logout
  29. log_out
  30. redirect_to login_path
  31. end
  32. def create_password_form
  33. render layout: 'void'
  34. end
  35. def login_status
  36. render text: is_logged_in?
  37. end
  38. def create_password
  39. person = Person.find_by(email: params[:user][:email])
  40. unless person
  41. flash_message(:warning, I18n.t('authentication.unknown_email'))
  42. redirect_to action: 'create_password_form'
  43. return
  44. end
  45. user = User.find_by(person: person)
  46. if user && user.confirmed
  47. flash_message(:warning, I18n.t('authentication.already_activated'))
  48. redirect_to action: 'login'
  49. return
  50. end
  51. unless user
  52. user = User.new
  53. user.person = person
  54. user.email = person.email
  55. user.password = user.password_confirmation = SecureRandom::urlsafe_base64 32
  56. user.confirmed = false
  57. user.save!
  58. end
  59. AuthenticationMailer::password_confirm_email(user).deliver_now
  60. flash_message(:success, I18n.t('authentication.emails.sent'))
  61. redirect_to action: 'login'
  62. end
  63. def forgotten_password_form
  64. render layout: 'void'
  65. end
  66. def forgotten_password
  67. user = User.find_by(email: params[:password_reset][:email])
  68. unless user
  69. flash_message(:danger, I18n.t('authentication.unknown_email'))
  70. redirect_to action: 'forgotten_password_form'
  71. return
  72. end
  73. AuthenticationMailer::password_reset_email(user).deliver_later
  74. flash_message(:success, I18n.t('authentication.emails.sent'))
  75. redirect_to action: 'login'
  76. end
  77. def reset_password_form
  78. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:password_reset])
  79. unless token_valid? token
  80. return
  81. end
  82. render layout: 'void'
  83. end
  84. def reset_password
  85. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:password_reset])
  86. return unless token_valid? token
  87. if params[:password_reset][:password].blank?
  88. flash_message :warning, I18n.t('authentication.password_blank')
  89. render 'authentication/reset_password_form', layout: 'void'
  90. return
  91. end
  92. unless params[:password_reset][:password] == params[:password_reset][:password_confirmation]
  93. flash_message(:warning, I18n.t('authentication.password_repeat_mismatch'))
  94. redirect_to action: 'reset_password_form', token: params[:token]
  95. return
  96. end
  97. user = token.user
  98. user.password = params[:password_reset][:password]
  99. user.password_confirmation = params[:password_reset][:password_confirmation]
  100. user.save!
  101. token.destroy!
  102. flash_message(:success, I18n.t('authentication.password_reset_complete'))
  103. redirect_to action: 'login'
  104. end
  105. def confirm_account_form
  106. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:account_confirmation])
  107. return unless token_valid? token
  108. @user = token.user
  109. render layout: 'void'
  110. end
  111. def confirm_account
  112. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:account_confirmation])
  113. return unless token_valid? token
  114. user = token.user
  115. user.password = params[:account_confirmation][:password]
  116. user.password_confirmation = params[:account_confirmation][:password_confirmation]
  117. user.confirmed = true
  118. user.save!
  119. token.destroy!
  120. flash_message(:success, I18n.t('authentication.activation_complete'))
  121. redirect_to action: 'login'
  122. end
  123. private
  124. def session_params
  125. params.require(:session).permit(:email, :password, :remember_me)
  126. end
  127. def token_valid?(token)
  128. if token.nil?
  129. flash_message(:warning, I18n.t('authentication.invalid_token'))
  130. redirect_to action: 'login'
  131. return false
  132. end
  133. if token.expires && (token.expires < DateTime.now)
  134. flash_message(:warning, I18n.t('authentication.token_expired'))
  135. redirect_to action: 'login'
  136. return false
  137. end
  138. true
  139. end
  140. end