2014-07-03 108 views
0

我正在使用Laravel的Auth類來驗證我的網站上的用戶,基本爲Auth::attempt(...)的東西。設置Laravel的身份驗證用戶

最近出現了一個新的需求(yay stakeholders!),現在需要用戶創建新用戶(輔助用戶)。由於主要用戶的登錄是通過第三方系統進行的,因此我無法將輔助用戶存儲到主要用戶(並重新使用當前的身份驗證系統)。

我想到的是以某種方式告訴Auth類登錄並強制設置用戶在Auth::user()方法。

有沒有辦法做到這一點?

回答

1

編輯

爲了做到這一點,你有你的次要用戶模型使用UserInterface

use Illuminate\Auth\UserInterface; 

然後,你需要實現5種所需的方法:getAuthIdentifiergetAuthPasswordgetRememberTokensetRememberTokengetRememberTokenName

由於顯然config > auth不能在運行時更改,所以您必須手動檢查用戶憑據,獲取實例並執行Auth::login($secondaryUser)

<?php 

use Illuminate\Auth\UserInterface; 

class SecondaryUser extends Eloquent implements UserInterface { 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'secondary_users'; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = array('password'); 

    /** 
    * Get the unique identifier for the secondary user. 
    * 
    * @return mixed 
    */ 
    public function getAuthIdentifier() 
    { 
     return $this->getKey(); 
    } 

    /** 
    * Get the password for the secondary user. 
    * 
    * @return string 
    */ 
    public function getAuthPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Get the token value for the "remember me" session. 
    * 
    * @return string 
    */ 
    public function getRememberToken() 
    { 
     return $this->remember_token; 
    } 

    /** 
    * Set the token value for the "remember me" session. 
    * 
    * @param string $value 
    * @return void 
    */ 
    public function setRememberToken($value) 
    { 
     $this->remember_token = $value; 
    } 

    /** 
    * Get the column name for the "remember me" token. 
    * 
    * @return string 
    */ 
    public function getRememberTokenName() 
    { 
     return 'remember_token'; 
    } 

    public function mainUser() 
    { 
     return $this->belongsTo('User'); 
    } 

} 

原來的答案

我不知道已經明白你想要的東西,但也許這可以幫助:http://laravel.com/docs/security#manually

$user = User::find(1); 
Auth::login($user); 

如果你有2個user模型,我認爲它應該工作,只要他們擴展主用戶類別

+1

或甚至更簡單:''Auth :: loginUsingId(1);'':) –

+1

我會編輯您的問題,添加一些除您的解決方案之外的要求,然後接受它。 – Ben