Files
compras/scripts/repair-synthetic-profile-labels.php
T
nalaandClaude Opus 5.5 ec934455e7 Aplicar Pint e formatação de frontend; delimitar o âmbito do lint
Só formatação automática (pint, vp check --fix), sem alterar comportamento.
Excluídos da formatação os registos e artefactos com bytes fixos: docs/,
integrations/, storage/, tmp/, output/, resources/data/, tests/Fixtures/,
compras-release.json e AGENTS*.md. Scripts operacionais, integrations e
auxiliares de teste ficam fora do lint (usam new Function; limpeza na fase 2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 23:01:42 +02:00

58 lines
3.0 KiB
PHP

<?php
use App\Models\User;
use App\Services\OrganizationConfiguration;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
require dirname(__DIR__).'/vendor/autoload.php';
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if (($argv[1] ?? '') !== '--repair-owned-synthetic-labels' || config('app.demonstration') !== true
|| DB::connection()->getDatabaseName() !== 'compras_demo_20261129') {
throw new LogicException('Only generated sample labels may be repaired.');
}
$backup = '/var/backups/compras-presentation-20261129/context-alignment/profile-labels.before.private.json';
if (is_file($backup)) {
throw new LogicException('Preserve the prior label repair.');
}
$result = DB::transaction(function () use ($backup): array {
$actor = User::query()->where('email', 'admin@demo.compras.tempo.co.mz')->sole();
auth()->guard()->setUser($actor);
$profiles = DB::table('organization_profiles')->where('tenant_id', 2)->orderBy('id')->get();
file_put_contents($backup, json_encode($profiles, JSON_THROW_ON_ERROR));
chmod($backup, 0600);
$repaired = 0;
foreach ($profiles as $profile) {
if (! str_contains($profile->name, '?')) {
continue;
}
$userId = DB::table('organization_memberships')->where('tenant_id', 2)->where('profile_id', $profile->id)->orderBy('user_id')->value('user_id');
$name = User::query()->where('tenant_id', 2)->whereKey($userId)->value('name');
if (! is_string($name) || str_contains($name, '?')) {
throw new LogicException('Do not invent a replacement label.');
}
app(OrganizationConfiguration::class)->execute($actor, 'profile', [
'version' => DB::table('organization_access')->where('tenant_id', 2)->value('version'), 'id' => $profile->id,
'name' => $name, 'permissions' => json_decode($profile->permissions, true, 32, JSON_THROW_ON_ERROR),
'work_contexts' => json_decode($profile->work_contexts, true, 32, JSON_THROW_ON_ERROR),
'reason' => 'Corrigir apenas a codificacao dos rotulos sinteticos gerados, reutilizando o nome existente do titular.']);
$repaired++;
}
return ['state' => 'passed', 'generated_profile_labels_repaired' => $repaired, 'permissions_contexts_users_and_credentials_changed' => false];
});
$path = '/root/compras-demo-access.json';
$credentials = json_decode(file_get_contents($path), true, 32, JSON_THROW_ON_ERROR);
foreach ($credentials as &$credential) {
$name = User::query()->where('tenant_id', 2)->where('email', $credential['email'])->value('name');
if (is_string($name)) {
$credential['name'] = $name;
}
}
unset($credential);
file_put_contents($path, json_encode($credentials, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR).PHP_EOL);
chmod($path, 0600);
file_put_contents(dirname($backup).'/profile-labels-result.json', json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL;