You are here

class UserRestrictionsManager in User restrictions 8

Defines the user restriction manager.

Hierarchy

Expanded class hierarchy of UserRestrictionsManager

1 file declares its use of UserRestrictionsManager
user_restrictions.module in ./user_restrictions.module
Specifies rules for restricting the data users can set for their accounts.
1 string reference to 'UserRestrictionsManager'
user_restrictions.services.yml in ./user_restrictions.services.yml
user_restrictions.services.yml
1 service uses UserRestrictionsManager
user_restrictions.manager in ./user_restrictions.services.yml
Drupal\user_restrictions\UserRestrictionsManager

File

src/UserRestrictionsManager.php, line 13

Namespace

Drupal\user_restrictions
View source
class UserRestrictionsManager implements UserRestrictionsManagerInterface {
  use StringTranslationTrait;

  /**
   * List of restriction errors.
   *
   * @var string[]
   */
  protected $errors = [];

  /**
   * The entity storage interfacce.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityStorage;

  /**
   * The type manager interfacce.
   *
   * @var \Drupal\user_restrictions\UserRestrictionTypeManagerInterface
   */
  protected $typeManager;

  /**
   * The logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Constructs a UserRestrictionsManager object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity storage.
   * @param \Drupal\user_restrictions\UserRestrictionTypeManagerInterface $type_manager
   *   The user restriction type manager.
   * @param Psr\Log\LoggerInterface $logger
   *   The user_restrictions logger channel.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager, UserRestrictionTypeManagerInterface $type_manager, LoggerInterface $logger) {
    $this->entityStorage = $entity_manager
      ->getStorage('user_restrictions');
    $this->typeManager = $type_manager;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public function matchesRestrictions(array $data) {

    /** @var \Drupal\user_restrictions\Plugin\UserRestrictionTypeInterface $type */
    foreach ($this->typeManager
      ->getTypes() as $key => $type) {
      if ($type
        ->matches($data)) {
        $this
          ->setError($key, $type
          ->getErrorMessage());

        // Break after first match.
        return TRUE;
      }
    }

    // No restrictions match.
    return FALSE;
  }

  /**
   * Set error message for a specific restriction type.
   *
   * @param string $type
   *   Type of restriction, i.e. "name".
   * @param string $message
   *   Error message.
   *
   * @return \Drupal\user_restrictions\UserRestrictionsManagerInterface
   *   The service for chaining.
   */
  protected function setError($type, $message) {
    $this->errors[$type] = $message;
    return $this;
  }

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

  /**
   * {@inheritdoc}
   */
  public function deleteExpiredRules() {
    $rules = $this->entityStorage
      ->loadMultiple();

    /* @var $rule \Drupal\user_restrictions\Entity\UserRestrictions */
    foreach ($rules as $rule) {
      $expiry = $rule
        ->getExpiry();
      if ($expiry !== UserRestrictions::NO_EXPIRY && $expiry < \Drupal::time()
        ->getRequestTime()) {
        $rule
          ->delete();
        $this->logger
          ->notice('Expired rule %label has been deleted.', [
          '%label' => $rule
            ->label(),
        ]);
      }
    }
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UserRestrictionsManager::$entityStorage protected property The entity storage interfacce.
UserRestrictionsManager::$errors protected property List of restriction errors.
UserRestrictionsManager::$logger protected property The logger instance.
UserRestrictionsManager::$typeManager protected property The type manager interfacce.
UserRestrictionsManager::deleteExpiredRules public function Delete expired rules. Overrides UserRestrictionsManagerInterface::deleteExpiredRules
UserRestrictionsManager::getErrors public function Get all error messages. Overrides UserRestrictionsManagerInterface::getErrors
UserRestrictionsManager::matchesRestrictions public function Check if a the given data matches any restrictions. Overrides UserRestrictionsManagerInterface::matchesRestrictions
UserRestrictionsManager::setError protected function Set error message for a specific restriction type.
UserRestrictionsManager::__construct public function Constructs a UserRestrictionsManager object.