public function testUserOwnership()
 {
     $metadata = new OwnershipMetadata('USER', 'usr', 'user_id');
     $this->assertTrue($metadata->hasOwner());
     $this->assertFalse($metadata->isOrganizationOwned());
     $this->assertFalse($metadata->isBusinessUnitOwned());
     $this->assertTrue($metadata->isUserOwned());
     $this->assertEquals('usr', $metadata->getOwnerFieldName());
     $this->assertEquals('user_id', $metadata->getOwnerColumnName());
 }
 public function testUserOwnership()
 {
     $metadata = new OwnershipMetadata('USER', 'usr', 'user_id');
     $this->assertEquals(OwnershipMetadata::OWNER_TYPE_USER, $metadata->getOwnerType());
     $this->assertTrue($metadata->hasOwner());
     $this->assertFalse($metadata->isGlobalLevelOwned());
     $this->assertFalse($metadata->isLocalLevelOwned());
     $this->assertTrue($metadata->isBasicLevelOwned());
     $this->assertFalse($metadata->isOrganizationOwned());
     $this->assertFalse($metadata->isBusinessUnitOwned());
     $this->assertTrue($metadata->isUserOwned());
     $this->assertEquals('usr', $metadata->getOwnerFieldName());
     $this->assertEquals('user_id', $metadata->getOwnerColumnName());
 }
 /**
  * @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());
                 $constraint = $this->getCondition($buIds, $metadata, 'id');
             } elseif ($metadata->isBusinessUnitOwned()) {
                 $buIds = $tree->getUserBusinessUnitIds($this->getUserId());
                 $constraint = $this->getCondition($buIds, $metadata);
             } elseif ($metadata->isUserOwned()) {
                 $userIds = [];
                 $this->fillBusinessUnitUserIds($this->getUserId(), $userIds);
                 $constraint = $this->getCondition($userIds, $metadata);
             }
         } elseif (AccessLevel::DEEP_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getBusinessUnitClass() === $targetEntityClassName) {
                 $buIds = [];
                 $this->fillSubordinateBusinessUnitIds($this->getUserId(), $buIds);
                 $constraint = $this->getCondition($buIds, $metadata, 'id');
             } elseif ($metadata->isBusinessUnitOwned()) {
                 $buIds = [];
                 $this->fillSubordinateBusinessUnitIds($this->getUserId(), $buIds);
                 $constraint = $this->getCondition($buIds, $metadata);
             } elseif ($metadata->isUserOwned()) {
                 $userIds = [];
                 $this->fillSubordinateBusinessUnitUserIds($this->getUserId(), $userIds);
                 $constraint = $this->getCondition($userIds, $metadata);
             }
         } elseif (AccessLevel::GLOBAL_LEVEL === $accessLevel) {
             if ($metadata->isOrganizationOwned()) {
                 $orgIds = $tree->getUserOrganizationIds($this->getUserId());
                 $constraint = $this->getCondition($orgIds, $metadata);
             } elseif ($metadata->isBusinessUnitOwned()) {
                 $buIds = [];
                 $this->fillOrganizationBusinessUnitIds($this->getUserId(), $buIds);
                 $constraint = $this->getCondition($buIds, $metadata);
             } elseif ($metadata->isUserOwned()) {
                 $userIds = [];
                 $this->fillOrganizationUserIds($this->getUserId(), $userIds);
                 $constraint = $this->getCondition($userIds, $metadata);
             }
         }
     }
     return $constraint;
 }