Skip to content

Commit 9795063

Browse files
authored
Merge pull request #12 from TomHAnderson/feature/has-scope
Added hasScope() to ApiKey entity
2 parents 8f5e164 + 9267fe9 commit 9795063

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Entity/ApiKey.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ public function __construct()
6262
$this->scopes = new \Doctrine\Common\Collections\ArrayCollection();
6363
}
6464

65+
/**
66+
* A convenience function to check if a scope is assigned to the apikey
67+
* by name
68+
*/
69+
public function hasScope(string $scopeName): bool
70+
{
71+
foreach ($this->getScopes() as $scope) {
72+
if ($scope->getName() === $scopeName) {
73+
return true;
74+
}
75+
}
76+
77+
return false;
78+
}
79+
6580
/**
6681
* Set name.
6782
*

test/Feature/Entity/ApiKeyTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ApiSkeletonsTest\Laravel\Doctrine\ApiKey\Feature\Entity;
44

55
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
6+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
67
use ApiSkeletons\Laravel\Doctrine\ApiKey\Http\Middleware\AuthorizeApiKey;
78
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
89
use ApiSkeletonsTest\Laravel\Doctrine\ApiKey\TestCase;
@@ -60,4 +61,21 @@ public function testAccessEvent(): void
6061

6162
$this->assertEquals(0, count($apiKey->getAccessEvents()));
6263
}
64+
65+
public function testHasScope(): void
66+
{
67+
$entityManager = $this->createDatabase(app('em'));
68+
$apiKeyRepository = $entityManager->getRepository(ApiKey::class);
69+
$scopeRepository = $entityManager->getRepository(Scope::class);
70+
71+
$apiKey = $apiKeyRepository->generate('testing');
72+
$scope = $scopeRepository->generate('scopetest');
73+
$entityManager->flush();
74+
75+
$apiKeyRepository->addScope($apiKey, $scope);
76+
$entityManager->flush();
77+
78+
$this->assertTrue($apiKey->hasScope('scopetest'));
79+
$this->assertFalse($apiKey->hasScope('fail'));
80+
}
6381
}

0 commit comments

Comments
 (0)