argument('action')) { case null: case 'list': $this->table(['id', 'name', 'token_date'], ApiToken::all()->map(function (ApiToken $apiToken) { return [ 'id' => $apiToken->id, 'name' => $apiToken->name, 'token_date' => $apiToken->token_date, ]; })); return 0; case 'add': return $this->createApiToken(); case 'remove': return $this->removeApiToken(); case 'regen': return $this->regenApiToken(); default: $this->error(\sprintf( 'action \'%s\' is not availaible, use --help to see usage.', $this->argument('action') )); return -1; } //end switch } /** * Create api Token * * @return integer */ private function createApiToken(): int { return DB::transaction(function () { $apiToken = new ApiToken(); do { if (!($apiToken->name = $this->ask('Give your token a name'))) { $this->warn('Please provide a valid name.'); } } while (!$apiToken->name); $publicToken = $apiToken->generatePublicToken(); $privateToken = $apiToken->generatePrivateToken(); if ( !$this->confirm( "Are you should you want to create the following '{$apiToken->name}'" ) ) { $this->info('Nothing was created.'); return 0; } $apiToken->saveOrFail(); Mail::send(new ApiTokenCreated($apiToken)); $this->alert(" Your new api Token for '{$apiToken->id}' named '{$apiToken->name}' public: {$publicToken} private: {$privateToken} Please take note as it wont be available to display anymore ! "); return 0; }); } /** * Remove Api token * * @return integer */ private function removeApiToken(): int { return DB::transaction(function () { if (!\is_numeric($apiTokenId = $this->argument('api_token'))) { $this->error('remove command needs an id, use `list` to see all.'); return -1; } /** @var \App\Models\Api\ApiToken|null */ if (!($apiToken = ApiToken::where('id', $apiTokenId)->first())) { $this->error("api_token '{$apiTokenId}' was not found"); return -1; } if ( !$this->confirm( "Are you sur you want to remove apiToken '{$apiToken->id}' '{$apiToken->name}' ?" ) ) { $this->info('Nothing was deleted.'); return 0; } if (!$apiToken->deleteOrFail()) { $this->error('removing apiToken failed'); } Mail::send(new ApiTokenRemoved($apiToken)); $this->alert("apiToken '{$apiToken->id}' '{$apiToken->name}' has been removed !"); return 0; }); } /** * Regenerate Api token * * @return integer */ private function regenApiToken(): int { return DB::transaction(function () { if (!\is_numeric($apiTokenId = $this->argument('api_token'))) { $this->error('remove command needs an id, use `list` to see all.'); return -1; } /** @var \App\Models\Api\ApiToken|null */ if (!($apiToken = ApiToken::where('id', $apiTokenId)->first())) { $this->error("api_token '{$apiTokenId}' was not found"); return -1; } $publicToken = $apiToken->generatePublicToken(); $privateToken = $apiToken->generatePrivateToken(); if ( !$this->confirm( "Are you sur you want to regen apiToken '{$apiToken->id}' '{$apiToken->name}' ?" ) ) { $this->info('Nothing was done.'); return 0; } $apiToken->saveOrFail(); Mail::send(new ApiTokenRegenerated($apiToken)); $this->alert(" Your new api Token for '{$apiToken->id}' named '{$apiToken->name}' public: {$publicToken} private: {$privateToken} Please take note as it wont be available to display anymore ! "); return 0; }); } }