Files
hado/core/app/Console/Commands/AgentTokenCommand.php

37 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Console\Commands;
use App\Ingest\EventUpserter;
use App\Models\AgentToken;
use Illuminate\Console\Command;
class AgentTokenCommand extends Command
{
protected $signature = 'hado:agent:token {login : Логин пользователя, как в X-Remote-User} {--revoke : Отозвать токен, ничего не печатать}';
protected $description = 'Выпускает токен агента (MCP) для пользователя и печатает его один раз; повторный вызов перевыпускает';
public function handle(EventUpserter $users): int
{
$user = $users->ensureUser((string) $this->argument('login'));
$had = AgentToken::where('user_id', $user->id)->exists();
if ($this->option('revoke')) {
AgentToken::where('user_id', $user->id)->delete();
$this->info($had ? "Токен агента для «{$user->login}» отозван." : "У «{$user->login}» не было токена агента.");
return self::SUCCESS;
}
$token = AgentToken::issue($user);
$this->info($had
? "Токен агента для «{$user->login}» перевыпущен, старый больше не работает."
: "Токен агента для «{$user->login}» создан.");
$this->line('Показывается один раз, в базе только хэш.');
$this->line("Token: {$token}");
return self::SUCCESS;
}
}