Skip to content

Commit cae2d5f

Browse files
committed
refactor(policy): encapsulate identification_documents resolution
Signed-off-by: Vitor Mattos <[email protected]>
1 parent ec0efb9 commit cae2d5f

5 files changed

Lines changed: 20 additions & 38 deletions

File tree

lib/Service/AccountService.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
use OCA\Libresign\Helper\FileUploadHelper;
2727
use OCA\Libresign\Helper\ValidateHelper;
2828
use OCA\Libresign\Service\Crl\CrlService;
29-
use OCA\Libresign\Service\Policy\PolicyService;
3029
use OCA\Libresign\Service\Policy\PolicyAuthorizationService;
31-
use OCA\Libresign\Service\Policy\Provider\IdentificationDocuments\IdentificationDocumentsPolicy;
32-
use OCA\Libresign\Service\Policy\Provider\IdentificationDocuments\IdentificationDocumentsPolicyValue;
3330
use OCA\Libresign\Service\Policy\RequestSignAuthorizationService;
3431
use OCA\Settings\Mailer\NewUserMailHelper;
3532
use OCP\Accounts\IAccountManager;
@@ -79,7 +76,7 @@ public function __construct(
7976
private Pkcs12Handler $pkcs12Handler,
8077
private IGroupManager $groupManager,
8178
private PolicyAuthorizationService $policyAuthorizationService,
82-
private PolicyService $policyService,
79+
private IdDocsPolicyService $idDocsPolicyService,
8380
private IdDocsService $idDocsService,
8481
private SignerElementsService $signerElementsService,
8582
private UserElementMapper $userElementMapper,
@@ -202,10 +199,7 @@ public function getCertificateEngineName(): string {
202199
* @return array<string, mixed>
203200
*/
204201
public function getConfig(?IUser $user = null): array {
205-
$resolvedIdentificationDocuments = $user
206-
? $this->policyService->resolveForUser(IdentificationDocumentsPolicy::KEY, $user)
207-
: $this->policyService->resolve(IdentificationDocumentsPolicy::KEY);
208-
$info['identificationDocumentsFlow'] = IdentificationDocumentsPolicyValue::normalize($resolvedIdentificationDocuments->getEffectiveValue(), false);
202+
$info['identificationDocumentsFlow'] = $this->idDocsPolicyService->isIdentificationDocumentsEnabled($user);
209203
$info['hasSignatureFile'] = $this->hasSignatureFile($user);
210204
$info['phoneNumber'] = $this->getPhoneNumber($user);
211205
$info['isApprover'] = $this->validateHelper->userCanApproveValidationDocuments($user, false);

lib/Service/IdDocsPolicyService.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public function canApproverSignIdDoc(IUser $user, int $fileId, int $status): boo
4444
}
4545
}
4646

47-
private function isIdentificationDocumentsEnabled(IUser $user): bool {
48-
$value = $this->policyService->resolveForUser(IdentificationDocumentsPolicy::KEY, $user)->getEffectiveValue();
47+
public function isIdentificationDocumentsEnabled(?IUser $user = null): bool {
48+
$resolved = $user
49+
? $this->policyService->resolveForUser(IdentificationDocumentsPolicy::KEY, $user)
50+
: $this->policyService->resolve(IdentificationDocumentsPolicy::KEY);
51+
$value = $resolved->getEffectiveValue();
4952
return IdentificationDocumentsPolicyValue::normalize($value, false);
5053
}
5154
}

lib/Settings/Admin.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
use OCA\Libresign\Service\CertificatePolicyService;
1616
use OCA\Libresign\Service\DocMdp\ConfigService as DocMdpConfigService;
1717
use OCA\Libresign\Service\FooterService;
18+
use OCA\Libresign\Service\IdDocsPolicyService;
1819
use OCA\Libresign\Service\IdentifyMethodService;
1920
use OCA\Libresign\Service\Policy\PolicyService;
20-
use OCA\Libresign\Service\Policy\Provider\IdentificationDocuments\IdentificationDocumentsPolicy;
21-
use OCA\Libresign\Service\Policy\Provider\IdentificationDocuments\IdentificationDocumentsPolicyValue;
2221
use OCA\Libresign\Service\SignatureBackgroundService;
2322
use OCA\Libresign\Service\SignatureTextService;
2423
use OCP\AppFramework\Http\ContentSecurityPolicy;
@@ -50,6 +49,7 @@ public function __construct(
5049
private FooterService $footerService,
5150
private DocMdpConfigService $docMdpConfigService,
5251
private PolicyService $policyService,
52+
private IdDocsPolicyService $idDocsPolicyService,
5353
) {
5454
}
5555
#[\Override]
@@ -106,11 +106,7 @@ public function getForm(): TemplateResponse {
106106
]);
107107
$this->initialState->provideInitialState('signing_mode', $this->getSigningModeInitialState());
108108
$this->initialState->provideInitialState('worker_type', $this->getWorkerTypeInitialState());
109-
$resolvedIdentificationDocuments = $this->policyService->resolve(IdentificationDocumentsPolicy::KEY);
110-
$this->initialState->provideInitialState(
111-
'identification_documents',
112-
IdentificationDocumentsPolicyValue::normalize($resolvedIdentificationDocuments->getEffectiveValue(), false),
113-
);
109+
$this->initialState->provideInitialState('identification_documents', $this->idDocsPolicyService->isIdentificationDocumentsEnabled());
114110
$this->initialState->provideInitialState('approval_group', $this->appConfig->getValueArray(Application::APP_ID, 'approval_group', ['admin']));
115111
$this->initialState->provideInitialState('envelope_enabled', $this->appConfig->getValueBool(Application::APP_ID, 'envelope_enabled', true));
116112
$this->initialState->provideInitialState('parallel_workers', $this->appConfig->getValueString(Application::APP_ID, 'parallel_workers', '4'));

tests/php/Unit/Service/AccountServiceTest.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
use OCA\Libresign\Service\AccountService;
2727
use OCA\Libresign\Service\Crl\CrlService;
2828
use OCA\Libresign\Service\FolderService;
29+
use OCA\Libresign\Service\IdDocsPolicyService;
2930
use OCA\Libresign\Service\IdDocsService;
3031
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
3132
use OCA\Libresign\Service\IdentifyMethodService;
32-
use OCA\Libresign\Service\Policy\Model\ResolvedPolicy;
33-
use OCA\Libresign\Service\Policy\PolicyService;
3433
use OCA\Libresign\Service\Policy\PolicyAuthorizationService;
3534
use OCA\Libresign\Service\Policy\RequestSignAuthorizationService;
3635
use OCA\Libresign\Service\RequestSignatureService;
@@ -84,7 +83,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
8483
private IGroupManager&MockObject $groupManager;
8584
private ISubAdmin&MockObject $subAdmin;
8685
private PolicyAuthorizationService $policyAuthorizationService;
87-
private PolicyService&MockObject $policyService;
86+
private IdDocsPolicyService&MockObject $idDocsPolicyService;
8887
private IdDocsService&MockObject $idDocsService;
8988
private SignerElementsService&MockObject $signerElementsService;
9089
private UserElementMapper&MockObject $userElementMapper;
@@ -125,17 +124,8 @@ public function setUp(): void {
125124
$this->groupManager = $this->createMock(IGroupManager::class);
126125
$this->subAdmin = $this->createMock(ISubAdmin::class);
127126
$this->policyAuthorizationService = new PolicyAuthorizationService($this->groupManager, $this->subAdmin);
128-
$this->policyService = $this->createMock(PolicyService::class);
129-
$this->policyService->method('resolveForUser')->willReturn(
130-
(new ResolvedPolicy())
131-
->setPolicyKey('identification_documents')
132-
->setEffectiveValue(false)
133-
);
134-
$this->policyService->method('resolve')->willReturn(
135-
(new ResolvedPolicy())
136-
->setPolicyKey('identification_documents')
137-
->setEffectiveValue(false)
138-
);
127+
$this->idDocsPolicyService = $this->createMock(IdDocsPolicyService::class);
128+
$this->idDocsPolicyService->method('isIdentificationDocumentsEnabled')->willReturn(false);
139129
$this->idDocsService = $this->createMock(IdDocsService::class);
140130
$this->signerElementsService = $this->createMock(SignerElementsService::class);
141131
$this->userElementMapper = $this->createMock(UserElementMapper::class);
@@ -171,7 +161,7 @@ private function getService(): AccountService {
171161
$this->pkcs12Handler,
172162
$this->groupManager,
173163
$this->policyAuthorizationService,
174-
$this->policyService,
164+
$this->idDocsPolicyService,
175165
$this->idDocsService,
176166
$this->signerElementsService,
177167
$this->userElementMapper,

tests/php/Unit/Settings/AdminTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use OCA\Libresign\Service\CertificatePolicyService;
1616
use OCA\Libresign\Service\DocMdp\ConfigService as DocMdpConfigService;
1717
use OCA\Libresign\Service\FooterService;
18+
use OCA\Libresign\Service\IdDocsPolicyService;
1819
use OCA\Libresign\Service\IdentifyMethodService;
19-
use OCA\Libresign\Service\Policy\Model\ResolvedPolicy;
2020
use OCA\Libresign\Service\Policy\PolicyService;
2121
use OCA\Libresign\Service\SignatureBackgroundService;
2222
use OCA\Libresign\Service\SignatureTextService;
@@ -45,6 +45,7 @@ final class AdminTest extends \OCA\Libresign\Tests\Unit\TestCase {
4545
private FooterService&MockObject $footerService;
4646
private DocMdpConfigService&MockObject $docMdpConfigService;
4747
private PolicyService&MockObject $policyService;
48+
private IdDocsPolicyService&MockObject $idDocsPolicyService;
4849
public function setUp(): void {
4950
$this->initialState = $this->createMock(IInitialState::class);
5051
$this->accountService = $this->createMock(AccountService::class);
@@ -58,6 +59,7 @@ public function setUp(): void {
5859
$this->footerService = $this->createMock(FooterService::class);
5960
$this->docMdpConfigService = $this->createMock(DocMdpConfigService::class);
6061
$this->policyService = $this->createMock(PolicyService::class);
62+
$this->idDocsPolicyService = $this->createMock(IdDocsPolicyService::class);
6163
$this->admin = new Admin(
6264
$this->initialState,
6365
$this->accountService,
@@ -71,6 +73,7 @@ public function setUp(): void {
7173
$this->footerService,
7274
$this->docMdpConfigService,
7375
$this->policyService,
76+
$this->idDocsPolicyService,
7477
);
7578
$this->stubGetFormDependencies();
7679
}
@@ -85,11 +88,7 @@ private function stubGetFormDependencies(): void {
8588
$this->identifyMethodService->method('getIdentifyMethodsSettings')->willReturn([]);
8689
$this->docMdpConfigService->method('getConfig')->willReturn([]);
8790
$this->policyService->method('resolveKnownPolicies')->willReturn([]);
88-
$this->policyService->method('resolve')->willReturn(
89-
(new ResolvedPolicy())
90-
->setPolicyKey('identification_documents')
91-
->setEffectiveValue(false)
92-
);
91+
$this->idDocsPolicyService->method('isIdentificationDocumentsEnabled')->willReturn(false);
9392

9493
$engine = $this->createMock(OpenSslHandler::class);
9594
$engine->method('getName')->willReturn('openssl');

0 commit comments

Comments
 (0)