testapi/app/Console/Commands/mkapiadmin.php

58 lines
1.4 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
use App\Models\ApiAdmins;
class MkApiAdmin extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mkapiadmin';
/**
* The console command description.
*
* @var string
*/
protected $description = 'List users without an ApiAdmins record and assign one to a selected user';
public function handle() {
$usersWithoutAdmin = User::doesntHave('apiAdmin')->get(['id', 'name', 'email']);
if ($usersWithoutAdmin->isEmpty()) {
$this->info("All users already have an ApiAdmins entry.");
return 0;
}
$options = $usersWithoutAdmin->map(function ($user) {
return "{$user->id}: {$user->name} ({$user->email})";
})->toArray();
$chosenString = $this->choice(
'Select a user to make an API Admin',
$options,
0, // default to the first index
null,
false
);
[$chosenId] = explode(':', $chosenString, 2);
$chosenId = (int) trim($chosenId);
ApiAdmins::create([
'user_id' => $chosenId,
]);
$this->info("User [ID: {$chosenId}] has been assigned as an API Admin.");
return 0;
}
}