簡體   English   中英

Laravel 4自定義身份驗證

[英]Laravel 4 custom Auth

首先,對我的英語感到抱歉,我會盡力而為。

我是Laravel的新手,嘗試通過SOAP WS實現自定義身份驗證,我聲明了實現UserProviderInterface的新類。 我成功實現了retrieveByCredentials和validateCredentials方法,但是由於我無法訪問數據庫或全局用戶信息,因此無法實現retrieveByID方法。 有沒有辦法根據用戶ID進行自定義身份驗證?

我需要:

- Login and validate user throught SOAP WS
- Store User Info returned by WS.
- Remember me functionality
- Secure routes based on logged user and level of access
- Logout

實施課程:

 <?php
    namespace Spt\Common\Providers;
    use Illuminate\Auth\UserProviderInterface;
    use Illuminate\Auth\GenericUser;
    use Illuminate\Auth\UserInterface;

    class AuthUserProvider implements UserProviderInterface{
        private $user;

        public function __construct(){
            $this->user = null;
        }

        public function retrieveByID($identifier){
            return $this->user;
        }

        public function retrieveByCredentials(array $credentials){
            $client = new \SoapClient('webserviceurl');
            $res = $client->Validar_Cliente($credentials);
            $res = $res->Validar_ClienteResult;

            if($res->infoError->bError === true){
                return;
            }

            $res->id = $res->id_cliente;
            $user =  new GenericUser((array) $res);
            return $user;
        }

        public function validateCredentials(UserInterface $user, array $credentials){
             //Assumed that if WS returned a User is validated
             return true;
        }
    }

我認為重新實現UserProviderInterface不是解決方案,但我用Google搜索並沒有找到其他方式

任何的想法?

除了AuthUserProvider私有變量$user不能在當前的http請求中AuthUserProvider之外,您幾乎已經完成了。 如果您不能從Web服務中“按ID檢索”,我想唯一的方法是將整個用戶存儲在會話中-Laravel本身將用戶的ID存儲在會話中,並且事實是它僅存儲ID(而不是整個ID)。用戶)是需要使用retrieveByID方法的原因之一。
以下內容僅作澄清,未經測試。

class AuthUserProvider implements UserProviderInterface {

    public function retrieveByCredentials(array $credentials) {
        $client = new \SoapClient('webserviceurl');
        $res = $client->Validar_Cliente($credentials);
        $res = $res->Validar_ClienteResult;
        if($res->infoError->bError === true) {
            return;
        }
        $res->id = $res->id_cliente;

        Session::put('entireuser', $res);

        $user =  new GenericUser((array) $res);
        return $user;
    }


    public function retrieveByID($identifier) {

        $res = Session::get('entireuser');

        return new GenericUser((array) $res);
    }

//  ...

}

如果您無法通過Web服務通過ID進行檢索 ,那么我猜您也無法通過記住令牌進行檢索 ,因此除非您將部分用戶數據存儲在第二個數據庫中 ,否則可能無法實現“記住我”功能(此時可以使用它代替上面的會話)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM