我自己做了一个基于角色控件的权限管理系统
表结构:
大家一看就应该明白这几张表是什么关系了吧?一个用户可以拥有多个角色,一个角色也可以拥有多个用户,一个角色可以拥有多个操作权限,而一个操作权限也可以属于多个角色.
另外要说的一点就是:在表permissions中,name表示操作的名字,就是用来显示在页面上的,如"删除新闻",而action是就代表真正的操作.
现在对于user,role,permission的操作我都实现了(包括对usser对role的勾选和role对permission的勾选)
而且在我的User模型中,我也写了方法,用以取得user所拥有的权限:
现在说说对于这个系统,我的思想.我的权限主要是用于后台管理方面的(/admin),因此在/app/controllers/admin下有
对于admin模块下的其它controller,都是继承这个Admin Controller,我现在就是迷惑了,不知道如何验证权限.
我以前用Java做权限管理系统(SSH),大体思想是这样的:所用的操作最终都是基于URL请求的,eg:http://localhost:8080/admin/postAction.do?method=list(postAction代表了一个模块,而list代表了一个操作)
我在用户登录是就把用户的权限操作列表保存到Session中,然后写一个作用于/admin的过滤器,用来检查当前登录用户的权限列表中的许可的url是否包含请求的url,这样就能实现权限的管理.
然而现在在rails中,url不适用了,大家给指点一下,如何基于我目前所做的来应用这个权限验证.
问题补充:
难道我还要在/admin模块下的每一个controller中的action方法中加入判断当前登录的用户是否拥有权限?
如在Admin:ostController中
def index
#if session[:user].permissions has "post_index" ? if not,redirect to 'you have no permission to do this!!' page
end
哇,要是这样我累死了!!
希望大家不要给的是与这样类似的答案,说简单是一点就是,我想尽量让我的权限管理与业务无关,也就是说移植性要好!!!
问题补充:
谢谢xxj,我现在的Admin::PostController是这样的
我的permission_urls中明明存在"posts/index",为什么我请求/admin/posts/始终跳到没有权限的提示页面呢?我用pust输入作了二者的比较,明明相等,却还是输出false
表结构:
引用
users:
id,name,password
roles_users:
roles_id,users_id
roles:
id,name
permissions_roles:
permissions_id,roles_id
permissions:
id,name,action
id,name,password
roles_users:
roles_id,users_id
roles:
id,name
permissions_roles:
permissions_id,roles_id
permissions:
id,name,action
大家一看就应该明白这几张表是什么关系了吧?一个用户可以拥有多个角色,一个角色也可以拥有多个用户,一个角色可以拥有多个操作权限,而一个操作权限也可以属于多个角色.
另外要说的一点就是:在表permissions中,name表示操作的名字,就是用来显示在页面上的,如"删除新闻",而action是就代表真正的操作.
现在对于user,role,permission的操作我都实现了(包括对usser对role的勾选和role对permission的勾选)
而且在我的User模型中,我也写了方法,用以取得user所拥有的权限:
- class User < ActiveRecord::Base
- has_and_belongs_to_many :roles
- #User login validate
- def self.login_validate(name, password)
- user = User.find_by_name(name)
- if user
- if user.password == password
- return user
- else
- user = nil
- end
- end
- user
- end
- #Get user's all permissions
- def permissions
- user_permissions = Array.new
- for role in self.roles
- user_permissions += role.permissions
- end
- user_permissions
- end
- end
class User < ActiveRecord::Base has_and_belongs_to_many :roles #User login validate def self.login_validate(name, password) user = User.find_by_name(name) if user if user.password == password return user else user = nil end end user end #Get user's all permissions def permissions user_permissions = Array.new for role in self.roles user_permissions += role.permissions end user_permissions end end
现在说说对于这个系统,我的思想.我的权限主要是用于后台管理方面的(/admin),因此在/app/controllers/admin下有
- class Admin::AdminController < ActionController::Base
- #Set layout for Admin board
- layout "admin"
- before_filter :authorizate
- def authorizate
- puts "authorizate"
- is_login?
- #这里现在写什么啊?大家给指点一下啊
- end
- #Validate the user has loged in or not
- def is_login?
- unless session[:user]
- flash[:notice] = "Please login first!"
- redirect_to "/login"
- end
- end
- end
class Admin::AdminController < ActionController::Base #Set layout for Admin board layout "admin" before_filter :authorizate def authorizate puts "authorizate" is_login? #这里现在写什么啊?大家给指点一下啊 end #Validate the user has loged in or not def is_login? unless session[:user] flash[:notice] = "Please login first!" redirect_to "/login" end end end
对于admin模块下的其它controller,都是继承这个Admin Controller,我现在就是迷惑了,不知道如何验证权限.
我以前用Java做权限管理系统(SSH),大体思想是这样的:所用的操作最终都是基于URL请求的,eg:http://localhost:8080/admin/postAction.do?method=list(postAction代表了一个模块,而list代表了一个操作)
我在用户登录是就把用户的权限操作列表保存到Session中,然后写一个作用于/admin的过滤器,用来检查当前登录用户的权限列表中的许可的url是否包含请求的url,这样就能实现权限的管理.
然而现在在rails中,url不适用了,大家给指点一下,如何基于我目前所做的来应用这个权限验证.
问题补充:
难道我还要在/admin模块下的每一个controller中的action方法中加入判断当前登录的用户是否拥有权限?
如在Admin:ostController中
def index
#if session[:user].permissions has "post_index" ? if not,redirect to 'you have no permission to do this!!' page
end
哇,要是这样我累死了!!
希望大家不要给的是与这样类似的答案,说简单是一点就是,我想尽量让我的权限管理与业务无关,也就是说移植性要好!!!
问题补充:
谢谢xxj,我现在的Admin::PostController是这样的
- class Admin::PostsController < Admin::AdminController
- before_filter :authorize, :only => [:index, :show, :new, :edit, :destroy]
- def authorize
- logger.debug self.controller_name << "/" << self.action_name
- logger.debug "-------------------------#{self.action_name}"
- logger.debug session[:user].permission_urls[3]
- p session[:user].permission_urls[3] == (self.controller_name << "/" << self.action_name)
- unless session[:user].permission_urls.include?( self.controller_name + "/" + self.action_name )
- flash[:notice] = "You have not permission to do it!"
- redirect_to :controller => "admin/welcome"
- end
- end
class Admin::PostsController < Admin::AdminController before_filter :authorize, :only => [:index, :show, :new, :edit, :destroy] def authorize logger.debug self.controller_name << "/" << self.action_name logger.debug "-------------------------#{self.action_name}" logger.debug session[:user].permission_urls[3] p session[:user].permission_urls[3] == (self.controller_name << "/" << self.action_name) unless session[:user].permission_urls.include?( self.controller_name + "/" + self.action_name ) flash[:notice] = "You have not permission to do it!" redirect_to :controller => "admin/welcome" end end
我的permission_urls中明明存在"posts/index",为什么我请求/admin/posts/始终跳到没有权限的提示页面呢?我用pust输入作了二者的比较,明明相等,却还是输出false
采纳的答案
2008-07-09 xxj (初级程序员)
一:
url当然可以适用的,你可以获取当前的controller 和 action,然后跟你之前一样的操作就可以了。在rails中是很方便的
第二: 没必要这么紧耦合的,你可以做成plugins,偷懒的话也可以考虑简单的写个module,然后mixin进去,就可以了。譬如:
这样业务代码就无效关系权限了,权限单独的运行。当然如果做的好的话,可以做成plugins共享给大家使用
![](http://www.javaeye.com/images/smiles/icon_smile.gif)
- puts self.controller_name
puts self.controller_name
第二: 没必要这么紧耦合的,你可以做成plugins,偷懒的话也可以考虑简单的写个module,然后mixin进去,就可以了。譬如:
- class Admin::AdminController < ActionController::Base
- include AuthenticatedSystem
- end
class Admin::AdminController < ActionController::Base include AuthenticatedSystem end
- module AuthenticatedSystem
- def self.included(target)
- target.send :before_filter, :authorized?
- end
- #...
- def authorized?
- true
- end
- end
module AuthenticatedSystem def self.included(target) target.send :before_filter, :authorized? end #... def authorized? true end end
这样业务代码就无效关系权限了,权限单独的运行。当然如果做的好的话,可以做成plugins共享给大家使用
![](http://www.javaeye.com/images/smiles/icon_biggrin.gif)
提问者对于答案的评价:
谢谢你!希望你能继续讨论一下这个:
http://www.javaeye.com/topic/215074
其他回答
- logger.debug self.controller_name << "/" << self.action_name
logger.debug self.controller_name << "/" << self.action_name
这里已经改变了controller_name的值了
![](http://www.javaeye.com/images/smiles/icon_sad.gif)
现在controller_name 已经是"posts/index"了
![](http://www.javaeye.com/images/smiles/icon_biggrin.gif)
- p session[:user].permission_urls[3] == (self.controller_name << "/" << self.action_name)
p session[:user].permission_urls[3] == (self.controller_name << "/" << self.action_name)
这里为false是正确的,但是此时的controller_name已经被你通过"<<"改变了
- unless session[:user].permission_urls.include?( self.controller_name + "/" + self.action_name )
- end
unless session[:user].permission_urls.include?( self.controller_name + "/" + self.action_name ) end
这里显然不再是"posts/index"了,而是" posts/index/index/index"了,因为你已经两次操作"<<"了
将上面的修改成临时变量好了
- before_filter :authorize
- def authorize
- url = "#{self.controller_name}/#{self.action_name}"
- p session[:user].permission_urls[3] == url
- unless session[:user].permission_urls.include?(url)
- flash[:notice] = "You have not permission to do it!"
- redirect_to :controller => "admin/welcome"
- end
- end