我有一个Rails 4应用程序,我根据我在应用程序中的模型设置编写了一个CanCan能力模型。在测试的时候,它有时会起作用,然后就不起作用了。我不知道问题出在哪里。日志中没有任何东西会指向问题。我也不认为它是那么干净。对如何改进这一能力模式有何建议?这似乎是多余的和令人困惑的。
if user
##
can :manage, User do |u|
case u.account
## If user is a Developer allow it to only be managed by itself (user)
when Developer
u == user
## If user is a Organization allow it to only be managed by it's founders
when Organization
u.account.founders.exists?(user)
else
false
end
end
can :manage, App do |a|
case a.appable
## If app belongs to a Developer allow it to only be managed by it owner (developer)
when Developer
a.appable.id == user.id
## If app belongs to a Organization allow it to only be managed by its organizations' founders
when Organization
a.appable.founders.exists?(user)
else
false
end
end
##
end
发布于 2013-08-28 04:38:40
至于清理它,您可以考虑用用户角色来组织逻辑。通过这种方式,您可以轻松地查看给定的角色,并了解它们所做的和无法访问的所有能力。
就像这样:
if user.developer?
can :manage User, id: user.id
can :manage App, App.appable_by(user)
end
注意:为了测试规则,您可能需要编写一些自定义查找器,比如App.appable_by(user)
。
我没有完全遵循你对组织/创始人所做的努力。如果这是一个像user.founder这样的角色?然后像上面提到的那样对待它,但是使用不同的授权规则。
https://stackoverflow.com/questions/18478873
复制相似问题