<source id="4vppl"><ins id="4vppl"></ins></source>
<u id="4vppl"><sub id="4vppl"><label id="4vppl"></label></sub></u>
<object id="4vppl"></object>
  • <u id="4vppl"><li id="4vppl"><label id="4vppl"></label></li></u>

    <object id="4vppl"></object>
    <b id="4vppl"><sub id="4vppl"><tr id="4vppl"></tr></sub></b>

      <i id="4vppl"><thead id="4vppl"></thead></i>

      <thead id="4vppl"><li id="4vppl"><label id="4vppl"></label></li></thead>

      當前位置:首頁 > 網站舊欄目 > 學習園地 > 設計軟件教程 > 每天一劑Rails良藥之Authentication

      每天一劑Rails良藥之Authentication
      2010-01-14 07:03:49  作者:  來源:
      今天我們來看看Rails怎么處理登錄認證
      雖然Rails有很多登錄認證的插件,但是我們可以自己動手豐衣足食

      1,db/migrate/001_add_user_table.rb
      Java代碼 復制代碼
      1. class AddUserTable < ActiveRecord::Migration   
      2.   def self.up   
      3.     create_table :users do |t|   
      4.       t.column :username, :string   
      5.       t.column :password_salt, :string   
      6.       t.column :password_hash, :string   
      7.     end   
      8.   end   
      9.   
      10.   def self.down   
      11.     drop_table :users   
      12.   end   
      13. end  


      2,app/models/user.rb
      Java代碼 復制代碼
      1. class User < ActiveRecord::Base   
      2.   validates_uniqueness_of :username   
      3.   
      4.   def password=(pass)   
      5.     salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp   
      6.     self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass + salt)   
      7.   end   
      8.   
      9.   def self.authenticate(username, password)   
      10.     user = User.find(:first, :conditions => ['username = ?', username])   
      11.     if user.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash   
      12.       raise "Username or password invalid"  
      13.     end   
      14.     user   
      15.   end   
      16. end  


      3,app/controllers/admin_controller.rb
      Java代碼 復制代碼
      1. class AdminController < ApplicationController   
      2.   before_filter :check_authentication, :except => [:signin_form, :signin]   
      3.   
      4.   def check_authentication   
      5.     unless session[:user]   
      6.       session[:intended_action] = action_name   
      7.       redirect_to :action => :signin_form   
      8.     end   
      9.   end   
      10.   
      11.   def signin   
      12.     session[:user] = User.authenticate(params[:username], params[:password]).id   
      13.     redirect_to :action => session[:intended_action]   
      14.   end   
      15.   
      16.   def signout   
      17.     session[:user] = nil   
      18.     redirect_to home_url   
      19.   end   
      20.   
      21. end  


      4,app/views/sign_form.rhtml
      Java代碼 復制代碼
      1. <html>   
      2.   <head>   
      3.     <title>Signin for Admin Access</title>   
      4.   </head>   
      5.   <body>   
      6.     <%= start_form_tag :action => "signin" %>   
      7.       <label for="username">Username:</label>   
      8.       <%= text_field_tag "username" %><br/>   
      9.       <label for="password">Password:</label>   
      10.       <%= text_field_tag "password" %><br/>   
      11.       <%= submit_tag "Sign in" %>   
      12.     <%= end_form_tag %>   
      13.   </body>   
      14. </html>  

      我們也可以將check_authentication方法放到application.rb里面,供其他Controllers使用

      安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢
      国产午夜福三级在线播放_亚洲精品成a人片在线观看_亚洲自慰一区二区三区_久久棈精品久久久久久噜噜
      <source id="4vppl"><ins id="4vppl"></ins></source>
      <u id="4vppl"><sub id="4vppl"><label id="4vppl"></label></sub></u>
      <object id="4vppl"></object>
    1. <u id="4vppl"><li id="4vppl"><label id="4vppl"></label></li></u>

      <object id="4vppl"></object>
      <b id="4vppl"><sub id="4vppl"><tr id="4vppl"></tr></sub></b>

        <i id="4vppl"><thead id="4vppl"></thead></i>

        <thead id="4vppl"><li id="4vppl"><label id="4vppl"></label></li></thead>
        在线观看国产99 | 亚洲不卡永远在线 | 亚洲中文色资源 | 中文字幕交换系列在线 | 最新国产啪精品视频网站免 | 亚洲色欧美国产综合 |