Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCP\Federation\ICloudIdManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Mail\IEmailValidator;
use OCP\Share\IShare;
Expand All @@ -30,6 +31,7 @@ public function __construct(
KnownUserService $knownUserService,
IUserSession $userSession,
IEmailValidator $emailValidator,
IUserManager $userManager,
mixed $shareWithGroupOnlyExcludeGroupsList = [],
) {
parent::__construct(
Expand All @@ -40,6 +42,7 @@ public function __construct(
$knownUserService,
$userSession,
$emailValidator,
$userManager,
$shareWithGroupOnlyExcludeGroupsList,
IShare::TYPE_EMAIL,
);
Expand Down
247 changes: 117 additions & 130 deletions lib/private/Collaboration/Collaborators/MailPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Collaboration\Collaborators;

use OC\KnownUser\KnownUserService;
Expand All @@ -16,9 +17,11 @@
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Mail\IEmailValidator;
use OCP\Share\IShare;
use RuntimeException;

class MailPlugin implements ISearchPlugin {
protected bool $shareWithGroupOnly;
Expand All @@ -41,6 +44,7 @@ public function __construct(
private KnownUserService $knownUserService,
private IUserSession $userSession,
private IEmailValidator $emailValidator,
private IUserManager $userManager,
private mixed $shareWithGroupOnlyExcludeGroupsList,
private int $shareType,
) {
Expand All @@ -66,83 +70,121 @@ public function search($search, $limit, $offset, ISearchResult $searchResult): b
}

// Extract the email address from "Foo Bar <foo.bar@example.tld>" and then search with "foo.bar@example.tld" instead
$result = preg_match('/<([^@]+@.+)>$/', $search, $matches);
if ($result && filter_var($matches[1], FILTER_VALIDATE_EMAIL)) {
return $this->search($matches[1], $limit, $offset, $searchResult);
if (preg_match('/<([^@]+@.+)>$/', $search, $matches) && filter_var($matches[1], FILTER_VALIDATE_EMAIL)) {
$search = $matches[1];
}

$currentUserId = $this->userSession->getUser()->getUID();
$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());

$result = $userResults = ['wide' => [], 'exact' => []];
$userType = new SearchResultType('users');
$emailType = new SearchResultType('emails');
$hasMore = false;
$count = 0;
$results = ['wide' => [], 'exact' => []];
$type = match ($this->shareType) {
IShare::TYPE_USER => new SearchResultType('users'),
IShare::TYPE_EMAIL => new SearchResultType('emails'),
default => throw new RuntimeException(),
};

// Search in contacts
$addressBookContacts = $this->contactsManager->search(
$search,
['EMAIL', 'FN'],
[
'limit' => $limit,
// We request one more, so we can check if there are more results available
'limit' => $limit + 1,
'offset' => $offset,
'enumeration' => $this->shareeEnumeration,
'fullmatch' => $this->shareeEnumerationFullMatch,
]
);
$lowerSearch = strtolower($search);
foreach ($addressBookContacts as $contact) {
if (isset($contact['EMAIL'])) {
$emailAddresses = $contact['EMAIL'];
if (\is_string($emailAddresses)) {
$emailAddresses = [$emailAddresses];
if (!isset($contact['EMAIL'])) {
continue;
}

$emailAddresses = $contact['EMAIL'];
if (\is_string($emailAddresses)) {
$emailAddresses = [$emailAddresses];
}
foreach ($emailAddresses as $emailAddress) {
$displayName = $emailAddress;
$emailAddressType = null;
if (\is_array($emailAddress)) {
$emailAddressData = $emailAddress;
$emailAddress = $emailAddressData['value'];
$emailAddressType = $emailAddressData['type'];
}

if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
continue;
}
foreach ($emailAddresses as $type => $emailAddress) {
$displayName = $emailAddress;
$emailAddressType = null;
if (\is_array($emailAddress)) {
$emailAddressData = $emailAddress;
$emailAddress = $emailAddressData['value'];
$emailAddressType = $emailAddressData['type'];

if (isset($contact['FN'])) {
$displayName = $contact['FN'] . ' (' . $emailAddress . ')';
}
$exactEmailMatch = strtolower($emailAddress) === $lowerSearch;

if (isset($contact['isLocalSystemBook'])) {
$contactUser = $this->userManager->get($contact['UID']);
if ($contactUser === null) {
continue;
}

if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
$contactGroups = $this->groupManager->getUserGroupIds($contactUser);
if ($this->shareWithGroupOnly && array_intersect($contactGroups, array_diff($userGroups, $this->shareWithGroupOnlyExcludeGroupsList)) === []) {
continue;
}

if (isset($contact['FN'])) {
$displayName = $contact['FN'] . ' (' . $emailAddress . ')';
if ($exactEmailMatch && $this->shareeEnumerationFullMatch) {
try {
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0] ?? '');
} catch (\InvalidArgumentException $e) {
continue;
}

if ($this->shareType === IShare::TYPE_USER && !$this->isCurrentUser($cloud) && !$searchResult->hasResult($type, $cloud->getUser())) {
$singleResult = [[
'label' => $displayName,
'uuid' => $contact['UID'] ?? $emailAddress,
'name' => $contact['FN'] ?? $displayName,
'value' => [
'shareType' => IShare::TYPE_USER,
'shareWith' => $cloud->getUser(),
],
'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser()
]];
$searchResult->addResultSet($type, [], $singleResult);
$searchResult->markExactIdMatch($type);
}
return false;
}
$exactEmailMatch = strtolower($emailAddress) === $lowerSearch;

if (isset($contact['isLocalSystemBook'])) {
if ($this->shareWithGroupOnly) {
/*
* Check if the user may share with the user associated with the e-mail of the just found contact
*/
$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());

// ShareWithGroupOnly filtering
$userGroups = array_diff($userGroups, $this->shareWithGroupOnlyExcludeGroupsList);

$found = false;
foreach ($userGroups as $userGroup) {
if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
$found = true;
break;
}
}
if (!$found) {

if ($this->shareeEnumeration && $this->shareType === IShare::TYPE_USER) {
try {
if (!isset($contact['CLOUD'])) {
continue;
}
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0] ?? '');
} catch (\InvalidArgumentException $e) {
continue;
}
if ($exactEmailMatch && $this->shareeEnumerationFullMatch) {
try {
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0] ?? '');
} catch (\InvalidArgumentException $e) {
continue;
}
$addToWide = !($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone);

if ($this->shareType === IShare::TYPE_USER && !$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
$singleResult = [[
if (!$addToWide && $this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $contact['UID'])) {
$addToWide = true;
}

if (!$addToWide && $this->shareeEnumerationInGroupOnly) {
$addToWide = array_intersect($contactGroups, $userGroups) !== [];
}

if ($addToWide && !$this->isCurrentUser($cloud) && !$searchResult->hasResult($type, $cloud->getUser())) {
if ($count++ >= $limit) {
$hasMore = true;
} else {
$results['wide'][] = [
'label' => $displayName,
'uuid' => $contact['UID'] ?? $emailAddress,
'name' => $contact['FN'] ?? $displayName,
Expand All @@ -151,68 +193,23 @@ public function search($search, $limit, $offset, ISearchResult $searchResult): b
'shareWith' => $cloud->getUser(),
],
'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser()

]];
$searchResult->addResultSet($userType, [], $singleResult);
$searchResult->markExactIdMatch($emailType);
];
}
return false;
}

if ($this->shareeEnumeration) {
try {
if (!isset($contact['CLOUD'])) {
continue;
}
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0] ?? '');
} catch (\InvalidArgumentException $e) {
continue;
}

$addToWide = !($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone);
if (!$addToWide && $this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $contact['UID'])) {
$addToWide = true;
}

if (!$addToWide && $this->shareeEnumerationInGroupOnly) {
$addToWide = false;
$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
foreach ($userGroups as $userGroup) {
if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
$addToWide = true;
break;
}
}
}
if ($addToWide && !$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
if ($this->shareType === IShare::TYPE_USER) {
$userResults['wide'][] = [
'label' => $displayName,
'uuid' => $contact['UID'] ?? $emailAddress,
'name' => $contact['FN'] ?? $displayName,
'value' => [
'shareType' => IShare::TYPE_USER,
'shareWith' => $cloud->getUser(),
],
'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser()
];
}
continue;
}
}
continue;
}

if ($this->shareType !== IShare::TYPE_EMAIL) {
continue;
}
continue;
}

if ($exactEmailMatch
|| (isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch)) {
if ($this->shareType === IShare::TYPE_EMAIL) {
if ($count++ >= $limit) {
$hasMore = true;
} elseif ($exactEmailMatch || (isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch)) {
if ($exactEmailMatch) {
$searchResult->markExactIdMatch($emailType);
$searchResult->markExactIdMatch($type);
}
$result['exact'][] = [

$results['exact'][] = [
'label' => $displayName,
'uuid' => $contact['UID'] ?? $emailAddress,
'name' => $contact['FN'] ?? $displayName,
Expand All @@ -223,7 +220,7 @@ public function search($search, $limit, $offset, ISearchResult $searchResult): b
],
];
} else {
$result['wide'][] = [
$results['wide'][] = [
'label' => $displayName,
'uuid' => $contact['UID'] ?? $emailAddress,
'name' => $contact['FN'] ?? $displayName,
Expand All @@ -238,35 +235,25 @@ public function search($search, $limit, $offset, ISearchResult $searchResult): b
}
}

$reachedEnd = true;
if ($this->shareeEnumeration) {
$reachedEnd = (count($result['wide']) < $offset + $limit)
&& (count($userResults['wide']) < $offset + $limit);

$result['wide'] = array_slice($result['wide'], $offset, $limit);
$userResults['wide'] = array_slice($userResults['wide'], $offset, $limit);
}

if ($this->shareType === IShare::TYPE_EMAIL
&& !$searchResult->hasExactIdMatch($emailType) && $this->emailValidator->isValid($search)) {
$result['exact'][] = [
'label' => $search,
'uuid' => $search,
'value' => [
'shareType' => IShare::TYPE_EMAIL,
'shareWith' => $search,
],
];
&& !$searchResult->hasExactIdMatch($type) && $this->emailValidator->isValid($search)) {
if ($count++ >= $limit) {
$hasMore = true;
} else {
$results['exact'][] = [
'label' => $search,
'uuid' => $search,
'value' => [
'shareType' => IShare::TYPE_EMAIL,
'shareWith' => $search,
],
];
}
}

if ($this->shareType === IShare::TYPE_USER && !empty($userResults['wide'])) {
$searchResult->addResultSet($userType, $userResults['wide'], []);
}
if ($this->shareType === IShare::TYPE_EMAIL) {
$searchResult->addResultSet($emailType, $result['wide'], $result['exact']);
}
$searchResult->addResultSet($type, $results['wide'], $results['exact']);

return !$reachedEnd;
return $hasMore;
}

public function isCurrentUser(ICloudId $cloud): bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCP\Federation\ICloudIdManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Mail\IEmailValidator;
use OCP\Share\IShare;
Expand All @@ -30,6 +31,7 @@ public function __construct(
KnownUserService $knownUserService,
IUserSession $userSession,
IEmailValidator $emailValidator,
IUserManager $userManager,
mixed $shareWithGroupOnlyExcludeGroupsList = [],
) {
parent::__construct(
Expand All @@ -40,6 +42,7 @@ public function __construct(
$knownUserService,
$userSession,
$emailValidator,
$userManager,
$shareWithGroupOnlyExcludeGroupsList,
IShare::TYPE_USER,
);
Expand Down
Loading
Loading