防止用户将自己设为管理员

发布于 2024-09-08 16:57:13 字数 556 浏览 5 评论 0原文

在我的应用程序中,我有一个“用户”模型,其中包含许多属性,包括“has_admin_rights”。如果为 true,则用户是管理员,如果为 false,则不是。

每个用户都有一个个人资料,包括他们的登录名、电子邮件地址、个人资料图片等。

如果我以普通用户身份登录,我可以单击名为“个人资料”的页面,然后可以编辑自己的帐户,例如更新我的电子邮件地址、个人资料图片、密码等等。我只能编辑我的帐户,而不能编辑其他帐户。

如果我以管理员身份登录,我可以做更多事情:例如,我可以让另一个用户成为管理员,或者取消他们的管理员权限。

现在,只有管理员可以访问出现“make admin”复选框的视图,但我感觉仅仅限制对视图的访问是不够的。

我担心的是,由于任何用户都可以编辑自己的个人资料,那么如何阻止用户提交自定义表单帖子,其中包含自己帐户上的“has_admin_rights”=>“1”参数 -从而授予自己管理员访问权限?

我的想法是,在用户控制器中,在对“has_admin_rights”字段进行任何更改之前,我需要检查以确保发出请求的用户当前是管理员 - 否则我完全忽略该请求,并且不做任何改变。

In my app, I have a "User" model, which includes a number of attributes including "has_admin_rights". If true, the user is an admin, if false, they aren't.

Each user has a profile, with their login name, email address, profile pic, etc.

If I'm logged in as a regular user, I can click on a page called "profile", and I can edit my own account, e.g. updating my email address, profile pic, password, whatever. I can ONLY edit my account, and no other.

If I'm logged in as an admin, I can do a little more: for example, I can make ANOTHER user an admin, or take away their admin rights.

Now, only an admin has access to the view where the "make admin" check box appears, but I have a feeling that simply restricting access to the view isn't sufficient.

What I'm concerned about is, since any user can edit their own profile, what's there to stop a user from submitting a custom form post, which has in it the "has_admin_rights"=>"1" parameter on their own account - thereby granting themselves admin access?

What I'm thinking is that, in the User controller, before applying any changes to the "has_admin_rights" field, that I need to check to make sure the user making the request is currently an admin - otherwise I ignore the request altogether, and make no changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独夜无伴 2024-09-15 16:57:13

在用户控制器中,在对“has_admin_rights”字段应用任何更改之前,我需要检查以确保发出请求的用户当前是管理员 - 否则我完全忽略该请求,并且不进行任何更改。< /p>

是的,正是如此。永远不要相信客户;请记住,任何人都可以直接使用 Firebug 或其他方式调整页面。

我还建议您考虑添加审核跟踪,并在一个管理员将另一个用户设为管理员时记录一些内容。也许还可以向特定组的所有管理员发送电子邮件,让他们知道管理员已被创建(或权限已被撤销)。

in the User controller, before applying any changes to the "has_admin_rights" field, that I need to check to make sure the user making the request is currently an admin - otherwise I ignore the request altogether, and make no changes.

yes, exactly. Never trust the client; remember that anybody can just tweak the page directly with Firebug or whatever.

I'd also suggest that you consider adding an audit trail, and log something whenever one admin makes another user into an admin. Maybe also send email to all the admins for a particular group to let them know that an admin has been created (or that rights have been revoked).

怎言笑 2024-09-15 16:57:13

attr_protected 也非常有用

class User < ActiveRecord::Base

  attr_protected :is_admin

end

attr_protected is pretty useful, too

class User < ActiveRecord::Base

  attr_protected :is_admin

end
筱果果 2024-09-15 16:57:13

在执行此验证的用户模型中添加 before_save 。

Add a before_save in your User model that performs this validation .

梦境 2024-09-15 16:57:13

Ue attr_accessible 可以通过质量设置的模型属性白名单- 作业

  class User < ActiveRecord::Base
    attr_accessible :has_admin_rights
  end

和在控制器中

  @user.has_admin_rights = current_user.is_admin? "1" : "0"

Ue attr_accessible which white list of model attributes that can be set via mass-assignment

  class User < ActiveRecord::Base
    attr_accessible :has_admin_rights
  end

& in controller

  @user.has_admin_rights = current_user.is_admin? "1" : "0"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文