我们的系统往往并不只是靠登录这么简单来控制权限,今天我们来看看基于角色的授权
假设我们的系统已经建立了昨天的users表
1,migration
class AddRolesAndRightsTables < ActiveRecord::Migration
def self.up
create_table :users_roles, :id => false do |t|
t.column :user_id, :integer
t.column :role_id, :integer
end
create_table :roles, :do |t|
t.column :name, :string
end
create_table :roles_rights, :id => false do |t|
t.column :role_id, :integer
t.column :right_id, :integer
end
create_table :rights do |t|
t.column :name, :string
t.column :controller, :string
t.column :action, :string
end
end
def self.down
drop_table :users_roles
drop_table :roles
drop_table :rights
drop_table :rights_roles
end
end
2,model
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :rights
end
class Right < ActiveRecord::Base
has_and_belongs_to_many :roles
end
3,application.rb
class ApplicationController < ActionController::Base
layout 'standard'
before_filter :check_authentication,
:check_authorization,
:except => [:signin_form, :signin]
def check_authentication
unless session[:user]
session[:intended_action] = action_name
redirect_to :controller => :admin, :action => signin_form
return false
end
end
def check_authorization
user = User.find(session[:user])
unless user.roles.detect{|role|
role.rights.detect{|right|
right.action == action_name && right.controller == controller_name
}
}
flash[:notice] = "You are not authorized to view the page you requested"
request.env["HTTP_REFERER"] ? (redirect_to :back) : (redirect_to home_url)
return false
end
end
end
end
4,layout
<% if flash[:notice] %>
<div class="errors">
<% flash[:notice] %>
</div>
<% end %>
如果我们的某一个controller或者action不想要check_authentication和check_authorization这两个filter,我们可以skip掉:
class HomeController < ApplicationController
skip_before_filter :check_authentication, :check_authorization
def index
render :text => "A page that doesn't require a signin or any rights"
end
end
但这只能精确到controller和action级别的权限控制
如果我们想控制对models实例的访问权限,可以参考Bruce Perens的
ModelSecurity
分享到:
相关推荐
Component-Based Rails Applications: Large Domains Under Control (Addison-Wesley Professional Ruby Series) By 作者: Stephan Hagemann ISBN-10 书号: 0134774582 ISBN-13 书号: 9780134774589 Edition 版本: ...
Ruby on Rails 初体验--北大青鸟教师专题讲座PPT 想学Ruby的赶快下载看看。 Ruby--目前最快速开发工具
awesome-rails-gem-zh_CN, Rails 常用 Gem 列表 - Awesome Rails Gem 中文版
rails-hackernews-reddit-producthunt-clone, 黑客 news/reddit/social 链接分享网站 用 Rails 构建 Rails 上的 Reddit-Hackernews-ProductHunt克隆演示 这是一个 readme.md的Ruby on Rails 应用程序,模仿了 Hacker...
Scrum Poker in Rails5, docker-compose
rails-angular-postgres-and-bootstrap-second-edition 英文原版
rails3-mongoid-devise, 示例 Rails 3.2应用,带有数据 Mongoid,用于验证 Rails 4.1有关设计的Rails 4.1示例应用程序,请参见:rails设计有一个用于设计的教程:Rails 设计教程。类似示例和教程这是来自 RailsApps...
Ruby on Rails Tutorial(3rd-1.0.2)适合初学者,详细。
rails-documentation-2-0-2
rails-beginner-s-guide是Rails 指导手册,帮组学习了解rails开发
rails-documentation-1-2-1.zip
upmin-admin 是一个为 Rails 应用开发的开源管理框架。用来管理 Rails 应用中各种对象(如 Model、View 和 Controller )。 标签:upmin
rails-documentation-1-2-0-rc1.chm
rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机 用于 Ruby on Rails 核心开发的虚拟机简介注意:这个虚拟机不是为 Rails 应用程序开发而设计的,只是为。 这个项目自动设置开发环境,以便在 Ruby on Rails ...
rails-react-components-源码.rar
rails-yelp-mvp-源码.rar
rails-builds-test-源码.rar
rails-development-environment-master.rar
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装