Guest User

Untitled

a guest
Jul 9th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. class HomeController < ApplicationController
  2. layout "main"
  3. before_filter :config, :except => [:login, :login_auth]
  4.  
  5. def index
  6. @xs_menu_item_name = "home"
  7. @xs_menu_subitem_name = ""
  8. end
  9.  
  10. def login
  11. if session[:user] != nil
  12. redirect_to :action => "index"
  13. else
  14. render :layout => false
  15. end
  16. end
  17. verify :method => :post, :only => [ :login_auth ],
  18. :redirect_to => { :action => :index }
  19.  
  20. def login_auth
  21. login = Users.authenticate(params[:home][:username],params[:home][:password])
  22. if login != false
  23. session[:user] = login
  24. redirect_to :action => "index"
  25. else
  26. session[:user] = nil
  27. flash[:error] = "<div class='error'><strong>KLAIDA:</strong> Prisijungti Nepavyko</div>"
  28. render :action => 'login',:layout => false
  29. end
  30. end
  31. def logout
  32. session[:user] = nil
  33. redirect_to :action => "login"
  34. end
  35. protected
  36.  
  37. def config
  38. @page_title = "Namai"
  39. if (session[:user] == nil)
  40. redirect_to :controller => "home", :action => "login"
  41. end
  42. end
  43. end
  44.  
  45.  
  46. def self.authenticate(username, pass)
  47. user = find(:first, :conditions => ['username = ?',username])
  48.  
  49. if Password::check(pass,user.h_password)
  50. user
  51. else
  52. return false
  53. end
  54. end
  55.  
  56. protected
  57.  
  58. # Hash the password before saving the record
  59. def before_save
  60. self.h_password = Password::update(self.password)
  61. end
  62.  
  63. require 'digest/sha2'
  64.  
  65. module Password
  66.  
  67. def Password.update(password)
  68. salt = self.salt
  69. hash = self.hash(password,salt)
  70. self.store(hash, salt)
  71. end
  72.  
  73. def Password.check(password, store)
  74. hash = self.get_hash(store)
  75. salt = self.get_salt(store)
  76. if self.hash(password,salt) == hash
  77. true
  78.  
  79. else
  80. false
  81. end
  82. end
  83.  
  84. protected
  85.  
  86. def Password.salt
  87. salt = ''
  88. 64.times { salt << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
  89. salt
  90. end
  91.  
  92. def Password.hash(password,salt)
  93. Digest::SHA512.hexdigest("#{password}:#{salt}")
  94. end
  95.  
  96. def Password.store(hash, salt)
  97. hash + salt
  98. end
  99.  
  100. def Password.get_hash(store)
  101. store[0..127]
  102. end
  103.  
  104. def Password.get_salt(store)
  105. store[128..192]
  106. end
  107. end
Add Comment
Please, Sign In to add comment