mirror of
https://github.com/projectsend/projectsend.git
synced 2026-09-17 17:15:08 +00:00
28e18497b5
ProfileController::destroy() validates current_password and soft-deletes. It never asks StaffAccounts::guardLastAdministrator(), and every other door does: Staff update(), guardDeletable(), and both directions of the role conversion. This is the one door where the account being removed is certainly signed in. An installation with a single administrator therefore had a button that emptied it. Measured on main: DELETE /settings/profile 302, the account is gone live staff rows 0 (the row is trashed, not removed) anonymous GET / 302 -> /setup anonymous POST /setup a new active System Administrator EnsureSetupIsComplete asks ->exists(), which excludes trashed rows, and routes/web.php registers GET and POST setup with no auth and no guest middleware -- correctly, since a fresh installation has nobody to authenticate. SetupController::store() re-checks the same condition, so both halves agreed with each other and both were wrong once the last staff row was trashed. Two locks, because one of them is asked at five doors and the other at one. First: destroy() now asks guardLastAdministrator(), the same call with the same message as everywhere else. An administrator with a colleague still goes, a non-administrator staff member still goes, and a client still closes their own account. Second: "has this installation been set up" is not the same question as "does it have a working administrator right now", and only the first one belongs in EnsureSetupIsComplete. A trashed staff row is still evidence that setup happened, so it now counts -- in the middleware and in SetupController::setupIsComplete(), which have to agree or the result is either a redirect loop or an open form. That second lock holds even if a future door forgets the first one. Measured with the guard bypassed entirely and the row trashed directly: GET / answers with the login screen and POST /setup creates nothing. Worth stating plainly: an installation that has already lost its last administrator will now find setup shut rather than open. That is the point -- the recovery path for it is `php artisan projectsend:admin`, which is also how every unattended container installs itself, not a form that anybody on the internet can reach. Six tests, two measured red against the unfixed code (2 failed / 4 passed) -- one per lock. The other four are the boundaries: a colleague present, a staff member who is not an administrator, a client, and a genuinely fresh installation that must still reach setup. Two existing tests needed saying more clearly rather than changing: ProfileUpdateTest's deletion cases now create a second administrator, so that what they assert is self-deletion and not this new refusal; and GettingStartedTest's "fresh installation" cases forceDelete rather than delete, because a soft-deleted staff row is no longer a fresh installation -- which is the whole of the second lock. Full suite passes (2054 passed / 2 skipped), PHPStan level 8 clean.
113 lines
3.8 KiB
PHP
113 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Identity\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use App\Modules\Audit\Action;
|
|
use App\Modules\Audit\ActivityLogger;
|
|
use App\Modules\Identity\Models\Role;
|
|
use App\Modules\Identity\Permissions\SystemRole;
|
|
use App\Modules\Identity\UserType;
|
|
use App\Modules\Platform\Onboarding\InstallationWelcome;
|
|
use App\Modules\Platform\Settings\Setting;
|
|
use App\Modules\Platform\Settings\Settings;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rules\Password;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
/**
|
|
* First-run setup: create the initial staff administrator. Only reachable
|
|
* while no staff user exists; afterwards the routes bounce home.
|
|
*/
|
|
class SetupController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly Settings $settings,
|
|
private readonly ActivityLogger $activity,
|
|
private readonly InstallationWelcome $welcome,
|
|
) {}
|
|
|
|
public function show(): Response|RedirectResponse
|
|
{
|
|
if ($this->setupIsComplete()) {
|
|
return redirect()->route('home');
|
|
}
|
|
|
|
return Inertia::render('setup');
|
|
}
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
if ($this->setupIsComplete()) {
|
|
return redirect()->route('home');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'site_name' => ['required', 'string', 'max:255'],
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'email' => ['required', 'string', 'lowercase', 'email', 'max:255'],
|
|
'password' => ['required', 'confirmed', Password::defaults()],
|
|
]);
|
|
|
|
$this->settings->set(Setting::SiteName, $validated['site_name']);
|
|
|
|
$admin = User::create([
|
|
'type' => UserType::Staff,
|
|
'active' => true,
|
|
'role_id' => Role::query()->where('name', SystemRole::SystemAdministrator->value)->value('id'),
|
|
'name' => $validated['name'],
|
|
'email' => $validated['email'],
|
|
'password' => $validated['password'],
|
|
'email_verified_at' => now(),
|
|
]);
|
|
|
|
// v1 logged installation as action 0; setup is a recorded action.
|
|
$this->activity->log(Action::SetupCompleted, $admin);
|
|
$this->activity->log(Action::UserCreated, $admin, $admin);
|
|
|
|
if ($this->settings->get(Setting::AdminNotificationEmails) === []) {
|
|
$this->settings->set(Setting::AdminNotificationEmails, [$admin->email]);
|
|
}
|
|
|
|
// They will be shown around the first time they sign in — which is
|
|
// the next thing that happens, since setup deliberately does not
|
|
// log anybody in.
|
|
$this->welcome->raise();
|
|
|
|
// Deliberately no auto-login: the new administrator proves their
|
|
// credentials at the login form, which also confirms they work.
|
|
return redirect()->route('setup.success')->with('setup_completed', true);
|
|
}
|
|
|
|
public function success(Request $request): Response|RedirectResponse
|
|
{
|
|
if (! $this->setupIsComplete()) {
|
|
return redirect()->route('setup');
|
|
}
|
|
|
|
if (! $request->session()->get('setup_completed')) {
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
return Inertia::render('setup-success');
|
|
}
|
|
|
|
/**
|
|
* Trashed staff count, for the reason EnsureSetupIsComplete gives:
|
|
* this asks whether the installation was ever set up, and store()
|
|
* below is the door a stranger walks through if the answer is wrong.
|
|
* The middleware and this must agree — one of them saying "not set
|
|
* up" while the other says "set up" is either a redirect loop or an
|
|
* open form.
|
|
*/
|
|
private function setupIsComplete(): bool
|
|
{
|
|
return User::query()->withTrashed()->where('type', UserType::Staff)->exists();
|
|
}
|
|
}
|