日本一区二区免费播放_麻豆导航_久久精品99_国产性av_色婷婷噜噜久久国产精品12p_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

安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢
主站蜘蛛池模板: 亚洲人成网www| 国产精品女丝袜白丝袜 | 国产下药迷倒白嫩美女网站 | 欧美精品自拍 | 久久久久久免费视频 | 永久黄网站色视频免费观看99 | 国产成人aa免费视频 | 国产探花视频在线观看 | 啪啪国产| 久久www免费人成_看片高清 | 久久免费久久 | 韩国三级黄色片 | 波多野结衣一区二区三区四区 | 99re6免费视频 | 精品久久久久久综合另类小说 | 国产视频资源在线观看 | 特黄特黄黄色大片 | 免费99视频有精品视频高清 | 日色视频 | 成人在线视频免费看 | 国产成人欧美 | 99精品国产成人a∨免费看 | 伦伦影院精品一区 | 日本一区二区视频在线观看 | 天天摸夜夜添狠狠添2018 | 日夜操在线视频 | 国产下药迷倒白嫩美女网站 | 三级特黄30分钟在线播放 | 青青草成人免费在线视频 | 微拍秒拍99福利精品小视频 | 久久se精品动漫一区二区三区 | a级片在线视频 | 欧美精品免费xxxxx视频 | 欧美人bbbwww | 亚洲美女一级片 | 欧美成人生活片 | 久久久久亚洲视频 | www色日本 | 久久99精品久久久久久青青日本 | 精品无码三级在线观看视频 | 久久精品一区二区三区不卡牛牛 |