Add api stuff
This commit is contained in:
63
app/Console/Commands/ActivateApiKey.php
Normal file
63
app/Console/Commands/ActivateApiKey.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\ApiKey;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class ActivateApiKey extends Command
|
||||
{
|
||||
/**
|
||||
* Error messages
|
||||
*/
|
||||
const MESSAGE_ERROR_INVALID_NAME = 'Invalid name.';
|
||||
const MESSAGE_ERROR_NAME_DOES_NOT_EXIST = 'Name does not exist.';
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'apikey:activate {name}';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Activate an API key by name';
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->argument('name');
|
||||
$error = $this->validateName($name);
|
||||
if ($error) {
|
||||
$this->error($error);
|
||||
return;
|
||||
}
|
||||
$key = ApiKey::where('name', $name)->first();
|
||||
if ($key->active) {
|
||||
$this->info('Key "' . $name . '" is already active');
|
||||
return;
|
||||
}
|
||||
$key->active = 1;
|
||||
$key->save();
|
||||
$this->info('Activated key: ' . $name);
|
||||
}
|
||||
/**
|
||||
* Validate name
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
protected function validateName($name)
|
||||
{
|
||||
if (!ApiKey::isValidName($name)) {
|
||||
return self::MESSAGE_ERROR_INVALID_NAME;
|
||||
}
|
||||
if (!ApiKey::nameExists($name)) {
|
||||
return self::MESSAGE_ERROR_NAME_DOES_NOT_EXIST;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
63
app/Console/Commands/DeactivateApiKey.php
Normal file
63
app/Console/Commands/DeactivateApiKey.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\ApiKey;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class DeactivateApiKey extends Command
|
||||
{
|
||||
/**
|
||||
* Error messages
|
||||
*/
|
||||
const MESSAGE_ERROR_INVALID_NAME = 'Invalid name.';
|
||||
const MESSAGE_ERROR_NAME_DOES_NOT_EXIST = 'Name does not exist.';
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'apikey:deactivate {name}';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Deactivate an API key by name';
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->argument('name');
|
||||
$error = $this->validateName($name);
|
||||
if ($error) {
|
||||
$this->error($error);
|
||||
return;
|
||||
}
|
||||
$key = ApiKey::where('name', $name)->first();
|
||||
if (!$key->active) {
|
||||
$this->info('Key "' . $name . '" is already deactivated');
|
||||
return;
|
||||
}
|
||||
$key->active = 0;
|
||||
$key->save();
|
||||
$this->info('Deactivated key: ' . $name);
|
||||
}
|
||||
/**
|
||||
* Validate name
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
protected function validateName($name)
|
||||
{
|
||||
if (!ApiKey::isValidName($name)) {
|
||||
return self::MESSAGE_ERROR_INVALID_NAME;
|
||||
}
|
||||
if (!ApiKey::nameExists($name)) {
|
||||
return self::MESSAGE_ERROR_NAME_DOES_NOT_EXIST;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
62
app/Console/Commands/DeleteApiKey.php
Normal file
62
app/Console/Commands/DeleteApiKey.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\ApiKey;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class DeleteApiKey extends Command
|
||||
{
|
||||
/**
|
||||
* Error messages
|
||||
*/
|
||||
const MESSAGE_ERROR_INVALID_NAME = 'Invalid name.';
|
||||
const MESSAGE_ERROR_NAME_DOES_NOT_EXIST = 'Name does not exist.';
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'apikey:delete {name}';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Delete an API key by name';
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->argument('name');
|
||||
$error = $this->validateName($name);
|
||||
if ($error) {
|
||||
$this->error($error);
|
||||
return;
|
||||
}
|
||||
$confirmMessage = 'Are you sure you want to delete API key \'' . $name . '\'?';
|
||||
if (!$this->confirm($confirmMessage)) {
|
||||
return;
|
||||
}
|
||||
$key = ApiKey::where('name', $name)->first();
|
||||
$key->delete();
|
||||
$this->info('Deleted key: ' . $name);
|
||||
}
|
||||
/**
|
||||
* Validate name
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
protected function validateName($name)
|
||||
{
|
||||
if (!ApiKey::isValidName($name)) {
|
||||
return self::MESSAGE_ERROR_INVALID_NAME;
|
||||
}
|
||||
if (!ApiKey::nameExists($name)) {
|
||||
return self::MESSAGE_ERROR_NAME_DOES_NOT_EXIST;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
62
app/Console/Commands/GenerateApiKey.php
Normal file
62
app/Console/Commands/GenerateApiKey.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Models\ApiKey;
|
||||
|
||||
class GenerateApiKey extends Command
|
||||
{
|
||||
/**
|
||||
* Error messages
|
||||
*/
|
||||
const MESSAGE_ERROR_INVALID_NAME_FORMAT = 'Invalid name. Must be lowercase alpha & hyphens less than 255 char.';
|
||||
const MESSAGE_ERROR_NAME_ALREADY_USED = 'Name is unavailable.';
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'apikey:generate {name}';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generate a new API key';
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->argument('name');
|
||||
$error = $this->validateName($name);
|
||||
if ($error) {
|
||||
$this->error($error);
|
||||
return;
|
||||
}
|
||||
$apiKey = new ApiKey;
|
||||
$apiKey->name = $name;
|
||||
$apiKey->key = ApiKey::generate();
|
||||
$apiKey->save();
|
||||
$this->info('API key created');
|
||||
$this->info('Name: ' . $apiKey->name);
|
||||
$this->info('Key: ' . $apiKey->key);
|
||||
}
|
||||
/**
|
||||
* Validate name
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
protected function validateName($name)
|
||||
{
|
||||
if (!ApiKey::isValidName($name)) {
|
||||
return self::MESSAGE_ERROR_INVALID_NAME_FORMAT;
|
||||
}
|
||||
if (ApiKey::nameExists($name)) {
|
||||
return self::MESSAGE_ERROR_NAME_ALREADY_USED;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
49
app/Console/Commands/ListApiKeys.php
Normal file
49
app/Console/Commands/ListApiKeys.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Models\ApiKey;
|
||||
|
||||
class ListApiKeys extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'apikey:list {--D|deleted}';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'List all API Keys';
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$keys = $this->option('deleted')
|
||||
? ApiKey::withTrashed()->orderBy('name')->get()
|
||||
: ApiKey::orderBy('name')->get();
|
||||
if ($keys->count() === 0) {
|
||||
$this->info('There are no API keys');
|
||||
return;
|
||||
}
|
||||
$headers = ['Name', 'ID', 'Status', 'Status Date', 'Key'];
|
||||
$rows = $keys->map(function ($key) {
|
||||
$status = $key->active ? 'active' : 'deactivated';
|
||||
$status = $key->trashed() ? 'deleted' : $status;
|
||||
$statusDate = $key->deleted_at ?: $key->updated_at;
|
||||
return [
|
||||
$key->name,
|
||||
$key->id,
|
||||
$status,
|
||||
$statusDate,
|
||||
$key->key
|
||||
];
|
||||
});
|
||||
$this->table($headers, $rows);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user