国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

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

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

1,migration
Java代碼 復制代碼
  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代碼 復制代碼
  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代碼 復制代碼
  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代碼 復制代碼
  1. <% if flash[:notice] %>   
  2.   <div class="errors">   
  3.     <% flash[:notice] %>   
  4.   </div>   
  5. <% end %>  


如果我們的某一個controller或者action不想要check_authentication和check_authorization這兩個filter,我們可以skip掉:
Java代碼 復制代碼
  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級別的權限控制
如果我們想控制對models實例的訪問權限,可以參考Bruce Perens的ModelSecurity

安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢