没有数据库的PHP Symfony2自定义身份验证(SOAP)

时间:2014-09-06 08:54:06

标签: php symfony

我有肥皂来验证用户

<?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)
    );
}

我不能改变肥皂

抱歉我的英语不好! :(

1 个答案:

答案 0 :(得分: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);
    }
}