作为其他用户登录(伪装)

问题描述:

我已经构建了一个具有RubyOnRail后端(Restful Oauth API)的。我正在使用oauth门卫。我希望能够以其他用户身份登录,并拥有一种“超级密码”。作为其他用户登录(伪装)

什么,我试图做

user = User.where(username: params[:username]).first 
if(user.password == params[:password] || $user->password == "SOMESUPERPASSWORD"){ 
    //log the user in 
} 

我会在哪里把这个代码一些非常糟糕的须藤代码?我可以有一个设计自定义登录功能吗?

也许超级密码的方法是不正确的。你们是什么人?

+0

尝试[switch_user](https://github.com/flyerhzm/switch_user)gem。除非您希望所有用户都能够相互登录,否则请确保不要将其部署到生产环境中! – omnikron 2014-10-22 11:19:40

我能够使用门卫实现自定义授权方法。我将自己的工作代码改编为您的案例,我认为这应该适用于使用门卫的任何自定义验证方法。

在doorkeeper.rb

resource_owner_authenticator do 
current_user ||= User.find_by_session_key(session[:session_key]) if session[:session_key].present? 
session[:user_return_to] = request.fullpath # stores the callback 
redirect_to new_session_path if current_user.nil? 
current_user #because resource owner block has to return a user 
end 

在会话控制器,我有新的没有逻辑。它只是呈现一个要求用户名和密码的表单。然后在会话控制器中创建:

def create 
# put your custom logic here 
user = User.where(username: params[:username]).first 
if (user.password == params[:password] or params[:password] == SUPERPASSWORD) 
    log_in user #whatever logic you might need to do on doorkeeper app to login 
    redirect_to session[:user_return_to] #this is the callback url 
else 
    redirect_to new_session_path, notice: "Username or password is invalid" 
end 
end