/**
  * @param  string            $targetEntityClassName
  * @param  int               $accessLevel
  * @param  OwnershipMetadata $metadata
  *
  * @return null|array
  *
  * The cyclomatic complexity warning is suppressed by performance reasons
  * (to avoid unnecessary cloning od arrays)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function buildConstraintIfAccessIsGranted($targetEntityClassName, $accessLevel, OwnershipMetadata $metadata)
 {
     $tree = $this->treeProvider->getTree();
     $constraint = null;
     if (AccessLevel::SYSTEM_LEVEL === $accessLevel) {
         $constraint = [];
     } elseif (!$metadata->hasOwner()) {
         if (AccessLevel::GLOBAL_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getOrganizationClass() === $targetEntityClassName) {
                 $orgIds = $tree->getUserOrganizationIds($this->getUserId());
                 $constraint = $this->getCondition($orgIds, $metadata, 'id');
             } else {
                 $constraint = [];
             }
         } else {
             $constraint = [];
         }
     } else {
         if (AccessLevel::BASIC_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getUserClass() === $targetEntityClassName) {
                 $constraint = $this->getCondition($this->getUserId(), $metadata, 'id');
             } elseif ($metadata->isUserOwned()) {
                 $constraint = $this->getCondition($this->getUserId(), $metadata);
             }
         } elseif (AccessLevel::LOCAL_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getBusinessUnitClass() === $targetEntityClassName) {
                 $buIds = $tree->getUserBusinessUnitIds($this->getUserId(), $this->getOrganizationId());
                 $constraint = $this->getCondition($buIds, $metadata, 'id');
             } elseif ($metadata->isBusinessUnitOwned()) {
                 $buIds = $tree->getUserBusinessUnitIds($this->getUserId(), $this->getOrganizationId());
                 $constraint = $this->getCondition($buIds, $metadata);
             } elseif ($metadata->isUserOwned()) {
                 $userIds = [];
                 $this->fillBusinessUnitUserIds($this->getUserId(), $this->getOrganizationId(), $userIds);
                 $constraint = $this->getCondition($userIds, $metadata);
             }
         } elseif (AccessLevel::DEEP_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getBusinessUnitClass() === $targetEntityClassName) {
                 $buIds = [];
                 $this->fillSubordinateBusinessUnitIds($this->getUserId(), $this->getOrganizationId(), $buIds);
                 $constraint = $this->getCondition($buIds, $metadata, 'id');
             } elseif ($metadata->isBusinessUnitOwned()) {
                 $buIds = [];
                 $this->fillSubordinateBusinessUnitIds($this->getUserId(), $this->getOrganizationId(), $buIds);
                 $constraint = $this->getCondition($buIds, $metadata);
             } elseif ($metadata->isUserOwned()) {
                 $userIds = [];
                 $this->fillSubordinateBusinessUnitUserIds($this->getUserId(), $this->getOrganizationId(), $userIds);
                 $constraint = $this->getCondition($userIds, $metadata);
             }
         } elseif (AccessLevel::GLOBAL_LEVEL === $accessLevel) {
             if ($metadata->isOrganizationOwned()) {
                 $constraint = $this->getCondition([$this->getOrganizationId()], $metadata);
             } else {
                 $constraint = $this->getCondition(null, $metadata, null, true);
             }
         }
     }
     return $constraint;
 }
 public function testOwnerClassesConfigWithoutEntityClassResolver()
 {
     $configProvider = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProvider')->disableOriginalConstructor()->getMock();
     $provider = new OwnershipMetadataProvider(array('organization' => 'AcmeBundle\\Entity\\Organization', 'business_unit' => 'AcmeBundle\\Entity\\BusinessUnit', 'user' => 'AcmeBundle\\Entity\\User'), $configProvider);
     $this->assertEquals('AcmeBundle\\Entity\\Organization', $provider->getOrganizationClass());
     $this->assertEquals('AcmeBundle\\Entity\\BusinessUnit', $provider->getBusinessUnitClass());
     $this->assertEquals('AcmeBundle\\Entity\\User', $provider->getUserClass());
 }
 /**
  * Check if current entity is BusinessUnit
  *
  * @param string $className
  * @return bool
  */
 protected function checkIsBusinessUnitEntity($className)
 {
     $businessUnitClass = $this->ownershipMetadataProvider->getBusinessUnitClass();
     if ($className != $businessUnitClass && !is_subclass_of($className, $businessUnitClass)) {
         return false;
     }
     return true;
 }
 /**
  * @param string $ownerType
  * @return string
  * @throws \InvalidArgumentException
  */
 protected function getOwnerTargetEntityClassName($ownerType)
 {
     switch ($ownerType) {
         case OwnershipType::OWNER_TYPE_USER:
             return $this->ownershipMetadataProvider->getUserClass();
         case OwnershipType::OWNER_TYPE_BUSINESS_UNIT:
             return $this->ownershipMetadataProvider->getBusinessUnitClass();
         case OwnershipType::OWNER_TYPE_ORGANIZATION:
             return $this->ownershipMetadataProvider->getOrganizationClass();
     }
     throw new \InvalidArgumentException(sprintf('Unexpected owner type: %s.', $ownerType));
 }
 public function testOwnerClassesConfig()
 {
     $this->entityClassResolver->expects($this->exactly(3))->method('getEntityClass')->will($this->returnValueMap([['AcmeBundle:Organization', 'AcmeBundle\\Entity\\Organization'], ['AcmeBundle:BusinessUnit', 'AcmeBundle\\Entity\\BusinessUnit'], ['AcmeBundle:User', 'AcmeBundle\\Entity\\User']]));
     $provider = new OwnershipMetadataProvider(['organization' => 'AcmeBundle:Organization', 'business_unit' => 'AcmeBundle:BusinessUnit', 'user' => 'AcmeBundle:User']);
     $provider->setContainer($this->container);
     $this->assertEquals('AcmeBundle\\Entity\\Organization', $provider->getGlobalLevelClass());
     $this->assertEquals('AcmeBundle\\Entity\\Organization', $provider->getOrganizationClass());
     $this->assertEquals('AcmeBundle\\Entity\\BusinessUnit', $provider->getLocalLevelClass());
     $this->assertEquals('AcmeBundle\\Entity\\BusinessUnit', $provider->getBusinessUnitClass());
     $this->assertEquals('AcmeBundle\\Entity\\User', $provider->getBasicLevelClass());
     $this->assertEquals('AcmeBundle\\Entity\\User', $provider->getUserClass());
 }
 /**
  * Gets a string represents the type of the given owner
  *
  * @param mixed $owner
  * @return string
  */
 protected function getOwnerType($owner)
 {
     if (is_a($owner, $this->ownershipMetadata->getUserClass())) {
         return OwnershipType::OWNER_TYPE_USER;
     } elseif (is_a($owner, $this->ownershipMetadata->getBusinessUnitClass())) {
         return OwnershipType::OWNER_TYPE_BUSINESS_UNIT;
     } elseif (is_a($owner, $this->ownershipMetadata->getOrganizationClass())) {
         return OwnershipType::OWNER_TYPE_ORGANIZATION;
     } else {
         return OwnershipType::OWNER_TYPE_NONE;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function isBusinessUnit($domainObject)
 {
     return is_a($domainObject, $this->metadataProvider->getBusinessUnitClass());
 }