自定义表单密码验证器仅返回UserInterface符号


Custom Form Password Authenticator return only UserInterface symfony

Symfony implement-自定义表单密码验证器http://symfony.com/doc/current/cookbook/security/custom_password_authenticator.html

因此,这部分代码通过UserProviderInterface在services.yml中定义我的用户提供程序类,并返回UserInterface

public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
    try {
        $user = $userProvider->loadUserByUsername($token->getUsername());

默认情况下,我的自定义用户类提供程序使用更多函数实现AdvancedUserInterface:http://api.symfony.com/2.6/Symfony/Component/Security/Core/User/AdvancedUserInterface.html

class Users implements AdvancedUserInterface, 'Serializable

问题:如何用UserProviderInterface实现AdvancedUserInterface。或者我只需要手动重新创建这个AdvancedUserInterface函数,并在加载用户对象后手动检查?

谢谢。我希望你能理解。

编辑:它看起来像是AdvancedUser Interface函数没有在自定义密码身份验证中自动调用。所以在得到用户对象后需要调用手动这个函数

   try {
        $user = $userProvider->loadUserByUsername($token->getUsername());
        if ($user->isAccountNonExpired()){ throw new AuthenticationException('expired');}
        if ($user->isAccountNonLocked()){ throw new AuthenticationException('locked');}
        if ($user->isCredentialsNonExpired()){ throw new AuthenticationException('credExpired');}
        if (!$user->isEnabled()){ throw new AuthenticationException('disabled');}

您可能有一些困惑。

AdvancedUserInterface-表示用户实例,并从UserInterface扩展而来。

UserProviderInterface-表示加载用户的提供程序(UserInterface(。

在方法loadUserByUsername中,您可以返回AdvancedUserInterface实例。

例如:

用户:

class User implements AdvancedUserInterface
{
  // .. Some fields and functions
}

用户提供商:

class MyUserProvider implements UserProviderInterface
{
  public function loadUserByUsername($username)
  {
     // Get the User instance from DB or another system
     return $user;
  }
  // Some functions
}