Files
device-management/app/Console/Commands/CreatePlatformAdmin.php
T
Wian Drs 1e80be180e
tests / ci (push) Has been cancelled
Initial commit device-management
2026-08-25 09:42:00 +07:00

62 lines
2.0 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;
#[Signature('app:create-platform-admin {--name=} {--email=}')]
#[Description('Create the first platform-level super administrator')]
class CreatePlatformAdmin extends Command
{
public function handle(): int
{
if (config('deployment.mode') === 'self_hosted') {
$this->error('Platform Super Admin hanya dibuat pada RADIQ control plane atau managed cloud.');
return self::FAILURE;
}
$name = $this->option('name') ?: $this->ask('Full name');
$email = $this->option('email') ?: $this->ask('Email address');
$password = $this->secret('Password (minimum 12 characters)');
$confirmation = $this->secret('Confirm password');
$validator = Validator::make(
compact('name', 'email', 'password') + ['password_confirmation' => $confirmation],
[
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', 'unique:users,email'],
'password' => ['required', 'confirmed', Password::min(12)->mixedCase()->numbers()->symbols()],
],
);
if ($validator->fails()) {
foreach ($validator->errors()->all() as $error) {
$this->error($error);
}
return self::FAILURE;
}
$admin = new User;
$admin->forceFill([
'tenant_id' => null,
'name' => $name,
'email' => $email,
'email_verified_at' => now(),
'password' => $password,
'is_platform_admin' => true,
'is_active' => true,
])->save();
$this->info('Platform Master Admin created successfully.');
return self::SUCCESS;
}
}