Guest User

Untitled

a guest
Mar 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. class PasswordsController < ApplicationController
  2.  
  3. before_filter :not_logged_in_required, :only => [:new, :create]
  4.  
  5. # Enter email address to recover password
  6. def new
  7. end
  8.  
  9. # Forgot password action
  10. def create
  11. return unless request.post?
  12. if @user = User.find_for_forget(params[:email])
  13. @user.forgot_password
  14. @user.save
  15. flash[:notice] = "A password reset link has been sent to your email address."
  16. redirect_to login_path
  17. else
  18. flash[:notice] = "Could not find a user with that email address."
  19. render :action => 'new'
  20. end
  21. end
  22.  
  23. # Action triggered by clicking on the /reset_password/:id link recieved via email
  24. # Makes sure the id code is included
  25. # Checks that the id code matches a user in the database
  26. # Then if everything checks out, shows the password reset fields
  27. def edit
  28. if params[:id].nil?
  29. render :action => 'new'
  30. return
  31. end
  32. @user = User.find_by_password_reset_code(params[:id]) if params[:id]
  33. raise if @user.nil?
  34. rescue
  35. logger.error "Invalid Reset Code entered."
  36. flash[:notice] = "Sorry - That is an invalid password reset code. Please check your code and try again. (Perhaps your email client inserted a carriage return?)"
  37. #redirect_back_or_default('/')
  38. redirect_to new_user_path
  39. end
  40.  
  41. # Reset password action /reset_password/:id
  42. # Checks once again that an id is included and makes sure that the password field isn't blank
  43. def update
  44. if params[:id].nil?
  45. render :action => 'new'
  46. return
  47. end
  48. if params[:password].blank?
  49. flash[:notice] = "Password field cannot be blank."
  50. render :action => 'edit', :id => params[:id]
  51. return
  52. end
  53. @user = User.find_by_password_reset_code(params[:id]) if params[:id]
  54. raise if @user.nil?
  55. return if @user unless params[:password]
  56. if (params[:password] == params[:password_confirmation])
  57. #Uncomment and comment lines with @user to have the user logged in after reset - not recommended
  58. #self.current_user = @user #for the next two lines to work
  59. #current_user.password_confirmation = params[:password_confirmation]
  60. #current_user.password = params[:password]
  61. #@user.reset_password
  62. #flash[:notice] = current_user.save ? "Password reset" : "Password not reset"
  63. @user.password_confirmation = params[:password_confirmation]
  64. @user.password = params[:password]
  65. @user.reset_password
  66. flash[:notice] = @user.save ? "Password reset." : "Password not reset."
  67. else
  68. flash[:notice] = "Password mismatch."
  69. render :action => 'edit', :id => params[:id]
  70. return
  71. end
  72. redirect_to login_path
  73. rescue
  74. logger.error "Invalid Reset Code entered"
  75. flash[:notice] = "Sorry - That is an invalid password reset code. Please check your code and try again. (Perhaps your email client inserted a carriage return?)"
  76. redirect_to new_user_path
  77. end
  78.  
  79. end
Add Comment
Please, Sign In to add comment