Guest User

Untitled

a guest
Apr 7th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #-----Model
  2.  
  3. require 'digest/sha1'
  4. class User < ActiveRecord::Base
  5.  
  6.  
  7.  
  8.  
  9. # ---------------------------------------
  10. # The following code has been generated by role_requirement.
  11. # You may wish to modify it to suit your need
  12. has_and_belongs_to_many :roles, :join_table => 'roles_users'
  13. #belongs_to :role
  14. attr_protected :roles
  15.  
  16.  
  17. # has_role? simply needs to return true or false whether a user has a role or not.
  18. # It may be a good idea to have "admin" roles return true always
  19. def has_role?(role_in_question)
  20. @_list ||= self.roles.collect(&:name)
  21. return true if @_list.include?("admin")
  22. (@_list.include?(role_in_question.to_s) )
  23. end
  24. # ---------------------------------------
  25.  
  26.  
  27.  
  28.  
  29. # Virtual attribute for the unencrypted password
  30. attr_accessor :password
  31.  
  32. validates_presence_of :login, :email
  33. validates_presence_of :password, :if => :password_required?
  34. validates_presence_of :password_confirmation, :if => :password_required?
  35. validates_length_of :password, :within => 4..40, :if => :password_required?
  36. validates_confirmation_of :password, :if => :password_required?
  37. validates_length_of :login, :within => 3..40
  38. validates_length_of :email, :within => 3..100
  39. validates_uniqueness_of :login, :email, :case_sensitive => false
  40. before_save :encrypt_password
  41.  
  42. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  43. def self.authenticate(login, password)
  44. u = find_by_login(login) # need to get the salt
  45. u && u.authenticated?(password) ? u : nil
  46.  
  47. end
  48.  
  49. # Encrypts some data with the salt.
  50. def self.encrypt(password, salt)
  51. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  52. end
  53.  
  54. # Encrypts the password with the user salt
  55. def encrypt(password)
  56. self.class.encrypt(password, salt)
  57. end
  58.  
  59. def authenticated?(password)
  60. crypted_password == encrypt(password)
  61. end
  62.  
  63. def remember_token?
  64. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  65. end
  66.  
  67. # These create and unset the fields required for remembering users between browser closes
  68. def remember_me
  69. self.remember_token_expires_at = 2.weeks.from_now.utc
  70. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  71. save(false)
  72. end
  73.  
  74. def forget_me
  75. self.remember_token_expires_at = nil
  76. self.remember_token = nil
  77. save(false)
  78. end
  79.  
  80. protected
  81. # before filter
  82. def encrypt_password
  83. return if password.blank?
  84. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  85. self.crypted_password = encrypt(password)
  86. end
  87.  
  88. def password_required?
  89. crypted_password.blank? || !password.blank?
  90. end
  91. def label
  92. login
  93. end
  94. end
  95.  
  96.  
  97.  
  98.  
  99. #------controller
  100.  
  101. class UsersController < ApplicationController
  102. # Be sure to include AuthenticationSystem in Application Controller instead
  103. require_role "user"
  104. before_filter :load_user, :login_required
  105. def load_user
  106. @user=current_user
  107. rolearr=@user.roles
  108. rolearr.each do |a|
  109. @role=a.name
  110. end
  111. end
  112. # render new.rhtml
  113. def new
  114. @roles=Role.find:all
  115.  
  116. end
  117.  
  118. active_scaffold :user do |config|
  119.  
  120. config.columns = [:id,:login, :email,:created_at, :roles]
  121. config.update.columns = [:login, :email,:password,:password_confirmation]
  122.  
  123. end
  124.  
  125.  
  126.  
  127.  
  128. def create
  129.  
  130. cookies.delete :auth_token
  131. # protects against session fixation attacks, wreaks havoc with
  132. # request forgery protection.
  133. # uncomment at your own risk
  134. # reset_session
  135. @user = User.new(params[:user])
  136. @user.save
  137.  
  138.  
  139. if @user.errors.empty
  140. self.current_user = @user
  141. redirect_back_or_default('/')
  142. flash[:notice] = "Thanks for signing up!"
  143. else
  144. render :action => 'new'
  145. end
  146.  
  147.  
  148. end
  149.  
  150. end
  151.  
  152.  
  153. #-----View
  154.  
  155. <%= error_messages_for :user %>
  156. <% form_for :user, :url => users_path do |f| -%>
  157. <p><label for="login">Login</label><br/>
  158. <%= f.text_field :login %></p>
  159.  
  160. <p><label for="email">Email</label><br/>
  161. <%= f.text_field :email %></p>
  162.  
  163. <p><label for="password">Password</label><br/>
  164. <%= f.password_field :password %></p>
  165.  
  166. <p><label for="password_confirmation">Confirm Password</label><br/>
  167. <%= f.password_field :password_confirmation %></p>
  168.  
  169.  
  170. <select id="role" name="role">
  171. <p><label for="role">role</label><br/>
  172. <%= options_from_collection_for_select(@roles,"id","name") %></p>
  173. </select>
  174. <p><%= submit_tag 'Sign up' %></p>
  175. <% end -%>
Add Comment
Please, Sign In to add comment