代码比较简单:
只需要在ApplicationController中实现roles和groups这2个方法,对数据库模式没有任何限制,只要能保证这2个方法能够得到当前用户的角色和组即可。
有4个check方法可用,可任意使用一个或多个。
简单模拟测试一下:
其中,test1、test2都会成功,而test3和test4则会失败显示异常,只需要处理rescue_action把它修改为自己的显示页面即可。
module ActionController
#
:nodoc:
class CheckGroupError < ActionControllerError # :nodoc:
attr_reader : group_name
def initialize(group_name)
@group_name = group_name
end
end
class CheckRoleError < ActionControllerError # :nodoc:
attr_reader : role_name
def initialize(role_name)
@role_name = role_name
end
end
class Base # :nodoc:
def roles
[]
end
def groups
[]
end
def check_roles( * role_args)
role_args . each do | role |
check_role(role)
end
end
def check_groups( * group_args)
group_args . each do | group |
check_group(group)
end
end
def check_group(group)
raise CheckGroupError . new(group . to_s) unless groups() . include ? (group . to_s)
end
def check_role(role)
raise CheckRoleError . new(role . to_s) unless roles() . include ? (role . to_s)
end
end
end
class CheckGroupError < ActionControllerError # :nodoc:
attr_reader : group_name
def initialize(group_name)
@group_name = group_name
end
end
class CheckRoleError < ActionControllerError # :nodoc:
attr_reader : role_name
def initialize(role_name)
@role_name = role_name
end
end
class Base # :nodoc:
def roles
[]
end
def groups
[]
end
def check_roles( * role_args)
role_args . each do | role |
check_role(role)
end
end
def check_groups( * group_args)
group_args . each do | group |
check_group(group)
end
end
def check_group(group)
raise CheckGroupError . new(group . to_s) unless groups() . include ? (group . to_s)
end
def check_role(role)
raise CheckRoleError . new(role . to_s) unless roles() . include ? (role . to_s)
end
end
end
只需要在ApplicationController中实现roles和groups这2个方法,对数据库模式没有任何限制,只要能保证这2个方法能够得到当前用户的角色和组即可。
有4个check方法可用,可任意使用一个或多个。
简单模拟测试一下:
class ApplicationController
<
ActionController
::
Base
def roles
%w (add show)
end
def groups
%w (users)
end
end
def roles
%w (add show)
end
def groups
%w (users)
end
end
class TestController
<
ApplicationController
def test1
check_role : add
render_text " OK "
end
def test2
check_role : add
check_group : users
render_text " OK "
end
def test3
check_groups : admin , : users
render_text " OK "
end
def test4
check_roles : add , : remove
render_text " OK "
end
end
def test1
check_role : add
render_text " OK "
end
def test2
check_role : add
check_group : users
render_text " OK "
end
def test3
check_groups : admin , : users
render_text " OK "
end
def test4
check_roles : add , : remove
render_text " OK "
end
end
其中,test1、test2都会成功,而test3和test4则会失败显示异常,只需要处理rescue_action把它修改为自己的显示页面即可。