Sprankelprachtig aan/afmeldsysteem

authentication_controller.rb 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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[:warning] = "You forgot to add value"
  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[:success] = "Hello, #{u.person.full_name}!"
  15. redirect_to root_path
  16. elsif u and not u.confirmed
  17. flash[:warning] = "Your account has not been activated yet, please confirm using the email you have received."
  18. redirect_to action: 'login_form'
  19. else
  20. flash[:danger] = "Invalid username/password combination!"
  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. if not person
  41. flash[:warning] = "That email address is unknown!"
  42. redirect_to action: 'create_password_form'
  43. return
  44. end
  45. user = User.find_by(person: person)
  46. if user and user.confirmed
  47. flash[:warning] = "Your account has already been activated, please use the login form if you have forgotten your password."
  48. redirect_to action: 'login'
  49. return
  50. end
  51. if not 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[:success] = "An email has been sent, check your inbox!"
  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. if not user
  69. flash[:danger] = "That email address is not associated with any user."
  70. redirect_to action: 'forgotten_password_form'
  71. return
  72. end
  73. AuthenticationMailer::password_reset_email(user).deliver_later
  74. flash[:success] = "An email has been sent, check your inbox!"
  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. if not 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. if not token_valid? token
  87. return
  88. end
  89. if not params[:password] == params[:password_confirmation]
  90. flash[:warning] = "Password confirmation does not match your password!"
  91. redirect_to action: 'reset_password_form', token: params[:token]
  92. return
  93. end
  94. user = token.user
  95. user.password = params[:password_reset][:password]
  96. user.password_confirmation = params[:password_reset][:password_confirmation]
  97. user.save!
  98. token.destroy!
  99. flash[:success] = "Your password has been reset, you may now log in."
  100. redirect_to action: 'login'
  101. end
  102. def confirm_account_form
  103. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:account_confirmation])
  104. return unless token_valid? token
  105. @user = token.user
  106. render layout: 'void'
  107. end
  108. def confirm_account
  109. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:account_confirmation])
  110. return unless token_valid? token
  111. user = token.user
  112. user.password = params[:account_confirmation][:password]
  113. user.password_confirmation = params[:account_confirmation][:password_confirmation]
  114. user.confirmed = true
  115. user.save!
  116. token.destroy!
  117. flash[:success] = "Your account has been confirmed, you may now log in."
  118. redirect_to action: 'login'
  119. end
  120. private
  121. def session_params
  122. params.require(:session).permit(:email, :password, :remember_me)
  123. end
  124. def token_valid?(token)
  125. if token.nil?
  126. flash[:warning] = "No valid token specified!"
  127. redirect_to action: 'login'
  128. return false
  129. end
  130. if token.expires and token.expires < DateTime.now
  131. flash[:warning] = "That token has expired, please request a new one."
  132. redirect_to action: 'login'
  133. return false
  134. end
  135. true
  136. end
  137. end