Skip to content

Commit 5c89494

Browse files
committed
fixes #11 Non cascadable authorizations were still cascaded
1 parent 8844ae9 commit 5c89494

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

src/Repository/AuthorizationRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function findCascadableAuthorizationsForResource(ResourceInterface $resou
151151
$qb->where('a.cascadable = true');
152152

153153
// Root authorizations means no parent
154-
$qb->where('a.parentAuthorization IS NULL');
154+
$qb->andWhere('a.parentAuthorization IS NULL');
155155

156156
if ($resource instanceof EntityResource) {
157157
$qb->andWhere('a.entityClass = :entityClass');

tests/Unit/Repository/AuthorizationRepositoryTest.php

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use MyCLabs\ACL\Doctrine\ACLSetup;
1111
use MyCLabs\ACL\Model\Actions;
1212
use MyCLabs\ACL\Model\Authorization;
13+
use MyCLabs\ACL\Model\ClassResource;
1314
use MyCLabs\ACL\Repository\AuthorizationRepository;
1415
use Tests\MyCLabs\ACL\Unit\Repository\Model\File;
1516
use Tests\MyCLabs\ACL\Unit\Repository\Model\FileOwnerRole;
@@ -68,7 +69,6 @@ public function testInsertBulk()
6869
$this->em->persist($resource);
6970
$role = new FileOwnerRole($user, $resource);
7071
$this->em->persist($role);
71-
7272
$this->em->flush();
7373

7474
$authorizations = [
@@ -96,4 +96,102 @@ public function testInsertBulk()
9696
$this->assertEquals(0, count($authorization->getChildAuthorizations()));
9797
$this->assertTrue($authorization->isCascadable());
9898
}
99+
100+
/**
101+
* @depends testInsertBulk
102+
*/
103+
public function testFindCascadableAuthorizations()
104+
{
105+
$user = new User();
106+
$this->em->persist($user);
107+
$resource = new File();
108+
$this->em->persist($resource);
109+
$role = new FileOwnerRole($user, $resource);
110+
$this->em->persist($role);
111+
$this->em->flush();
112+
113+
$classResource = new ClassResource('\Tests\MyCLabs\ACL\Unit\Repository\Model\File');
114+
115+
$authorizations = [
116+
// VIEW cascades (entity resource)
117+
Authorization::create($role, new Actions([ Actions::VIEW ]), $resource, true),
118+
// EDIT doesn't cascade (entity resource)
119+
Authorization::create($role, new Actions([ Actions::EDIT ]), $resource, false),
120+
121+
// VIEW cascades (class resource)
122+
Authorization::create($role, new Actions([ Actions::VIEW ]), $classResource, true),
123+
// EDIT doesn't cascade (class resource)
124+
Authorization::create($role, new Actions([ Actions::EDIT ]), $classResource, false),
125+
];
126+
127+
/** @var AuthorizationRepository $repository */
128+
$repository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization');
129+
130+
$repository->insertBulk($authorizations);
131+
132+
// Test for entity resource
133+
$result = $repository->findCascadableAuthorizationsForResource($resource);
134+
$this->assertCount(1, $result);
135+
$this->assertTrue($result[0]->getActions()->view);
136+
$this->assertFalse($result[0]->getActions()->edit);
137+
138+
// Test for class resource
139+
$result = $repository->findCascadableAuthorizationsForResource($classResource);
140+
$this->assertCount(1, $result);
141+
$this->assertTrue($result[0]->getActions()->view);
142+
$this->assertFalse($result[0]->getActions()->edit);
143+
}
144+
145+
/**
146+
* @depends testInsertBulk
147+
*/
148+
public function testIsAllowedOnEntity()
149+
{
150+
$user = new User();
151+
$this->em->persist($user);
152+
$resource = new File();
153+
$this->em->persist($resource);
154+
$role = new FileOwnerRole($user, $resource);
155+
$this->em->persist($role);
156+
$this->em->flush();
157+
158+
$authorizations = [
159+
Authorization::create($role, new Actions([ Actions::VIEW ]), $resource),
160+
];
161+
162+
/** @var AuthorizationRepository $repository */
163+
$repository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization');
164+
$repository->insertBulk($authorizations);
165+
166+
$this->assertTrue($repository->isAllowedOnEntity($user, Actions::VIEW, $resource));
167+
$this->assertFalse($repository->isAllowedOnEntity($user, Actions::EDIT, $resource));
168+
}
169+
170+
/**
171+
* @depends testInsertBulk
172+
*/
173+
public function testIsAllowedOnEntityClass()
174+
{
175+
$user = new User();
176+
$this->em->persist($user);
177+
$resource = new File();
178+
$this->em->persist($resource);
179+
$role = new FileOwnerRole($user, $resource);
180+
$this->em->persist($role);
181+
$this->em->flush();
182+
183+
$class = 'Tests\MyCLabs\ACL\Unit\Repository\Model\File';
184+
$classResource = new ClassResource($class);
185+
186+
$authorizations = [
187+
Authorization::create($role, new Actions([ Actions::VIEW ]), $classResource),
188+
];
189+
190+
/** @var AuthorizationRepository $repository */
191+
$repository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization');
192+
$repository->insertBulk($authorizations);
193+
194+
$this->assertTrue($repository->isAllowedOnEntityClass($user, Actions::VIEW, $class));
195+
$this->assertFalse($repository->isAllowedOnEntityClass($user, Actions::EDIT, $class));
196+
}
99197
}

0 commit comments

Comments
 (0)