3 namespace Drupal\Core\Session;
6 * An implementation of the user account interface for the global user.
8 * @todo: Change all properties to protected.
10 class UserSession implements AccountInterface {
20 * List of the roles this user has.
22 * Defaults to the anonymous role.
26 protected $roles = [AccountInterface::ANONYMOUS_ROLE];
29 * The Unix timestamp when the user last accessed the site.
36 * The name of this account.
43 * The preferred language code of the account.
47 protected $preferred_langcode;
50 * The preferred administrative language code of the account.
54 protected $preferred_admin_langcode;
57 * The email address of this account.
64 * The timezone of this account.
71 * Constructs a new user session.
73 * @param array $values
74 * Array of initial values for the user session.
76 public function __construct(array $values = []) {
77 foreach ($values as $key => $value) {
85 public function id() {
92 public function getRoles($exclude_locked_roles = FALSE) {
93 $roles = $this->roles;
95 if ($exclude_locked_roles) {
96 $roles = array_values(array_diff($roles, [AccountInterface::ANONYMOUS_ROLE, AccountInterface::AUTHENTICATED_ROLE]));
105 public function hasPermission($permission) {
106 // User #1 has all privileges.
107 if ((int) $this->id() === 1) {
111 return $this->getRoleStorage()->isPermissionInRoles($permission, $this->getRoles());
117 public function isAuthenticated() {
118 return $this->uid > 0;
124 public function isAnonymous() {
125 return $this->uid == 0;
131 public function getPreferredLangcode($fallback_to_default = TRUE) {
132 $language_list = \Drupal::languageManager()->getLanguages();
133 if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) {
134 return $language_list[$this->preferred_langcode]->getId();
137 return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';
144 public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
145 $language_list = \Drupal::languageManager()->getLanguages();
146 if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) {
147 return $language_list[$this->preferred_admin_langcode]->getId();
150 return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';
157 public function getUsername() {
158 return $this->getAccountName();
164 public function getAccountName() {
171 public function getDisplayName() {
172 $name = $this->name ?: \Drupal::config('user.settings')->get('anonymous');
173 \Drupal::moduleHandler()->alter('user_format_name', $name, $this);
180 public function getEmail() {
187 public function getTimeZone() {
188 return $this->timezone;
194 public function getLastAccessedTime() {
195 return $this->access;
199 * Returns the role storage object.
201 * @return \Drupal\user\RoleStorageInterface
202 * The role storage object.
204 protected function getRoleStorage() {
205 return \Drupal::entityManager()->getStorage('user_role');