Guest User

Untitled

a guest
Feb 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. class UsersController < ApplicationController
  2.  
  3. include Authentication::ControllerUtils
  4.  
  5. AUTHENTICATION_SETTINGS =
  6. {:action_on =>
  7. {:create => {
  8. :success => Proc.new { redirect_to_stored },
  9. :fail => Proc.new { render :action => :new }
  10. },
  11. :change_password => {
  12. :success => Proc.new { redirect_to @model }
  13. },
  14. :recover_password => {
  15. :success => Proc.new { redirect_to :action => :change_password }
  16. }
  17. }}
  18.  
  19. def new
  20. @user = User.new
  21. end
  22.  
  23. def create
  24. @model = model = auth_model.create(params[auth_model_name])
  25.  
  26. if model.errors.empty?
  27. session[auth_model_name] = model.id # auto signing in
  28. self.send("#{auth_model_name.to_s}=".to_sym, model)
  29. #self.mailer.deliver_signup(model)
  30. save_cookies({:id => model.id, :password => model.password})
  31. flash[:message] = "You've successfully created an account"
  32. action_on(:success)
  33. else
  34. self.send("#{auth_model_name.to_s}=".to_sym, model)
  35. action_on(:fail)
  36. end
  37. end
  38.  
  39. def change_password
  40. @model = model = auth_model.find(session[auth_model_name])
  41. if request.put?
  42. model.update_attributes(params[auth_model_name])
  43. if model.errors.empty?
  44. save_cookies({:password => model.password, :id => model.id})
  45. flash[:message] = "Password has been changed successfully"
  46. action_on(:success)
  47. end
  48. end
  49. # If get request or errors, then just render the form
  50. self.send("#{auth_model_name.to_s}=".to_sym, model)
  51. end
  52.  
  53. def lost_password
  54. if request.post?
  55. if model = auth_model.find_by_email(
  56. params[auth_model_name][:email]
  57. )
  58. model.set_password_token
  59. model.save!
  60. UserMailer.deliver_lost_password(model)
  61. flash[:message] = "You password recovery token has been emailed to you"
  62. else # just render form again
  63. flash.now[:error] = "Sorry, there's no such email in our database"
  64. end
  65. end
  66. end
  67.  
  68. def recover_password
  69. model = self.auth_model.find_by_password_token(params[:token]) if params[:token]
  70. if model && model.password_token_expires > Time.now
  71. session[auth_model_name] = model.id
  72. action_on(:success)
  73. else
  74. flash.now[:error] = "This token is invalid"
  75. end
  76. model.destroy_password_token if model
  77. end
  78.  
  79.  
  80. end
Add Comment
Please, Sign In to add comment