Skip to content

Commit

Permalink
Merge pull request #1734 from LibreSign/feature/fix-psalm
Browse files Browse the repository at this point in the history
Fix psalm
  • Loading branch information
vitormattos authored May 26, 2023
2 parents a1d879e + bbbd062 commit 942d757
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 168 deletions.
25 changes: 10 additions & 15 deletions lib/Db/FileUserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,16 @@ public function getByEmailAndFileId(string $email, int $fileId): \OCP\AppFramewo
return $this->findEntity($qb);
}

/**
* @param array<IIdentifyMethod> $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);
}

/**
Expand Down
35 changes: 18 additions & 17 deletions lib/Helper/ValidateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 11 additions & 10 deletions lib/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
67 changes: 34 additions & 33 deletions lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 2 additions & 7 deletions lib/Service/IdentifyMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -153,12 +148,12 @@ public function getDefaultIdentiyMethod(int $fileUserId): IdentifyMethod {
}

/**
* @return array<IIdentifyMethod>
* @return array<string,array<IIdentifyMethod>>
*/
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;
Expand Down
41 changes: 21 additions & 20 deletions lib/Service/NotifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// }
// }
}
}
11 changes: 4 additions & 7 deletions lib/Service/RequestSignatureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -213,12 +213,9 @@ private function setDataToUser(FileUserEntity $fileUser, array $user, int $fileI
}
}

/**
* @param array<IdentifyMethod> $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();
}
Expand Down
Loading

0 comments on commit 942d757

Please sign in to comment.