ProfileAccessControlHandler.php in Profile 8
File
src/ProfileAccessControlHandler.php
View source
<?php
namespace Drupal\profile;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity\UncacheableEntityAccessControlHandler;
use Drupal\profile\Entity\ProfileType;
class ProfileAccessControlHandler extends UncacheableEntityAccessControlHandler {
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
$result = parent::checkCreateAccess($account, $context, $entity_bundle);
if ($result
->isAllowed() && !empty($context['profile_owner'])) {
$result = $result
->andIf($this
->checkRoleAccess($context['profile_owner'], $entity_bundle));
}
return $result;
}
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$result = parent::checkAccess($entity, $operation, $account);
if ($result
->isAllowed()) {
$result = $result
->andIf($this
->checkRoleAccess($entity
->getOwner(), $entity
->bundle()));
}
return $result;
}
protected function checkRoleAccess(AccountInterface $account, $profile_type_id) {
$profile_type = ProfileType::load($profile_type_id);
$profile_type_roles = array_filter($profile_type
->getRoles());
$role_check = !$profile_type_roles || array_intersect($account
->getRoles(), $profile_type_roles);
return AccessResult::allowedIf($role_check)
->addCacheableDependency($profile_type);
}
}