Guest User

Untitled

a guest
Mar 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. ## app/moodels/user.rb
  2. require 'digest/sha2'
  3. class User < ActiveRecord::Base
  4. def password=(pass)
  5. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  6. self.password_salt, self.password_hash =
  7. salt, Digest::SHA256.hexdigest(pass + salt)
  8. end
  9. end
  10.  
  11.  
  12. require 'digest/sha2'
  13. class User < ActiveRecord::Base
  14. validates_uniqueness_of :username
  15.  
  16. def self.authenticate(username, password)
  17. user = User.find(:first, :conditions => ['username = ?', username])
  18. if user.blank? ||
  19. Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
  20. raise "Username or password invalid"
  21. end
  22. user
  23. end
  24. end
  25.  
  26. ## apps/controllers/welcome_controller
  27.  
  28. class WelcomeController < ApplicationController
  29. before_filter :check_authentication, :except => [signin]
  30.  
  31. def check_authentication
  32. unless session[:user]
  33. session[:intended_action] = action_name
  34. session[:intended_controller] = controller_name
  35. redirect_to :action => "signin"
  36. end
  37. end
  38.  
  39. def signin
  40. session[:user] = User.authenticate(params[:username], params[:password]).id
  41. redirect_to :action => session[:intended_action],
  42. :controller => session[:intended_controller]
  43. end
  44.  
  45. end
  46.  
  47. ## app/views/welcome/signin.html.erb
  48.  
  49. <html>
  50. <head>
  51. <title>Signin for Admin Access</title>
  52. </head>
  53. <body>
  54. <%= start_form_tag :action => "signin" %>
  55. <label for="username" >Username:</label>
  56. <%= text_field_tag "username" %><br />
  57. <label for="password" >Password:</label>
  58. <%= password_field_tag "password" %><br />
  59. <%= submit_tag "Sign in" %>
  60. <%= end_form_tag %>
  61. </body>
  62. </html>
Add Comment
Please, Sign In to add comment