Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Session/UserSession.php
- 9 core/lib/Drupal/Core/Session/UserSession.php
Namespace
Drupal\Core\Session
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 hasRole(string $rid) : bool {
return in_array($rid, $this
->getRoles(), TRUE);
}
public function hasPermission($permission) {
if (!is_string($permission)) {
@trigger_error('Calling ' . __METHOD__ . '() with a $permission parameter of type other than string is deprecated in drupal:10.3.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3411485', E_USER_DEPRECATED);
return FALSE;
}
return \Drupal::service('permission_checker')
->hasPermission($permission, $this);
}
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 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