2014-09-06 61 views
5

我有肥皂來驗證用戶身份沒有數據庫(SOAP)PHP Symfony2的自定義驗證

<?php 
$client = new SoapClient('http://mysite/code/soap.wsdl'); 
$user = $client->login('username','password'); 

if(isset($user['IdAccount']) && ($user['IdAccount'] > 0)) 
    echo 'Logon OK'; 
else 
    echo 'Logon Fail!'; 

我想在Symfony2中使用無需數據庫,只在內存...

我試圖執行一個Custom UserProvider,但我沒有使它工作所需的所有數據...

class WebserviceUserProvider implements UserProviderInterface 
{ 
    public function loadUserByUsername($username) 
    { 
     $client = new SoapClient('http://mysite/code/soap.wsdl'); 

     $user = $client->login($username, PASSWORD????); 

     if (isset($user['IdAccount']) && ($user['IdAccount'] > 0)) 
      return new WebserviceUser($username, PASSWORD????, SALT???, ROLES????); 

     throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username) 
    ); 
} 

我不能改變肥皂

抱歉我的英文不好! :(

+2

要做到這一點正確的方式,你需要你自己的自定義身份驗證提供程序。http://symfony.com/doc/currentbook/security/custom_authentication_provider.html。不容易的事情來實現。教程後退效果最好。但我強烈建議尋找一個SOAP認證包。我確定有一些好的。 – Cerad 2014-09-06 20:15:36

回答

2

據我瞭解,要使用形式的認證,並使用SOAP服務檢查密碼

如果您使用的Symfony 2.4以上,就可以使用SimpleFormAuthenticatorInterface

實現示例:

// ... namespace and uses 

class Authenticator implements SimpleFormAuthenticatorInterface 
{ 
    private $soapClient; 

    public function __construct(\SoapClient $soapClient) 
    { 
     $this->soapClient = $soapClient; 
    } 

    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) 
    { 
     $user = $this->soapClient->login($token->getUsername(), $token->getCredentials()); 
     if (isset($user['IdAccount']) && ($user['IdAccount'] > 0)) { 
      $user = $userProvider->loadUserByUsername($token->getUsername()); 
      return new UsernamePasswordToken(
       $user, 
       null, 
       $providerKey, 
       $user->getRoles() 
      ); 
     } 

     throw new AuthenticationException('Invalid username or password'); 
    } 

    public function supportsToken(TokenInterface $token, $providerKey) 
    { 
     return $token instanceof UsernamePasswordToken 
      && $token->getProviderKey() === $providerKey; 
    } 

    public function createToken(Request $request, $username, $password, $providerKey) 
    { 
     return new UsernamePasswordToken($username, $password, $providerKey); 
    } 
} 
+0

但我不明白如何爲'$ userProvider-> loadUserByUsername($ token-> getUsername())' – 2014-10-04 08:07:49

+1

實現'UserProviderInterface'您可以跳過該行並從SOAP創建用戶對象(應該實現'UserInterface')方法結果。或者只用用戶名設置創建空的(例如'Symfony \ Component \ Security \ Core \ User \ User')。不需要在那裏使用UserProvider。 – 2014-10-08 21:02:53