class User

User authentication and authorization.

Constants

LogoutManual

Log-out reason

LogoutInactivity

Log-out reason

LOGOUT_MANUAL

MANUAL

LOGOUT_INACTIVITY

INACTIVITY

Properties

$guestRole

role for an unauthenticated user, unless a guest identity provides its own roles

$authenticatedRole

default role for authenticated user without own identity

$persistIdentity

keep identity available (via getIdentity() and getId()) after logout or expiration; depends on the storage implementation

$onLoggedIn
$onLoggedOut
bool read-only $loggedIn
IIdentity read-only $identity

Methods

__construct(UserStorage $storage, Authenticator|null $authenticator = null, Authorizator|null $authorizator = null)

No description

getStorage()

No description

void
login(IIdentity $username, string|null $password = null)

Authenticates the user. Accepts username and password, or an IIdentity directly.

void
logout(bool $clearIdentity = false)

Logs out the user from the current session. The identity is kept available afterwards, unless $clearIdentity is set or the $persistIdentity property is disabled.

bool
isLoggedIn()

Checks whether the user is authenticated.

IIdentity|null
getIdentity()

Returns the user identity. When not logged in, this is the retained identity (unless $persistIdentity is disabled) or a guest identity if the authenticator provides one; null otherwise.

string|int|null
getId()

Returns the ID of the identity returned by getIdentity(), so it may be the retained or guest identity's ID even when not logged in; null if there is no identity.

void
refreshStorage()

Discards the cached authentication state and identity, forcing a reload on next access.

setAuthenticator(Authenticator $handler)

Sets authentication handler.

getAuthenticator()

Returns authentication handler.

Authenticator|null
getAuthenticatorIfExists()

Returns authentication handler, or null if none is set.

bool
hasAuthenticator() deprecated

No description

setExpiration(string|null $expire, bool $clearIdentity = false)

Enables log out after inactivity (like '20 minutes'). The identity is kept available afterwards, unless $clearIdentity is set or the $persistIdentity property is disabled.

int|null
getLogoutReason()

Returns the logout reason: LogoutManual or LogoutInactivity, or null if not applicable.

array
getRoles()

Returns effective roles derived from the login state, not from the (possibly retained) identity.

bool
isInRole(string $role)

Checks whether the user has the specified effective role.

bool
isAllowed(mixed $resource = Authorizator::All, mixed $privilege = Authorizator::All)

Checks whether the user has access to the given resource and privilege.

setAuthorizator(Authorizator $handler)

Sets authorization handler.

getAuthorizator()

Returns current authorization handler.

Authorizator|null
getAuthorizatorIfExists()

Returns authorization handler, or null if none is set.

bool
hasAuthorizator() deprecated

No description

Details

at line 69
__construct(UserStorage $storage, Authenticator|null $authenticator = null, Authorizator|null $authorizator = null)

No description

Parameters

UserStorage $storage
Authenticator|null $authenticator
Authorizator|null $authorizator

at line 77
final UserStorage getStorage()

No description

Return Value

UserStorage

at line 91
void login(IIdentity $username, string|null $password = null)

Authenticates the user. Accepts username and password, or an IIdentity directly.

Parameters

IIdentity $username

username or identity

string|null $password

Return Value

void

Exceptions

AuthenticationException

at line 120
final void logout(bool $clearIdentity = false)

Logs out the user from the current session. The identity is kept available afterwards, unless $clearIdentity is set or the $persistIdentity property is disabled.

Parameters

bool $clearIdentity

Return Value

void

at line 138
final bool isLoggedIn()

Checks whether the user is authenticated.

Return Value

bool

at line 149
final IIdentity|null getIdentity()

Returns the user identity. When not logged in, this is the retained identity (unless $persistIdentity is disabled) or a guest identity if the authenticator provides one; null otherwise.

Return Value

IIdentity|null

at line 194
string|int|null getId()

Returns the ID of the identity returned by getIdentity(), so it may be the retained or guest identity's ID even when not logged in; null if there is no identity.

Return Value

string|int|null

at line 204
final void refreshStorage()

Discards the cached authentication state and identity, forcing a reload on next access.

Return Value

void

at line 215
User setAuthenticator(Authenticator $handler)

Sets authentication handler.

Parameters

Authenticator $handler

Return Value

User

at line 226
final Authenticator getAuthenticator()

Returns authentication handler.

Return Value

Authenticator

at line 239
final Authenticator|null getAuthenticatorIfExists()

Returns authentication handler, or null if none is set.

Return Value

Authenticator|null

at line 246
final bool hasAuthenticator() deprecated

deprecated

No description

Return Value

bool

at line 256
User setExpiration(string|null $expire, bool $clearIdentity = false)

Enables log out after inactivity (like '20 minutes'). The identity is kept available afterwards, unless $clearIdentity is set or the $persistIdentity property is disabled.

Parameters

string|null $expire
bool $clearIdentity

Return Value

User

at line 266
final int|null getLogoutReason()

Returns the logout reason: LogoutManual or LogoutInactivity, or null if not applicable.

Return Value

int|null

at line 280
array getRoles()

Returns effective roles derived from the login state, not from the (possibly retained) identity.

Logged in: the identity's roles, or authenticatedRole. Otherwise: the guest identity's roles, or guestRole.

Return Value

array

at line 294
final bool isInRole(string $role)

Checks whether the user has the specified effective role.

Parameters

string $role

Return Value

bool

at line 310
bool isAllowed(mixed $resource = Authorizator::All, mixed $privilege = Authorizator::All)

Checks whether the user has access to the given resource and privilege.

Null means all resources or all privileges.

Parameters

mixed $resource
mixed $privilege

Return Value

bool

at line 325
User setAuthorizator(Authorizator $handler)

Sets authorization handler.

Parameters

Authorizator $handler

Return Value

User

at line 335
final Authorizator getAuthorizator()

Returns current authorization handler.

Return Value

Authorizator

at line 348
final Authorizator|null getAuthorizatorIfExists()

Returns authorization handler, or null if none is set.

Return Value

Authorizator|null

at line 355
final bool hasAuthorizator() deprecated

deprecated

No description

Return Value

bool

Traits

SmartObject