2

在我的应用程序中,我使用switch_user( https://github.com/flyerhzm/switch_user ) gem 允许管理员以其他用户身份登录。gem 能够以管理员身份重新登录,但我很难概念化如何做到这一点。

这是我的配置:

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

他们的自述文件说你可以在你的视图中拥有这些链接

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

但是没有办法加载“原始用户”来检查是否需要显示管理员登录链接..其他人有使用这个 gem 的经验吗?

4

2 回答 2

2

您可以通过执行从控制器访问原始用户

provider = SwitchUser::Provider.init(self)
provider.original_user

干杯

于 2015-08-28T05:32:14.853 回答
1

我在切换用户及其切换回选项方面遇到了类似的问题,所以最后我试图自己实现一些东西。

以此为起点,希望对您也有帮助。

于 2013-08-12T07:42:59.173 回答