2014-10-22 144 views
0

我已經構建了一個具有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

回答

0

我能夠使用門衛實現自定義授權方法。我將自己的工作代碼改編爲您的案例,我認爲這應該適用於使用門衛的任何自定義驗證方法。

在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 
相關問題