You are here

abstract class AccessCheckBase in Allowed Languages 8

Same name and namespace in other branches
  1. 2.x src/Access/AccessCheckBase.php \Drupal\allowed_languages\Access\AccessCheckBase

Allowed languages access check base class.

Hierarchy

Expanded class hierarchy of AccessCheckBase

File

src/Access/AccessCheckBase.php, line 14

Namespace

Drupal\allowed_languages\Access
View source
abstract class AccessCheckBase implements AccessInterface {

  /**
   * Drupal entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  private $entityTypeManager;

  /**
   * AccessCheck constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Drupal entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Checks if the user is allowed to translate the specified language.
   *
   * @param \Drupal\user\UserInterface $user
   *   The user to check.
   * @param \Drupal\Core\Language\LanguageInterface $language
   *   The language to check for.
   *
   * @return bool
   *   If the user is allowed to or not.
   */
  protected function userIsAllowedToTranslateLanguage(UserInterface $user, LanguageInterface $language) {

    // Bypass the access check if the user has permission to translate all languages.
    if ($user
      ->hasPermission('translate all languages')) {
      return TRUE;
    }
    $allowed_languages = $this
      ->getUsersAllowedLanguages($user);
    return in_array($language
      ->getId(), $allowed_languages);
  }

  /**
   * Get the allowed languages for the specified user.
   *
   * @param \Drupal\user\UserInterface $user
   *   The user to get allowed languages for.
   *
   * @return \Drupal\language\Entity\ConfigurableLanguage[]
   *   An array of language entities.
   */
  protected function getUsersAllowedLanguages(UserInterface $user) {
    return allowed_languages_get_allowed_languages_for_user($user);
  }

  /**
   * Loads a user entity based on account proxy object.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account proxy used to load the full user entity.
   *
   * @return \Drupal\Core\Entity\EntityInterface|\Drupal\user\UserInterface|null
   *   User entity or NULL.
   */
  protected function loadUserEntityFromAccountProxy(AccountInterface $account) {
    return $this->entityTypeManager
      ->getStorage('user')
      ->load($account
      ->id());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessCheckBase::$entityTypeManager private property Drupal entity type manager. 2
AccessCheckBase::getUsersAllowedLanguages protected function Get the allowed languages for the specified user.
AccessCheckBase::loadUserEntityFromAccountProxy protected function Loads a user entity based on account proxy object.
AccessCheckBase::userIsAllowedToTranslateLanguage protected function Checks if the user is allowed to translate the specified language.
AccessCheckBase::__construct public function AccessCheck constructor. 2