国产精品一区二区精品_久久小视频_天堂va在线观看_99久久夜色精品国产亚洲96_日本手机在线视频_av成人免费

當前位置:首頁 > 網站舊欄目 > 學習園地 > 設計軟件教程 > 每天一劑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

安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢
主站蜘蛛池模板: 99视频在线播放 | 免费看黄色软件大全 | 欧美亚洲网站 | 高清在线一区 | 欧美一区二区三区国产 | 亚洲免费天堂 | 黄色资源在线观看 | 日本在线观看中文字幕 | 成人小视频免费看 | 一区二区高清视频 | 欧美成人第一页 | 一区二区三区免费 | 久久久久久久久久免费 | 免费观看91视频 | 欧美成人精品h版在线观看 欧美成人精品二区三区99精品 | www亚洲| 亚洲成人www | 亚洲天堂成人在线 | 中文字幕第一页亚洲 | 香蕉日日| 男人把ji放进女人舒服视频 | 久久久91精品国产一区二区 | 激情黄色一级片 | 国产午夜影院 | 一级特黄视频 | 美女国产在线 | 天天好逼综合 | 欧美日韩啪啪 | 日韩aaaa| 国产一区二区三区91 | 久久久久婷婷 | 成人性生交大片免费7 | 日本亚洲网站 | 亚洲天堂首页 | 日韩免费一级片 | 国产视频亚洲 | 91av视频在线 | 激情小视频在线观看 | 九九热精彩视频 | xxxxx影院| 天天操夜夜操 |