Webiny\Component\Security\User\AbstractUser::populate PHP Méthode

populate() public méthode

Populate the user object.
public populate ( string $username, string $password, array $roles, boolean $isAuthenticated = false )
$username string Username.
$password string Hashed password.
$roles array Array of the assigned roles.
$isAuthenticated boolean Boolean flag that tells us if user is already authenticated or not.
    public function populate($username, $password, array $roles, $isAuthenticated = false)
    {
        // store general data
        $this->username = $username;
        $this->password = $password;
        $this->isAuthenticated = $isAuthenticated;
        $this->roles = $this->arr([]);
        foreach ($roles as $r) {
            if ($this->isInstanceOf($r, '\\Webiny\\Component\\Security\\Role\\Role')) {
                $this->roles->append($r);
            } else {
                $this->roles->append(new Role($r));
            }
        }
        // append anonymous role
        $this->roles->append(new Role('ROLE_ANONYMOUS'));
    }

Usage Example

Exemple #1
0
 /**
  * Tries to retrieve the user from current token.
  * If the token does not exist, AnonymousUser is returned.
  *
  * @throws FirewallException
  * @return bool|\Webiny\Component\Security\User\AbstractUser
  */
 public function getUser()
 {
     if ($this->userAuthenticated) {
         return $this->user;
     }
     try {
         // get token
         $this->user = new AnonymousUser();
         $tokenData = $this->getToken()->getUserFromToken();
         if (!$tokenData) {
             $this->eventManager()->fire(SecurityEvent::NOT_AUTHENTICATED, new SecurityEvent($this->user));
             $this->userAuthenticated = false;
             return $this->user;
         } else {
             $this->user->populate($tokenData->getUsername(), '', $tokenData->getRoles(), true);
             $this->user->setAuthProviderName($tokenData->getAuthProviderName());
             $this->user->setUserProviderName($tokenData->getUserProviderName());
             $this->eventManager()->fire(SecurityEvent::AUTHENTICATED, new SecurityEvent($this->user));
             $this->setUserRoles();
             $this->userAuthenticated = true;
             return $this->user;
         }
     } catch (\Exception $e) {
         $this->userAuthenticated = true;
         throw new FirewallException($e->getMessage());
     }
 }
All Usage Examples Of Webiny\Component\Security\User\AbstractUser::populate