@@ -19,160 +19,160 @@ |
||
19 | 19 | use OCP\Share\IShare; |
20 | 20 | |
21 | 21 | class RemotePlugin implements ISearchPlugin { |
22 | - protected bool $shareeEnumeration; |
|
22 | + protected bool $shareeEnumeration; |
|
23 | 23 | |
24 | - private string $userId; |
|
24 | + private string $userId; |
|
25 | 25 | |
26 | - public function __construct( |
|
27 | - private IManager $contactsManager, |
|
28 | - private ICloudIdManager $cloudIdManager, |
|
29 | - private IConfig $config, |
|
30 | - private IUserManager $userManager, |
|
31 | - IUserSession $userSession, |
|
32 | - private IAppConfig $appConfig, |
|
33 | - private ?TrustedServers $trustedServers, |
|
34 | - ) { |
|
35 | - $this->userId = $userSession->getUser()?->getUID() ?? ''; |
|
36 | - $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
37 | - } |
|
26 | + public function __construct( |
|
27 | + private IManager $contactsManager, |
|
28 | + private ICloudIdManager $cloudIdManager, |
|
29 | + private IConfig $config, |
|
30 | + private IUserManager $userManager, |
|
31 | + IUserSession $userSession, |
|
32 | + private IAppConfig $appConfig, |
|
33 | + private ?TrustedServers $trustedServers, |
|
34 | + ) { |
|
35 | + $this->userId = $userSession->getUser()?->getUID() ?? ''; |
|
36 | + $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
37 | + } |
|
38 | 38 | |
39 | 39 | |
40 | - public function search($search, $limit, $offset, ISearchResult $searchResult): bool { |
|
41 | - $result = ['wide' => [], 'exact' => []]; |
|
42 | - $resultType = new SearchResultType('remotes'); |
|
40 | + public function search($search, $limit, $offset, ISearchResult $searchResult): bool { |
|
41 | + $result = ['wide' => [], 'exact' => []]; |
|
42 | + $resultType = new SearchResultType('remotes'); |
|
43 | 43 | |
44 | - // Search in contacts |
|
45 | - $addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN', 'EMAIL'], [ |
|
46 | - 'limit' => $limit, |
|
47 | - 'offset' => $offset, |
|
48 | - 'enumeration' => false, |
|
49 | - 'fullmatch' => false, |
|
50 | - ]); |
|
51 | - foreach ($addressBookContacts as $contact) { |
|
52 | - if (isset($contact['isLocalSystemBook'])) { |
|
53 | - continue; |
|
54 | - } |
|
55 | - if (isset($contact['CLOUD'])) { |
|
56 | - $cloudIds = $contact['CLOUD']; |
|
57 | - if (is_string($cloudIds)) { |
|
58 | - $cloudIds = [$cloudIds]; |
|
59 | - } |
|
60 | - $lowerSearch = strtolower($search); |
|
61 | - foreach ($cloudIds as $cloudId) { |
|
62 | - $cloudIdType = ''; |
|
63 | - if (\is_array($cloudId)) { |
|
64 | - $cloudIdData = $cloudId; |
|
65 | - $cloudId = $cloudIdData['value']; |
|
66 | - $cloudIdType = $cloudIdData['type']; |
|
67 | - } |
|
68 | - try { |
|
69 | - [$remoteUser, $serverUrl] = $this->splitUserRemote($cloudId); |
|
70 | - } catch (\InvalidArgumentException $e) { |
|
71 | - continue; |
|
72 | - } |
|
44 | + // Search in contacts |
|
45 | + $addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN', 'EMAIL'], [ |
|
46 | + 'limit' => $limit, |
|
47 | + 'offset' => $offset, |
|
48 | + 'enumeration' => false, |
|
49 | + 'fullmatch' => false, |
|
50 | + ]); |
|
51 | + foreach ($addressBookContacts as $contact) { |
|
52 | + if (isset($contact['isLocalSystemBook'])) { |
|
53 | + continue; |
|
54 | + } |
|
55 | + if (isset($contact['CLOUD'])) { |
|
56 | + $cloudIds = $contact['CLOUD']; |
|
57 | + if (is_string($cloudIds)) { |
|
58 | + $cloudIds = [$cloudIds]; |
|
59 | + } |
|
60 | + $lowerSearch = strtolower($search); |
|
61 | + foreach ($cloudIds as $cloudId) { |
|
62 | + $cloudIdType = ''; |
|
63 | + if (\is_array($cloudId)) { |
|
64 | + $cloudIdData = $cloudId; |
|
65 | + $cloudId = $cloudIdData['value']; |
|
66 | + $cloudIdType = $cloudIdData['type']; |
|
67 | + } |
|
68 | + try { |
|
69 | + [$remoteUser, $serverUrl] = $this->splitUserRemote($cloudId); |
|
70 | + } catch (\InvalidArgumentException $e) { |
|
71 | + continue; |
|
72 | + } |
|
73 | 73 | |
74 | - $localUser = $this->userManager->get($remoteUser); |
|
75 | - if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId()) { |
|
76 | - $result['wide'][] = [ |
|
77 | - 'label' => $contact['FN'], |
|
78 | - 'uuid' => $contact['UID'], |
|
79 | - 'value' => [ |
|
80 | - 'shareType' => IShare::TYPE_USER, |
|
81 | - 'shareWith' => $remoteUser |
|
82 | - ], |
|
83 | - 'shareWithDisplayNameUnique' => $contact['EMAIL'] !== null && $contact['EMAIL'] !== '' ? $contact['EMAIL'] : $contact['UID'], |
|
84 | - ]; |
|
85 | - } |
|
74 | + $localUser = $this->userManager->get($remoteUser); |
|
75 | + if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId()) { |
|
76 | + $result['wide'][] = [ |
|
77 | + 'label' => $contact['FN'], |
|
78 | + 'uuid' => $contact['UID'], |
|
79 | + 'value' => [ |
|
80 | + 'shareType' => IShare::TYPE_USER, |
|
81 | + 'shareWith' => $remoteUser |
|
82 | + ], |
|
83 | + 'shareWithDisplayNameUnique' => $contact['EMAIL'] !== null && $contact['EMAIL'] !== '' ? $contact['EMAIL'] : $contact['UID'], |
|
84 | + ]; |
|
85 | + } |
|
86 | 86 | |
87 | - $emailMatch = false; |
|
88 | - if (isset($contact['EMAIL'])) { |
|
89 | - $emails = is_array($contact['EMAIL']) ? $contact['EMAIL'] : [$contact['EMAIL']]; |
|
90 | - foreach ($emails as $email) { |
|
91 | - if (is_string($email) && strtolower($email) === $lowerSearch) { |
|
92 | - $emailMatch = true; |
|
93 | - break; |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - if ($emailMatch || strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) { |
|
98 | - if (strtolower($cloudId) === $lowerSearch) { |
|
99 | - $searchResult->markExactIdMatch($resultType); |
|
100 | - } |
|
101 | - $result['exact'][] = [ |
|
102 | - 'label' => $contact['FN'] . " ($cloudId)", |
|
103 | - 'uuid' => $contact['UID'], |
|
104 | - 'name' => $contact['FN'], |
|
105 | - 'type' => $cloudIdType, |
|
106 | - 'value' => [ |
|
107 | - 'shareType' => IShare::TYPE_REMOTE, |
|
108 | - 'shareWith' => $cloudId, |
|
109 | - 'server' => $serverUrl, |
|
110 | - 'isTrustedServer' => $this->trustedServers?->isTrustedServer($serverUrl) ?? false, |
|
111 | - ], |
|
112 | - ]; |
|
113 | - } else { |
|
114 | - $result['wide'][] = [ |
|
115 | - 'label' => $contact['FN'] . " ($cloudId)", |
|
116 | - 'uuid' => $contact['UID'], |
|
117 | - 'name' => $contact['FN'], |
|
118 | - 'type' => $cloudIdType, |
|
119 | - 'value' => [ |
|
120 | - 'shareType' => IShare::TYPE_REMOTE, |
|
121 | - 'shareWith' => $cloudId, |
|
122 | - 'server' => $serverUrl, |
|
123 | - 'isTrustedServer' => $this->trustedServers?->isTrustedServer($serverUrl) ?? false, |
|
124 | - ], |
|
125 | - ]; |
|
126 | - } |
|
127 | - } |
|
128 | - } |
|
129 | - } |
|
87 | + $emailMatch = false; |
|
88 | + if (isset($contact['EMAIL'])) { |
|
89 | + $emails = is_array($contact['EMAIL']) ? $contact['EMAIL'] : [$contact['EMAIL']]; |
|
90 | + foreach ($emails as $email) { |
|
91 | + if (is_string($email) && strtolower($email) === $lowerSearch) { |
|
92 | + $emailMatch = true; |
|
93 | + break; |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + if ($emailMatch || strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) { |
|
98 | + if (strtolower($cloudId) === $lowerSearch) { |
|
99 | + $searchResult->markExactIdMatch($resultType); |
|
100 | + } |
|
101 | + $result['exact'][] = [ |
|
102 | + 'label' => $contact['FN'] . " ($cloudId)", |
|
103 | + 'uuid' => $contact['UID'], |
|
104 | + 'name' => $contact['FN'], |
|
105 | + 'type' => $cloudIdType, |
|
106 | + 'value' => [ |
|
107 | + 'shareType' => IShare::TYPE_REMOTE, |
|
108 | + 'shareWith' => $cloudId, |
|
109 | + 'server' => $serverUrl, |
|
110 | + 'isTrustedServer' => $this->trustedServers?->isTrustedServer($serverUrl) ?? false, |
|
111 | + ], |
|
112 | + ]; |
|
113 | + } else { |
|
114 | + $result['wide'][] = [ |
|
115 | + 'label' => $contact['FN'] . " ($cloudId)", |
|
116 | + 'uuid' => $contact['UID'], |
|
117 | + 'name' => $contact['FN'], |
|
118 | + 'type' => $cloudIdType, |
|
119 | + 'value' => [ |
|
120 | + 'shareType' => IShare::TYPE_REMOTE, |
|
121 | + 'shareWith' => $cloudId, |
|
122 | + 'server' => $serverUrl, |
|
123 | + 'isTrustedServer' => $this->trustedServers?->isTrustedServer($serverUrl) ?? false, |
|
124 | + ], |
|
125 | + ]; |
|
126 | + } |
|
127 | + } |
|
128 | + } |
|
129 | + } |
|
130 | 130 | |
131 | - if (!$this->shareeEnumeration) { |
|
132 | - $result['wide'] = []; |
|
133 | - } else { |
|
134 | - $result['wide'] = array_slice($result['wide'], $offset, $limit); |
|
135 | - } |
|
131 | + if (!$this->shareeEnumeration) { |
|
132 | + $result['wide'] = []; |
|
133 | + } else { |
|
134 | + $result['wide'] = array_slice($result['wide'], $offset, $limit); |
|
135 | + } |
|
136 | 136 | |
137 | - if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) { |
|
138 | - try { |
|
139 | - [$remoteUser, $serverUrl] = $this->splitUserRemote($search); |
|
140 | - $localUser = $this->userManager->get($remoteUser); |
|
141 | - if ($localUser === null || $search !== $localUser->getCloudId()) { |
|
142 | - $result['exact'][] = [ |
|
143 | - 'label' => $remoteUser . " ($serverUrl)", |
|
144 | - 'uuid' => $remoteUser, |
|
145 | - 'name' => $remoteUser, |
|
146 | - 'value' => [ |
|
147 | - 'shareType' => IShare::TYPE_REMOTE, |
|
148 | - 'shareWith' => $search, |
|
149 | - 'server' => $serverUrl, |
|
150 | - 'isTrustedServer' => $this->trustedServers?->isTrustedServer($serverUrl) ?? false, |
|
151 | - ], |
|
152 | - ]; |
|
153 | - } |
|
154 | - } catch (\InvalidArgumentException $e) { |
|
155 | - } |
|
156 | - } |
|
137 | + if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) { |
|
138 | + try { |
|
139 | + [$remoteUser, $serverUrl] = $this->splitUserRemote($search); |
|
140 | + $localUser = $this->userManager->get($remoteUser); |
|
141 | + if ($localUser === null || $search !== $localUser->getCloudId()) { |
|
142 | + $result['exact'][] = [ |
|
143 | + 'label' => $remoteUser . " ($serverUrl)", |
|
144 | + 'uuid' => $remoteUser, |
|
145 | + 'name' => $remoteUser, |
|
146 | + 'value' => [ |
|
147 | + 'shareType' => IShare::TYPE_REMOTE, |
|
148 | + 'shareWith' => $search, |
|
149 | + 'server' => $serverUrl, |
|
150 | + 'isTrustedServer' => $this->trustedServers?->isTrustedServer($serverUrl) ?? false, |
|
151 | + ], |
|
152 | + ]; |
|
153 | + } |
|
154 | + } catch (\InvalidArgumentException $e) { |
|
155 | + } |
|
156 | + } |
|
157 | 157 | |
158 | - $searchResult->addResultSet($resultType, $result['wide'], $result['exact']); |
|
158 | + $searchResult->addResultSet($resultType, $result['wide'], $result['exact']); |
|
159 | 159 | |
160 | - return true; |
|
161 | - } |
|
160 | + return true; |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * split user and remote from federated cloud id |
|
165 | - * |
|
166 | - * @param string $address federated share address |
|
167 | - * @return array [user, remoteURL] |
|
168 | - * @throws \InvalidArgumentException |
|
169 | - */ |
|
170 | - public function splitUserRemote(string $address): array { |
|
171 | - try { |
|
172 | - $cloudId = $this->cloudIdManager->resolveCloudId($address); |
|
173 | - return [$cloudId->getUser(), $this->cloudIdManager->removeProtocolFromUrl($cloudId->getRemote(), true)]; |
|
174 | - } catch (\InvalidArgumentException $e) { |
|
175 | - throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e); |
|
176 | - } |
|
177 | - } |
|
163 | + /** |
|
164 | + * split user and remote from federated cloud id |
|
165 | + * |
|
166 | + * @param string $address federated share address |
|
167 | + * @return array [user, remoteURL] |
|
168 | + * @throws \InvalidArgumentException |
|
169 | + */ |
|
170 | + public function splitUserRemote(string $address): array { |
|
171 | + try { |
|
172 | + $cloudId = $this->cloudIdManager->resolveCloudId($address); |
|
173 | + return [$cloudId->getUser(), $this->cloudIdManager->removeProtocolFromUrl($cloudId->getRemote(), true)]; |
|
174 | + } catch (\InvalidArgumentException $e) { |
|
175 | + throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e); |
|
176 | + } |
|
177 | + } |
|
178 | 178 | } |