Skip to content

Commit e451185

Browse files
committed
All console commands tested
1 parent e1ca701 commit e451185

File tree

12 files changed

+389
-23
lines changed

12 files changed

+389
-23
lines changed

src/Console/Command/ActivateApiKey.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ public function handle()
6363
$apiKeyRepository->updateActive($apiKey, true);
6464
$this->apiKeyService->getEntityManager()->flush();
6565

66-
$headers = ['name', 'key', 'status'];
67-
$rows = [[$apiKey->getName(), $apiKey->getKey(), $apiKey->getIsActive() ? 'active': 'deactivated']];
66+
$scopeNames = [];
67+
foreach ($apiKey->getScopes() as $s) {
68+
$scopeNames[] = $s->getName();
69+
}
70+
71+
$headers = ['name', 'key', 'status', 'scopes'];
72+
$rows = [[
73+
$apiKey->getName(),
74+
$apiKey->getKey(),
75+
$apiKey->getIsActive() ? 'active': 'deactivated',
76+
implode(',', $scopeNames)
77+
]];
6878

6979
$this->table($headers, $rows);
7080

src/Console/Command/AddScope.php

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,41 @@
22

33
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Console\Command;
44

5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
6+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateScopeForApiKey;
8+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ScopeHasApiKeys;
9+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
510
use Illuminate\Console\Command;
611

712
final class AddScope extends Command
813
{
14+
private ApiKeyService $apiKeyService;
15+
916
/**
1017
* The name and signature of the console command.
1118
*
1219
* @var string
1320
*/
14-
protected $signature = 'apikey:scope:add {scopeName} {apiKeyName}';
21+
protected $signature = 'apikey:scope:add {apiKeyName} {scopeName}';
1522

1623
/**
1724
* The console command description.
1825
*
1926
* @var string
2027
*/
21-
protected $description = 'Add a scope to an ApiKey';
28+
protected $description = 'Add a Scope to an ApiKey';
2229

2330
/**
2431
* Create a new command instance.
2532
*
2633
* @return void
2734
*/
28-
public function __construct()
35+
public function __construct(ApiKeyService $apiKeyService)
2936
{
3037
parent::__construct();
38+
39+
$this->apiKeyService = $apiKeyService;
3140
}
3241

3342
/**
@@ -37,6 +46,56 @@ public function __construct()
3746
*/
3847
public function handle()
3948
{
40-
// $drip->send(User::find($this->argument('user')));
49+
$apiKeyName = $this->argument('apiKeyName');
50+
$scopeName = $this->argument('scopeName');
51+
52+
$apiKeyRepository = $this->apiKeyService->getEntityManager()
53+
->getRepository(ApiKey::class);
54+
$scopeRepository = $this->apiKeyService->getEntityManager()
55+
->getRepository(Scope::class);
56+
57+
$apiKey = $apiKeyRepository->findOneBy([
58+
'name' => $apiKeyName,
59+
]);
60+
if (! $apiKey) {
61+
$this->error('Cannot find ApiKey with name: ' . $apiKeyName);
62+
63+
return 1;
64+
}
65+
66+
$scope = $scopeRepository->findOneBy([
67+
'name' => $scopeName,
68+
]);
69+
if (! $scope) {
70+
$this->error('Cannot find scope with name: ' . $scopeName);
71+
72+
return 1;
73+
}
74+
75+
try {
76+
$apiKeyRepository->addScope($apiKey, $scope);
77+
$this->apiKeyService->getEntityManager()->flush();
78+
} catch (DuplicateScopeForApiKey $e) {
79+
$this->error($e->getMessage());
80+
81+
return 1;
82+
}
83+
84+
$scopeNames = [];
85+
foreach ($apiKey->getScopes() as $s) {
86+
$scopeNames[] = $s->getName();
87+
}
88+
89+
$headers = ['name', 'key', 'status', 'scopes'];
90+
$rows = [[
91+
$apiKey->getName(),
92+
$apiKey->getKey(),
93+
$apiKey->getIsActive() ? 'active': 'deactivated',
94+
implode(',', $scopeNames)
95+
]];
96+
97+
$this->table($headers, $rows);
98+
99+
return 0;
41100
}
42101
}

src/Console/Command/DeactivateApiKey.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ public function handle()
6363
$apiKeyRepository->updateActive($apiKey, false);
6464
$this->apiKeyService->getEntityManager()->flush();
6565

66-
$headers = ['name', 'key', 'status'];
67-
$rows = [[$apiKey->getName(), $apiKey->getKey(), $apiKey->getIsActive() ? 'active': 'deactivated']];
66+
$scopeNames = [];
67+
foreach ($apiKey->getScopes() as $s) {
68+
$scopeNames[] = $s->getName();
69+
}
70+
71+
$headers = ['name', 'key', 'status', 'scopes'];
72+
$rows = [[
73+
$apiKey->getName(),
74+
$apiKey->getKey(),
75+
$apiKey->getIsActive() ? 'active': 'deactivated',
76+
implode(',', $scopeNames)
77+
]];
6878

6979
$this->table($headers, $rows);
7080

src/Console/Command/GenerateApiKey.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,18 @@ public function handle()
6464

6565
$this->apiKeyService->getEntityManager()->flush();
6666

67-
$headers = ['name', 'key', 'status'];
68-
$rows = [[$apiKey->getName(), $apiKey->getKey(), $apiKey->getIsActive() ? 'active': 'deactivated']];
67+
$scopeNames = [];
68+
foreach ($apiKey->getScopes() as $s) {
69+
$scopeNames[] = $s->getName();
70+
}
71+
72+
$headers = ['name', 'key', 'status', 'scopes'];
73+
$rows = [[
74+
$apiKey->getName(),
75+
$apiKey->getKey(),
76+
$apiKey->getIsActive() ? 'active': 'deactivated',
77+
implode(',', $scopeNames)
78+
]];
6979

7080
$this->table($headers, $rows);
7181

src/Console/Command/PrintApiKey.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,18 @@ public function handle()
6060
return 1;
6161
}
6262

63-
$headers = ['name', 'key', 'status'];
64-
$rows = [[$apiKey->getName(), $apiKey->getKey(), $apiKey->getIsActive() ? 'active': 'deactivated']];
63+
$scopeNames = [];
64+
foreach ($apiKey->getScopes() as $s) {
65+
$scopeNames[] = $s->getName();
66+
}
67+
68+
$headers = ['name', 'key', 'status', 'scopes'];
69+
$rows = [[
70+
$apiKey->getName(),
71+
$apiKey->getKey(),
72+
$apiKey->getIsActive() ? 'active': 'deactivated',
73+
implode(',', $scopeNames)
74+
]];
6575

6676
$this->table($headers, $rows);
6777

src/Console/Command/RemoveScope.php

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,41 @@
22

33
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Console\Command;
44

5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
6+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ApiKeyDoesNotHaveScope;
8+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateScopeForApiKey;
9+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
510
use Illuminate\Console\Command;
611

712
final class RemoveScope extends Command
813
{
14+
private ApiKeyService $apiKeyService;
15+
916
/**
1017
* The name and signature of the console command.
1118
*
1219
* @var string
1320
*/
14-
protected $signature = 'apikey:scope:remove {scopeName} {apiKeyName}';
21+
protected $signature = 'apikey:scope:remove {apiKeyName} {scopeName}';
1522

1623
/**
1724
* The console command description.
1825
*
1926
* @var string
2027
*/
21-
protected $description = 'Remove a scope from an ApiKey';
28+
protected $description = 'Remove a Scope from an ApiKey';
2229

2330
/**
2431
* Create a new command instance.
2532
*
2633
* @return void
2734
*/
28-
public function __construct()
35+
public function __construct(ApiKeyService $apiKeyService)
2936
{
3037
parent::__construct();
38+
39+
$this->apiKeyService = $apiKeyService;
3140
}
3241

3342
/**
@@ -37,6 +46,56 @@ public function __construct()
3746
*/
3847
public function handle()
3948
{
40-
// $drip->send(User::find($this->argument('user')));
49+
$apiKeyName = $this->argument('apiKeyName');
50+
$scopeName = $this->argument('scopeName');
51+
52+
$apiKeyRepository = $this->apiKeyService->getEntityManager()
53+
->getRepository(ApiKey::class);
54+
$scopeRepository = $this->apiKeyService->getEntityManager()
55+
->getRepository(Scope::class);
56+
57+
$apiKey = $apiKeyRepository->findOneBy([
58+
'name' => $apiKeyName,
59+
]);
60+
if (! $apiKey) {
61+
$this->error('Cannot find ApiKey with name: ' . $apiKeyName);
62+
63+
return 1;
64+
}
65+
66+
$scope = $scopeRepository->findOneBy([
67+
'name' => $scopeName,
68+
]);
69+
if (! $scope) {
70+
$this->error('Cannot find scope with name: ' . $scopeName);
71+
72+
return 1;
73+
}
74+
75+
try {
76+
$apiKeyRepository->removeScope($apiKey, $scope);
77+
$this->apiKeyService->getEntityManager()->flush();
78+
} catch (ApiKeyDoesNotHaveScope $e) {
79+
$this->error($e->getMessage());
80+
81+
return 1;
82+
}
83+
84+
$scopeNames = [];
85+
foreach ($apiKey->getScopes() as $s) {
86+
$scopeNames[] = $s->getName();
87+
}
88+
89+
$headers = ['name', 'key', 'status', 'scopes'];
90+
$rows = [[
91+
$apiKey->getName(),
92+
$apiKey->getKey(),
93+
$apiKey->getIsActive() ? 'active': 'deactivated',
94+
implode(',', $scopeNames)
95+
]];
96+
97+
$this->table($headers, $rows);
98+
99+
return 0;
41100
}
42101
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Exception;
4+
5+
use Exception;
6+
7+
class ApiKeyDoesNotHaveScope extends Exception
8+
{
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Exception;
4+
5+
use Exception;
6+
7+
class DuplicateScopeForApiKey extends Exception
8+
{
9+
10+
}

src/Repository/ApiKeyRepository.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
66
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ApiKeyDoesNotHaveScope;
78
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateName;
9+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateScopeForApiKey;
810
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\InvalidName;
911
use DateTime;
1012
use Doctrine\ORM\EntityRepository;
@@ -60,7 +62,7 @@ public function addScope(ApiKey $apiKey, Scope $scope): ApiKey
6062
// Do not add scopes twice
6163
foreach ($apiKey->getScopes() as $s) {
6264
if ($s === $scope) {
63-
return $apiKey;
65+
throw new DuplicateScopeForApiKey('ApiKey already has requested scope');
6466
}
6567
}
6668

@@ -80,11 +82,15 @@ public function removeScope(ApiKey $apiKey, Scope $scope): ApiKey
8082
}
8183
}
8284

83-
if ($found) {
84-
$apiKey->removeScope($scope);
85-
$scope->removeApiKey($apiKey);
85+
if (! $found) {
86+
throw new ApiKeyDoesNotHaveScope(
87+
'The requested Scope to remove does not exist on the ApiKey'
88+
);
8689
}
8790

91+
$apiKey->removeScope($scope);
92+
$scope->removeApiKey($apiKey);
93+
8894
return $apiKey;
8995
}
9096

0 commit comments

Comments
 (0)