3 namespace Drupal\Core\Session;
6 * Defines an account interface which represents the current user.
8 * Defines an object that has a user id, roles and can have session data. The
9 * interface is implemented both by the global session and the user entity.
13 interface AccountInterface {
16 * Role ID for anonymous users.
18 const ANONYMOUS_ROLE = 'anonymous';
21 * Role ID for authenticated users.
23 const AUTHENTICATED_ROLE = 'authenticated';
26 * Returns the user ID or 0 for anonymous.
34 * Returns a list of roles.
36 * @param bool $exclude_locked_roles
37 * (optional) If TRUE, locked roles (anonymous/authenticated) are not returned.
42 public function getRoles($exclude_locked_roles = FALSE);
45 * Checks whether a user has a certain permission.
47 * @param string $permission
48 * The permission string to check.
51 * TRUE if the user has the permission, FALSE otherwise.
53 public function hasPermission($permission);
56 * Returns TRUE if the account is authenticated.
59 * TRUE if the account is authenticated.
61 public function isAuthenticated();
64 * Returns TRUE if the account is anonymous.
67 * TRUE if the account is anonymous.
69 public function isAnonymous();
72 * Returns the preferred language code of the account.
74 * @param bool $fallback_to_default
75 * (optional) Whether the return value will fall back to the site default
76 * language if the user has no language preference.
79 * Returned language code depends upon following:
80 * - The user preferred language code is returned if set in the account.
81 * - If the user has no preferred language and $fallback_to_default is TRUE
82 * then the site default language code is returned.
83 * - If the user has no preferred language and $fallback_to_default is FALSE
84 * then empty string is returned.
86 public function getPreferredLangcode($fallback_to_default = TRUE);
89 * Returns the preferred administrative language code of the account.
91 * Defines which language is used on administrative pages.
93 * @param bool $fallback_to_default
94 * (optional) Whether the return value will fall back to the site default
95 * language if the user has no administration language preference.
98 * The language code that is preferred by the account for administration
99 * pages. If the preferred language is not set or is a language not
100 * configured anymore on the site, the site default is returned or an empty
101 * string is returned (if $fallback_to_default is FALSE).
103 public function getPreferredAdminLangcode($fallback_to_default = TRUE);
106 * Returns the unaltered login name of this account.
109 * An unsanitized plain-text string with the name of this account that is
110 * used to log in. Only display this name to admins and to the user who owns
111 * this account, and only in the context of the name used to log in. For
112 * any other display purposes, use
113 * \Drupal\Core\Session\AccountInterface::getDisplayName() instead.
115 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
116 * Use \Drupal\Core\Session\AccountInterface::getAccountName() or
117 * \Drupal\user\UserInterface::getDisplayName() instead.
119 public function getUsername();
122 * Returns the unaltered login name of this account.
125 * An unsanitized plain-text string with the name of this account that is
126 * used to log in. Only display this name to admins and to the user who owns
127 * this account, and only in the context of the name used to login. For
128 * any other display purposes, use
129 * \Drupal\Core\Session\AccountInterface::getDisplayName() instead.
131 public function getAccountName();
134 * Returns the display name of this account.
136 * By default, the passed-in object's 'name' property is used if it exists, or
137 * else, the site-defined value for the 'anonymous' variable. However, a
138 * module may override this by implementing
139 * hook_user_format_name_alter(&$name, $account).
141 * @see hook_user_format_name_alter()
143 * @return string|\Drupal\Component\Render\MarkupInterface
144 * Either a string that will be auto-escaped on output or a
145 * MarkupInterface object that is already HTML escaped. Either is safe
146 * to be printed within HTML fragments.
148 public function getDisplayName();
151 * Returns the email address of this account.
153 * @return string|null
154 * The email address, or NULL if the account is anonymous or the user does
155 * not have an email address.
157 public function getEmail();
160 * Returns the timezone of this account.
163 * Name of the timezone.
165 public function getTimeZone();
168 * The timestamp when the account last accessed the site.
170 * A value of 0 means the user has never accessed the site.
173 * Timestamp of the last access.
175 public function getLastAccessedTime();