fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** Additive installation and separate, explicit bootstrap. No identities are inferred. */
|
||||
class PortalProfileInstallation
|
||||
@@ -33,6 +34,7 @@ class PortalProfileInstallation
|
||||
return false;
|
||||
}
|
||||
$target = config('portal_profiles.bootstrap_designated_database', '');
|
||||
|
||||
return $target !== '' && DB::selectOne('SELECT current_database() AS database')->database === $target;
|
||||
}
|
||||
|
||||
@@ -134,40 +136,47 @@ class PortalProfileInstallation
|
||||
|
||||
/** @param array<string,mixed> $input
|
||||
* @return array<string,mixed> */
|
||||
|
||||
public function bootstrap(User $actor, array $input): array
|
||||
{
|
||||
$this->authorize($actor, true, true);
|
||||
$data = Validator::make($input, ['hash' => 'required|string|size:64', 'confirm' => 'required|accepted', 'idempotency_key' => 'required|uuid',
|
||||
'first_user_id' => 'required|integer|exists:users,id', 'second_user_id' => 'required|integer|different:first_user_id|exists:users,id', 'reason' => 'required|string|min:20|max:1000'])->validate();
|
||||
abort_unless((int) $data['first_user_id'] === $actor->id, 403, 'O primeiro titular deve apresentar o seu próprio pedido; não é permitido inicializar terceiros.');
|
||||
|
||||
return $this->receipt($actor, 'bootstrap-request', $data, function () use ($actor, $data) {
|
||||
$p = $this->preview($actor);
|
||||
abort_unless(hash_equals($p['hash'], $data['hash']), 409, 'Plano alterado.');
|
||||
abort_unless($p['installed'] && $p['consistent'] && ! in_array(false, $p['dependencies'], true) && ! $p['bootstrap']['initialized'], 409, 'Schema consistente instalado e dupla não inicializada necessários.');
|
||||
$second = User::findOrFail($data['second_user_id']);
|
||||
$second = User::whereKey($data['second_user_id'])->firstOrFail();
|
||||
if ($this->designatedBootstrap($actor)) {
|
||||
abort_unless($this->designatedBootstrap($second), 403, 'Use os dois titulares expressamente designados para este destino.');
|
||||
}
|
||||
foreach ([$actor, $second] as $user) app(PortalSuperusers::class)->validateBootstrapHolder($user);
|
||||
foreach ([$actor, $second] as $user) {
|
||||
app(PortalSuperusers::class)->validateBootstrapHolder($user);
|
||||
}
|
||||
abort_if(DB::table('portal_security_approvals')->where('kind', 'bootstrap')->whereNull('consumed_at')->where('expires_at', '>', now())->exists(), 409, 'Já existe pedido pendente. Recupere-o; não duplique a cerimónia.');
|
||||
$id = (string) \Illuminate\Support\Str::uuid();
|
||||
$id = (string) Str::uuid();
|
||||
DB::table('portal_security_approvals')->insert(['id' => $id, 'kind' => 'bootstrap', 'requested_by' => $actor->id, 'subject_user_id' => $second->id,
|
||||
'scope' => json_encode(['first_user_id' => $actor->id, 'second_user_id' => $second->id, 'code_hash' => $this->fingerprint(),
|
||||
'target' => $p['target'], 'preview_hash' => $p['hash']], JSON_THROW_ON_ERROR), 'reason' => $data['reason'],
|
||||
'expires_at' => now()->addMinutes(15), 'created_at' => now(), 'updated_at' => now()]);
|
||||
|
||||
return ['initialized' => false, 'pending' => true, 'approval_id' => $id, 'confirmation_url' => '/management/profile-install/bootstrap/'.$id,
|
||||
'holders' => [$actor->id, $second->id], 'expires_in_seconds' => 900, 'flags_changed' => false, 'privileges_granted' => false];
|
||||
});
|
||||
}
|
||||
|
||||
/** Purpose-specific view; designation grants no general management access. */
|
||||
/** Purpose-specific view; designation grants no general management access.
|
||||
* @return array<array-key, mixed>
|
||||
*/
|
||||
public function bootstrapPreview(User $actor, string $id): array
|
||||
{
|
||||
abort_unless($actor->hasVerifiedEmail(), 403);
|
||||
$row = DB::table('portal_security_approvals')->where('id', $id)->where('kind', 'bootstrap')->first();
|
||||
abort_unless($row, 404);
|
||||
abort_unless(in_array($actor->id, [(int) $row->requested_by, (int) $row->subject_user_id], true), 403, 'Apenas os dois titulares designados consultam este pedido.');
|
||||
abort_unless($row !== null, 404);
|
||||
if (! (in_array($actor->id, [(int) $row->requested_by, (int) $row->subject_user_id], true))) {
|
||||
abort(403, 'Apenas os dois titulares designados consultam este pedido.');
|
||||
}
|
||||
app(PortalSuperusers::class)->validateBootstrapHolder($actor);
|
||||
$scope = json_decode($row->scope, true, 512, JSON_THROW_ON_ERROR);
|
||||
$identity = DB::selectOne('SELECT current_database() AS database,inet_server_port() AS port');
|
||||
@@ -178,9 +187,14 @@ class PortalProfileInstallation
|
||||
'can_confirm' => $actor->id === (int) $row->subject_user_id && $row->consumed_at === null && strtotime($row->expires_at) > time() && $current,
|
||||
'mfa_confirmed_in_session' => $this->access->mfa($actor)];
|
||||
$view['hash'] = hash('sha256', json_encode([$id, $row->requested_by, $row->subject_user_id, $row->scope, $row->reason, $row->expires_at], JSON_THROW_ON_ERROR));
|
||||
|
||||
return $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array-key, mixed> $input
|
||||
* @return array<array-key, mixed>
|
||||
*/
|
||||
public function confirmBootstrap(User $actor, string $id, array $input): array
|
||||
{
|
||||
$view = $this->bootstrapPreview($actor, $id);
|
||||
@@ -188,12 +202,13 @@ class PortalProfileInstallation
|
||||
abort_unless($this->access->mfa($actor), 403, 'Conclua MFA nesta sessão.');
|
||||
$data = Validator::make($input, ['hash' => 'required|string|size:64', 'confirm' => 'required|accepted', 'idempotency_key' => 'required|uuid', 'reason' => 'required|string|min:20|max:1000'])->validate();
|
||||
$data['approval_id'] = $id;
|
||||
|
||||
return $this->receipt($actor, 'bootstrap-confirm', $data, function () use ($actor, $id, $data) {
|
||||
$row = DB::table('portal_security_approvals')->where('id', $id)->lockForUpdate()->first();
|
||||
$view = $this->bootstrapPreview($actor, $id);
|
||||
abort_unless(hash_equals($view['hash'], $data['hash']), 409, 'Pedido alterado; consulte novamente.');
|
||||
abort_unless($view['can_confirm'], 409, 'Pedido expirado, já consumido ou revisão/destino alterados.');
|
||||
$first = User::findOrFail($row->requested_by);
|
||||
$first = User::whereKey($row->requested_by)->firstOrFail();
|
||||
$this->authorize($first, false, true);
|
||||
$p = $this->preview($first);
|
||||
abort_unless($p['installed'] && $p['consistent'] && ! in_array(false, $p['dependencies'], true) && ! $p['bootstrap']['initialized'], 409, 'Dependências ou dupla alteradas.');
|
||||
@@ -205,6 +220,7 @@ class PortalProfileInstallation
|
||||
} finally {
|
||||
config(['portal_profiles.bootstrap_authorized' => false]);
|
||||
}
|
||||
|
||||
return ['initialized' => true, 'approval_id' => $id, 'holders' => [$first->id, $actor->id], 'version' => 1, 'flags_changed' => false];
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user