Guest User

Untitled

a guest
Mar 9th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. class ApplicationController < ActionController::Base
  2. # Pick a unique cookie name to distinguish our session data from others'
  3. session :session_key => '_aesolutions_session_id'
  4.  
  5. # Ensure all users are authenticated
  6. before_filter :check_authentication
  7.  
  8. user_id = session[:user]
  9. @user = User.find(user_id)
  10.  
  11. def check_authentication
  12. unless session[:user]
  13. session[:intended_action] = action_name
  14. session[:intended_controller] = controller_name
  15. redirect_to :action => "signin", :controller => "user"
  16. end
  17. end
  18.  
  19. end
  20.  
  21. class UserController < ApplicationController
  22. before_filter :check_authentication, :except => [:signin]
  23.  
  24. # Allows a user to log in.
  25. def signin
  26. if request.post?
  27. begin
  28. session[:user] = User.authenticate(params[:username], params[:password]).id
  29. redirect_to :action => 'list', :controller => 'companies'
  30. # redirect_to :action => session[:intended_action],
  31. # :controller => session[:intended_controller]
  32. rescue
  33. flash[:error] = "Username or password invalid."
  34. end
  35. end
  36. end
  37.  
  38. # Logs a user out.
  39. def signout
  40. session[:user] = nil
  41. redirect_to login_url
  42. end
  43.  
  44. end
  45.  
  46. require 'digest/sha2'
  47. class User < ActiveRecord::Base
  48.  
  49. validates_uniqueness_of :username => '- that username already exists.'
  50.  
  51. def self.authenticate(username, password)
  52. user = User.find(:first, :conditions => ['username = ?', username])
  53. if user.blank? ||
  54. Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
  55. raise "Username or password invalid"
  56. end
  57. user
  58. end
  59.  
  60. def password=(pass)
  61. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  62. self.password_salt, self.password_hash =
  63. salt, Digest::SHA256.hexdigest(pass + salt)
  64. end
  65.  
  66. end
  67.  
  68.  
  69. For my application controller:
  70.  
  71. user_id = session[:user]
  72. @user = User.find(user_id)
  73.  
  74. The first line causes:
  75.  
  76. Symbol as array index
  77. ./script/../config/../app/controllers/application.rb:11:in `[]'
Add Comment
Please, Sign In to add comment