['user_id'=>$i,'permission'=>'remunerations.view'],[13,14,15,16,17]);$expected[]=['user_id'=>13,'permission'=>'remunerations.prepare'];$expected[]=['user_id'=>14,'permission'=>'remunerations.review'];if(($p['status']??null)==='superseded_by_autonomous_hr_directive'||($p['approved']??false)!==true||($p['authorization']['decision']??null)!=='Autorizar o bloco QA e os três direitos apresentados'||$p['grants_per_book']!==$expected)throw new LogicException('Exact approved powers required');if($book==='8085'&&!is_file($s['root'].'/remunerations-backup-pointer.json'))throw new LogicException('Private baseline required'); $count=0;$sql=file_get_contents($repo.'/database/sql/remunerations.sql');$sha=hash('sha256',$sql);$host->transaction(function()use($host,$p,$sql,$sha,$tag,&$count){if(!$host->getSchemaBuilder()->hasTable('rm_meta')){$host->unprepared($sql);$host->table('rm_meta')->insert(['policy'=>'SGF-REMUNERATIONS-QA-v1','composition'=>$tag,'source_hash'=>$sha]);}else{$m=$host->table('rm_meta')->where('policy','SGF-REMUNERATIONS-QA-v1')->first();if(!$m||$m->composition!==$tag||$m->source_hash!==$sha)throw new LogicException('Preserve different schema');}$host->statement('REVOKE ALL ON TABLE rm_meta,rm_versions,rm_commands FROM PUBLIC');$host->statement('REVOKE ALL ON SEQUENCE rm_versions_id_seq,rm_commands_id_seq FROM PUBLIC');$host->statement('REVOKE ALL ON FUNCTION rm_append_only() FROM PUBLIC');$old=$host->selectOne("SELECT pg_get_constraintdef(oid) d FROM pg_constraint WHERE conrelid='permission_grants'::regclass AND conname='permission_grants_scope_check'")->d;if(!str_contains($old,'remunerations.view')){if(!preg_match('/^CHECK \((.*)\)$/s',$old,$m))throw new LogicException('Unknown scope check');$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('remunerations.view','remunerations.prepare','remunerations.review')))");}foreach($p['grants_per_book']as$g){$g=$p['scope']+$g;if($host->table('permission_grants')->where($g)->exists())continue;$host->table('permission_grants')->insert($g);App\Modules\Identity\Services\Audit::record('access.granted_on_creation',null,1,2,2,$g['user_id'],['permission'=>$g['permission'],'reason'=>$p['authorization'],'limits'=>'Remunerações artificiais QA; sem salário legal, dívida, GL ou pagamento.']);$count++;}});file_put_contents($work.'/remunerations-setup.json',json_encode(['passed'=>true,'book'=>$book,'new_grants'=>$count,'new_right_names'=>3,'new_pg_grants'=>0,'new_users'=>0,'new_demo'=>false,'schema_sha256'=>$sha],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'book'=>$book,'new_grants'=>$count,'proof'=>$work.'/remunerations-setup.json']).PHP_EOL;