You are here

ProfileAccessControlHandler.php in Profile 2 8

Namespace

Drupal\profile

File

src/ProfileAccessControlHandler.php
View source
<?php

/**
 * @file
 * Contains \Drupal\profile\ProfileAccessControlHandler.
 */
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;

/**
 * Defines the access control handler for the profile entity type.
 *
 * @see \Drupal\profile\Entity\Profile
 */
class ProfileAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {

  /**
   * Constructs a NodeAccessControlHandler object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   */
  public function __construct(EntityTypeInterface $entity_type) {
    parent::__construct($entity_type);
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type);
  }

  /**
   * {@inheritdoc}
   */
  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');

    // Some times, operation edit is called update.
    // Use edit in any case.
    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();
    }
  }

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, $langcode, AccountInterface $account) {

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

  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
    return AccessResult::allowedIf($account
      ->hasPermission('add ' . $entity_bundle . ' content'))
      ->cachePerRole();
  }

}

Classes

Namesort descending Description
ProfileAccessControlHandler Defines the access control handler for the profile entity type.