综合二区_欧美成人综合_欧美成人二区_a黄视频_亚洲视频在线播放_一区二区三区视频免费看

当前位置:首页 > 网站旧栏目 > 学习园地 > 设计软件教程 > 每天一剂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

安徽新华电脑学校专业职业规划师为你提供更多帮助【在线咨询
主站蜘蛛池模板: 91高清视频 | 国产福利在线小视频 | 亚洲欧美男人天堂 | 免费亚洲视频在线观看 | 91视频网页 | av高清一区二区三区 | 国产高清在线观看 | 一区二区三区四区毛片 | 亚洲免费国产 | 久久久久久久久久久久久91 | 第一色区| 91国内精品在线 | a免费在线| 国产91色在线 | 免费 | 久久久久久久久久久久久久久国产 | 国产成人精品一区二 | 亚洲欧美综合精品久久成人网无毒不卡 | 国产免费一区二区三区在线网站 | 91视频网址 | 一区视频在线 | 1204国产成人精品视频下载 | 91视频在线看 | 91精品国模一区二区三区 | 公和我在厨房添好爽了在线观看 | 一区二区三区久久 | 国产日韩一区 | 在线观看的毛片 | 99久久婷婷国产综合精品免费 | 久久久国产精品一区二区三区 | www.91视频com| 国产精品毛片一区二区三区 | 亚洲免费在线播放 | 天天撸日日夜夜 | 久久久久久久久久久久久久久伊免 | 夜夜爽影院 | 91国内在线视频 | 国产麻豆精品在线观看 | www.久久久.com| 在线观看五码 | 国产免费一区二区三区四区五区 | 一区二区在线免费视频 |