You are here

class MailingListManager in Mailing List 8

Mailing list manager implementation.

Hierarchy

Expanded class hierarchy of MailingListManager

1 string reference to 'MailingListManager'
mailing_list.services.yml in ./mailing_list.services.yml
mailing_list.services.yml
1 service uses MailingListManager
mailing_list.manager in ./mailing_list.services.yml
Drupal\mailing_list\MailingListManager

File

src/MailingListManager.php, line 13

Namespace

Drupal\mailing_list
View source
class MailingListManager implements MailingListManagerInterface {

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

  /**
   * The session manager.
   *
   * @var \Drupal\Core\Session\SessionManagerInterface
   */
  protected $sessionManager;

  /**
   * The user private temp store factory.
   *
   * @var \Drupal\user\PrivateTempStoreFactory
   */
  protected $userPrivateTempstore;

  /**
   * Create a new mailing list manager instance.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Session\SessionManagerInterface $session_manager
   *   The session manager.
   * @param \Drupal\user\PrivateTempStoreFactory $user_private_tempstore
   *   The user temp store.
   */
  public function __construct(AccountInterface $current_user, SessionManagerInterface $session_manager, PrivateTempStoreFactory $user_private_tempstore) {
    $this->currentUser = $current_user;
    $this->sessionManager = $session_manager;
    $this->userPrivateTempstore = $user_private_tempstore;
  }

  /**
   * {@inheritdoc}
   */
  public function grantSessionAccess(SubscriptionInterface $subscription) {

    // We need user session even for anonymous users.
    if ($this->currentUser
      ->isAnonymous() && !isset($_SESSION['session_started'])) {
      $_SESSION['session_started'] = TRUE;
      $this->sessionManager
        ->start();
    }
    if (!$this->sessionManager
      ->isStarted()) {

      // Unable to start the session, may be called from CLI or no cookies
      // allowed.
      return;
    }

    // Add subscription to the session access permissions.
    $collection = $this->userPrivateTempstore
      ->get('mailing_list');
    if (!($grants = $collection
      ->get('grants'))) {
      $grants = [];
    }
    $grants[$subscription
      ->uuid()] = REQUEST_TIME;
    $collection
      ->set('grants', $grants);
  }

  /**
   * {@inheritdoc}
   */
  public function revokeSessionAccess(SubscriptionInterface $subscription) {
    if ($this
      ->hasSessionAccess($subscription)) {
      $collection = $this->userPrivateTempstore
        ->get('mailing_list');
      $grants = $collection
        ->get('grants');
      unset($grants[$subscription
        ->uuid()]);
      $collection
        ->set('grants', $grants);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function hasSessionAccess(SubscriptionInterface $subscription) {
    return $this->sessionManager
      ->isStarted() && ($grants = $this->userPrivateTempstore
      ->get('mailing_list')
      ->get('grants')) && isset($grants[$subscription
      ->uuid()]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MailingListManager::$currentUser protected property The current user.
MailingListManager::$sessionManager protected property The session manager.
MailingListManager::$userPrivateTempstore protected property The user private temp store factory.
MailingListManager::grantSessionAccess public function Grants the current user access to a subscription for the current session. Overrides MailingListManagerInterface::grantSessionAccess
MailingListManager::hasSessionAccess public function Checks if the current user has session access to a given subscription. Overrides MailingListManagerInterface::hasSessionAccess
MailingListManager::revokeSessionAccess public function Revoke any session access to a subscription to the current user. Overrides MailingListManagerInterface::revokeSessionAccess
MailingListManager::__construct public function Create a new mailing list manager instance.