commit compras1 3011
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-30 09:33:36 +02:00
parent 96b30368bc
commit 47649bd46c
163 changed files with 9419 additions and 145 deletions
+82
View File
@@ -0,0 +1,82 @@
<?php
use App\Models\Tenant;
use App\Models\User;
use App\Services\OrganizationConfiguration;
use App\Services\WorkContext;
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] ?? '') !== '--align-authorized-synthetic' || config('app.demonstration') !== true
|| DB::connection()->getDatabaseName() !== 'compras_demo_20261129') {
throw new LogicException('Only the authorised public sample company may be aligned.');
}
$backup = '/var/backups/compras-presentation-20261129/context-alignment';
if (! is_file($backup.'/selected.env.private') || ! is_file($backup.'/database.dump')) {
throw new LogicException('Private recovery files required before changing configuration.');
}
$result = DB::transaction(function () use ($backup): array {
DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
$actor = User::query()->where('email', 'admin@demo.compras.tempo.co.mz')->sole();
if ($actor->tenant_id !== 2) {
throw new LogicException('Unexpected company.');
}
auth()->guard()->setUser($actor);
$company = Tenant::query()->whereKey(2)->lockForUpdate()->firstOrFail();
if ($company->work_context_policy !== null) {
throw new LogicException('Preserve an already configured company.');
}
$options = array_keys(WorkContext::OPTIONS);
if (app(WorkContext::class)->purchased($actor) !== $options) {
throw new LogicException('The selected installation must expose the same four pilot contexts.');
}
$profiles = DB::table('organization_profiles')->where('tenant_id', 2)->orderBy('id')->get();
$excluded = ['tenants', 'organization_profiles', 'organization_access', 'audit_logs',
'sessions', 'cache', 'cache_locks', 'jobs', 'failed_jobs', 'job_batches'];
$tables = collect(DB::select('select tablename from pg_tables where schemaname=current_schema()'))->pluck('tablename')->diff($excluded)->sort()->values();
$digest = static function () use ($tables): array {
$values = [];
foreach ($tables as $table) {
$rows = DB::table($table)->get()->map(fn ($row) => json_encode($row, JSON_THROW_ON_ERROR))->sort()->values()->all();
$values[$table] = hash('sha256', json_encode($rows, JSON_THROW_ON_ERROR));
}
return $values;
};
$before = $digest();
file_put_contents($backup.'/contexts.before.private.json', json_encode(['company' => $company->toArray(), 'profiles' => $profiles->all(), 'protected_table_hashes' => $before], JSON_THROW_ON_ERROR));
chmod($backup.'/contexts.before.private.json', 0600);
$service = app(OrganizationConfiguration::class);
foreach ($profiles as $profile) {
$service->execute($actor, 'profile', ['version' => DB::table('organization_access')->where('tenant_id', 2)->value('version'),
'id' => $profile->id, 'name' => $profile->name, 'permissions' => json_decode($profile->permissions, true, 32, JSON_THROW_ON_ERROR),
'work_contexts' => $options, 'reason' => 'Reposicao expressamente pedida do painel do piloto na empresa sintetica, sem alterar competencias.']);
}
$service->execute($actor, 'work_contexts', ['version' => DB::table('organization_access')->where('tenant_id', 2)->value('version'),
'enabled' => $options, 'confirm' => true, 'reason' => 'Aplicar o mesmo painel de contextos do piloto, por pedido expresso do proprietario.']);
foreach ($profiles as $profile) {
$after = DB::table('organization_profiles')->where('id', $profile->id)->first();
if ($after->name !== $profile->name || json_decode($after->permissions, true) !== json_decode($profile->permissions, true)
|| json_decode($after->work_contexts, true) !== $options) {
throw new LogicException('Profile competencies must remain unchanged.');
}
}
if ($digest() !== $before) {
throw new LogicException('An unrelated table changed; roll back the entire alignment.');
}
$available = app(WorkContext::class)->available($actor->fresh());
if ($available !== $options) {
throw new LogicException('Administration must see the same four pilot choices.');
}
return ['state' => 'passed', 'company_id' => 2, 'context_options' => $options, 'administration_available_contexts' => $available,
'profiles_aligned' => $profiles->count(), 'profile_permissions_and_names_preserved' => true,
'unrelated_tables_preserved' => count($before), 'native_configuration_commands' => true,
'operational_flags_preserved' => array_map(fn ($v) => $v['operational'], WorkContext::OPTIONS),
'original_pilot_or_previous_public_database_changed' => false];
});
file_put_contents($backup.'/result.json', json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL;