如何使用 Devise 根据用户的角色重定向用户的主(根)路径?
我正在开发一个项目管理应用程序,在该应用程序中,我有 project_managers 和 客户。我正在使用 Devise 和 CanCan 进行身份验证/授权。
登录后什么时候我应该将用户重定向到他们自己的特定控制器/布局/视图?有没有办法检查 routes.rb
中的 current_user.role
并根据他们是项目经理还是客户来设置根(或重定向) ?这是我可以在 Devise 中进行的更改吗?
预先感谢您的任何帮助! - 标记
I'm working on a project management app, and in the app, I have project_managers and clients. I'm using Devise and CanCan for authentication/authorization.
At what point after login should I be redirecting the user to their own specific controller/layout/views? Is there a way to check for current_user.role
in routes.rb
and set the root (or redirect) based on whether or not they're a project manager or a client? Is this a change I can make in Devise somewhere?
Thanks in advance for any help!
--Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的
routes.rb
文件不会知道用户具有什么角色,因此您将无法使用它来分配特定的根路由。您可以做的是设置一个控制器(例如,
passthrough_controller.rb
),该控制器可以读取角色并重定向。像这样:这样,所有用户都将拥有一个入口点,该入口点又根据他们的角色将他们重定向到适当的控制器/操作。
Your
routes.rb
file won't have any idea what role the user has, so you won't be able to use it to assign specific root routes.What you can do is set up a controller (for example,
passthrough_controller.rb
) which in turn can read the role and redirect. Something like this:This way, all users will have one point of entry, which in turn redirects them to the appropriate controller/action depending on their role.
另一种选择是将过程传递给经过身份验证的方法,如下所示(我在本例中使用 rolify):
请注意,在 Rails 4 中,您必须指定一个唯一的名称对于每个根路由,请参阅此问题了解详细信息。
Another option is to pass a proc to the
authenticated
method like this (I'm using rolify in this example):Note that in Rails 4 you have to specify a unique name for each root route, see this issue for details.
最简单的解决方案是使用 lambda:
The simplest solution is to use
lambda
:我在使用 Warden 的 Rails 3 应用程序中执行此操作。由于 Devise 是建立在 Warden 之上的,我认为它会为您工作,但在依赖它之前一定要先尝试一下。
如果用户的角色是“project_manager”,则将使用 ProjectManagersController - 如果不是,则将使用 ClientsController。如果他们根本没有登录,
env['warden'].user
将为 nil,并且您会收到错误,因此您可能需要解决这个问题,但这会让你开始吧。I do this in a Rails 3 app that uses Warden. Since Devise is built on top of Warden, I think it'll work for you, but be sure to experiment with it a bit before relying on it.
If the user's role is "project_manager" the ProjectManagersController will be used - if it's not the ClientsController will be used. If they're not logged in at all,
env['warden'].user
will be nil and you'll get an error, so you'll probably want to work around that, but this will get you started.博客文章与实现
Blog post with the implementation http://minhajuddin.com/2011/10/24/how-to-change-the-rails-root-url-based-on-the-user-or-role/