1414use OCA \Libresign \Db \IdentifyMethodMapper ;
1515use OCA \Libresign \Db \SignRequest as SignRequestEntity ;
1616use OCA \Libresign \Db \SignRequestMapper ;
17+ use OCA \Libresign \Enum \DocMdpLevel ;
1718use OCA \Libresign \Enum \FileStatus ;
1819use OCA \Libresign \Enum \SignatureFlow ;
1920use OCA \Libresign \Events \SignRequestCanceledEvent ;
2829use OCA \Libresign \Service \IdentifyMethod \IIdentifyMethod ;
2930use OCA \Libresign \Service \Policy \Model \ResolvedPolicy ;
3031use OCA \Libresign \Service \Policy \PolicyService ;
32+ use OCA \Libresign \Service \Policy \Provider \DocMdp \DocMdpPolicy ;
3133use OCA \Libresign \Service \Policy \Provider \Signature \SignatureFlowPolicy ;
3234use OCA \Libresign \Service \SignRequest \SignRequestService ;
3335use OCP \EventDispatcher \IEventDispatcher ;
@@ -320,6 +322,7 @@ public function saveFile(array $data): FileEntity {
320322 if (!empty ($ data ['uuid ' ])) {
321323 $ file = $ this ->fileMapper ->getByUuid ($ data ['uuid ' ]);
322324 $ this ->updateSignatureFlowIfAllowed ($ file , $ data );
325+ $ this ->updateDocMdpLevelFromPolicy ($ file , $ data );
323326 if (!empty ($ data ['name ' ])) {
324327 $ file ->setName ($ data ['name ' ]);
325328 $ this ->fileService ->update ($ file );
@@ -336,6 +339,7 @@ public function saveFile(array $data): FileEntity {
336339 try {
337340 $ file = $ this ->fileMapper ->getByNodeId ($ fileId );
338341 $ this ->updateSignatureFlowIfAllowed ($ file , $ data );
342+ $ this ->updateDocMdpLevelFromPolicy ($ file , $ data );
339343 return $ this ->fileStatusService ->updateFileStatusIfUpgrade ($ file , $ data ['status ' ] ?? 0 );
340344 } catch (\Throwable ) {
341345 }
@@ -377,7 +381,7 @@ public function saveFile(array $data): FileEntity {
377381 }
378382
379383 $ this ->setSignatureFlow ($ file , $ data );
380- $ this ->setDocMdpLevelFromGlobalConfig ($ file );
384+ $ this ->setDocMdpLevelFromPolicy ($ file, $ data );
381385
382386 $ this ->fileMapper ->insert ($ file );
383387 return $ file ;
@@ -445,11 +449,41 @@ private function storePolicySnapshot(FileEntity $file, ResolvedPolicy $resolvedP
445449 $ file ->setMetadata ($ metadata );
446450 }
447451
448- private function setDocMdpLevelFromGlobalConfig (FileEntity $ file ): void {
449- if ($ this ->docMdpConfigService ->isEnabled ()) {
450- $ docmdpLevel = $ this ->docMdpConfigService ->getLevel ();
451- $ file ->setDocmdpLevelEnum ($ docmdpLevel );
452+ private function updateDocMdpLevelFromPolicy (FileEntity $ file , array $ data ): void {
453+ $ resolvedPolicy = $ this ->policyService ->resolveForUserId (
454+ DocMdpPolicy::KEY ,
455+ $ file ->getUserId (),
456+ $ this ->getDocMdpRequestOverrides ($ data ),
457+ );
458+ $ newLevel = DocMdpLevel::tryFrom ((int )$ resolvedPolicy ->getEffectiveValue ()) ?? DocMdpLevel::NOT_CERTIFIED ;
459+ $ metadataBeforeUpdate = $ file ->getMetadata () ?? [];
460+ $ this ->storePolicySnapshot ($ file , $ resolvedPolicy );
461+ $ metadataChanged = ($ file ->getMetadata () ?? []) !== $ metadataBeforeUpdate ;
462+
463+ if ($ file ->getDocmdpLevelEnum () !== $ newLevel || $ metadataChanged ) {
464+ $ file ->setDocmdpLevelEnum ($ newLevel );
465+ $ this ->fileService ->update ($ file );
466+ }
467+ }
468+
469+ private function setDocMdpLevelFromPolicy (FileEntity $ file , array $ data ): void {
470+ $ user = ($ data ['userManager ' ] ?? null ) instanceof IUser ? $ data ['userManager ' ] : null ;
471+ $ resolvedPolicy = $ this ->policyService ->resolveForUser (
472+ DocMdpPolicy::KEY ,
473+ $ user ,
474+ $ this ->getDocMdpRequestOverrides ($ data ),
475+ );
476+ $ file ->setDocmdpLevelEnum (DocMdpLevel::tryFrom ((int )$ resolvedPolicy ->getEffectiveValue ()) ?? DocMdpLevel::NOT_CERTIFIED );
477+ $ this ->storePolicySnapshot ($ file , $ resolvedPolicy );
478+ }
479+
480+ /** @return array<string, int> */
481+ private function getDocMdpRequestOverrides (array $ data ): array {
482+ if (!isset ($ data ['docmdpLevel ' ]) || $ data ['docmdpLevel ' ] === null || $ data ['docmdpLevel ' ] === '' ) {
483+ return [];
452484 }
485+
486+ return [DocMdpPolicy::KEY => (int )$ data ['docmdpLevel ' ]];
453487 }
454488
455489 private function getFileMetadata (\OCP \Files \Node $ node ): array {
0 commit comments