在我的应用程序中,用户可以邀请其他人成为他们帐户的“贡献者”。这些贡献者一旦登录,就会被重定向到一个仪表板,显示他们可以登录的每个帐户。
以下是一个控制器,用于允许贡献者和学校管理员登录运动员帐户。当原始用户登录到某个帐户时,该应用会检查session[:original_user_id]
变量,以便在屏幕顶部显示一个带有链接的消息横幅,以便管理员可以重新登录到他们的帐户中 - 这就是我所拥有的试图找出如何将原始用户重新登录的问题。
SignInAsController:
class SignInAsController < ApplicationController
before_filter :authenticate_user!
include SchoolAdmin::Athletes
def create
session[:original_user_id] = if (current_user.school_admin? || current_user.athlete_contributor?)
current_user.id
else
nil
end
user = User.find(params[:id])
if current_user.can_manage?(user)
sign_out(User.find(current_user.id))
handle_request(athlete)
redirect_to user_root_path
else
redirect_to :back, notice: "You do not have access to that account"
end
end
private
def handle_request(athlete)
sign_in(:user, athlete, { bypass: true })
end
end
UserModel can_manage?方法:
class User < ActiveRecord::Base
#other methods
def can_manage?(user)
if athlete_contributor?
managed_athletes.include?(user)
elsif school_admin?
active_subscription.athletes.include?(user)
end
false
end
end
答案 0 :(得分:0)
Devise有sessions
&amp; registrations
个控制器,可让您创建&amp;覆盖设计的各种方法
<强>会话强>
考虑到登录过程大约是creating
一个会话,我会考虑将destroy
当前会话实现自己的方法。 create
一个新的管理员统计信息,如下所示:
#config/routes.rb
devise_for :users, :controllers => { :sessions => "sessions" }
devise_scope :user do
post "admin", :to => "devise/sessions#admin_switch"
end
#app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController
#Switch to "admin" mode
def admin_switch
sign_out #method to destroy current user's session
sign_in #create new session -- needs more work
end
end
这是为了展示您如何实现目标。如果你想让我添加更多代码,我当然会修改我的帖子!具体来说,sign_in
命令不起作用(需要传递给它的特定数据,并使用Warden)
答案 1 :(得分:0)
您可以使用可以使用的Switch User gem: :devise,:authlogic,:clearance,:restful_authentication或:sorcery
答案 2 :(得分:0)
这对我有用。它允许管理员用户以另一个用户身份登录。
class AdminController < ApplicationController
before_filter :authenticate_user!
def become
return unless current_user.is_an_admin?
sign_in(:user, User.find(params[:id]))
redirect_to root_path
end
end
如果您在管理员成为用户时不想更新sign_in(:user, User.find(params[:id]), { :bypass => true })
和last_sign_in_at
,也可以current_sign_in
。
答案 3 :(得分:0)
此功能“冒充其他用户”。管理员可以进入 Admin::Users#index -> Find a user,并模拟他们。也就是说,登录他们的帐户。需要一个发布此操作的按钮
def impersonate
@user = User.find(params[:id])
authorize! :impersonate, @user
# Impersonate
session[:impersonation_user_id] = current_user.id
expire_data_after_sign_in!
warden.session_serializer.store(@user, Devise::Mapping.find_scope!(@user))
redirect_to(root_path)
end
before_action :authenticate_user!
skip_authorization_check only: [:destroy]
def destroy
@user = User.find(session[:impersonation_user_id])
# Reset impersonation
session[:impersonation_user_id] = nil
expire_data_after_sign_in!
warden.session_serializer.store(@user, Devise::Mapping.find_scope!(@user))
redirect_to(admin_users_path)
end
end
使用设计,设置会话 [:impersonation_user_id] 以便稍后您知道您正在冒充另一个用途,然后使用这个使您以新用户身份登录的warden.session_serializer.store。 如果关闭选项卡。重新打开选项卡。您仍然会冒充该用户。 在站点中放置一个部分 (haml) 以在用户冒充时在每个页面上向用户显示警报
.bg-warning.d-print-none.text-center
You are logged in as <strong>#{current_user}</strong>.
= link_to 'click here', impersonate_path, 'data-method': :delete
to return to your original account.