UserSession.php 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

/**
 * @file
 * Contains \Drupal\Core\Session\UserSession.
 */

namespace Drupal\Core\Session;

/**
 * An implementation of the user account interface for the global user.
 *
 * @todo: Change all properties to protected.
 */
class UserSession implements AccountInterface {

  /**
   * User ID.
   *
   * @var int
   */
22
  protected $uid = 0;
23 24 25 26

  /**
   * List of the roles this user has.
   *
27 28
   * Defaults to the anonymous role.
   *
29 30
   * @var array
   */
31
  protected $roles = array('anonymous');
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

  /**
   * Session ID.
   *
   * @var string.
   */
  public $sid;

  /**
   * Secure session ID.
   *
   * @var string.
   */
  public $ssid;

  /**
   * Session data.
   *
   * @var array.
   */
  public $session;

  /**
   * The Unix timestamp when this session last requested a page.
   *
   * @var string.
   */
59
  protected $timestamp;
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  /**
   * The name of this account.
   *
   * @var string
   */
  public $name;

  /**
   * The preferred language code of the account.
   *
   * @var string
   */
  protected $preferred_langcode;

  /**
   * The preferred administrative language code of the account.
   *
   * @var string
   */
  protected $preferred_admin_langcode;

82
  /**
83
   * The email address of this account.
84 85 86 87 88 89 90 91 92 93 94 95
   *
   * @var string
   */
  protected $mail;

  /**
   * The timezone of this account.
   *
   * @var string
   */
  protected $timezone;

96 97 98 99 100 101 102
  /**
   * The hostname for this user session.
   *
   * @var string
   */
  protected $hostname = '';

103 104 105 106
  /**
   * Constructs a new user session.
   *
   * @param array $values
107
   *   Array of initial values for the user session.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
   */
  public function __construct(array $values = array()) {
    foreach ($values as $key => $value) {
      $this->$key = $value;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->uid;
  }

  /**
   * {@inheritdoc}
   */
125 126 127 128
  public function getRoles($exclude_locked_roles = FALSE) {
    $roles = $this->roles;

    if ($exclude_locked_roles) {
129
      $roles = array_values(array_diff($roles, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)));
130 131 132
    }

    return $roles;
133 134
  }

135 136 137 138 139 140 141 142 143
  /**
   * {@inheritdoc}
   */
  public function hasPermission($permission) {
    // User #1 has all privileges.
    if ((int) $this->id() === 1) {
      return TRUE;
    }

144
    return $this->getRoleStorage()->isPermissionInRoles($permission, $this->getRoles());
145 146
  }

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  /**
   * {@inheritdoc}
   */
  public function getSecureSessionId() {
    return $this->ssid;
  }

  /**
   * {@inheritdoc}
   */
  public function getSessionData() {
    return $this->session;
  }

  /**
   * {@inheritdoc}
   */
  public function getSessionId() {
    return $this->sid;
  }

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  /**
   * {@inheritdoc}
   */
  public function isAuthenticated() {
    return $this->uid > 0;
  }

  /**
   * {@inheritdoc}
   */
  public function isAnonymous() {
    return $this->uid == 0;
  }

  /**
   * {@inheritdoc}
   */
185
  function getPreferredLangcode($fallback_to_default = TRUE) {
186
    $language_list = \Drupal::languageManager()->getLanguages();
187
    if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) {
188
      return $language_list[$this->preferred_langcode]->getId();
189 190
    }
    else {
191
      return $fallback_to_default ? language_default()->getId() : '';
192 193 194 195 196 197
    }
  }

  /**
   * {@inheritdoc}
   */
198
  function getPreferredAdminLangcode($fallback_to_default = TRUE) {
199
    $language_list = \Drupal::languageManager()->getLanguages();
200
    if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) {
201
      return $language_list[$this->preferred_admin_langcode]->getId();
202 203
    }
    else {
204
      return $fallback_to_default ? language_default()->getId() : '';
205 206 207 208 209 210 211 212 213 214 215 216
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getUsername() {
    $name = $this->name ?: \Drupal::config('user.settings')->get('anonymous');
    \Drupal::moduleHandler()->alter('user_format_name', $name, $this);
    return $name;
  }

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  /**
   * {@inheritdoc}
   */
  public function getEmail() {
    return $this->mail;
  }

  /**
   * {@inheritdoc}
   */
  public function getTimeZone() {
    return $this->timezone;
  }

  /**
   * {@inheritdoc}
   */
  public function getLastAccessedTime() {
    return $this->timestamp;
  }

238 239 240 241 242 243 244
  /**
   * {@inheritdoc}
   */
  public function getHostname() {
    return $this->hostname;
  }

245 246 247 248 249 250 251 252 253 254
  /**
   * Returns the role storage object.
   *
   * @return \Drupal\user\RoleStorageInterface
   *   The role storage object.
   */
  protected function getRoleStorage() {
    return \Drupal::entityManager()->getStorage('user_role');
  }

255
}