|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Console\LnmsCommand; |
| 6 | +use App\Models\User; |
| 7 | +use Symfony\Component\Console\Input\InputArgument; |
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | + |
| 10 | +class ApiTokenCommand extends LnmsCommand |
| 11 | +{ |
| 12 | + protected $name = 'api:token'; |
| 13 | + protected $description = 'Manage API tokens for users'; |
| 14 | + |
| 15 | + public function __construct() |
| 16 | + { |
| 17 | + parent::__construct(); |
| 18 | + |
| 19 | + $this->addArgument('action', InputArgument::REQUIRED, 'Action to perform: create, list, revoke'); |
| 20 | + $this->addArgument('username', InputArgument::REQUIRED, 'Username to manage tokens for'); |
| 21 | + $this->addOption('token-name', null, InputOption::VALUE_REQUIRED, 'Name for the token', 'api-token'); |
| 22 | + $this->addOption('token-id', null, InputOption::VALUE_REQUIRED, 'Token ID to revoke (for revoke action)'); |
| 23 | + } |
| 24 | + |
| 25 | + public function handle(): int |
| 26 | + { |
| 27 | + $action = $this->argument('action'); |
| 28 | + $username = $this->argument('username'); |
| 29 | + |
| 30 | + $user = User::where('username', $username)->first(); |
| 31 | + |
| 32 | + if (! $user) { |
| 33 | + $this->error("User '{$username}' not found."); |
| 34 | + |
| 35 | + return 1; |
| 36 | + } |
| 37 | + |
| 38 | + return match ($action) { |
| 39 | + 'create' => $this->createToken($user), |
| 40 | + 'list' => $this->listTokens($user), |
| 41 | + 'revoke' => $this->revokeToken($user), |
| 42 | + default => $this->invalidAction($action), |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + private function createToken(User $user): int |
| 47 | + { |
| 48 | + $name = $this->option('token-name'); |
| 49 | + $token = $user->createToken($name); |
| 50 | + |
| 51 | + $this->info('Token created successfully.'); |
| 52 | + $this->newLine(); |
| 53 | + $this->line($token->plainTextToken); |
| 54 | + $this->newLine(); |
| 55 | + $this->warn('Save this token — it will not be shown again.'); |
| 56 | + |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + private function listTokens(User $user): int |
| 61 | + { |
| 62 | + $tokens = $user->tokens; |
| 63 | + |
| 64 | + if ($tokens->isEmpty()) { |
| 65 | + $this->info("No tokens found for user '{$user->username}'."); |
| 66 | + |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + $this->table( |
| 71 | + ['ID', 'Name', 'Last Used', 'Created'], |
| 72 | + $tokens->map(fn ($token) => [ |
| 73 | + $token->id, |
| 74 | + $token->name, |
| 75 | + $token->last_used_at?->diffForHumans() ?? 'Never', |
| 76 | + $token->created_at->diffForHumans(), |
| 77 | + ]) |
| 78 | + ); |
| 79 | + |
| 80 | + return 0; |
| 81 | + } |
| 82 | + |
| 83 | + private function revokeToken(User $user): int |
| 84 | + { |
| 85 | + $tokenId = $this->option('token-id'); |
| 86 | + |
| 87 | + if (! $tokenId) { |
| 88 | + $this->error('The --token-id option is required for the revoke action.'); |
| 89 | + |
| 90 | + return 1; |
| 91 | + } |
| 92 | + |
| 93 | + $token = $user->tokens()->where('id', $tokenId)->first(); |
| 94 | + |
| 95 | + if (! $token) { |
| 96 | + $this->error("Token ID {$tokenId} not found for user '{$user->username}'."); |
| 97 | + |
| 98 | + return 1; |
| 99 | + } |
| 100 | + |
| 101 | + $token->delete(); |
| 102 | + $this->info("Token '{$token->name}' (ID: {$tokenId}) revoked."); |
| 103 | + |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + private function invalidAction(string $action): int |
| 108 | + { |
| 109 | + $this->error("Unknown action '{$action}'. Use: create, list, revoke"); |
| 110 | + |
| 111 | + return 1; |
| 112 | + } |
| 113 | +} |
0 commit comments