184 lines
5.6 KiB
PHP
184 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Api;
|
|
|
|
use App\Mail\Api\ApiTokenCreated;
|
|
use App\Mail\Api\ApiTokenRegenerated;
|
|
use App\Mail\Api\ApiTokenRemoved;
|
|
use App\Models\Api\ApiToken;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class TokenCrud extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'api:token
|
|
{action? : Can be list, add, remove, regen, default is list}
|
|
{api_token? : Only if action is remove, pass the ID to remove}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Manage api tokens';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return inteager
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
switch ($this->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;
|
|
});
|
|
}
|
|
}
|