Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions apps/files_sharing/lib/Controller/ShareesAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,24 +593,52 @@ protected function searchSharees($search, $itemType, array $shareTypes, $page, $
* @return array
*/
protected function getEmail($search) {
$result = ['results' => [], 'exact' => []];
$result = ['results' => [], 'exact' => [], 'exactIdMatch' => false];

// Search in contacts
//@todo Pagination missing
$addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']);
$result['exactIdMatch'] = false;
$lowerSearch = strtolower($search);
foreach ($addressBookContacts as $contact) {
if (isset($contact['isLocalSystemBook'])) {
continue;
}
if (isset($contact['EMAIL'])) {
$emailAddresses = $contact['EMAIL'];
if (!is_array($emailAddresses)) {
$emailAddresses = [$emailAddresses];
}
foreach ($emailAddresses as $emailAddress) {
if (strtolower($contact['FN']) === strtolower($search) || strtolower($emailAddress) === strtolower($search)) {
if (strtolower($emailAddress) === strtolower($search)) {
$exactEmailMatch = strtolower($emailAddress) === $lowerSearch;

if (isset($contact['isLocalSystemBook'])) {
if ($exactEmailMatch) {
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition led to a crash on my NC instance: users cannot share anymore with users with an email address set in NC.

For user "pab", with an email address, $contact['CLOUD'][0] resolves to "pab@" and resolveCloudId fails saying: Invalid cloud id.

See here for full error stack: https://help.nextcloud.com/t/cant-share-to-a-local-user/12339/4?u=pab

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can someone tell me where the ['CLOUD'][0] value is supposed to be found? Is there a doc of the contactsManager to see to which fields of the database CLOUD shall be linked to?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @nickvergessen this is where this weird share name could come from

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix is in #6099

if (!$this->hasUserInResult($cloud->getUser())) {
$this->result['exact']['users'][] = [
'label' => $contact['FN'] . " ($emailAddress)",
'value' => [
'shareType' => Share::SHARE_TYPE_USER,
'shareWith' => $cloud->getUser(),
],
];
}
return ['results' => [], 'exact' => [], 'exactIdMatch' => true];
}
if ($this->shareeEnumeration) {
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
if (!$this->hasUserInResult($cloud->getUser())) {
$this->result['users'][] = [
'label' => $contact['FN'] . " ($emailAddress)",
'value' => [
'shareType' => Share::SHARE_TYPE_USER,
'shareWith' => $cloud->getUser(),
],
];
}
}
continue;
}

if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) {
if ($exactEmailMatch) {
$result['exactIdMatch'] = true;
}
$result['exact'][] = [
Expand Down Expand Up @@ -688,6 +716,28 @@ protected function getLookup($search) {
$this->result['lookup'] = $result;
}

/**
* Check if a given user is already part of the result
*
* @param string $userId
* @return bool
*/
protected function hasUserInResult($userId) {
foreach ($this->result['exact']['users'] as $result) {
if ($result['value']['shareWith'] === $userId) {
return true;
}
}

foreach ($this->result['users'] as $result) {
if ($result['value']['shareWith'] === $userId) {
return true;
}
}

return false;
}

/**
* Generates a bunch of pagination links for the current page
*
Expand Down
15 changes: 15 additions & 0 deletions apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,21 @@ public function dataGetEmail() {
['results' => [], 'exact' => [], 'exactIdMatch' => false],
true,
],
// Local user found by email
[
'test@example.com',
[
[
'FN' => 'User',
'EMAIL' => ['test@example.com'],
'CLOUD' => ['test@localhost'],
'isLocalSystemBook' => true,
]
],
false,
['results' => [], 'exact' => [], 'exactIdMatch' => true],
false,
]
];
}

Expand Down