UserSession.php in Drupal 8
File
core/lib/Drupal/Core/Session/UserSession.php
View source
<?php
namespace Drupal\Core\Session;
class UserSession implements AccountInterface {
protected $uid = 0;
protected $roles = [
AccountInterface::ANONYMOUS_ROLE,
];
protected $access;
public $name = '';
protected $preferred_langcode;
protected $preferred_admin_langcode;
protected $mail;
protected $timezone;
public function __construct(array $values = []) {
foreach ($values as $key => $value) {
$this->{$key} = $value;
}
}
public function id() {
return $this->uid;
}
public function getRoles($exclude_locked_roles = FALSE) {
$roles = $this->roles;
if ($exclude_locked_roles) {
$roles = array_values(array_diff($roles, [
AccountInterface::ANONYMOUS_ROLE,
AccountInterface::AUTHENTICATED_ROLE,
]));
}
return $roles;
}
public function hasPermission($permission) {
if ((int) $this
->id() === 1) {
return TRUE;
}
return $this
->getRoleStorage()
->isPermissionInRoles($permission, $this
->getRoles());
}
public function isAuthenticated() {
return $this->uid > 0;
}
public function isAnonymous() {
return $this->uid == 0;
}
public function getPreferredLangcode($fallback_to_default = TRUE) {
$language_list = \Drupal::languageManager()
->getLanguages();
if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) {
return $language_list[$this->preferred_langcode]
->getId();
}
else {
return $fallback_to_default ? \Drupal::languageManager()
->getDefaultLanguage()
->getId() : '';
}
}
public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
$language_list = \Drupal::languageManager()
->getLanguages();
if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) {
return $language_list[$this->preferred_admin_langcode]
->getId();
}
else {
return $fallback_to_default ? \Drupal::languageManager()
->getDefaultLanguage()
->getId() : '';
}
}
public function getUsername() {
@trigger_error('\\Drupal\\Core\\Session\\AccountInterface::getUsername() is deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. Use \\Drupal\\Core\\Session\\AccountInterface::getAccountName() or \\Drupal\\user\\UserInterface::getDisplayName() instead. See https://www.drupal.org/node/2572493', E_USER_DEPRECATED);
return $this
->getAccountName();
}
public function getAccountName() {
return $this->name;
}
public function getDisplayName() {
$name = $this->name ?: \Drupal::config('user.settings')
->get('anonymous');
\Drupal::moduleHandler()
->alter('user_format_name', $name, $this);
return $name;
}
public function getEmail() {
return $this->mail;
}
public function getTimeZone() {
return $this->timezone;
}
public function getLastAccessedTime() {
return $this->access;
}
protected function getRoleStorage() {
return \Drupal::entityTypeManager()
->getStorage('user_role');
}
}
Classes
Name |
Description |
UserSession |
An implementation of the user account interface for the global user. |