<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>

      當(dāng)前位置:首頁 > 網(wǎng)站舊欄目 > 學(xué)習(xí)園地 > 設(shè)計軟件教程 > 每天一劑Rails良藥之Role-Based Authorization

      每天一劑Rails良藥之Role-Based Authorization
      2010-01-14 07:04:21  作者:  來源:
      我們的系統(tǒng)往往并不只是靠登錄這么簡單來控制權(quán)限,今天我們來看看基于角色的授權(quán)
      假設(shè)我們的系統(tǒng)已經(jīng)建立了昨天的users表

      1,migration
      Java代碼 復(fù)制代碼
      1. class AddRolesAndRightsTables < ActiveRecord::Migration   
      2.   def self.up   
      3.     create_table :users_roles, :id => false do |t|   
      4.       t.column :user_id, :integer   
      5.       t.column :role_id, :integer   
      6.     end   
      7.   
      8.     create_table :roles, :do |t|   
      9.       t.column :name, :string   
      10.     end   
      11.   
      12.     create_table :roles_rights, :id => false do |t|   
      13.       t.column :role_id, :integer   
      14.       t.column :right_id, :integer   
      15.     end   
      16.   
      17.     create_table :rights do |t|   
      18.       t.column :name, :string   
      19.       t.column :controller, :string   
      20.       t.column :action, :string   
      21.     end   
      22.   end   
      23.   
      24.   def self.down   
      25.     drop_table :users_roles   
      26.     drop_table :roles   
      27.     drop_table :rights   
      28.     drop_table :rights_roles   
      29.   end   
      30. end  


      2,model
      Java代碼 復(fù)制代碼
      1. class User < ActiveRecord::Base   
      2.   has_and_belongs_to_many :roles   
      3. end   
      4.   
      5. class Role < ActiveRecord::Base   
      6.   has_and_belongs_to_many :users   
      7.   has_and_belongs_to_many :rights   
      8. end   
      9.   
      10. class Right < ActiveRecord::Base   
      11.   has_and_belongs_to_many :roles   
      12. end  


      3,application.rb
      Java代碼 復(fù)制代碼
      1. class ApplicationController < ActionController::Base   
      2.   layout 'standard'  
      3.   before_filter :check_authentication,   
      4.                 :check_authorization,   
      5.                 :except => [:signin_form, :signin]   
      6.   def check_authentication   
      7.     unless session[:user]   
      8.       session[:intended_action] = action_name   
      9.       redirect_to :controller => :admin, :action => signin_form   
      10.       return false  
      11.     end   
      12.   end   
      13.   
      14.   def check_authorization   
      15.     user = User.find(session[:user])   
      16.     unless user.roles.detect{|role|   
      17.         role.rights.detect{|right|   
      18.           right.action == action_name && right.controller == controller_name   
      19.         }   
      20.       }   
      21.       flash[:notice] = "You are not authorized to view the page you requested"  
      22.       request.env["HTTP_REFERER"] ? (redirect_to :back) : (redirect_to home_url)   
      23.       return false  
      24.     end   
      25.   end   
      26. end   
      27. end  


      4,layout
      Java代碼 復(fù)制代碼
      1. <% if flash[:notice] %>   
      2.   <div class="errors">   
      3.     <% flash[:notice] %>   
      4.   </div>   
      5. <% end %>  


      如果我們的某一個controller或者action不想要check_authentication和check_authorization這兩個filter,我們可以skip掉:
      Java代碼 復(fù)制代碼
      1. class HomeController < ApplicationController   
      2.   skip_before_filter :check_authentication, :check_authorization   
      3.   def index   
      4.     render :text => "A page that doesn't require a signin or any rights"  
      5.   end   
      6. end  

      但這只能精確到controller和action級別的權(quán)限控制
      如果我們想控制對models實例的訪問權(quán)限,可以參考Bruce Perens的ModelSecurity

      安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢
      国产午夜福三级在线播放_亚洲精品成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>
        亚洲国产热久久综合 | 久久夜色精品国产噜噜亚洲AV | 这里有亚洲精品在线 | 久久国产午夜精品理 | 中文字幕偷乱视频 | 久久系列中文字幕 |