Files
SGF/scripts/sgf-payable-due-setup.php
T
2026-11-11 09:00:17 +02:00

7 lines
3.0 KiB
PHP

<?php
$book=$argv[1]??'regression';require __DIR__.'/sgf-single-bootstrap.php';$decision=json_decode(file_get_contents($repo.'/docs/integration/sgf-payable-due-access-proposal.json'),true);if(($decision['approved']??false)!==true||($decision['authorization']['decision']??null)!=='Autorizar o percurso QA e os dois direitos apresentados'||$decision['grants_per_book']!==[['user_id'=>13,'permission'=>'supplier_payables.due.prepare'],['user_id'=>14,'permission'=>'supplier_payables.due.review']])throw new LogicException('Exact approved access required');
if($book==='8085'&&!is_file($s['root'].'/payable-due-backup-pointer.json'))throw new LogicException('Selected baseline required first');
$sql=file_get_contents($repo.'/database/sql/payable_due.sql');$sha=hash('sha256',$sql);$native->transaction(function()use($native,$sql,$sha,$tag){if(!$native->getSchemaBuilder()->hasTable('pd_due_meta')){$native->unprepared($sql);$native->table('pd_due_meta')->insert(['policy'=>'SGF-PAYABLE-DUE-QA-v1','composition'=>$tag,'source_hash'=>$sha]);}else{$meta=$native->table('pd_due_meta')->where('policy','SGF-PAYABLE-DUE-QA-v1')->first();if(!$meta||$meta->composition!==$tag||$meta->source_hash!==$sha)throw new LogicException('Preserve different catalogue schema');}$native->statement('REVOKE ALL ON TABLE pd_due_meta,pd_due_versions,pd_due_commands FROM PUBLIC');$native->statement('REVOKE ALL ON SEQUENCE pd_due_versions_id_seq,pd_due_commands_id_seq FROM PUBLIC');$native->statement('REVOKE ALL ON FUNCTION pd_due_append_only() FROM PUBLIC');});
$count=0;$host->transaction(function()use($host,$decision,&$count){$constraint=$host->selectOne("SELECT pg_get_constraintdef(oid) value FROM pg_constraint WHERE conrelid='permission_grants'::regclass AND conname='permission_grants_scope_check'")->value;if(!str_contains($constraint,'supplier_payables.due.prepare')){if(!preg_match('/^CHECK \((.*)\)$/s',$constraint,$m))throw new LogicException('Scope constraint changed');$host->statement('ALTER TABLE permission_grants DROP CONSTRAINT permission_grants_scope_check');$host->statement("ALTER TABLE permission_grants ADD CONSTRAINT permission_grants_scope_check CHECK ((".$m[1].") OR (unit_id IS NOT NULL AND permission IN ('supplier_payables.due.prepare','supplier_payables.due.review')))");}foreach($decision['grants_per_book']as$g){$values=['tenant_id'=>1,'company_id'=>2,'unit_id'=>2]+$g;if($host->table('permission_grants')->where($values)->exists())continue;$host->table('permission_grants')->insert($values);App\Modules\Identity\Services\Audit::record('access.granted_on_creation',null,1,2,2,$g['user_id'],['permission'=>$g['permission'],'reason'=>$decision['authorization'],'limits'=>'Somente condições administrativas QA revistas; sem valores ou pagamentos.']);$count++;}});
file_put_contents($work.'/payable-due-setup.json',json_encode(['passed'=>true,'book'=>$book,'new_grants'=>$count,'new_pg_grants'=>0,'new_users'=>0,'new_demo'=>false,'schema_sha256'=>$sha],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'new_grants'=>$count,'proof'=>$work.'/payable-due-setup.json']).PHP_EOL;