铁轨授权?检查模型与控制器
我正在对 user
模型进行检查,以确定她/他是否有一个或多个 task_list
,如果她有多个 task_list,则允许她删除它,否则抛出异常。我基本上在用户模型中有一个名为 delete_list 的方法,以允许快速删除,例如 user1.delete_list(list1)
我正在争论是否将检查放在 CanCan 中,它将作为之前的应用控制器上的过滤器或是否也将其包含在用户模型中。推荐的做法是什么?
I am doing a check on a user
model to determine whether s/he has one or more task_list
, if she has more than one task_list only then she is allowed to delete it, otherwise an exception is thrown. I basically have an method called delete_list in the user model to allow for short hand deletions such as user1.delete_list(list1)
I am debating whether to put the check in CanCan where it would be apply as a before filter on the controller or whether to have it in the user model as well . What is the recommended practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为一个好的 DRY 方法是在模型中创建一个方法来测试是否允许删除。然后使用控制器或ability.rb 中的该方法。恕我直言,我认为当您将来有可能更改为不同的权限系统时,将复杂的权限/业务逻辑与 CanCan 分离会更好。
在您的模型中:
在ability.rb 中
您的控制器和视图还可以直接在模型实例上使用
can_destroy_list
if nessary 或使用: ifcan? :销毁,@列表
I think a good DRY approach to this would be to create a method in your model that tests whether a delete is allowed. Then use that method from your controller or from ability.rb. IMHO I think having complicated permission/business logic decoupled from CanCan is better when there is a chance you might change to a different permission system in the future.
In your model:
In ability.rb
Your controller and views can then also use
can_destroy_list
directly on the model instance if nessary or use: ifcan? :destroy, @list