Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.45% |
63 / 66 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ResultEnricher | |
95.45% |
63 / 66 |
|
60.00% |
3 / 5 |
25 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addHerselfAccount | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
6 | |||
| addHerselfEmail | |
91.30% |
21 / 23 |
|
0.00% |
0 / 1 |
8.04 | |||
| addEmailNotificationPreference | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
6 | |||
| userMatchesSearch | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Service\Identify; |
| 10 | |
| 11 | use OCA\Libresign\Service\IdentifyMethod\Account; |
| 12 | use OCA\Libresign\Service\IdentifyMethod\Email; |
| 13 | use OCA\Libresign\Service\NotificationPreferenceResolver; |
| 14 | use OCP\IUser; |
| 15 | use OCP\IUserManager; |
| 16 | use OCP\IUserSession; |
| 17 | |
| 18 | class ResultEnricher { |
| 19 | public function __construct( |
| 20 | private IUserSession $userSession, |
| 21 | private IUserManager $userManager, |
| 22 | private Email $identifyEmailMethod, |
| 23 | private Account $identifyAccountMethod, |
| 24 | private NotificationPreferenceResolver $notificationPreferenceResolver, |
| 25 | ) { |
| 26 | } |
| 27 | |
| 28 | public function addHerselfAccount(array $return, string $search, string $method = ''): array { |
| 29 | if (!empty($method) && $method !== 'account') { |
| 30 | return $return; |
| 31 | } |
| 32 | |
| 33 | $settings = $this->identifyAccountMethod->getSettings(); |
| 34 | if (empty($settings['enabled'])) { |
| 35 | return $return; |
| 36 | } |
| 37 | |
| 38 | $user = $this->userSession->getUser(); |
| 39 | $searchLower = strtolower($search); |
| 40 | |
| 41 | if (!$this->userMatchesSearch($user, $searchLower)) { |
| 42 | return $return; |
| 43 | } |
| 44 | |
| 45 | $filtered = array_filter($return, fn ($i) => $i['identify'] === $user->getUID()); |
| 46 | if (count($filtered)) { |
| 47 | return $return; |
| 48 | } |
| 49 | |
| 50 | $return[] = [ |
| 51 | 'identify' => $user->getUID(), |
| 52 | 'isNoUser' => false, |
| 53 | 'displayName' => $user->getDisplayName(), |
| 54 | 'subname' => $user->getEMailAddress(), |
| 55 | 'iconName' => 'account', |
| 56 | 'method' => 'account', |
| 57 | ]; |
| 58 | |
| 59 | return $return; |
| 60 | } |
| 61 | |
| 62 | public function addHerselfEmail(array $return, string $search, string $method = ''): array { |
| 63 | if (!empty($method) && $method !== 'email') { |
| 64 | return $return; |
| 65 | } |
| 66 | |
| 67 | $settings = $this->identifyEmailMethod->getSettings(); |
| 68 | if (empty($settings['enabled'])) { |
| 69 | return $return; |
| 70 | } |
| 71 | |
| 72 | $user = $this->userSession->getUser(); |
| 73 | if (empty($user->getEMailAddress())) { |
| 74 | return $return; |
| 75 | } |
| 76 | |
| 77 | if (!str_contains($user->getEMailAddress(), $search) |
| 78 | && !str_contains($user->getDisplayName(), $search) |
| 79 | ) { |
| 80 | return $return; |
| 81 | } |
| 82 | |
| 83 | $filtered = array_filter($return, fn ($i) => $i['identify'] === $user->getUID()); |
| 84 | if (count($filtered)) { |
| 85 | return $return; |
| 86 | } |
| 87 | |
| 88 | $return[] = [ |
| 89 | 'identify' => $user->getEMailAddress(), |
| 90 | 'isNoUser' => true, |
| 91 | 'displayName' => $user->getDisplayName(), |
| 92 | 'subname' => $user->getEMailAddress(), |
| 93 | 'iconName' => 'email', |
| 94 | 'method' => 'email', |
| 95 | ]; |
| 96 | |
| 97 | return $return; |
| 98 | } |
| 99 | |
| 100 | public function addEmailNotificationPreference(array $list): array { |
| 101 | foreach ($list as $key => $item) { |
| 102 | if ($item['method'] !== 'account') { |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | $user = $this->userManager->get($item['identify']); |
| 107 | if ($user === null) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | $email = $user->getEMailAddress(); |
| 112 | if (empty($email)) { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | $acceptsNotifications = !$this->notificationPreferenceResolver->isEmailNotificationDisabled( |
| 117 | $user->getUID(), |
| 118 | 'libresign_file_to_sign', |
| 119 | false, |
| 120 | ); |
| 121 | |
| 122 | if ($acceptsNotifications) { |
| 123 | $list[$key]['emailAddress'] = $email; |
| 124 | } |
| 125 | $list[$key]['acceptsEmailNotifications'] = $acceptsNotifications; |
| 126 | } |
| 127 | return $list; |
| 128 | } |
| 129 | |
| 130 | private function userMatchesSearch(IUser $user, string $searchLower): bool { |
| 131 | return str_contains($user->getUID(), $searchLower) |
| 132 | || str_contains(strtolower($user->getDisplayName()), $searchLower) |
| 133 | || ($user->getEMailAddress() !== null && str_contains($user->getEMailAddress(), $searchLower)); |
| 134 | } |
| 135 | } |