Files
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:00:11 +02:00

107 lines
3.4 KiB
PHP
Raw Permalink 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\Enums\UserRole;
use App\Models\Tenant;
use App\Models\User;
use App\Tenancy\TenantContext;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Validation\Rules\Password;
use function Laravel\Prompts\password;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
#[Signature('users:create {email? : E-Mail-Adresse} {--name= : Name} {--role= : admin, rentals oder content} {--tenant= : Kurzname des Mandanten}')]
#[Description('Create an admin panel account (the password is entered interactively and never passed as an argument)')]
class CreateUser extends Command
{
/**
* Execute the console command.
*/
public function handle(): int
{
$tenant = $this->resolveTenant();
if ($tenant === false) {
return self::FAILURE;
}
$email = $this->argument('email') ?? text('E-Mail-Adresse', required: true, validate: ['email' => 'email']);
$email = mb_strtolower(trim($email));
if (User::where('email', $email)->exists()) {
$this->error("Es gibt bereits einen Benutzer mit {$email}.");
return self::FAILURE;
}
$name = $this->option('name') ?? text('Name', required: true);
$role = $this->option('role') !== null
? UserRole::tryFrom($this->option('role'))
: UserRole::from(select('Rolle', collect(UserRole::cases())->mapWithKeys(fn (UserRole $role): array => [$role->value => "{$role->getLabel()} – {$role->getDescription()}"])->all(), default: UserRole::Admin->value));
if ($role === null) {
$this->error('Unbekannte Rolle. Erlaubt: '.implode(', ', array_column(UserRole::cases(), 'value')));
return self::FAILURE;
}
$password = password('Passwort (mind. 12 Zeichen)', required: true, validate: ['password' => Password::min(12)]);
if (password('Passwort wiederholen', required: true) !== $password) {
$this->error('Die Passwörter stimmen nicht überein.');
return self::FAILURE;
}
$user = User::create(['name' => $name, 'email' => $email, 'role' => $role, 'password' => $password]);
if ($tenant instanceof Tenant) {
app(TenantContext::class)->set($tenant);
$user->attachCurrentRole($tenant);
}
$this->info("Benutzer {$email} ({$role->getLabel()}) angelegt.");
$this->line('Bei der ersten Anmeldung wird die Zwei-Faktor-Anmeldung mit einer Authenticator-App eingerichtet.');
return self::SUCCESS;
}
/**
* One landlord is used automatically. Several landlords need --tenant. None is allowed:
* the account exists and is attached later.
*/
private function resolveTenant(): Tenant|false|null
{
$slug = $this->option('tenant');
if (is_string($slug) && $slug !== '') {
$tenant = Tenant::query()->where('slug', $slug)->first();
if ($tenant === null) {
$this->error("Kein Mandant mit dem Kurznamen {$slug}.");
return false;
}
return $tenant;
}
$count = Tenant::query()->count();
if ($count > 1) {
$this->error('Es gibt mehrere Mandanten. Bitte --tenant=kurzname angeben.');
return false;
}
return $count === 1 ? Tenant::query()->first() : null;
}
}