check if user has access to file for any request, factorize some functions

Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
This commit is contained in:
Julien Veyssier 2021-03-25 14:32:32 +01:00
Родитель acc9888e79
Коммит fd0db5a22d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4141FEE162030638
3 изменённых файлов: 25 добавлений и 21 удалений

Просмотреть файл

@ -66,10 +66,10 @@ class ActivityManager {
$subject = '';
switch ($subjectIdentifier) {
case self::SUBJECT_APPROVED:
$subject = $ownActivity ? $this->l10n->t('You have approved {file}'): $this->l10n->t('{user} has approved {file}');
$subject = $ownActivity ? $this->l10n->t('You approved {file}'): $this->l10n->t('{user} approved {file}');
break;
case self::SUBJECT_REJECTED:
$subject = $ownActivity ? $this->l10n->t('You have rejected {file}'): $this->l10n->t('{user} has rejected {file}');
$subject = $ownActivity ? $this->l10n->t('You rejected {file}'): $this->l10n->t('{user} rejected {file}');
break;
default:
break;

Просмотреть файл

@ -116,8 +116,8 @@ class Notifier implements INotifier {
? $l->t('A directory was approved')
: $l->t('A directory was rejected'));
$content = $notification->getSubject() === 'approved'
? $l->t('%1$s has approved %2$s.', [$p['approverId'], $p['fileName']])
: $l->t('%1$s has rejected %2$s.', [$p['approverId'], $p['fileName']]);
? $l->t('%1$s approved %2$s.', [$p['approverId'], $p['fileName']])
: $l->t('%1$s rejected %2$s.', [$p['approverId'], $p['fileName']]);
$iconUrl = $notification->getSubject() === 'approved'
? $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'approved.svg'))
: $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'rejected.svg'));
@ -127,7 +127,7 @@ class Notifier implements INotifier {
->setParsedMessage($content)
->setLink($linkToFile)
->setRichMessage(
$notification->getSubject() === 'approved' ? $l->t('{user} has approved {node}') : $l->t('{user} has rejected {node}'),
$notification->getSubject() === 'approved' ? $l->t('{user} approved {node}') : $l->t('{user} rejected {node}'),
[
'user' => $richSubjectUser,
'node' => $richSubjectNode,

Просмотреть файл

@ -72,30 +72,34 @@ class ApprovalService {
}
}
private function userHasAccessTo(int $fileId, ?string $userId): bool {
$user = $this->userManager->get($userId);
if ($user instanceof IUser) {
$userFolder = $this->root->getUserFolder($userId);
$found = $userFolder->getById($fileId);
return count($found) > 0;
}
return false;
}
/**
* @param int $fileId
* @return bool
*/
public function getApprovalState(int $fileId, ?string $userId): int {
// to return PENDING, 2 conditions:
// - user matches
// - tag matches
if (!$this->userHasAccessTo($fileId, $userId)) {
return Application::STATE_NOTHING;
}
$rules = $this->ruleService->getRules();
foreach ($rules as $id => $rule) {
try {
if ($this->tagObjectMapper->haveTag($fileId, 'files', $rule['tagPending'])
&& in_array($userId, $rule['users'])) {
return Application::STATE_APPROVABLE;
}
} catch (TagNotFoundException $e) {
}
}
// now check approved and rejected, we don't care about the user here
foreach ($rules as $id => $rule) {
try {
if ($this->tagObjectMapper->haveTag($fileId, 'files', $rule['tagPending'])) {
return Application::STATE_PENDING;
if (in_array($userId, $rule['users'])) {
return Application::STATE_APPROVABLE;
} else {
return Application::STATE_PENDING;
}
} elseif ($this->tagObjectMapper->haveTag($fileId, 'files', $rule['tagApproved'])) {
return Application::STATE_APPROVED;
} elseif ($this->tagObjectMapper->haveTag($fileId, 'files', $rule['tagRejected'])) {
@ -170,7 +174,7 @@ class ApprovalService {
return false;
}
private function sendNotification(int $fileId, ?string $approverId, bool $approved) {
private function sendNotification(int $fileId, ?string $approverId, bool $approved): void {
$paramsByUser = [];
$root = $this->root;
// notification for eveyone having access except the one approving/rejecting