Guest User

Untitled

a guest
Mar 8th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. # NOTE: current_user is a method that returns an object for the currently logged in user
  2.  
  3. ## dashboard_controller.rb
  4.  
  5. class DashboardController < ApplicationController
  6. before_filter :login_required
  7.  
  8. def index
  9. end
  10.  
  11. def profile
  12. @user = current_user
  13. if request.post?
  14. if @user.update_attributes(params[:user])
  15. flash[:notice] = 'Profile was successfully updated.'
  16. redirect_to :controller => 'dashboard' }
  17. else
  18. render :action => 'profile'
  19. end
  20. end
  21. end
  22. end
  23.  
  24. ## dashboard/profile.rhtml
  25.  
  26. <% title 'My Profile' %>
  27.  
  28. <pre><%= @user.to_yaml %></pre>
  29.  
  30. <%= error_messages_for :user %>
  31. <% form_for :user, @user, :url => { :action => 'profile' } do |f| %>
  32.  
  33. <p><label for="first_name">First Name</label><br/>
  34. <%= f.text_field :first_name %></p>
  35.  
  36. <p><label for="last_name">Last Name</label><br/>
  37. <%= f.text_field :last_name %></p>
  38.  
  39. <p><label for="city">City</label><br/>
  40. <%= f.text_field :city %></p>
  41.  
  42. <p><label for="state">State</label><br/>
  43. <%= f.text_field :state %></p>
  44.  
  45. <p><label for="zip_code">Zip Code</label><br/>
  46. <%= f.text_field :zip_code %></p>
  47.  
  48. <p><label for="country">Country</label><br/>
  49. <%= f.text_field :country %></p>
  50.  
  51. <p><%= submit_tag 'Submit' %></p>
  52. <% end -%>
  53.  
  54. ## user.rb
  55.  
  56. require 'digest/sha1'
  57. class User < ActiveRecord::Base
  58. # Virtual attribute for the unencrypted password
  59. attr_accessor :password
  60.  
  61. validates_presence_of :login, :email
  62. validates_presence_of :password, :if => :password_required?
  63. validates_presence_of :password_confirmation, :if => :password_required?
  64. validates_length_of :password, :within => 4..40, :if => :password_required?
  65. validates_confirmation_of :password, :if => :password_required?
  66. validates_length_of :login, :within => 3..40
  67. validates_length_of :email, :within => 3..100
  68. validates_uniqueness_of :login, :email, :case_sensitive => false
  69. before_save :encrypt_password
  70.  
  71. # prevents a user from submitting a crafted form that bypasses activation
  72. # anything else you want your user to change should be added here.
  73. attr_accessible :login, :email, :password, :password_confirmation
  74.  
  75. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  76. def self.authenticate(login, password)
  77. u = find_by_login(login) # need to get the salt
  78. u && u.authenticated?(password) ? u : nil
  79. end
  80.  
  81. # Encrypts some data with the salt.
  82. def self.encrypt(password, salt)
  83. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  84. end
  85.  
  86. # Encrypts the password with the user salt
  87. def encrypt(password)
  88. self.class.encrypt(password, salt)
  89. end
  90.  
  91. def authenticated?(password)
  92. crypted_password == encrypt(password)
  93. end
  94.  
  95. def remember_token?
  96. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  97. end
  98.  
  99. # These create and unset the fields required for remembering users between browser closes
  100. def remember_me
  101. remember_me_for 2.weeks
  102. end
  103.  
  104. def remember_me_for(time)
  105. remember_me_until time.from_now.utc
  106. end
  107.  
  108. def remember_me_until(time)
  109. self.remember_token_expires_at = time
  110. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  111. save(false)
  112. end
  113.  
  114. def forget_me
  115. self.remember_token_expires_at = nil
  116. self.remember_token = nil
  117. save(false)
  118. end
  119.  
  120. protected
  121. # before filter
  122. def encrypt_password
  123. return if password.blank?
  124. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  125. self.crypted_password = encrypt(password)
  126. end
  127.  
  128. def password_required?
  129. crypted_password.blank? || !password.blank?
  130. end
  131.  
  132. end
Add Comment
Please, Sign In to add comment