You are here

class CartLinkToken in Commerce Add To Cart Link 8

Same name and namespace in other branches
  1. 2.x src/CartLinkToken.php \Drupal\commerce_add_to_cart_link\CartLinkToken

Default cart link token service implementation.

Hierarchy

Expanded class hierarchy of CartLinkToken

1 string reference to 'CartLinkToken'
commerce_add_to_cart_link.services.yml in ./commerce_add_to_cart_link.services.yml
commerce_add_to_cart_link.services.yml
1 service uses CartLinkToken
commerce_add_to_cart_link.token in ./commerce_add_to_cart_link.services.yml
Drupal\commerce_add_to_cart_link\CartLinkToken

File

src/CartLinkToken.php, line 14

Namespace

Drupal\commerce_add_to_cart_link
View source
class CartLinkToken implements CartLinkTokenInterface {

  /**
   * The module configuration.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The CSRF token generator.
   *
   * @var \Drupal\Core\Access\CsrfTokenGenerator
   */
  protected $csrfTokenGenerator;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The session.
   *
   * @var \Symfony\Component\HttpFoundation\Session\Session
   */
  protected $session;

  /**
   * Constructs a new CartLinkToken object.
   *
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token_generator
   *   The CSRF token generator.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Symfony\Component\HttpFoundation\Session\Session $session
   *   The request stack.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(CsrfTokenGenerator $csrf_token_generator, AccountInterface $current_user, Session $session, ConfigFactoryInterface $config_factory) {
    $this->config = $config_factory
      ->get('commerce_add_to_cart_link.settings');
    $this->csrfTokenGenerator = $csrf_token_generator;
    $this->currentUser = $current_user;
    $this->session = $session;
  }

  /**
   * {@inheritdoc}
   */
  public function generate(ProductVariationInterface $variation) {
    if (!$this
      ->needsCsrfProtection($this->currentUser)) {
      return '';
    }

    // Ensure that an anonymous user has a session created, as we need to
    // generate a token, which won't work without having a session.
    if ($this->currentUser
      ->isAnonymous() && !$this->session
      ->isStarted()) {
      $this->session
        ->start();
    }
    $this->session
      ->set('forced', TRUE);
    $value = $this
      ->generateTokenValue($variation);
    return $this->csrfTokenGenerator
      ->get($value);
  }

  /**
   * {@inheritdoc}
   */
  public function validate(ProductVariationInterface $variation, $token) {
    if (!$this
      ->needsCsrfProtection($this->currentUser)) {
      return TRUE;
    }
    $value = $this
      ->generateTokenValue($variation);
    return $this->csrfTokenGenerator
      ->validate($token, $value);
  }

  /**
   * {@inheritdoc}
   */
  public function needsCsrfProtection(AccountInterface $account = NULL) {
    if (is_null($account)) {
      $account = $this->currentUser;
    }
    $csrf_protected_roles = $this->config
      ->get('csrf_token.roles');
    if (empty($csrf_protected_roles)) {
      return FALSE;
    }
    return !empty(array_intersect($csrf_protected_roles, $account
      ->getRoles()));
  }

  /**
   * Generates the value used for the token generation.
   *
   * @param \Drupal\commerce_product\Entity\ProductVariationInterface $variation
   *   The product variation.
   *
   * @return string
   *   The value used for the token generation.
   */
  protected function generateTokenValue(ProductVariationInterface $variation) {
    return sprintf('cart_link:%s:%s', $variation
      ->getProductId(), $variation
      ->id());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CartLinkToken::$config protected property The module configuration.
CartLinkToken::$csrfTokenGenerator protected property The CSRF token generator.
CartLinkToken::$currentUser protected property The current user.
CartLinkToken::$session protected property The session.
CartLinkToken::generate public function Generates a token for the given product variation. Overrides CartLinkTokenInterface::generate
CartLinkToken::generateTokenValue protected function Generates the value used for the token generation.
CartLinkToken::needsCsrfProtection public function Checks whether the given user account needs CSRF protection. Overrides CartLinkTokenInterface::needsCsrfProtection
CartLinkToken::validate public function Checks the given token for the given variation for validity. Overrides CartLinkTokenInterface::validate
CartLinkToken::__construct public function Constructs a new CartLinkToken object.