强制 Restful 身份验证以特定用户身份登录(管理功能)?

发布于 2024-08-13 20:52:52 字数 128 浏览 2 评论 0原文

我正在使用 Restful 身份验证,并且希望能够以不同用户的身份登录我们的网站,以调查他们可能遇到的问题(“看看他们看到了什么”)。由于所有密码都已加密,我显然不能只使用他们的密码。

那么,如何强制会话以特定用户身份登录呢?

I'm using Restful Authentication and I'd like to be able to log in as different users on our site to investigate issues they may be having ("see what they see"). Since all passwords are encrypted I obviously can't just use their passwords.

So, how can I force a session to be logged in as a specific user?

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

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

发布评论

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

评论(2

内心荒芜 2024-08-20 20:52:52
  • 在您的sessions_controller中添加操作impersonate,如下所示:

    def 模拟
      user = User.find(params[:id])
      logout_killing_session!
      self.current_user = 用户
      flash[:通知] = t(:logged_in)
      重定向到 root_url
    结尾
    
  • 然后在您的路由中使用成员 impersonate 扩展会话资源代码>:

    map.resource :session, :member => {:冒充=> :邮政}
    
  • 最后,在您的管理视图中的某个位置向每个用户添加一个名为“Impersonate”的按钮。它必须看起来像这样(假设用户位于局部变量 user 中):

    <%= button_to "模拟", impersonate_session_path(:id => user.id) %>
    

使用这种方法,您还可以避免覆盖任何跟踪数据,例如上次登录的时间等。

PS 不要忘记要求管理员在会话控制器中进行模拟操作。

  • In your sessions_controller add action impersonate like this:

    def impersonate
      user = User.find(params[:id])
      logout_killing_session!
      self.current_user = user
      flash[:notice] = t(:logged_in)
      redirect_to root_url
    end
    
  • Then in your routes extend session resource with the member impersonate:

    map.resource :session, :member => {:impersonate => :post}
    
  • Finally, somewhere in your admin views add a button to each user called "Impersonate". It will have to look something like this (assuming that user is in local variable user):

    <%= button_to "Impersonate", impersonate_session_path(:id => user.id) %>
    

Using this approach you also avoid overriding any tracking data such as time of the last login, etc.

P.S. Don't forget to require admin for impersonate action in sessions controller.

梦回旧景 2024-08-20 20:52:52

只需使用您想要成为的用户的 ID 覆盖 session[:user_id] 即可。一种简单的方法是让用户以管理员身份登录,然后为他们提供用户名下拉列表。当他们提交表单时,让控制器设置 session[:user_id],然后重新加载 current_user。然后管理员将“成为”该用户。

Simply override session[:user_id] with the id of the user you want to be. One easy way is to have the user log in as an admin and then give them a drop-down of usernames. When they submit the form, have the controller set session[:user_id] and then reload current_user. The admin will then 'become' that user.

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