Skip to content

Commit

Permalink
Merge pull request #31963 from nextcloud/feat/use_setting_in_dav_search
Browse files Browse the repository at this point in the history
Use share setting in DAV search
  • Loading branch information
artonge authored May 4, 2022
2 parents 8fecf34 + e8ab298 commit 7ccfddb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
9 changes: 6 additions & 3 deletions apps/dav/lib/Connector/Sabre/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ protected function searchUserPrincipals(array $searchProperties, $test = 'allof'
$limitEnumerationGroup = $this->shareManager->limitEnumerationToGroups();
$limitEnumerationPhone = $this->shareManager->limitEnumerationToPhone();
$allowEnumerationFullMatch = $this->shareManager->allowEnumerationFullMatch();
$ignoreSecondDisplayName = $this->shareManager->ignoreSecondDisplayName();
$matchEmail = $this->shareManager->matchEmail();

// If sharing is restricted to group members only,
// return only members that have groups in common
Expand Down Expand Up @@ -298,7 +300,7 @@ protected function searchUserPrincipals(array $searchProperties, $test = 'allof'
switch ($prop) {
case '{http://sabredav.org/ns}email-address':
if (!$allowEnumeration) {
if ($allowEnumerationFullMatch) {
if ($allowEnumerationFullMatch && $matchEmail) {
$users = $this->userManager->getByEmail($value);
} else {
$users = [];
Expand Down Expand Up @@ -349,8 +351,9 @@ protected function searchUserPrincipals(array $searchProperties, $test = 'allof'
if ($allowEnumerationFullMatch) {
$lowerSearch = strtolower($value);
$users = $this->userManager->searchDisplayName($value, $searchLimit);
$users = \array_filter($users, static function (IUser $user) use ($lowerSearch) {
return strtolower($user->getDisplayName()) === $lowerSearch;
$users = \array_filter($users, static function (IUser $user) use ($lowerSearch, $ignoreSecondDisplayName) {
$lowerDisplayName = strtolower($user->getDisplayName());
return $lowerDisplayName === $lowerSearch || ($ignoreSecondDisplayName && trim(preg_replace('/ \(.*\)$/', '', $lowerDisplayName)) === $lowerSearch);
});
} else {
$users = [];
Expand Down
4 changes: 4 additions & 0 deletions apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,10 @@ public function testSearchPrincipalWithEnumerationDisabledEmail(): void {
->method('allowEnumerationFullMatch')
->willReturn(true);

$this->shareManager->expects($this->once())
->method('matchEmail')
->willReturn(true);

$user2 = $this->createMock(IUser::class);
$user2->method('getUID')->willReturn('user2');
$user2->method('getDisplayName')->willReturn('User 2');
Expand Down
8 changes: 8 additions & 0 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,14 @@ public function allowEnumerationFullMatch(): bool {
return $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes') === 'yes';
}

public function matchEmail(): bool {
return $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes') === 'yes';
}

public function ignoreSecondDisplayName(): bool {
return $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_display_name', 'no') === 'yes';
}

public function currentUserCanEnumerateTargetUser(?IUser $currentUser, IUser $targetUser): bool {
if ($this->allowEnumerationFullMatch()) {
return true;
Expand Down
16 changes: 16 additions & 0 deletions lib/public/Share/IManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,22 @@ public function limitEnumerationToPhone(): bool;
*/
public function allowEnumerationFullMatch(): bool;

/**
* Check if the search should match the email
*
* @return bool
* @since 25.0.0
*/
public function matchEmail(): bool;

/**
* Check if the search should ignore the second in parentheses display name if there is any
*
* @return bool
* @since 25.0.0
*/
public function ignoreSecondDisplayName(): bool;

/**
* Check if the current user can enumerate the target user
*
Expand Down

0 comments on commit 7ccfddb

Please sign in to comment.