UserSession.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Drupal\Core\Session;
  3. /**
  4. * An implementation of the user account interface for the global user.
  5. *
  6. * @todo: Change all properties to protected.
  7. */
  8. class UserSession implements AccountInterface {
  9. /**
  10. * User ID.
  11. *
  12. * @var int
  13. */
  14. protected $uid = 0;
  15. /**
  16. * List of the roles this user has.
  17. *
  18. * Defaults to the anonymous role.
  19. *
  20. * @var array
  21. */
  22. protected $roles = [AccountInterface::ANONYMOUS_ROLE];
  23. /**
  24. * The Unix timestamp when the user last accessed the site.
  25. *
  26. * @var string
  27. */
  28. protected $access;
  29. /**
  30. * The name of this account.
  31. *
  32. * @var string
  33. */
  34. public $name = '';
  35. /**
  36. * The preferred language code of the account.
  37. *
  38. * @var string
  39. */
  40. protected $preferred_langcode;
  41. /**
  42. * The preferred administrative language code of the account.
  43. *
  44. * @var string
  45. */
  46. protected $preferred_admin_langcode;
  47. /**
  48. * The email address of this account.
  49. *
  50. * @var string
  51. */
  52. protected $mail;
  53. /**
  54. * The timezone of this account.
  55. *
  56. * @var string
  57. */
  58. protected $timezone;
  59. /**
  60. * Constructs a new user session.
  61. *
  62. * @param array $values
  63. * Array of initial values for the user session.
  64. */
  65. public function __construct(array $values = []) {
  66. foreach ($values as $key => $value) {
  67. $this->$key = $value;
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function id() {
  74. return $this->uid;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getRoles($exclude_locked_roles = FALSE) {
  80. $roles = $this->roles;
  81. if ($exclude_locked_roles) {
  82. $roles = array_values(array_diff($roles, [AccountInterface::ANONYMOUS_ROLE, AccountInterface::AUTHENTICATED_ROLE]));
  83. }
  84. return $roles;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function hasPermission($permission) {
  90. // User #1 has all privileges.
  91. if ((int) $this->id() === 1) {
  92. return TRUE;
  93. }
  94. return $this->getRoleStorage()->isPermissionInRoles($permission, $this->getRoles());
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function isAuthenticated() {
  100. return $this->uid > 0;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function isAnonymous() {
  106. return $this->uid == 0;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getPreferredLangcode($fallback_to_default = TRUE) {
  112. $language_list = \Drupal::languageManager()->getLanguages();
  113. if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) {
  114. return $language_list[$this->preferred_langcode]->getId();
  115. }
  116. else {
  117. return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';
  118. }
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
  124. $language_list = \Drupal::languageManager()->getLanguages();
  125. if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) {
  126. return $language_list[$this->preferred_admin_langcode]->getId();
  127. }
  128. else {
  129. return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';
  130. }
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getUsername() {
  136. return $this->getAccountName();
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function getAccountName() {
  142. return $this->name;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getDisplayName() {
  148. $name = $this->name ?: \Drupal::config('user.settings')->get('anonymous');
  149. \Drupal::moduleHandler()->alter('user_format_name', $name, $this);
  150. return $name;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getEmail() {
  156. return $this->mail;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getTimeZone() {
  162. return $this->timezone;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function getLastAccessedTime() {
  168. return $this->access;
  169. }
  170. /**
  171. * Returns the role storage object.
  172. *
  173. * @return \Drupal\user\RoleStorageInterface
  174. * The role storage object.
  175. */
  176. protected function getRoleStorage() {
  177. return \Drupal::entityManager()->getStorage('user_role');
  178. }
  179. }