Skip to content

Commit

Permalink
Merge pull request #245 from robertpustulka/component-apply-scope
Browse files Browse the repository at this point in the history
Add support for applyScope with missing identity in AuthorizationComponent
  • Loading branch information
markstory committed Aug 23, 2023
2 parents bca5822 + bd01fa3 commit 4da60be
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/Controller/Component/AuthorizationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

use Authorization\AuthorizationServiceInterface;
use Authorization\Exception\ForbiddenException;
use Authorization\Exception\MissingIdentityException;
use Authorization\IdentityInterface;
use Authorization\Policy\ResultInterface;
use Cake\Controller\Component;
Expand Down Expand Up @@ -133,7 +132,7 @@ protected function performCheck($resource, ?string $action = null, string $metho
}

$identity = $this->getIdentity($request);
if (empty($identity)) {
if ($identity === null) {
return $this->getService($request)->{$method}(null, $action, $resource);
}

Expand All @@ -158,7 +157,7 @@ public function applyScope($resource, ?string $action = null)
}
$identity = $this->getIdentity($request);
if ($identity === null) {
throw new MissingIdentityException('Identity must exist for applyScope() call.');
return $this->getService($request)->applyScope(null, $action, $resource);
}

return $identity->applyScope($action, $resource);
Expand Down
41 changes: 41 additions & 0 deletions tests/TestCase/Controller/Component/AuthorizationComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,33 @@ public function testApplyScopeImplicitAction()
$this->assertSame($query, $result);
}

public function testApplyScopeNoUser()
{
$this->request = $this->request
->withoutAttribute('identity');

$controller = new Controller($this->request);
$componentRegistry = new ComponentRegistry($controller);
$auth = new AuthorizationComponent($componentRegistry);

$articles = new ArticlesTable();
$query = $this->createMock(QueryInterface::class);
$query->method('getRepository')
->willReturn($articles);

$query->expects($this->once())
->method('where')
->with([
'visibility' => 'public',
])
->willReturn($query);

$result = $auth->applyScope($query);

$this->assertInstanceOf(QueryInterface::class, $result);
$this->assertSame($query, $result);
}

public function testApplyScopeMappedAction()
{
$articles = new ArticlesTable();
Expand Down Expand Up @@ -470,6 +497,20 @@ public function testCan()
$this->assertFalse($this->Auth->can($article, 'delete'));
}

public function testCanWithoutUser()
{
$this->request = $this->request
->withoutAttribute('identity');

$controller = new Controller($this->request);
$componentRegistry = new ComponentRegistry($controller);
$auth = new AuthorizationComponent($componentRegistry);

$article = new Article(['user_id' => 1, 'visibility' => 'public']);
$this->assertFalse($auth->can($article, 'edit'));
$this->assertTrue($auth->can($article, 'view'));
}

public function testCanWithResult()
{
$article = new Article(['user_id' => 1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/test_app/TestApp/Policy/ArticlePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function canAdd($user)
*/
public function canEdit($user, Article $article)
{
if ($user === null) {
return false;
}

if (in_array($user['role'], ['admin', 'author'])) {
return true;
}
Expand Down
8 changes: 7 additions & 1 deletion tests/test_app/TestApp/Policy/ArticlesTablePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ public function canModify(IdentityInterface $identity)
return $identity['can_edit'];
}

public function scopeEdit(IdentityInterface $user, QueryInterface $query)
public function scopeEdit(?IdentityInterface $user, QueryInterface $query)
{
if ($user === null) {
return $query->where([
'visibility' => 'public',
]);
}

return $query->where([
'user_id' => $user['id'],
]);
Expand Down

0 comments on commit 4da60be

Please sign in to comment.