class AccountProxy implements AccountProxyInterface (View source)

A proxied implementation of AccountInterface.

The reason why we need an account proxy is that we don't want to have global state directly stored in the container.

This proxy object avoids multiple invocations of the authentication manager which can happen if the current user is accessed in constructors. It also allows legacy code to change the current user where the user cannot be directly injected into dependent code.

Traits

Provides dependency injection friendly methods for serialization.

Properties

protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected AccountInterface $account

The instantiated account.

protected int $id

Account id.

protected int deprecated $initialAccountId

Initial account id.

protected EventDispatcherInterface $eventDispatcher

Event dispatcher.

Methods

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

__construct(EventDispatcherInterface $eventDispatcher = NULL)

AccountProxy constructor.

setAccount(AccountInterface $account)

Sets the currently wrapped account.

getAccount()

Gets the currently wrapped account.

int
id()

Returns the user ID or 0 for anonymous.

array
getRoles(bool $exclude_locked_roles = FALSE)

Returns a list of roles.

bool
hasPermission(string $permission)

Checks whether a user has a certain permission.

bool
isAuthenticated()

Returns TRUE if the account is authenticated.

bool
isAnonymous()

Returns TRUE if the account is anonymous.

string
getPreferredLangcode(bool $fallback_to_default = TRUE)

Returns the preferred language code of the account.

string
getPreferredAdminLangcode(bool $fallback_to_default = TRUE)

Returns the preferred administrative language code of the account.

string
getUsername()

Returns the unaltered login name of this account.

string
getAccountName()

Returns the unaltered login name of this account.

getDisplayName()

Returns the display name of this account.

string|null
getEmail()

Returns the email address of this account.

string
getTimeZone()

Returns the timezone of this account.

int
getLastAccessedTime()

The timestamp when the account last accessed the site.

setInitialAccountId(int $account_id)

Sets the id of the initial account.

loadUserEntity(int $account_id)

Load a user entity.

Details

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

__construct(EventDispatcherInterface $eventDispatcher = NULL)

AccountProxy constructor.

Parameters

EventDispatcherInterface $eventDispatcher

Event dispatcher.

setAccount(AccountInterface $account)

Sets the currently wrapped account.

Setting the current account is highly discouraged! Instead, make sure to inject the desired user object into the dependent code directly.

A preferable method of account impersonation is to use \Drupal\Core\Session\AccountSwitcherInterface::switchTo() and \Drupal\Core\Session\AccountSwitcherInterface::switchBack().

Parameters

AccountInterface $account

The current account.

AccountInterface getAccount()

Gets the currently wrapped account.

Return Value

AccountInterface

The current account.

int id()

Returns the user ID or 0 for anonymous.

Return Value

int

The user ID.

array getRoles(bool $exclude_locked_roles = FALSE)

Returns a list of roles.

Parameters

bool $exclude_locked_roles

(optional) If TRUE, locked roles (anonymous/authenticated) are not returned.

Return Value

array

List of role IDs.

bool hasPermission(string $permission)

Checks whether a user has a certain permission.

Parameters

string $permission

The permission string to check.

Return Value

bool

TRUE if the user has the permission, FALSE otherwise.

bool isAuthenticated()

Returns TRUE if the account is authenticated.

Return Value

bool

TRUE if the account is authenticated.

bool isAnonymous()

Returns TRUE if the account is anonymous.

Return Value

bool

TRUE if the account is anonymous.

string getPreferredLangcode(bool $fallback_to_default = TRUE)

Returns the preferred language code of the account.

Parameters

bool $fallback_to_default

(optional) Whether the return value will fall back to the site default language if the user has no language preference.

Return Value

string

Returned language code depends upon following:

  • The user preferred language code is returned if set in the account.
  • If the user has no preferred language and $fallback_to_default is TRUE then the site default language code is returned.
  • If the user has no preferred language and $fallback_to_default is FALSE then empty string is returned.

string getPreferredAdminLangcode(bool $fallback_to_default = TRUE)

Returns the preferred administrative language code of the account.

Defines which language is used on administrative pages.

Parameters

bool $fallback_to_default

(optional) Whether the return value will fall back to the site default language if the user has no administration language preference.

Return Value

string

The language code that is preferred by the account for administration pages. If the preferred language is not set or is a language not configured anymore on the site, the site default is returned or an empty string is returned (if $fallback_to_default is FALSE).

string getUsername()

Returns the unaltered login name of this account.

Return Value

string

An unsanitized plain-text string with the name of this account that is used to log in. Only display this name to admins and to the user who owns this account, and only in the context of the name used to log in. For any other display purposes, use \Drupal\Core\Session\AccountInterface::getDisplayName() instead.

string getAccountName()

Returns the unaltered login name of this account.

Return Value

string

An unsanitized plain-text string with the name of this account that is used to log in. Only display this name to admins and to the user who owns this account, and only in the context of the name used to login. For any other display purposes, use \Drupal\Core\Session\AccountInterface::getDisplayName() instead.

string|MarkupInterface getDisplayName()

Returns the display name of this account.

By default, the passed-in object's 'name' property is used if it exists, or else, the site-defined value for the 'anonymous' variable. However, a module may override this by implementing hook_user_format_name_alter(&$name, $account).

Return Value

string|MarkupInterface

Either a string that will be auto-escaped on output or a MarkupInterface object that is already HTML escaped. Either is safe to be printed within HTML fragments.

string|null getEmail()

Returns the email address of this account.

Return Value

string|null

The email address, or NULL if the account is anonymous or the user does not have an email address.

string getTimeZone()

Returns the timezone of this account.

Return Value

string

Name of the timezone.

int getLastAccessedTime()

The timestamp when the account last accessed the site.

A value of 0 means the user has never accessed the site.

Return Value

int

Timestamp of the last access.

setInitialAccountId(int $account_id)

Sets the id of the initial account.

Never use this method, its sole purpose is to work around weird effects during mid-request container rebuilds.

Parameters

int $account_id

The id of the initial account.

protected AccountInterface|null loadUserEntity(int $account_id)

Load a user entity.

The entity manager requires additional initialization code and cache clearing after the list of modules is changed. Therefore it is necessary to retrieve it as late as possible.

Because of serialization issues it is currently not possible to inject the container into the AccountProxy. Thus it is necessary to retrieve the entity manager statically.

Parameters

int $account_id

The id of an account to load.

Return Value

AccountInterface|null

An account or NULL if none is found.

See also

https://www.drupal.org/node/2430447