You are here

class PhotosAccessControlHandler in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 src/PhotosAccessControlHandler.php \Drupal\photos\PhotosAccessControlHandler

Defines an access control handler for photos_image items.

Hierarchy

Expanded class hierarchy of PhotosAccessControlHandler

File

src/PhotosAccessControlHandler.php, line 17

Namespace

Drupal\photos
View source
class PhotosAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager'));
  }

  /**
   * Constructs the photos access control handler instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager service.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_manager) {
    parent::__construct($entity_type);
    $this->entityTypeManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
    if ($account
      ->hasPermission('administer nodes')) {
      return AccessResult::allowed()
        ->cachePerPermissions();
    }
    $is_owner = $account
      ->id() && $account
      ->id() === $entity
      ->getOwnerId();
    switch ($operation) {
      case 'view':
        $accessResult = AccessResult::neutral()
          ->cachePerPermissions();

        // Value is fid, check if user can view this photo's album.
        if (\Drupal::config('photos.settings')
          ->get('photos_access_photos')) {

          // Check if album password is required.
          photos_access_request_album_password();
          $node = _photos_access_pass_type($entity
            ->id(), 1);
          $nid = NULL;
          $uid = FALSE;
          $viewId = 0;

          // Check if user is node author.
          if (isset($node['node'])) {
            $uid = $node['node']->uid;
            $viewId = $node['node']->viewid;
            $nid = $node['node']->nid;
          }
          elseif (isset($node['view'])) {
            $uid = $node['view']->uid;
            $viewId = $node['view']->viewid;
            $nid = $node['view']->nid;
          }
          elseif (isset($node['update'])) {
            $uid = $node['update']->uid;
            $viewId = $node['update']->viewid;
            $nid = $node['update']->nid;
          }
          if ($uid && $account
            ->id() == $uid) {

            // Node owner is allowed access.
            return AccessResult::allowed()
              ->cachePerPermissions();
          }
          if ($account
            ->hasPermission('view photo')) {
            if ($viewId && $viewId < 3) {

              // Check node access.
              $node = $this->entityTypeManager
                ->getStorage('node')
                ->load($nid);
              $accessResult = AccessResult::allowedIf($node
                ->access('view'))
                ->cachePerPermissions()
                ->addCacheableDependency($entity);
            }
            elseif (isset($node['node']) && $node['node']->viewid == 4) {

              // @todo move logic.
              // Check role access.
              $accountRoles = $account
                ->getRoles();
              $node = $this->entityTypeManager
                ->getStorage('node')
                ->load($node['node']->nid);
              if ($node && isset($node->photos_privacy) && isset($node->photos_privacy['roles'])) {
                if (count(array_intersect($accountRoles, $node->photos_privacy['roles'])) !== 0) {
                  $accessResult = AccessResult::allowedIf($account
                    ->hasPermission('view photo'))
                    ->cachePerPermissions()
                    ->addCacheableDependency($entity);
                }
              }
            }
            elseif (isset($node['view']) && $node['view']->viewid == 3 && isset($node['view']->pass)) {

              // Check password.
              $correctPassword = FALSE;
              if (isset($_SESSION[$node['view']->nid . '_' . session_id()]) && $node['view']->pass == $_SESSION[$node['view']->nid . '_' . session_id()] || !photos_access_pass_validate($node)) {
                $correctPassword = TRUE;
              }
              $accessResult = AccessResult::allowedIf($correctPassword)
                ->cachePerPermissions()
                ->addCacheableDependency($entity);
            }
            else {
              $accessResult = AccessResult::allowedIf($account
                ->hasPermission('view photo'))
                ->cachePerPermissions()
                ->addCacheableDependency($entity);
            }
          }
        }
        else {
          $accessResult = AccessResult::allowedIfHasPermission($account, 'view photo')
            ->cachePerPermissions()
            ->addCacheableDependency($entity);
        }

        // @todo check if $entity->isPublished().
        return $accessResult;
      case 'update':
        if ($account
          ->hasPermission('edit own photo') && $is_owner) {
          return AccessResult::allowed()
            ->cachePerPermissions()
            ->cachePerUser()
            ->addCacheableDependency($entity);
        }
        if ($account
          ->hasPermission('edit any photo')) {
          return AccessResult::allowed()
            ->cachePerPermissions();
        }
        return AccessResult::neutral("The following permissions are required: 'edit any photo' OR 'edit own photos'.")
          ->cachePerPermissions();
      case 'delete':
        if ($account
          ->hasPermission('delete any photo')) {
          return AccessResult::allowed()
            ->cachePerPermissions();
        }
        if ($account
          ->hasPermission('delete own photo') && $is_owner) {
          return AccessResult::allowed()
            ->cachePerPermissions()
            ->cachePerUser()
            ->addCacheableDependency($entity);
        }
        return AccessResult::neutral("The following permissions are required: 'delete any photo' OR 'delete own photos'.")
          ->cachePerPermissions();
      default:
        return AccessResult::neutral()
          ->cachePerPermissions();
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
    $permissions = [
      'administer nodes',
      'create photo',
    ];
    return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityAccessControlHandler::$accessCache protected property Stores calculated access check results.
EntityAccessControlHandler::$entityType protected property Information about the entity type.
EntityAccessControlHandler::$entityTypeId protected property The entity type ID of the access control handler instance.
EntityAccessControlHandler::$viewLabelOperation protected property Allows to grant access to just the labels. 5
EntityAccessControlHandler::access public function Checks access to an operation on a given entity or entity translation. Overrides EntityAccessControlHandlerInterface::access 1
EntityAccessControlHandler::checkFieldAccess protected function Default field access as determined by this access control handler. 4
EntityAccessControlHandler::createAccess public function Checks access to create an entity. Overrides EntityAccessControlHandlerInterface::createAccess 1
EntityAccessControlHandler::fieldAccess public function Checks access to an operation on a given entity field. Overrides EntityAccessControlHandlerInterface::fieldAccess
EntityAccessControlHandler::getCache protected function Tries to retrieve a previously cached access value from the static cache.
EntityAccessControlHandler::prepareUser protected function Loads the current account object, if it does not exist yet.
EntityAccessControlHandler::processAccessHookResults protected function We grant access to the entity if both of these conditions are met:
EntityAccessControlHandler::resetCache public function Clears all cached access checks. Overrides EntityAccessControlHandlerInterface::resetCache
EntityAccessControlHandler::setCache protected function Statically caches whether the given user has access.
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 5
EntityHandlerBase::moduleHandler protected function Gets the module handler. 5
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
PhotosAccessControlHandler::$entityTypeManager protected property The entity manager.
PhotosAccessControlHandler::checkAccess protected function Performs access checks. Overrides EntityAccessControlHandler::checkAccess
PhotosAccessControlHandler::checkCreateAccess protected function Performs create access checks. Overrides EntityAccessControlHandler::checkCreateAccess
PhotosAccessControlHandler::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
PhotosAccessControlHandler::__construct public function Constructs the photos access control handler instance. Overrides EntityAccessControlHandler::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.