Skip to content

Commit

Permalink
Merge 760738f into 02832f4
Browse files Browse the repository at this point in the history
  • Loading branch information
backportbot-libresign[bot] committed Apr 17, 2023
2 parents 02832f4 + 760738f commit 434fa7b
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 18 deletions.
5 changes: 3 additions & 2 deletions lib/Helper/ValidateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use OCA\Libresign\Db\File;
use OCA\Libresign\Db\FileElement;
use OCA\Libresign\Db\FileElementMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileTypeMapper;
use OCA\Libresign\Db\FileUser;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Db\UserElementMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Service\FileService;
use OCA\Libresign\Service\IdentifyMethodService;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\IConfig;
Expand Down Expand Up @@ -485,7 +486,7 @@ public function haveValidMail(array $data, ?int $type = null): void {
}

public function validateIdentifyMethod(string $identify): void {
if (!in_array($identify, ['nextcloud', 'email-link', 'email-token', 'sms-token'])) {
if (!in_array($identify, IdentifyMethodService::IDENTIFY_METHODS)) {
// TRANSLATORS When is requested to a person to sign a file, is
// necessary identify what is the identification method. The
// identification method is used to define how will be the sign
Expand Down
35 changes: 35 additions & 0 deletions lib/Service/IdentifyMethodService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* @copyright Copyright (c) 2023 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace OCA\Libresign\Service;

class IdentifyMethodService {
public const IDENTIFTY_NEXTCLOUD = 'nextcloud';
public const IDENTIFY_TOKEN = 'token';
public const IDENTIFY_METHODS = [
self::IDENTIFTY_NEXTCLOUD,
self::IDENTIFY_TOKEN,
];
}
2 changes: 1 addition & 1 deletion lib/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace OCA\Libresign\Service;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\File;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUser;
use OCA\Libresign\Exception\LibresignException;
use OCP\IConfig;
Expand Down
12 changes: 8 additions & 4 deletions lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use OCA\Libresign\Db\UserElementMapper;
use OCA\Libresign\Event\SignedEvent;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\Pkcs7Handler;
use OCA\Libresign\Handler\Pkcs12Handler;
use OCA\Libresign\Handler\Pkcs7Handler;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Helper\ValidateHelper;
use OCP\Accounts\IAccountManager;
Expand Down Expand Up @@ -841,7 +841,7 @@ public function getInfoOfFileToSignUsingFileUserUuid(?string $uuid, ?IUser $user
$this->trhowIfCantIdentifyUser($uuid, $user, $fileUser);
$this->throwIfUserIsNotSigner($user, $fileUser);
$this->throwIfAlreadySigned($fileEntity, $fileUser);
$this->throwIfInvalidUser($uuid, $user);
$this->throwIfInvalidUser($uuid, $fileUser, $user);
$userFolder = $this->root->getUserFolder($fileEntity->getUserId());
$fileToSign = $userFolder->getById($fileEntity->getNodeId());
if (count($fileToSign) < 1) {
Expand Down Expand Up @@ -895,7 +895,10 @@ public function getInfoOfFileToSignUsingFileUuid(?string $uuid, ?IUser $user, st
return $return;
}

private function throwIfInvalidUser(string $uuid, ?IUser $user): void {
private function throwIfInvalidUser(string $uuid, FileUserEntity $fileUser, ?IUser $user): void {
if ($fileUser->getIdentifyMethod() !== IdentifyMethodService::IDENTIFTY_NEXTCLOUD) {
return;
}
if (!$user) {
throw new LibresignException(json_encode([
'action' => JSActions::ACTION_REDIRECT,
Expand Down Expand Up @@ -963,7 +966,7 @@ private function throwIfUserIsNotSigner(?IUser $user, FileUserEntity $fileUser):
}
}

private function getFileData(FileEntity $fileData, IUser $user, ?FileUserEntity $fileUser = null): array {
private function getFileData(FileEntity $fileData, ?IUser $user, ?FileUserEntity $fileUser = null): array {
$return['action'] = JSActions::ACTION_SIGN;
$return['sign'] = [
'uuid' => $fileData->getUuid(),
Expand All @@ -972,6 +975,7 @@ private function getFileData(FileEntity $fileData, IUser $user, ?FileUserEntity
if ($fileUser) {
$return['user']['name'] = $fileUser->getDisplayName();
$return['sign']['description'] = $fileUser->getDescription();
$return['settings']['identify'] = $fileUser->getIdentifyMethod();
} else {
$return['user']['name'] = $user->getDisplayName();
}
Expand Down
3 changes: 0 additions & 3 deletions tests/Unit/Helper/ValidateHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,6 @@ public function dataValidateIdentifyMethod(): array {
['', true],
['invalid', true],
['nextcloud', false],
['email-link', false],
['email-token', false],
['sms-token', false],
];
}
}
37 changes: 33 additions & 4 deletions tests/Unit/Service/SignFileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,12 +759,14 @@ public function providerTestGetConfigWithInvalidUuid() {
->withConsecutive(
[$this->equalTo('getFileId')],
[$this->equalTo('getUserId')],
[$this->equalTo('getSigned')]
[$this->equalTo('getSigned')],
[$this->equalTo('getIdentifyMethod')],
)
->will($this->returnValueMap([
['getFileId', [], 1],
['getUserId', [], 'fileuser'],
['getSigned', [], false]
['getSigned', [], false],
['getIdentifyMethod', [], 'nextcloud'],
]));
$self->fileUserMapper
->method('getByUuid')
Expand Down Expand Up @@ -818,14 +820,16 @@ public function providerTestGetConfigWithInvalidUuid() {
[$self->equalTo('getUserId')],
[$self->equalTo('getUserId')],
[$self->equalTo('getSigned')],
[$self->equalTo('getNodeId')]
[$this->equalTo('getIdentifyMethod')],
[$self->equalTo('getNodeId')],
)
->will($self->returnValueMap([
['getFileId', [], 1],
['getUserId', [], 'username'],
['getUserId', [], 'username'],
['getSigned', [], false],
['getNodeId', [], 1]
['getIdentifyMethod', [], 'nextcloud'],
['getNodeId', [], 1],
]));
$self->fileUserMapper
->method('getByUuid')
Expand Down Expand Up @@ -857,6 +861,9 @@ public function providerTestGetConfigWithInvalidUuid() {
'uuid', 'username', 'base64',
[
'action' => JSActions::ACTION_SIGN,
'settings' => [
'identify' => 'nextcloud',
],
'sign' => [
'pdf' => [
'base64' => base64_encode('content')
Expand All @@ -878,6 +885,7 @@ public function providerTestGetConfigWithInvalidUuid() {
[$self->equalTo('getUserId')],
[$self->equalTo('getUserId')],
[$self->equalTo('getSigned')],
[$this->equalTo('getIdentifyMethod')],
[$self->equalTo('getDisplayName')],
[$self->equalTo('getDescription')]
)
Expand All @@ -886,6 +894,7 @@ public function providerTestGetConfigWithInvalidUuid() {
['getUserId', [], 'username'],
['getUserId', [], 'username'],
['getSigned', [], false],
['getIdentifyMethod', [], 'nextcloud'],
['getDisplayName', [], 'username'],
['getDescription', [], '']
]));
Expand Down Expand Up @@ -914,6 +923,9 @@ public function providerTestGetConfigWithInvalidUuid() {
'uuid', 'username', 'url',
[
'action' => JSActions::ACTION_SIGN,
'settings' => [
'identify' => 'nextcloud',
],
'sign' => [
'pdf' => [
'url' => ''
Expand All @@ -935,6 +947,7 @@ public function providerTestGetConfigWithInvalidUuid() {
[$self->equalTo('getUserId')],
[$self->equalTo('getUserId')],
[$self->equalTo('getSigned')],
[$this->equalTo('getIdentifyMethod')],
[$self->equalTo('getDisplayName')],
[$self->equalTo('getDescription')]
)
Expand All @@ -943,6 +956,7 @@ public function providerTestGetConfigWithInvalidUuid() {
['getUserId', [], 'username'],
['getUserId', [], 'username'],
['getSigned', [], false],
['getIdentifyMethod', [], 'nextcloud'],
['getDisplayName', [], 'username'],
['getDescription', [], '']
]));
Expand Down Expand Up @@ -989,6 +1003,9 @@ public function providerTestGetConfigWithInvalidUuid() {
'uuid', 'username', 'file',
[
'action' => JSActions::ACTION_SIGN,
'settings' => [
'identify' => 'nextcloud',
],
'sign' => [
'pdf' => [
'file' => new \stdClass()
Expand All @@ -1010,6 +1027,7 @@ public function providerTestGetConfigWithInvalidUuid() {
[$self->equalTo('getUserId')],
[$self->equalTo('getUserId')],
[$self->equalTo('getSigned')],
[$this->equalTo('getIdentifyMethod')],
[$self->equalTo('getDisplayName')],
[$self->equalTo('getDescription')]
)
Expand All @@ -1018,6 +1036,7 @@ public function providerTestGetConfigWithInvalidUuid() {
['getUserId', [], 'username'],
['getUserId', [], 'username'],
['getSigned', [], false],
['getIdentifyMethod', [], 'nextcloud'],
['getDisplayName', [], 'username'],
['getDescription', [], '']
]));
Expand Down Expand Up @@ -1046,6 +1065,9 @@ public function providerTestGetConfigWithInvalidUuid() {
'uuid', 'username', 'nodeId',
[
'action' => JSActions::ACTION_SIGN,
'settings' => [
'identify' => 'nextcloud',
],
'sign' => [
'pdf' => [
'nodeId' => null
Expand All @@ -1067,6 +1089,7 @@ public function providerTestGetConfigWithInvalidUuid() {
[$self->equalTo('getUserId')],
[$self->equalTo('getUserId')],
[$self->equalTo('getSigned')],
[$this->equalTo('getIdentifyMethod')],
[$self->equalTo('getDisplayName')],
[$self->equalTo('getDescription')]
)
Expand All @@ -1075,6 +1098,7 @@ public function providerTestGetConfigWithInvalidUuid() {
['getUserId', [], 'username'],
['getUserId', [], 'username'],
['getSigned', [], false],
['getIdentifyMethod', [], 'nextcloud'],
['getDisplayName', [], 'username'],
['getDescription', [], '']
]));
Expand Down Expand Up @@ -1112,6 +1136,7 @@ public function testGetConfigUsingFileTypeWithSuccess() {
[$this->equalTo('getUserId')],
[$this->equalTo('getUserId')],
[$this->equalTo('getSigned')],
[$this->equalTo('getIdentifyMethod')],
[$this->equalTo('getDisplayName')],
[$this->equalTo('getDescription')]
)
Expand All @@ -1120,6 +1145,7 @@ public function testGetConfigUsingFileTypeWithSuccess() {
['getUserId', [], 'username'],
['getUserId', [], 'username'],
['getSigned', [], false],
['getIdentifyMethod', [], 'nextcloud'],
['getDisplayName', [], 'username'],
['getDescription', [], '']
]));
Expand Down Expand Up @@ -1152,6 +1178,9 @@ public function testGetConfigUsingFileTypeWithSuccess() {
json_encode($actual),
json_encode([
'action' => JSActions::ACTION_SIGN,
'settings' => [
'identify' => 'nextcloud',
],
'sign' => [
'pdf' => [
'file' => new \stdClass()
Expand Down
21 changes: 19 additions & 2 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function theSignerHaveAFileToSign(string $signer): void {
Assert::assertGreaterThan(0, $response['data'], 'Haven\'t files to sign');
$this->signer = [];
$this->file = [];
foreach ($response['data'] as $file) {
foreach (array_reverse($response['data']) as $file) {
$currentSigner = array_filter($file['signers'], function ($signer): bool {
return $signer['me'];
});
if (count($currentSigner) === 1) {
$this->signer = current($currentSigner);
$this->signer = end($currentSigner);
$this->file = $file;
break;
}
Expand Down Expand Up @@ -73,4 +73,21 @@ protected function parseText(string $text): string {
$text = preg_replace($patterns, $replacements, $text);
return $text;
}

/**
* @Given the signer contains
*/
public function theSignerContains(TableNode $table): void {
if (!$this->signer) {
$this->theSignerHaveAFileToSign($this->currentUser);
}
$expectedValues = $table->getColumnsHash();
foreach ($expectedValues as $value) {
Assert::assertArrayHasKey($value['key'], $this->signer);
if ($value['value'] === '<IGNORE>') {
continue;
}
Assert::assertEquals($value['value'], $this->signer[$value['key']]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: page/sign
Feature: page/sign_identify_default
Background: Create users
Given user "signer1" exists
And as user "admin"
Expand All @@ -22,6 +22,9 @@ Feature: page/sign
| callback | |
| status | 2 |
| status_text | pending |
And the signer contains
| key | value |
| identify | nextcloud |

Scenario: Open sign file with invalid data
# With invalid UUID, need to be the signer UUID
Expand Down Expand Up @@ -56,5 +59,5 @@ Feature: page/sign
Then the response should contain the initial state "libresign-config" with the following values:
| key | value |
| action | 250 |
| settings | {"identificationDocumentsFlow":false,"certificateOk":false,"hasSignatureFile":false,"phoneNumber":"","signMethod":"password","isApprover":false} |
| settings | {"identificationDocumentsFlow":false,"certificateOk":false,"hasSignatureFile":false,"phoneNumber":"","identify":"nextcloud","signMethod":"password","isApprover":false} |
| user | {"name":""} |
Loading

0 comments on commit 434fa7b

Please sign in to comment.