82 lines
2.0 KiB
PHP
82 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Http\Services\AuthService;
|
|
use App\Models\AuthenticationToken;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
|
|
class AuthenticationCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'auth:escalate';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Escalate user to a management session';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$authTokens = AuthenticationToken::select('id', 'token', 'authenticated', 'valid_until')->whereDate('valid_until', '>', Carbon::now())->get();
|
|
|
|
$this->table(['id', 'token', 'authenticated', 'valid_until'], $authTokens);
|
|
|
|
$response = $this->ask('Token ID to escalate:');
|
|
|
|
if (strlen($response) === 0) {
|
|
$this->info('Nothing entered, exiting.');
|
|
return 0;
|
|
}
|
|
|
|
if (!is_numeric($response)) {
|
|
$this->info('Invalid input entered, exiting.');
|
|
return 1;
|
|
}
|
|
|
|
$id = (int)$response;
|
|
$token = AuthenticationToken::find($id);
|
|
|
|
if ($token === null) {
|
|
$this->info('Invalid token ID given, exiting.');
|
|
return 1;
|
|
}
|
|
|
|
$targetRole = $this->choice('Should this token be a manager or normal user?', ['user', 'manager']);
|
|
$authState = ['manager' => true, 'user' => false][$targetRole];
|
|
|
|
$token->authenticated = $authState;
|
|
$token->save();
|
|
|
|
if ($authState) {
|
|
$this->info('Token ' . $id . ' escalated to manager.');
|
|
} else {
|
|
$this->info('Token ' . $id . ' deescalated to user.');
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|