You are here

function cookies_user_login in COOKiES Consent Management 1.0.x

Implements hook_user_login().

If COOKiES cookie exists save/update content for user in key-value storage. If not, check key-value storage, if cookie content is stored and re-install cookie if exists.

File

./cookies.module, line 135
Contains cookies.module.

Code

function cookies_user_login(UserInterface $account) {
  $config = \Drupal::config('cookies.config');
  if ($config
    ->get('store_auth_user_consent')) {

    /** @var string $cookie_name */
    $cookie_name = $config
      ->get('cookie_name');
    $store = \Drupal::keyValue('cookies.consent.user');
    $key = "uid:{$account->id()}";
    if ($cookie_content = \Drupal::request()->cookies
      ->get($cookie_name)) {

      // User has set or update cookie before logged in.
      // => Update cookie consent in db.
      $store
        ->set($key, json_decode($cookie_content, true));
    }
    elseif ($cookie_content = $store
      ->get($key)) {

      // User gets COOKiES-cookie from storage.
      $expires = $config
        ->get('cookie_expires') ?? 365;
      $options = [
        "expires" => \Drupal::time()
          ->getRequestTime() + $expires * 24 * 60 * 60,
        "path" => "/",
        "domain" => $config
          ->get('cookie_domain') ?? "",
        "secure" => $config
          ->get('cookie_secure') ?? false,
        "samesite" => $config
          ->get('cookie_same_site') ?? "Lax",
      ];
      setcookie($cookie_name, json_encode($cookie_content), $options);
    }
  }
}