ProfileAccessControlHandler.php in Profile 2 8
File
src/ProfileAccessControlHandler.php
View source
<?php
namespace Drupal\profile;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProfileAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
public function __construct(EntityTypeInterface $entity_type) {
parent::__construct($entity_type);
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type);
}
public function access(EntityInterface $entity, $operation, $langcode = LanguageInterface::LANGCODE_DEFAULT, AccountInterface $account = NULL, $return_as_object = FALSE) {
$account = $this
->prepareUser($account);
$user_page = \Drupal::request()->attributes
->get('user');
if ($operation == 'update') {
$operation = 'edit';
}
if ($account
->hasPermission('bypass profile access')) {
$result = AccessResult::allowed()
->cachePerRole();
return $return_as_object ? $result : $result
->isAllowed();
}
if ($operation == 'add' && ($user_page
->id() == $account
->id() && $account
->hasPermission($operation . ' own ' . $entity
->id() . ' profile') || $account
->hasPermission($operation . ' any ' . $entity
->id() . ' profile')) || $operation != 'add' && ($entity
->getOwnerId() == $account
->id() && $account
->hasPermission($operation . ' own ' . $entity
->getType() . ' profile') || $account
->hasPermission($operation . ' any ' . $entity
->getType() . ' profile'))) {
$result = AccessResult::allowed()
->cachePerRole();
return $return_as_object ? $result : $result
->isAllowed();
}
else {
$result = AccessResult::forbidden()
->cachePerRole();
return $return_as_object ? $result : $result
->isAllowed();
}
}
public function createAccess($entity_bundle = NULL, AccountInterface $account = NULL, array $context = array(), $return_as_object = FALSE) {
$account = $this
->prepareUser($account);
if ($account
->hasPermission('bypass profile access')) {
$result = AccessResult::allowed()
->cachePerRole();
return $return_as_object ? $result : $result
->isAllowed();
}
$result = AccessResult::allowed()
->cachePerRole();
return $return_as_object ? $result : $result
->isAllowed();
}
protected function checkAccess(EntityInterface $entity, $operation, $langcode, AccountInterface $account) {
return AccessResult::neutral();
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIf($account
->hasPermission('add ' . $entity_bundle . ' content'))
->cachePerRole();
}
}