You are here

class ProfileAccessCheck in Profile 2 8

Checks access to add, edit and delete profiles.

Hierarchy

Expanded class hierarchy of ProfileAccessCheck

1 string reference to 'ProfileAccessCheck'
profile.services.yml in ./profile.services.yml
profile.services.yml
1 service uses ProfileAccessCheck
profile.profile_access_checker in ./profile.services.yml
Drupal\profile\Access\ProfileAccessCheck

File

src/Access/ProfileAccessCheck.php, line 23
Contains \Drupal\profile\Access\ProfileAccessCheck.

Namespace

Drupal\profile\Access
View source
class ProfileAccessCheck implements AccessCheckInterface {

  /**
   * A user account to check access for.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Service RequestStack
   * @var RequestStack
   */
  protected $requestStack;

  /**
   * Constructs a EntityCreateAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack.
   */
  public function __construct(EntityManagerInterface $entity_manager, RequestStack $requestStack) {
    $this->entityManager = $entity_manager;
    $this->requestStack = $requestStack;
  }

  /**
   * Implements AccessCheckInterface::applies().
   */
  public function applies(Route $route) {
    return FALSE;
  }
  public function access(AccountInterface $account, ProfileTypeInterface $profile_type = NULL) {
    $access_control_handler = $this->entityManager
      ->getAccessControlHandler('profile');
    $operation = $this->requestStack
      ->getCurrentRequest()->attributes
      ->get('operation');
    if ($operation == 'add') {
      return $access_control_handler
        ->access($profile_type, $operation, LanguageInterface::LANGCODE_DEFAULT, $account, TRUE);
    }

    // If checking whether a profile of a particular type may be created.
    if ($profile_type) {
      return $access_control_handler
        ->createAccess($profile_type
        ->id(), $account, [], TRUE);
    }

    // If checking whether a profile of any type may be created.
    foreach ($this->entityManager
      ->getStorage('profile_type')
      ->loadMultiple() as $profile_type) {
      if (($access = $access_control_handler
        ->createAccess($profile_type
        ->id(), $account, [], TRUE)) && $access
        ->isAllowed()) {
        return $access;
      }
    }

    // No opinion.
    return AccessResult::neutral();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProfileAccessCheck::$account protected property A user account to check access for.
ProfileAccessCheck::$entityManager protected property The entity manager.
ProfileAccessCheck::$requestStack protected property Service RequestStack
ProfileAccessCheck::access public function
ProfileAccessCheck::applies public function Implements AccessCheckInterface::applies(). Overrides AccessCheckInterface::applies
ProfileAccessCheck::__construct public function Constructs a EntityCreateAccessCheck object.