diff --git a/lib/Db/FileUserMapper.php b/lib/Db/FileUserMapper.php index 7ca8218945..9d430c6890 100644 --- a/lib/Db/FileUserMapper.php +++ b/lib/Db/FileUserMapper.php @@ -76,21 +76,16 @@ public function getByEmailAndFileId(string $email, int $fileId): \OCP\AppFramewo return $this->findEntity($qb); } - /** - * @param array $identifyMethods - */ - public function getByIdentifyMethodAndFileId(array $identifyMethods, int $fileId): \OCP\AppFramework\Db\Entity { - foreach ($identifyMethods as $identifyMethod) { - $qb = $this->db->getQueryBuilder(); - $qb->select('fu.*') - ->from($this->getTableName(), 'fu') - ->join('fu', 'libresign_identify_method', 'im', 'fu.file_id = im.file_user_id') - ->where($qb->expr()->eq('im.method', $qb->createNamedParameter($identifyMethod->getEntity()->getMethod()))) - ->andWhere($qb->expr()->eq('im.identifier_key', $qb->createNamedParameter($identifyMethod->getEntity()->getIdentifierKey()))) - ->andWhere($qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($identifyMethod->getEntity()->getIdentifierValue()))) - ->andWhere($qb->expr()->eq('fu.file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); - return $this->findEntity($qb); - } + public function getByIdentifyMethodAndFileId(IIdentifyMethod $identifyMethod, int $fileId): \OCP\AppFramework\Db\Entity { + $qb = $this->db->getQueryBuilder(); + $qb->select('fu.*') + ->from($this->getTableName(), 'fu') + ->join('fu', 'libresign_identify_method', 'im', 'fu.file_id = im.file_user_id') + ->where($qb->expr()->eq('im.method', $qb->createNamedParameter($identifyMethod->getEntity()->getMethod()))) + ->andWhere($qb->expr()->eq('im.identifier_key', $qb->createNamedParameter($identifyMethod->getEntity()->getIdentifierKey()))) + ->andWhere($qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($identifyMethod->getEntity()->getIdentifierValue()))) + ->andWhere($qb->expr()->eq('fu.file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); + return $this->findEntity($qb); } /** diff --git a/lib/Helper/ValidateHelper.php b/lib/Helper/ValidateHelper.php index 87d0e21899..cb1b7f885a 100644 --- a/lib/Helper/ValidateHelper.php +++ b/lib/Helper/ValidateHelper.php @@ -574,23 +574,24 @@ public function validateCredentials(FileUser $fileUser, array $params): void { throw new LibresignException($this->l10n->t('File already signed.')); } - $identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromFileUserId($fileUser->getId()); - $identifyMethod = array_filter($identifyMethods, function (IdentifyMethod $identifyMethod) use ($currentIdentifyMethod): bool { - return $identifyMethod->getMethod() === $currentIdentifyMethod; - }); - if (!$identifyMethod) { - throw new LibresignException($this->l10n->t('Invalid identification method')); - } - $identifyMethod = current($identifyMethod); - - switch ($identifyMethod->getEntity->getMethod()) { - case IdentifyMethodService::IDENTIFY_SMS: - case IdentifyMethodService::IDENTIFY_TELEGRAM: - case IdentifyMethodService::IDENTIFY_SIGNAL: - case IdentifyMethodService::IDENTIFY_EMAIL: - $this->valdateCode($fileUser, $params); - break; - } + // @todo implement this logic to validate the identify method, the follow code is complex and dont work + // $identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromFileUserId($fileUser->getId()); + // $identifyMethod = array_filter($identifyMethods, function (IdentifyMethod $identifyMethod) use ($currentIdentifyMethod): bool { + // return $identifyMethod->getMethod() === $currentIdentifyMethod; + // }); + // if (!$identifyMethod) { + // throw new LibresignException($this->l10n->t('Invalid identification method')); + // } + // $identifyMethod = current($identifyMethod); + + // switch ($identifyMethod->getEntity->getMethod()) { + // case IdentifyMethodService::IDENTIFY_SMS: + // case IdentifyMethodService::IDENTIFY_TELEGRAM: + // case IdentifyMethodService::IDENTIFY_SIGNAL: + // case IdentifyMethodService::IDENTIFY_EMAIL: + // $this->valdateCode($fileUser, $params); + // break; + // } } public function validateIdentifyMethods(array $identifyMethods): void { diff --git a/lib/Service/AccountService.php b/lib/Service/AccountService.php index 42a340b0cc..9e5a8b48e5 100644 --- a/lib/Service/AccountService.php +++ b/lib/Service/AccountService.php @@ -174,16 +174,17 @@ public function createToSign(string $uuid, string $email, string $password, ?str $newUser->setDisplayName($fileUser->getDisplayName()); $newUser->setSystemEMailAddress($email); - $identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromFileUserId($fileUser->getId()); - foreach ($identifyMethods as $name => $identifyMethod) { - if ($name === IdentifyMethodService::IDENTIFY_ACCOUNT) { - $entity = $identifyMethod->getEntity(); - if ($entity->getIdentifierKey() === IdentifyMethodService::IDENTIFY_ACCOUNT) { - $identifyMethod->getEntity()->setIdentifierValue($newUser->getUID()); - $this->identifyMethodService->save($fileUser, false); - } - } - } + // @todo implement this logic, the follow code is complex and dont work + // $identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromFileUserId($fileUser->getId()); + // foreach ($identifyMethods as $name => $identifyMethod) { + // if ($name === IdentifyMethodService::IDENTIFY_ACCOUNT) { + // $entity = $identifyMethod->getEntity(); + // if ($entity->getIdentifierKey() === IdentifyMethodService::IDENTIFY_ACCOUNT) { + // $identifyMethod->getEntity()->setIdentifierValue($newUser->getUID()); + // $this->identifyMethodService->save($fileUser, false); + // } + // } + // } if ($this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') { try { diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index 793c882650..548e1b57df 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -168,39 +168,40 @@ private function getSigners(): array { 'me' => false, 'fileUserId' => $signer->getId() ]; - if (!empty($uid)) { - $identifyMethodServices = $this->identifyMethodService->getIdentifyMethodsFromFileUserId($signer->getId()); - if ($uid === $this->file->getUserId()) { - $email = array_reduce($identifyMethodServices, function (?string $carry, IIdentifyMethod $identifyMethod): ?string { - if ($identifyMethod->getEntity()->getIdentifierKey() === IdentifyMethodService::IDENTIFY_EMAIL) { - $carry = $identifyMethod->getEntity()->getIdentifierValue(); - } - return $carry; - }); - $signatureToShow['email'] = $email; - $user = $this->userManager->getByEmail($email); - if ($user) { - $signatureToShow['uid'] = $user[0]->getUID(); - } - } - $accountIdentifyMethod = array_reduce($identifyMethodServices, function (?IIdentifyMethod $carry, IIdentifyMethod $identifyMethod): ?IIdentifyMethod { - if ($identifyMethod->getEntity()->getMethod() === IdentifyMethodService::IDENTIFY_ACCOUNT) { - $carry = $identifyMethod; - } - return $carry; - }); - if ($accountIdentifyMethod) { - $entity = $accountIdentifyMethod->getEntity(); - $signatureToShow['me'] = false; - if ($entity->getIdentifierKey() === IdentifyMethodService::IDENTIFY_EMAIL) { - $signatureToShow['me'] = $this->me->getEMailAddress() === $entity->getIdentifierValue(); - if (!$signer->getSigned()) { - $this->settings['canSign'] = true; - $this->settings['signerFileUuid'] = $signer->getUuid(); - } - } - } - } + // @todo refactor this code + // if (!empty($uid)) { + // $identifyMethodServices = $this->identifyMethodService->getIdentifyMethodsFromFileUserId($signer->getId()); + // if ($uid === $this->file->getUserId()) { + // $email = array_reduce($identifyMethodServices, function (?string $carry, IIdentifyMethod $identifyMethod): ?string { + // if ($identifyMethod->getEntity()->getIdentifierKey() === IdentifyMethodService::IDENTIFY_EMAIL) { + // $carry = $identifyMethod->getEntity()->getIdentifierValue(); + // } + // return $carry; + // }); + // $signatureToShow['email'] = $email; + // $user = $this->userManager->getByEmail($email); + // if ($user) { + // $signatureToShow['uid'] = $user[0]->getUID(); + // } + // } + // $accountIdentifyMethod = array_reduce($identifyMethodServices, function (?IIdentifyMethod $carry, IIdentifyMethod $identifyMethod): ?IIdentifyMethod { + // if ($identifyMethod->getEntity()->getMethod() === IdentifyMethodService::IDENTIFY_ACCOUNT) { + // $carry = $identifyMethod; + // } + // return $carry; + // }); + // if ($accountIdentifyMethod) { + // $entity = $accountIdentifyMethod->getEntity(); + // $signatureToShow['me'] = false; + // if ($entity->getIdentifierKey() === IdentifyMethodService::IDENTIFY_EMAIL) { + // $signatureToShow['me'] = $this->me->getEMailAddress() === $entity->getIdentifierValue(); + // if (!$signer->getSigned()) { + // $this->settings['canSign'] = true; + // $this->settings['signerFileUuid'] = $signer->getUuid(); + // } + // } + // } + // } $this->signers[] = $signatureToShow; } return $this->signers; diff --git a/lib/Service/IdentifyMethodService.php b/lib/Service/IdentifyMethodService.php index 206bc21e85..cf427e13e9 100644 --- a/lib/Service/IdentifyMethodService.php +++ b/lib/Service/IdentifyMethodService.php @@ -132,11 +132,6 @@ public function getDefaultIdentifyMethodName(): string { ?? IdentifyMethodService::IDENTIFY_ACCOUNT; } - public function getDefaultEntity(): IdentifyMethod { - $identifyMethodName = $this->getDefaultIdentifyMethodName(); - return $this->getIdentifyMethod($identifyMethodName)->getEntity(); - } - public function getDefaultIdentiyMethod(int $fileUserId): IdentifyMethod { $identifyMethods = $this->identifyMethodMapper->getIdentifyMethodsFromFileUserId($fileUserId); $default = array_filter($identifyMethods, function (IdentifyMethod $current): bool { @@ -153,12 +148,12 @@ public function getDefaultIdentiyMethod(int $fileUserId): IdentifyMethod { } /** - * @return array + * @return array> */ public function getIdentifyMethodsFromFileUserId(int $fileUserId): array { $entities = $this->identifyMethodMapper->getIdentifyMethodsFromFileUserId($fileUserId); foreach ($entities as $entity) { - $identifyMethod = $this->getIdentifyMethod($entity->getMethod()); + $identifyMethod = $this->newInstanceOfIdentifyMethod($entity->getMethod()); $identifyMethod->setEntity($entity); } return $this->identifyMethods; diff --git a/lib/Service/NotifyService.php b/lib/Service/NotifyService.php index 18ca130251..8ddf565d3c 100644 --- a/lib/Service/NotifyService.php +++ b/lib/Service/NotifyService.php @@ -25,25 +25,26 @@ public function signers(int $nodeId, array $signers): void { $this->validateHelper->signerWasAssociated($signer); $this->validateHelper->notSigned($signer); } - $fileUsers = $this->fileUserMapper->getByNodeId($nodeId); - foreach ($fileUsers as $fileUser) { - $identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromFileUserId($fileUser->getId()); - $identifyMethod = array_reduce($identifyMethods, function (?IIdentifyMethod $carry, IIdentifyMethod $identifyMethod) use ($signers): ?IIdentifyMethod { - foreach ($signers as $signer) { - $key = key($signer); - $value = current($signer); - $entity = $identifyMethod->getEntity(); - if ($entity->getIdentifierKey() === $key - && $entity->getIdentifierValue() === $value - ) { - return $identifyMethod; - } - } - return $carry; - }); - if ($identifyMethod instanceof IIdentifyMethod) { - $identifyMethod->notify(false, $fileUser); - } - } + // @todo refactor this code + // $fileUsers = $this->fileUserMapper->getByNodeId($nodeId); + // foreach ($fileUsers as $fileUser) { + // $identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromFileUserId($fileUser->getId()); + // $identifyMethod = array_reduce($identifyMethods, function (?IIdentifyMethod $carry, IIdentifyMethod $identifyMethod) use ($signers): ?IIdentifyMethod { + // foreach ($signers as $signer) { + // $key = key($signer); + // $value = current($signer); + // $entity = $identifyMethod->getEntity(); + // if ($entity->getIdentifierKey() === $key + // && $entity->getIdentifierValue() === $value + // ) { + // return $identifyMethod; + // } + // } + // return $carry; + // }); + // if ($identifyMethod instanceof IIdentifyMethod) { + // $identifyMethod->notify(false, $fileUser); + // } + // } } } diff --git a/lib/Service/RequestSignatureService.php b/lib/Service/RequestSignatureService.php index 0c645a55e4..ff7bf5921d 100644 --- a/lib/Service/RequestSignatureService.php +++ b/lib/Service/RequestSignatureService.php @@ -30,9 +30,9 @@ use OCA\Libresign\Db\FileMapper; use OCA\Libresign\Db\FileUser as FileUserEntity; use OCA\Libresign\Db\FileUserMapper; -use OCA\Libresign\Db\IdentifyMethod; use OCA\Libresign\Db\IdentifyMethodMapper; use OCA\Libresign\Helper\ValidateHelper; +use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod; use OCP\AppFramework\Db\DoesNotExistException; use OCP\Files\IMimeTypeDetector; use OCP\Http\Client\IClientService; @@ -136,7 +136,7 @@ private function associateToSigners(array $data, int $fileId): array { foreach ($data['users'] as $user) { $identifyMethods = $this->identifyMethod->getByUserData($user['identify']); $fileUser = $this->getFileUserByIdentifyMethod( - $identifyMethods, + current($identifyMethods), $fileId ); $this->setDataToUser($fileUser, $user, $fileId); @@ -213,12 +213,9 @@ private function setDataToUser(FileUserEntity $fileUser, array $user, int $fileI } } - /** - * @param array $identifyMethods - */ - private function getFileUserByIdentifyMethod(array $identifyMethods, int $fileId): FileUserEntity { + private function getFileUserByIdentifyMethod(IIdentifyMethod $identifyMethod, int $fileId): FileUserEntity { try { - $fileUser = $this->fileUserMapper->getByIdentifyMethodAndFileId($identifyMethods, $fileId); + $fileUser = $this->fileUserMapper->getByIdentifyMethodAndFileId($identifyMethod, $fileId); } catch (DoesNotExistException $e) { $fileUser = new FileUserEntity(); } diff --git a/lib/Service/SignFileService.php b/lib/Service/SignFileService.php index 4e855c882c..f154e7a1e6 100644 --- a/lib/Service/SignFileService.php +++ b/lib/Service/SignFileService.php @@ -499,65 +499,66 @@ private function throwIfAlreadySigned(FileEntity $fileEntity, ?FileUserEntity $f * @todo validate here if is possible identify the user by identification methods */ private function trhowIfCantIdentifyUser(string $uuid, ?IUser $user, ?FileUserEntity $fileUser): void { - $identifyMethods = $this->identifyMethod->getIdentifyMethodsFromFileUserId($fileUser->getId()); - $maxAttempts = (int) $this->config->getAppValue(Application::APP_ID, 'max_attempts', 5); - foreach ($identifyMethods as $identifyMethod) { - $entity = $identifyMethod->getEntity(); - if ($entity->getIdentifiedAtDate()) { - continue; - } - if ($entity->getAttempts() > $maxAttempts) { - throw new LibresignException(json_encode([ - 'action' => JSActions::ACTION_DO_NOTHING, - 'errors' => [$this->l10n->t('Exceeded identification attempts')], - ])); - } - /** @todo Validate identify method here if necessary */ - // $identifyMethod->validateContextBeforeSign($user, $fileUser); - } - if ($fileUser instanceof FileUserEntity) { - $identifyMethods = $this->identifyMethodMapper->getIdentifyMethodsFromFileUserId($fileUser->getId()); - $accountIdentifyMethod = array_filter($identifyMethods, function (IdentifyMethod $identifyMethod): bool { - return $identifyMethod->getMethod() === IdentifyMethodService::IDENTIFY_ACCOUNT; - }); - if (count($accountIdentifyMethod)) { - return; - } - } - if ($user) { - throw new LibresignException(json_encode([ - 'action' => JSActions::ACTION_DO_NOTHING, - 'errors' => [$this->l10n->t('This is not your file')], - ])); - } - $identifyMethods = $this->identifyMethodMapper->getIdentifyMethodsFromFileUserId($fileUser->getId()); - $email = array_reduce($identifyMethods, function (string $carry, IdentifyMethod $identifyMethod): string { - /** - * @todo go-horse to make working with implementation when is necessary to have an email - */ - if ($identifyMethod->getIdentifierKey() === 'uid') { - return $identifyMethod->getIdentifierValue(); - } elseif ($identifyMethod->getIdentifierKey() === 'email') { - return $identifyMethod->getIdentifierValue(); - } - return $carry; - }, ''); - if ($email && $this->userManager->getByEmail($email)) { - throw new LibresignException(json_encode([ - 'action' => JSActions::ACTION_REDIRECT, - 'errors' => [$this->l10n->t('User already exists. Please login.')], - 'redirect' => $this->urlGenerator->linkToRoute('core.login.showLoginForm', [ - 'redirect_url' => $this->urlGenerator->linkToRoute( - 'libresign.page.sign', - ['uuid' => $uuid] - ), - ]), - ])); - } - throw new LibresignException(json_encode([ - 'action' => JSActions::ACTION_CREATE_USER, - 'settings' => ['accountHash' => md5($email)], - ])); + // @todo complex code, check if is necessary and what's the best implementation + // $identifyMethods = $this->identifyMethod->getIdentifyMethodsFromFileUserId($fileUser->getId()); + // $maxAttempts = (int) $this->config->getAppValue(Application::APP_ID, 'max_attempts', 5); + // foreach ($identifyMethods as $identifyMethod) { + // $entity = $identifyMethod->getEntity(); + // if ($entity->getIdentifiedAtDate()) { + // continue; + // } + // if ($entity->getAttempts() > $maxAttempts) { + // throw new LibresignException(json_encode([ + // 'action' => JSActions::ACTION_DO_NOTHING, + // 'errors' => [$this->l10n->t('Exceeded identification attempts')], + // ])); + // } + // /** @todo Validate identify method here if necessary */ + // // $identifyMethod->validateContextBeforeSign($user, $fileUser); + // } + // if ($fileUser instanceof FileUserEntity) { + // $identifyMethods = $this->identifyMethodMapper->getIdentifyMethodsFromFileUserId($fileUser->getId()); + // $accountIdentifyMethod = array_filter($identifyMethods, function (IdentifyMethod $identifyMethod): bool { + // return $identifyMethod->getMethod() === IdentifyMethodService::IDENTIFY_ACCOUNT; + // }); + // if (count($accountIdentifyMethod)) { + // return; + // } + // } + // if ($user) { + // throw new LibresignException(json_encode([ + // 'action' => JSActions::ACTION_DO_NOTHING, + // 'errors' => [$this->l10n->t('This is not your file')], + // ])); + // } + // $identifyMethods = $this->identifyMethodMapper->getIdentifyMethodsFromFileUserId($fileUser->getId()); + // $email = array_reduce($identifyMethods, function (string $carry, IdentifyMethod $identifyMethod): string { + // /** + // * @todo go-horse to make working with implementation when is necessary to have an email + // */ + // if ($identifyMethod->getIdentifierKey() === 'uid') { + // return $identifyMethod->getIdentifierValue(); + // } elseif ($identifyMethod->getIdentifierKey() === 'email') { + // return $identifyMethod->getIdentifierValue(); + // } + // return $carry; + // }, ''); + // if ($email && $this->userManager->getByEmail($email)) { + // throw new LibresignException(json_encode([ + // 'action' => JSActions::ACTION_REDIRECT, + // 'errors' => [$this->l10n->t('User already exists. Please login.')], + // 'redirect' => $this->urlGenerator->linkToRoute('core.login.showLoginForm', [ + // 'redirect_url' => $this->urlGenerator->linkToRoute( + // 'libresign.page.sign', + // ['uuid' => $uuid] + // ), + // ]), + // ])); + // } + // throw new LibresignException(json_encode([ + // 'action' => JSActions::ACTION_CREATE_USER, + // 'settings' => ['accountHash' => md5($email)], + // ])); } /**