ProfileAccessCheck.php in Profile 2 8
File
src/Access/ProfileAccessCheck.php
View source
<?php
namespace Drupal\profile\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessCheckInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
use Drupal\profile\ProfileTypeInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ProfileAccessCheck implements AccessCheckInterface {
protected $account;
protected $entityManager;
protected $requestStack;
public function __construct(EntityManagerInterface $entity_manager, RequestStack $requestStack) {
$this->entityManager = $entity_manager;
$this->requestStack = $requestStack;
}
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 ($profile_type) {
return $access_control_handler
->createAccess($profile_type
->id(), $account, [], TRUE);
}
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;
}
}
return AccessResult::neutral();
}
}