0 follower

Final Class Yiisoft\User\CurrentUser

InheritanceYiisoft\User\CurrentUser

Maintains current identity and allows logging in and out using it.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\User\CurrentUser
can() Checks if the user can perform the operation as specified by the given permission. Yiisoft\User\CurrentUser
clear() Clears the data for working with the event loop. Yiisoft\User\CurrentUser
clearIdentityOverride() Clears the identity override. Yiisoft\User\CurrentUser
getId() Returns a value that uniquely represents the user. Yiisoft\User\CurrentUser
getIdentity() Returns the identity object associated with the currently logged-in user. Yiisoft\User\CurrentUser
isGuest() Returns a value indicating whether the user is a guest (not authenticated). Yiisoft\User\CurrentUser
login() Logs in a user. Yiisoft\User\CurrentUser
logout() Logs out the current user. Yiisoft\User\CurrentUser
overrideIdentity() Overrides identity. Yiisoft\User\CurrentUser
withAbsoluteAuthTimeout() Returns a new instance with the specified number of seconds in which the user will be logged out automatically regardless of activity. Yiisoft\User\CurrentUser
withAccessChecker() Returns a new instance with the specified access checker to check user permissions {@see can()}. Yiisoft\User\CurrentUser
withAuthTimeout() Returns a new instance with the specified number of seconds in which the user will be logged out automatically in case of remaining inactive. Yiisoft\User\CurrentUser
withSession() Returns a new instance with the specified session to store current user ID and auth timeouts. Yiisoft\User\CurrentUser

Constants

Hide inherited constants

Constant Value Description Defined By
SESSION_AUTH_ABSOLUTE_EXPIRE '__auth_absolute_expire' Yiisoft\User\CurrentUser
SESSION_AUTH_EXPIRE '__auth_expire' Yiisoft\User\CurrentUser
SESSION_AUTH_ID '__auth_id' Yiisoft\User\CurrentUser

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Auth\IdentityRepositoryInterface $identityRepository, \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher, Yiisoft\User\Guest\GuestIdentityFactoryInterface|null $guestIdentityFactory null )
$identityRepository \Yiisoft\Auth\IdentityRepositoryInterface
$eventDispatcher \Psr\EventDispatcher\EventDispatcherInterface
$guestIdentityFactory Yiisoft\User\Guest\GuestIdentityFactoryInterface|null

                public function __construct(
    private IdentityRepositoryInterface $identityRepository,
    private EventDispatcherInterface $eventDispatcher,
    ?GuestIdentityFactoryInterface $guestIdentityFactory = null
) {
    $this->guestIdentityFactory = $guestIdentityFactory ?? new GuestIdentityFactory();
}

            
can() public method

Checks if the user can perform the operation as specified by the given permission.

Note that you must provide access checker via {@see \Yiisoft\User\withAccessChecker()} in order to use this method. Otherwise, it will always return false.

public boolean can ( \BackedEnum|string $permissionName, array $params = [] )
$permissionName \BackedEnum|string

The name of the permission (e.g. "edit post") that needs access check. You can use backed enumerations as permission name, in this case the value of the enumeration will be used.

$params array

Name-value pairs that would be passed to the rules associated with the roles and permissions assigned to the user.

return boolean

Whether the user can perform the operation as specified by the given permission.

                public function can(string|BackedEnum $permissionName, array $params = []): bool
{
    if ($this->accessChecker === null) {
        return false;
    }
    if ($permissionName instanceof BackedEnum) {
        $permissionName = (string) $permissionName->value;
    }
    return $this->accessChecker->userHasPermission($this->getId(), $permissionName, $params);
}

            
clear() public method

Clears the data for working with the event loop.

public void clear ( )

                public function clear(): void
{
    $this->identity = null;
    $this->identityOverride = null;
}

            
clearIdentityOverride() public method

Clears the identity override.

public void clearIdentityOverride ( )

                public function clearIdentityOverride(): void
{
    $this->identityOverride = null;
}

            
getId() public method

Returns a value that uniquely represents the user.

See also getIdentity().

public string|null getId ( )
return string|null

The unique identifier for the user. If null, it means the user is a guest.

                public function getId(): ?string
{
    return $this->getIdentity()->getId();
}

            
getIdentity() public method

Returns the identity object associated with the currently logged-in user.

public \Yiisoft\Auth\IdentityInterface getIdentity ( )

                public function getIdentity(): IdentityInterface
{
    $identity = $this->identityOverride ?? $this->identity;
    if ($identity === null) {
        $id = $this->getSavedId();
        if ($id !== null) {
            $identity = $this->identityRepository->findIdentity($id);
        }
        $identity ??= $this->guestIdentityFactory->create();
        $this->identity = $identity;
    }
    return $identity;
}

            
isGuest() public method

Returns a value indicating whether the user is a guest (not authenticated).

See also getIdentity().

public boolean isGuest ( )
return boolean

Whether the current user is a guest.

                public function isGuest(): bool
{
    return $this->getIdentity() instanceof GuestIdentityInterface;
}

            
login() public method

Logs in a user.

public boolean login ( \Yiisoft\Auth\IdentityInterface $identity )
$identity \Yiisoft\Auth\IdentityInterface

The user identity (which should already be authenticated).

return boolean

Whether the user is logged in.

                public function login(IdentityInterface $identity): bool
{
    if ($this->beforeLogin($identity)) {
        $this->switchIdentity($identity);
        $this->afterLogin($identity);
    }
    return !$this->isGuest();
}

            
logout() public method

Logs out the current user.

public boolean logout ( )
return boolean

Whether the user is logged out.

                public function logout(): bool
{
    if ($this->isGuest()) {
        return false;
    }
    $identity = $this->getIdentity();
    if ($this->beforeLogout($identity)) {
        $this->switchIdentity($this->guestIdentityFactory->create());
        $this->afterLogout($identity);
    }
    return $this->isGuest();
}

            
overrideIdentity() public method

Overrides identity.

public void overrideIdentity ( \Yiisoft\Auth\IdentityInterface $identity )
$identity \Yiisoft\Auth\IdentityInterface

The identity instance to overriding.

                public function overrideIdentity(IdentityInterface $identity): void
{
    $this->identityOverride = $identity;
}

            
withAbsoluteAuthTimeout() public method

Returns a new instance with the specified number of seconds in which the user will be logged out automatically regardless of activity.

public self withAbsoluteAuthTimeout ( integer $timeout )
$timeout integer

The number of seconds in which the user will be logged out automatically regardless of activity. Default is null, the user will be logged out after the current session expires.

                public function withAbsoluteAuthTimeout(int $timeout): self
{
    $new = clone $this;
    $new->absoluteAuthTimeout = $timeout;
    return $new;
}

            
withAccessChecker() public method

Returns a new instance with the specified access checker to check user permissions {@see can()}.

public self withAccessChecker ( \Yiisoft\Access\AccessCheckerInterface $accessChecker )
$accessChecker \Yiisoft\Access\AccessCheckerInterface

The access checker instance.

                public function withAccessChecker(AccessCheckerInterface $accessChecker): self
{
    $new = clone $this;
    $new->accessChecker = $accessChecker;
    return $new;
}

            
withAuthTimeout() public method

Returns a new instance with the specified number of seconds in which the user will be logged out automatically in case of remaining inactive.

public self withAuthTimeout ( integer $timeout )
$timeout integer

The number of seconds in which the user will be logged out automatically in case of remaining inactive. Default is null, the user will be logged out after the current session expires.

                public function withAuthTimeout(int $timeout): self
{
    $new = clone $this;
    $new->authTimeout = $timeout;
    return $new;
}

            
withSession() public method

Returns a new instance with the specified session to store current user ID and auth timeouts.

public self withSession ( \Yiisoft\Session\SessionInterface $session )
$session \Yiisoft\Session\SessionInterface

The session instance.

                public function withSession(SessionInterface $session): self
{
    $new = clone $this;
    $new->session = $session;
    return $new;
}