File: /home/xedaptot/ai.naniguide.com/database/seeders/Demo/Stage01_AdminsSeeder.php
<?php
namespace Database\Seeders\Demo;
use App\Model\Admin;
use App\Model\AdminGroup;
use App\Model\Language;
use App\Model\Role;
use App\Model\User;
use Illuminate\Support\Facades\DB;
/**
* Creates demo admin accounts via the same flow the admin UI uses
* (AccountProvisioningService::createAdmin → User::saveUser → Admin::saveAdmin).
*
* The first admin (key=super) is the default admin account — its
* user row will be re-used for the primary customer in Stage 03 so that
* the same login works for both /admin and /rui/* customer routes.
*/
class Stage01_AdminsSeeder extends AbstractDemoSeeder
{
protected int $fakerOffset = 1;
/** Map of admin key → ['user' => User, 'admin' => Admin]. */
public static array $created = [];
public function run(): void
{
$language = Language::getFirstLanguageByCode('en') ?? Language::first();
if (!$language) {
throw new \RuntimeException('Cannot seed demo admins: no language exists. Run DatabaseInit first.');
}
// Make sure the default admin group exists.
if (!AdminGroup::find(1)) {
throw new \RuntimeException('Cannot seed demo admins: admin_groups table empty. Run DatabaseInit first.');
}
// Use the same direct flow as DefaultAdmin.php — User::saveUser + Admin::saveAdmin.
// We deliberately bypass AccountProvisioningService::createAdmin
// because it sets $admin->create_customer_account before save(), and that
// column doesn't exist on every install (notably the demo target DB).
$defaultRoleUid = Role::getDefaultAdminRole()->uid;
foreach (DemoConfig::admins() as $cfg) {
$this->time("create admin {$cfg['email']}", function () use ($cfg, $language, $defaultRoleUid) {
DB::transaction(function () use ($cfg, $language, $defaultRoleUid) {
$user = User::newDefault();
[$result, $errors] = $user->saveUser(
$cfg['email'],
$cfg['password'],
$cfg['password'],
$cfg['first_name'],
$cfg['last_name'],
null,
$defaultRoleUid
);
if (!$result) {
throw new \RuntimeException("saveUser failed for {$cfg['email']}: " . implode(', ', $errors->all()));
}
$admin = Admin::newAdmin();
[$result, $errors] = $admin->saveAdmin(
$user,
$cfg['group_id'],
null,
$cfg['timezone'],
$language->id,
null
);
if (!$result) {
throw new \RuntimeException("saveAdmin failed for {$cfg['email']}: " . implode(', ', $errors->all()));
}
self::$created[$cfg['key']] = ['user' => $user, 'admin' => $admin];
// Copy avatar
if (!empty($cfg['avatar'])) {
self::copyAvatar($user, $cfg['avatar']);
}
});
});
}
$this->log('seeded ' . count(self::$created) . ' admin accounts');
}
/**
* Copy a demo avatar image to the user's profile image path.
*/
public static function copyAvatar(User $user, string $avatarFile): void
{
$src = DemoConfig::avatarPath($avatarFile);
if (!file_exists($src)) return;
$dest = $user->getProfileImagePath();
$dir = dirname($dest);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
copy($src, $dest);
}
}