亚洲成人一区在线观看_天堂网www_国产精品久久9_中文在线播放_伊人天天_久久精品久久久精品美女

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

每天一劑Rails良藥之Role-Based Authorization
2010-01-14 07:04:21  作者:  來(lái)源:
我們的系統(tǒng)往往并不只是靠登錄這么簡(jiǎn)單來(lái)控制權(quán)限,今天我們來(lái)看看基于角色的授權(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 %>  


如果我們的某一個(gè)controller或者action不想要check_authentication和check_authorization這兩個(gè)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級(jí)別的權(quán)限控制
如果我們想控制對(duì)models實(shí)例的訪問(wèn)權(quán)限,可以參考Bruce Perens的ModelSecurity

安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢
主站蜘蛛池模板: 美女久久 | 成人精品视频在线观看 | 北条麻妃一区二区免费播放 | 成人在线免费视频 | 久草 在线 | 久久久精品免费看 | 久久国产日韩 | 妹子干综合 | 亚洲欧美日韩另类一区二区 | 欧美日韩一区二区中文字幕 | 欧美精品一区二区三区四区在线 | 久久久久久成人 | 日本在线黄色 | 欧美一级在线 | 国产精品一区一区三区 | 亚洲一区综合 | 欧洲美女7788成人免费视频 | 不卡二区| 欧美日韩专区 | 日韩城人免费 | 999国产在线视频 | 国产精品久久 | 国产日韩欧美一区二区 | 国产一区二区视频在线观看 | 91亚洲一区 | 日本一区二区不卡 | 欧美高清一区 | xvideos.蜜桃一区二区 | 中文字幕在线观看av | 中文字幕亚洲一区二区三区 | www.99精品 | 国产中文字幕在线 | 四虎影音 | 国产精品免费观看 | 国产精品a久久 | 精品久久久久久久久久久久 | www.国产.com | 色橹橹欧美在线观看视频高清 | 欧美成在线观看 | 欧美日韩专区 | 欧美激情视频一区二区三区在线播放 |