eZ\Publish\Core\Repository\Repository::canUser PHP Method

canUser() public method

Deprecation: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given objects.
public canUser ( string $module, string $function, eZ\Publish\API\Repository\Values\ValueObject $object, mixed $targets = null ) : boolean
$module string The module, aka controller identifier to check permissions on
$function string The function, aka the controller action to check permissions on
$object eZ\Publish\API\Repository\Values\ValueObject The object to check if the user has access to
$targets mixed The location, parent or "assignment" value object, or an array of the same
return boolean
    public function canUser($module, $function, ValueObject $object, $targets = null)
    {
        if ($targets instanceof ValueObject) {
            $targets = array($targets);
        } elseif ($targets === null) {
            $targets = [];
        } elseif (!is_array($targets)) {
            throw new InvalidArgumentType('$targets', 'null|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject[]', $targets);
        }
        return $this->getPermissionResolver()->canUser($module, $function, $object, $targets);
    }

Usage Example

 /**
  * Removes a relation of type COMMON from a draft.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed edit this version
  * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is no relation of type COMMON for the given destination
  *
  * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion
  * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent
  */
 public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent)
 {
     $sourceVersion = $this->loadVersionInfoById($sourceVersion->contentInfo->id, $sourceVersion->versionNo);
     if ($sourceVersion->status !== APIVersionInfo::STATUS_DRAFT) {
         throw new BadStateException('$sourceVersion', 'Relations of type common can only be removed from versions of status draft');
     }
     if (!$this->repository->canUser('content', 'edit', $sourceVersion)) {
         throw new UnauthorizedException('content', 'edit', array('contentId' => $sourceVersion->contentInfo->id));
     }
     $spiRelations = $this->persistenceHandler->contentHandler()->loadRelations($sourceVersion->getContentInfo()->id, $sourceVersion->versionNo, APIRelation::COMMON);
     if (empty($spiRelations)) {
         throw new InvalidArgumentException('$sourceVersion', 'There are no relations of type COMMON for the given destination');
     }
     // there should be only one relation of type COMMON for each destination,
     // but in case there were ever more then one, we will remove them all
     // @todo: alternatively, throw BadStateException?
     $this->repository->beginTransaction();
     try {
         foreach ($spiRelations as $spiRelation) {
             if ($spiRelation->destinationContentId == $destinationContent->id) {
                 $this->persistenceHandler->contentHandler()->removeRelation($spiRelation->id, APIRelation::COMMON);
             }
         }
         $this->repository->commit();
     } catch (Exception $e) {
         $this->repository->rollback();
         throw $e;
     }
 }
All Usage Examples Of eZ\Publish\Core\Repository\Repository::canUser