Rails:切换用户宝石,并重新切换到原始用户

编程入门 行业动态 更新时间:2024-10-07 14:34:12
本文介绍了Rails:切换用户宝石,并重新切换到原始用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用程序中,我正在使用 switch_user ( https:宝石,以允许管理员以另一个用户身份登录,//github/flyerhzm/switch_user )宝石有能力以管理员身份登录,但我很难理解如何做到这一点。

In my app I am using the switch_user (github/flyerhzm/switch_user) gem to allow admins to login as another user. The gem has the ability to log back in as an admin, but I am having a hard time conceptualizing how to do it.

这是我的配置:

SwitchUser.setup do |config| # provider may be :devise, :authlogic, :clearance, :restful_authentication, :sorcery, or :session config.provider = :devise # available_users is a hash, # key is the model name of user (:user, :admin, or any name you use), # value is a block that return the users that can be switched. config.available_users = { :user => lambda { User.all } } # available_users_identifiers is a hash, # keys in this hash should match a key in the available_users hash # value is the name of the identifying column to find by, # defaults to id # this hash is to allow you to specify a different column to # expose for instance a username on a User model instead of id config.available_users_identifiers = { :user => :id } # available_users_names is a hash, # keys in this hash should match a key in the available_users hash # value is the column name which will be displayed in select box config.available_users_names = { :user => :email } # controller_guard is a block, # if it returns true, the request will continue, # else the request will be refused and returns "Permission Denied" # if you switch from "admin" to user, the current_user param is "admin" config.controller_guard = lambda { |current_user, request, original_user| current_user.school_admin? || original_user.school_admin? } # view_guard is a block, # if it returns true, the switch user select box will be shown, # else the select box will not be shown # if you switch from admin to "user", the current_user param is "user" config.view_guard = lambda { |current_user, request, original_user| current_user.school_admin? || original_user.school_admin? } # redirect_path is a block, it returns which page will be redirected # after switching a user. config.redirect_path = lambda { |request, params| '/' } # helper_with_guest is a boolean value, if it set to false # the guest item in the helper won't be shown config.helper_with_guest = true # false = login from one scope to another and you are logged in only in both scopes # true = you are logged only into one scope at a time config.login_exclusive = true # switch_back allows you to switch back to a previously selected user. See # README for more details. config.switch_back = true end

他们的README说你可以拥有您的视图中的链接

Their README says you can have these links in your view

<%= link_to user.login, "/switch_user?scope_identifier=user_#{user.id}" %> <%= link_to admin.login, "/switch_user?scope_identifier=admin_#{admin.id}" %>

但是没有办法加载原始用户来检查是否需要显示管理员登录链接..任何人都有使用这个宝石的经验?

But there is no way to load the "original user" to check to see if you need to display the admin login link.. anyone else have experience using this gem?

推荐答案

我有与交换机用户类似的问题及其切换回选项,所以最后我试图通过自己来实现某些东西。

I had similar issues with switch user and its switching back option, so at the end I am trying to implement something by myself.

我正在使用这个作为起点,我希望它也可以帮助你。

I'm using this as a starting point, I hope it helps you as well.

更多推荐

Rails:切换用户宝石,并重新切换到原始用户

本文发布于:2023-07-22 14:57:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1188586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   切换到   原始   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!