Devise/Rails - 如何仅允许管理员为其他人创建帐户?

发布于 2024-10-21 22:23:06 字数 158 浏览 2 评论 0原文

我正在使用 devise 作为我的身份验证解决方案,现在我正在考虑授权。在我的项目中,我(管理员)是唯一被授权为其他人创建帐户的人。

我想知道是否有一种方法可以做到这一点而不需要太多的黑客。事实上,如果用户已经登录,Devise 不允许用户访问注册页面。

感谢您的建议!

I am using devise as my authentication solution and now i am thinking about authorization. In my project I (the admin) is the only person authorized to create account for others.

I wonder if there is a way to do it without to much hack. In fact, Devise doesn't allow user to access to the signup page if he is already logged in.

Thanks for your advice on it!

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

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

发布评论

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

评论(3

涙—继续流 2024-10-28 22:23:06

设置 :skip => :registrations 还会终止用户编辑其用户信息的能力。如果这不是您想要的,您可以创建一个(最小)自定义注册控制器,并且仅删除 new_user_registration_path,同时保留 edit_user_registration_path

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController

 def new
    # If you're not using CanCan, raise some other exception, or redirect as you please
    raise CanCan::AccessDenied
  end

end

# routes.rb
devise_for :users, :controllers => { :registrations => "registrations" }

完成此操作后,您还需要将目录 views/devise/registrations 移动到 views/registrations

Setting :skip => :registrations also kills the ability for a user to edit their user info. If that's not what you are after you can instead create a (minimal) custom registrations controller and only remove the new_user_registration_path while preserving the edit_user_registration_path.

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController

 def new
    # If you're not using CanCan, raise some other exception, or redirect as you please
    raise CanCan::AccessDenied
  end

end

# routes.rb
devise_for :users, :controllers => { :registrations => "registrations" }

Once you do this you also need to move the directory views/devise/registrations to just views/registrations.

蓝色星空 2024-10-28 22:23:06

您可以尝试将 rails_admin gem 与 Devise 结合使用来处理任何管理 -具体任务。您需要添加更多代码来设置它,但至少您可以避免在更改与 Devise 的交互方面破坏解决方案。

You can try the rails_admin gem in conjunction with Devise to handle any admin-specific tasks. You'll need to add more code to set it up, but at least you avoid hacking around the solution in terms of changing your interactions with Devise.

怪异←思 2024-10-28 22:23:06

实际上,在 Devise 的更高版本中,您只需从模型中删除“可注册”声明,它就会为您处理这个问题。

It actually looks like in the later versions of Devise you can just remove the "registerable" declaration from your model and it will take care of this for you.

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