Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Form Password Authenticator return only UserInterface symfony

Tags:

php

symfony

Symfony implement - Custom Form Password Authenticator http://symfony.com/doc/current/cookbook/security/custom_password_authenticator.html

So this part of code get my user provider class defined in services.yml via UserProviderInterface and return UserInterface.

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

By default my custom user class provider implement AdvancedUserInterface with some more functions: http://api.symfony.com/2.6/Symfony/Component/Security/Core/User/AdvancedUserInterface.html

class Users implements AdvancedUserInterface, \Serializable

Question: How to implement AdvancedUserInterface with UserProviderInterface. Or I simply need to recreate this AdvancedUserInterface functions manually and check manually after loaded user object?

Thanks. I hope you understand.

EDITED: It look like AdvancedUser Interface functions not called in custom password authentication automaticaly. So after get user object need to call manual this functions

   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');}
like image 969
Agris Avatar asked Aug 21 '26 12:08

Agris


1 Answers

You probably have something confused.

The AdvancedUserInterface - indicate user instance, and extends from UserInterface.

The UserProviderInterface - indicate of providers for loads users (UserInterface).

In method loadUserByUsername you can returns AdvancedUserInterface instances.

As example:

User:

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

User Provider:

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

  // Some functions
}
like image 173
ZhukV Avatar answered Aug 23 '26 09:08

ZhukV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!