Skip to content

Commit 335c529

Browse files
committed
Added AdminEvent logging
1 parent 0b6a451 commit 335c529

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

src/Repository/ApiKeyRepository.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Repository;
66

7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\AdminEvent;
78
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
89
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
910
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ApiKeyDoesNotHaveScope;
@@ -27,7 +28,7 @@ public function generate(string $name): ApiKey
2728
throw new DuplicateName('An API key already exists with the name: ' . $name);
2829
}
2930

30-
if (! $this->isValidName($name)) {
31+
if (!$this->isValidName($name)) {
3132
throw new InvalidName('Please provide a valid name: [a-z0-9-]');
3233
}
3334

@@ -44,6 +45,7 @@ public function generate(string $name): ApiKey
4445
->setStatusAt(new DateTime());
4546

4647
$this->getEntityManager()->persist($apiKey);
48+
$this->getEntityManager()->persist($this->logAdminEvent($apiKey,'generate'));
4749

4850
return $apiKey;
4951
}
@@ -54,6 +56,9 @@ public function updateActive(ApiKey $apiKey, bool $status): ApiKey
5456
->setIsActive($status)
5557
->setStatusAt(new DateTime());
5658

59+
$eventName = ($status) ? 'activate': 'deactivate';
60+
$this->getEntityManager()->persist($this->logAdminEvent($apiKey, $eventName));
61+
5762
return $apiKey;
5863
}
5964

@@ -69,6 +74,8 @@ public function addScope(ApiKey $apiKey, Scope $scope): ApiKey
6974
$apiKey->addScope($scope);
7075
$scope->addApiKey($apiKey);
7176

77+
$this->getEntityManager()->persist($this->logAdminEvent($apiKey, 'add scope: ' . $scope->getName()));
78+
7279
return $apiKey;
7380
}
7481

@@ -82,7 +89,7 @@ public function removeScope(ApiKey $apiKey, Scope $scope): ApiKey
8289
}
8390
}
8491

85-
if (! $found) {
92+
if (!$found) {
8693
throw new ApiKeyDoesNotHaveScope(
8794
'The requested Scope to remove does not exist on the ApiKey'
8895
);
@@ -91,11 +98,23 @@ public function removeScope(ApiKey $apiKey, Scope $scope): ApiKey
9198
$apiKey->removeScope($scope);
9299
$scope->removeApiKey($apiKey);
93100

101+
$this->getEntityManager()->persist($this->logAdminEvent($apiKey, 'remove scope: ' . $scope->getName()));
102+
94103
return $apiKey;
95104
}
96105

97106
public function isValidName(string $name): bool
98107
{
99-
return (bool) preg_match('/^[a-z0-9-]{1,255}$/', $name);
108+
return (bool)preg_match('/^[a-z0-9-]{1,255}$/', $name);
109+
}
110+
111+
protected function logAdminEvent(ApiKey $apiKey, string $eventName)
112+
{
113+
return (new AdminEvent())
114+
->setIpAddress(request()->ip())
115+
->setApiKey($apiKey)
116+
->setEvent($eventName)
117+
->setCreatedAt(new DateTime())
118+
;
100119
}
101120
}

test/Feature/Repository/ApiKeyRepositoryTest.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiSkeletonsTest\Laravel\Doctrine\ApiKey\Feature\Repository;
44

5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\AdminEvent;
56
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
67
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
78
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ApiKeyDoesNotHaveScope;
@@ -25,6 +26,14 @@ public function testGenerate(): void
2526
$this->assertEquals('testing', $apiKey->getName());
2627
$this->assertEquals(64, strlen($apiKey->getApiKey()));
2728
$this->assertEquals(true, $apiKey->getIsActive());
29+
30+
$adminEvents = $entityManager->getRepository(AdminEvent::class)
31+
->findAll();
32+
33+
foreach ($adminEvents as $adminEvent) {
34+
$this->assertEquals('generate', $adminEvent->getEvent());
35+
$this->assertEquals($apiKey, $adminEvent->getApiKey());
36+
}
2837
}
2938

3039
public function testGenerateValidatesName(): void
@@ -50,7 +59,7 @@ public function testGenerateDoesNotCollideNames(): void
5059
$result = $repository->generate('testing');
5160
}
5261

53-
public function testSetStatus(): void
62+
public function testDeativate(): void
5463
{
5564
$entityManager = $this->createDatabase(app('em'));
5665
$repository = $entityManager->getRepository(ApiKey::class);
@@ -66,6 +75,49 @@ public function testSetStatus(): void
6675

6776
$this->assertGreaterThan($beforeSetStatus, $apiKey->getStatusAt());
6877
$this->assertEquals(false, $apiKey->getIsActive());
78+
79+
$adminEvents = $entityManager->getRepository(AdminEvent::class)
80+
->findBy([
81+
'apiKey' => $apiKey,
82+
'event' => 'deactivate',
83+
]);
84+
85+
$this->assertEquals(1, count($adminEvents));
86+
foreach ($adminEvents as $adminEvent) {
87+
$this->assertEquals('deactivate', $adminEvent->getEvent());
88+
$this->assertEquals($apiKey, $adminEvent->getApiKey());
89+
}
90+
}
91+
92+
public function testActivate(): void
93+
{
94+
$entityManager = $this->createDatabase(app('em'));
95+
$repository = $entityManager->getRepository(ApiKey::class);
96+
$apiKey = $repository->generate('testing');
97+
$apiKey->setIsActive(false);
98+
$entityManager->flush();
99+
100+
$beforeSetStatus = new DateTime();
101+
102+
$this->assertEquals(false, $apiKey->getIsActive());
103+
104+
$repository->updateActive($apiKey, true);
105+
$entityManager->flush();
106+
107+
$this->assertGreaterThan($beforeSetStatus, $apiKey->getStatusAt());
108+
$this->assertEquals(true, $apiKey->getIsActive());
109+
110+
$adminEvents = $entityManager->getRepository(AdminEvent::class)
111+
->findBy([
112+
'apiKey' => $apiKey,
113+
'event' => 'activate',
114+
]);
115+
116+
$this->assertEquals(1, count($adminEvents));
117+
foreach ($adminEvents as $adminEvent) {
118+
$this->assertEquals('activate', $adminEvent->getEvent());
119+
$this->assertEquals($apiKey, $adminEvent->getApiKey());
120+
}
69121
}
70122

71123
public function testAddScope(): void
@@ -91,6 +143,18 @@ public function testAddScope(): void
91143
}
92144

93145
$this->assertTrue($found);
146+
147+
$adminEvents = $entityManager->getRepository(AdminEvent::class)
148+
->findBy([
149+
'apiKey' => $apiKey,
150+
'event' => 'add scope: ' . $scope->getName(),
151+
]);
152+
153+
$this->assertEquals(1, count($adminEvents));
154+
foreach ($adminEvents as $adminEvent) {
155+
$this->assertEquals('add scope: ' . $scope->getName(), $adminEvent->getEvent());
156+
$this->assertEquals($apiKey, $adminEvent->getApiKey());
157+
}
94158
}
95159

96160
public function testCannotAddSameScopeTwice(): void
@@ -131,6 +195,18 @@ public function testRemoveScope(): void
131195
$entityManager->flush();
132196

133197
$this->assertEquals(0, sizeof($apiKey->getScopes()));
198+
199+
$adminEvents = $entityManager->getRepository(AdminEvent::class)
200+
->findBy([
201+
'apiKey' => $apiKey,
202+
'event' => 'remove scope: ' . $scope->getName(),
203+
]);
204+
205+
$this->assertEquals(1, count($adminEvents));
206+
foreach ($adminEvents as $adminEvent) {
207+
$this->assertEquals('remove scope: ' . $scope->getName(), $adminEvent->getEvent());
208+
$this->assertEquals($apiKey, $adminEvent->getApiKey());
209+
}
134210
}
135211

136212
public function testRemoveScopeWhichIsNotAssigned(): void

0 commit comments

Comments
 (0)