-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from myclabs/RebuildResouceAuthorizations
Rebuild resouce authorizations
- Loading branch information
Showing
5 changed files
with
233 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace MyCLabs\ACL\Repository; | ||
|
||
use Doctrine\Common\Util\ClassUtils; | ||
use Doctrine\ORM\EntityRepository; | ||
use MyCLabs\ACL\Model\Role; | ||
use MyCLabs\ACL\Model\ClassResource; | ||
use MyCLabs\ACL\Model\EntityResource; | ||
use MyCLabs\ACL\Model\ResourceInterface; | ||
|
||
/** | ||
* Authorizations repository. | ||
* | ||
* @author Valentin Claras <[email protected]> | ||
*/ | ||
class RoleRepository extends EntityRepository | ||
{ | ||
/** | ||
* Returns Roles that are directly linked to the given resource. | ||
* | ||
* @param ResourceInterface $resource | ||
* @return Role[] | ||
*/ | ||
public function findRolesDirectlyLinkedToResource(ResourceInterface $resource) | ||
{ | ||
$qb = $this->createQueryBuilder('role'); | ||
|
||
// Join | ||
$qb->join('role.authorizations', 'a'); | ||
|
||
// Root authorizations means they are attached to the given resource | ||
$qb->andWhere('a.parentAuthorization IS NULL'); | ||
|
||
if ($resource instanceof EntityResource) { | ||
$qb->andWhere('a.entityClass = :entityClass'); | ||
$qb->andWhere('a.entityId = :entityId'); | ||
$qb->setParameter('entityClass', ClassUtils::getClass($resource)); | ||
$qb->setParameter('entityId', $resource->getId()); | ||
} | ||
if ($resource instanceof ClassResource) { | ||
$qb->andWhere('a.entityClass = :entityClass'); | ||
$qb->andWhere('a.entityId IS NULL'); | ||
$qb->setParameter('entityClass', $resource->getClass()); | ||
} | ||
|
||
return $qb->getQuery()->getResult(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Tests\MyCLabs\ACL\Unit\Repository; | ||
|
||
use Doctrine\Common\Cache\ArrayCache; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Tools\SchemaTool; | ||
use Doctrine\ORM\Tools\Setup; | ||
use MyCLabs\ACL\ACL; | ||
use MyCLabs\ACL\Doctrine\ACLSetup; | ||
use MyCLabs\ACL\Model\Actions; | ||
use MyCLabs\ACL\Model\Authorization; | ||
use MyCLabs\ACL\Model\ClassResource; | ||
use MyCLabs\ACL\Repository\AuthorizationRepository; | ||
use MyCLabs\ACL\Repository\RoleRepository; | ||
use Tests\MyCLabs\ACL\Unit\Repository\Model\File; | ||
use Tests\MyCLabs\ACL\Unit\Repository\Model\FileOwnerRole; | ||
use Tests\MyCLabs\ACL\Unit\Repository\Model\User; | ||
|
||
/** | ||
* @covers \MyCLabs\ACL\Repository\RoleRepository | ||
*/ | ||
class RoleRepositoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var EntityManager | ||
*/ | ||
private $em; | ||
|
||
/** | ||
* @var ACL | ||
*/ | ||
private $acl; | ||
|
||
public function setUp() | ||
{ | ||
$paths = [ | ||
__DIR__ . '/../../../src/Model', | ||
__DIR__ . '/Model', | ||
]; | ||
$dbParams = [ | ||
'driver' => 'pdo_sqlite', | ||
'memory' => true, | ||
]; | ||
|
||
$setup = new ACLSetup(); | ||
$setup->setSecurityIdentityClass('Tests\MyCLabs\ACL\Unit\Repository\Model\User'); | ||
$setup->registerRoleClass('Tests\MyCLabs\ACL\Unit\Repository\Model\FileOwnerRole', 'fileOwner'); | ||
|
||
// Create the entity manager | ||
$config = Setup::createAnnotationMetadataConfiguration($paths, true, null, new ArrayCache(), false); | ||
$this->em = EntityManager::create($dbParams, $config); | ||
|
||
$this->acl = new ACL($this->em); | ||
|
||
$setup->setUpEntityManager($this->em, function () { | ||
return $this->acl; | ||
}); | ||
|
||
// Create the DB | ||
$tool = new SchemaTool($this->em); | ||
$tool->createSchema($this->em->getMetadataFactory()->getAllMetadata()); | ||
} | ||
|
||
public function testFindRolesDirectlyLinkedToResource() | ||
{ | ||
$user = new User(); | ||
$this->em->persist($user); | ||
$resource = new File(); | ||
$this->em->persist($resource); | ||
$directRole = new FileOwnerRole($user, $resource); | ||
$this->em->persist($directRole); | ||
$parentRole = new FileOwnerRole($user, $resource); | ||
$this->em->persist($parentRole); | ||
$this->em->flush(); | ||
|
||
$classResource = new ClassResource('\Tests\MyCLabs\ACL\Unit\Repository\Model\File'); | ||
|
||
|
||
$parentView = Authorization::create($parentRole, new Actions([ Actions::VIEW ]), $classResource, true); | ||
|
||
$authorizations = [ | ||
Authorization::create($directRole, new Actions([ Actions::EDIT ]), $resource, true), | ||
Authorization::create($directRole, new Actions([ Actions::DELETE ]), $resource, true), | ||
$parentView, | ||
$parentView->createChildAuthorization($resource) | ||
]; | ||
|
||
/** @var AuthorizationRepository $authorizationRepository */ | ||
$authorizationRepository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization'); | ||
|
||
$authorizationRepository->insertBulk($authorizations); | ||
|
||
// Check user can VIEW and EDIT the Resource | ||
$this->assertTrue($authorizationRepository->isAllowedOnEntity($user, Actions::VIEW, $resource)); | ||
$this->assertTrue($authorizationRepository->isAllowedOnEntity($user, Actions::EDIT, $resource)); | ||
$this->assertTrue($authorizationRepository->isAllowedOnEntity($user, Actions::DELETE, $resource)); | ||
|
||
// Check user can only VIEW the ClassResource | ||
$this->assertTrue($authorizationRepository->isAllowedOnEntityClass($user, Actions::VIEW, $classResource->getClass())); | ||
$this->assertFalse($authorizationRepository->isAllowedOnEntityClass($user, Actions::EDIT, $classResource->getClass())); | ||
$this->assertFalse($authorizationRepository->isAllowedOnEntityClass($user, Actions::DELETE, $classResource->getClass())); | ||
|
||
/** @var RoleRepository $roleRepository */ | ||
$roleRepository = $this->em->getRepository('MyCLabs\ACL\Model\Role'); | ||
|
||
// Test for entity resource | ||
$result = $roleRepository->findRolesDirectlyLinkedToResource($resource); | ||
$this->assertCount(1, $result); | ||
$this->assertSame($directRole, $result[0]); | ||
|
||
// Test for class resource | ||
$result = $roleRepository->findRolesDirectlyLinkedToResource($classResource); | ||
$this->assertCount(1, $result); | ||
$this->assertSame($parentRole, $result[0]); | ||
} | ||
} |