You are here

class Cookie in Zircon Profile 8.0

Same name in this branch
  1. 8.0 vendor/symfony/http-foundation/Cookie.php \Symfony\Component\HttpFoundation\Cookie
  2. 8.0 vendor/symfony/browser-kit/Cookie.php \Symfony\Component\BrowserKit\Cookie
  3. 8.0 vendor/jcalderonzumba/gastonjs/src/Cookie.php \Zumba\GastonJS\Cookie
  4. 8.0 core/modules/user/src/Authentication/Provider/Cookie.php \Drupal\user\Authentication\Provider\Cookie
Same name and namespace in other branches
  1. 8 core/modules/user/src/Authentication/Provider/Cookie.php \Drupal\user\Authentication\Provider\Cookie

Cookie based authentication provider.

Hierarchy

Expanded class hierarchy of Cookie

8 string references to 'Cookie'
ClientTest::testUsesCookies in vendor/fabpot/goutte/Goutte/Tests/ClientTest.php
ClientTest::testUsesCookiesWithCustomPort in vendor/fabpot/goutte/Goutte/Tests/ClientTest.php
CookieJar::withCookieHeader in vendor/guzzlehttp/guzzle/src/Cookie/CookieJar.php
Create a request with added cookie headers.
FinishResponseSubscriber::setResponseCacheable in core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php
Add Cache-Control and Expires headers to a cacheable response.
HttpCache::__construct in vendor/symfony/http-kernel/HttpCache/HttpCache.php
Constructor.

... See full list

1 service uses Cookie
user.authentication.cookie in core/modules/user/user.services.yml
Drupal\user\Authentication\Provider\Cookie

File

core/modules/user/src/Authentication/Provider/Cookie.php, line 21
Contains \Drupal\user\Authentication\Provider\Cookie.

Namespace

Drupal\user\Authentication\Provider
View source
class Cookie implements AuthenticationProviderInterface {

  /**
   * The session configuration.
   *
   * @var \Drupal\Core\Session\SessionConfigurationInterface
   */
  protected $sessionConfiguration;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a new cookie authentication provider.
   *
   * @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration
   *   The session configuration.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(SessionConfigurationInterface $session_configuration, Connection $connection) {
    $this->sessionConfiguration = $session_configuration;
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(Request $request) {
    return $request
      ->hasSession() && $this->sessionConfiguration
      ->hasSession($request);
  }

  /**
   * {@inheritdoc}
   */
  public function authenticate(Request $request) {
    return $this
      ->getUserFromSession($request
      ->getSession());
  }

  /**
   * Returns the UserSession object for the given session.
   *
   * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
   *   The session.
   *
   * @return \Drupal\Core\Session\AccountInterface|NULL
   *   The UserSession object for the current user, or NULL if this is an
   *   anonymous session.
   */
  protected function getUserFromSession(SessionInterface $session) {
    if ($uid = $session
      ->get('uid')) {

      // @todo Load the User entity in SessionHandler so we don't need queries.
      // @see https://www.drupal.org/node/2345611
      $values = $this->connection
        ->query('SELECT * FROM {users_field_data} u WHERE u.uid = :uid AND u.default_langcode = 1', [
        ':uid' => $uid,
      ])
        ->fetchAssoc();

      // Check if the user data was found and the user is active.
      if (!empty($values) && $values['status'] == 1) {

        // Add the user's roles.
        $rids = $this->connection
          ->query('SELECT roles_target_id FROM {user__roles} WHERE entity_id = :uid', [
          ':uid' => $values['uid'],
        ])
          ->fetchCol();
        $values['roles'] = array_merge([
          AccountInterface::AUTHENTICATED_ROLE,
        ], $rids);
        return new UserSession($values);
      }
    }

    // This is an anonymous session.
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Cookie::$connection protected property The database connection.
Cookie::$sessionConfiguration protected property The session configuration.
Cookie::applies public function Checks whether suitable authentication credentials are on the request. Overrides AuthenticationProviderInterface::applies
Cookie::authenticate public function Authenticates the user. Overrides AuthenticationProviderInterface::authenticate
Cookie::getUserFromSession protected function Returns the UserSession object for the given session.
Cookie::__construct public function Constructs a new cookie authentication provider.