如何确保 Rails 中的用户只能更新/插入/删除他/她在数据库中的记录
抱歉,如果这是一个基本问题,但我刚刚开始考虑我是否一直以来都正确地这样做了。通常,当用户尝试更新数据库时,我只需使用他/她的用户名作为用户表中的键,然后基于该用户名进行所有操作。然而我刚刚意识到,狡猾的用户可能能够使用另一个用户名提交查询,从而绕过这种弱形式的强制执行权利。所以我的问题实际上是如何防止用户可能对不同用户 ID 下的数据库提交破坏性操作?
Sorry if this is an elementary question but I've just started to consider whether I've been doing this correctly all along. Usually when a user tries to update the database, I simply use his/her username as the key in a user table and then base all operations on that. However I just realized that a crafty user MIGHT be able to submit a query using another username name thus circumventing this weak form of enforcing entitlements. So my question really is how do I prevent a user from potentially submitting a destructive action against a database under a different userid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将当前用户的 ID 存储在会话中,这不容易被操纵。
我通常通过 User 对象上的关系来引用对象:
这是进行所有权测试的一种可读且简单的方法。
http://guides.rubyonrails.org/security.html 是关于这个主题的令人惊讶的好读物。
有很多现成的解决方案可用于维护用户身份(身份验证)和确保用户有权在 Rails 中执行操作(授权)。
You should store the current user's ID in the session, which isn't easily manipulated.
I usually refer to the objects through a relation on a User object:
It's a readable and simple way of doing an ownership test.
http://guides.rubyonrails.org/security.html is a surprisingly good read on the subject.
There are plenty of readymade solutions for maintaining user identity (authentication) and ensuring user has clearance for an action (authorization) in Rails.