瀏覽代碼

allow user principal search for dav displayname property

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
tags/v15.0.0beta1
Georg Ehrke 5 年之前
父節點
當前提交
218253db76
沒有連結到貢獻者的電子郵件帳戶。
共有 2 個檔案被更改,包括 69 行新增15 行删除
  1. 22
    2
      apps/dav/lib/Connector/Sabre/Principal.php
  2. 47
    13
      apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php

+ 22
- 2
apps/dav/lib/Connector/Sabre/Principal.php 查看文件

@@ -1,6 +1,7 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @copyright Copyright (c) 2018, Georg Ehrke
*
* @author Bart Visscher <bartv@thisnet.nl>
* @author Jakob Sack <mail@jakobsack.de>
@@ -11,6 +12,7 @@
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
* @author Vincent Petry <pvince81@owncloud.com>
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @license AGPL-3.0
*
@@ -249,6 +251,23 @@ class Principal implements BackendInterface {
}, []);
break;

case '{DAV:}displayname':
$users = $this->userManager->searchDisplayName($value);

$results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) {
// is sharing restricted to groups only?
if ($restrictGroups !== false) {
$userGroups = $this->groupManager->getUserGroupIds($user);
if (count(array_intersect($userGroups, $restrictGroups)) === 0) {
return $carry;
}
}

$carry[] = $this->principalPrefix . '/' . $user->getUID();
return $carry;
}, []);
break;

default:
$results[] = [];
break;
@@ -263,11 +282,11 @@ class Principal implements BackendInterface {

switch ($test) {
case 'anyof':
return array_unique(array_merge(...$results));
return array_values(array_unique(array_merge(...$results)));

case 'allof':
default:
return array_intersect(...$results);
return array_values(array_intersect(...$results));
}
}

@@ -354,6 +373,7 @@ class Principal implements BackendInterface {
$principal = [
'uri' => $this->principalPrefix . '/' . $userId,
'{DAV:}displayname' => is_null($displayName) ? $userId : $displayName,
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
];

$email = $user->getEMailAddress();

+ 47
- 13
apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php 查看文件

@@ -1,12 +1,14 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @copyright Copyright (c) 2018, Georg Ehrke
*
* @author Joas Schilling <coding@schilljs.com>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @license AGPL-3.0
*
@@ -38,16 +40,22 @@ use OCP\IUserManager;
use Test\TestCase;

class PrincipalTest extends TestCase {

/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
private $userManager;

/** @var \OCA\DAV\Connector\Sabre\Principal */
private $connector;

/** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */
private $groupManager;

/** @var IManager | \PHPUnit_Framework_MockObject_MockObject */
private $shareManager;

/** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */
private $userSession;

/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
private $config;

@@ -94,7 +102,7 @@ class PrincipalTest extends TestCase {
$barUser
->expects($this->exactly(1))
->method('getEMailAddress')
->will($this->returnValue('bar@owncloud.org'));
->will($this->returnValue('bar@nextcloud.com'));
$this->userManager
->expects($this->once())
->method('search')
@@ -104,12 +112,14 @@ class PrincipalTest extends TestCase {
$expectedResponse = [
0 => [
'uri' => 'principals/users/foo',
'{DAV:}displayname' => 'Dr. Foo-Bar'
'{DAV:}displayname' => 'Dr. Foo-Bar',
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
],
1 => [
'uri' => 'principals/users/bar',
'{DAV:}displayname' => 'bar',
'{http://sabredav.org/ns}email-address' => 'bar@owncloud.org'
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
'{http://sabredav.org/ns}email-address' => 'bar@nextcloud.com',
]
];
$response = $this->connector->getPrincipalsByPrefix('principals/users');
@@ -141,7 +151,8 @@ class PrincipalTest extends TestCase {

$expectedResponse = [
'uri' => 'principals/users/foo',
'{DAV:}displayname' => 'foo'
'{DAV:}displayname' => 'foo',
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
];
$response = $this->connector->getPrincipalByPath('principals/users/foo');
$this->assertSame($expectedResponse, $response);
@@ -152,7 +163,7 @@ class PrincipalTest extends TestCase {
$fooUser
->expects($this->exactly(1))
->method('getEMailAddress')
->will($this->returnValue('foo@owncloud.org'));
->will($this->returnValue('foo@nextcloud.com'));
$fooUser
->expects($this->exactly(1))
->method('getUID')
@@ -166,7 +177,8 @@ class PrincipalTest extends TestCase {
$expectedResponse = [
'uri' => 'principals/users/foo',
'{DAV:}displayname' => 'foo',
'{http://sabredav.org/ns}email-address' => 'foo@owncloud.org'
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
'{http://sabredav.org/ns}email-address' => 'foo@nextcloud.com',
];
$response = $this->connector->getPrincipalByPath('principals/users/foo');
$this->assertSame($expectedResponse, $response);
@@ -283,7 +295,7 @@ class PrincipalTest extends TestCase {
/**
* @dataProvider searchPrincipalsDataProvider
*/
public function testSearchPrincipals($sharingEnabled, $groupsOnly, $result) {
public function testSearchPrincipals($sharingEnabled, $groupsOnly, $test, $result) {
$this->shareManager->expects($this->once())
->method('shareAPIEnabled')
->will($this->returnValue($sharingEnabled));
@@ -302,7 +314,7 @@ class PrincipalTest extends TestCase {
$this->groupManager->expects($this->at(0))
->method('getUserGroupIds')
->with($user)
->will($this->returnValue(['group1', 'group2']));
->will($this->returnValue(['group1', 'group2', 'group5']));
}
} else {
$this->shareManager->expects($this->never())
@@ -315,15 +327,25 @@ class PrincipalTest extends TestCase {
$user2->method('getUID')->will($this->returnValue('user2'));
$user3 = $this->createMock(IUser::class);
$user3->method('getUID')->will($this->returnValue('user3'));
$user4 = $this->createMock(IUser::class);
$user4->method('getUID')->will($this->returnValue('user4'));

if ($sharingEnabled) {
$this->userManager->expects($this->at(0))
->method('getByEmail')
->with('user')
->with('user@example.com')
->will($this->returnValue([$user2, $user3]));

$this->userManager->expects($this->at(1))
->method('searchDisplayName')
->with('User 12')
->will($this->returnValue([$user3, $user4]));
} else {
$this->userManager->expects($this->never())
->method('getByEmail');

$this->userManager->expects($this->never())
->method('searchDisplayName');
}

if ($sharingEnabled && $groupsOnly) {
@@ -335,18 +357,30 @@ class PrincipalTest extends TestCase {
->method('getUserGroupIds')
->with($user3)
->will($this->returnValue(['group3', 'group4']));
$this->groupManager->expects($this->at(3))
->method('getUserGroupIds')
->with($user3)
->will($this->returnValue(['group3', 'group4']));
$this->groupManager->expects($this->at(4))
->method('getUserGroupIds')
->with($user4)
->will($this->returnValue(['group4', 'group5']));
}


$this->assertEquals($result, $this->connector->searchPrincipals('principals/users',
['{http://sabredav.org/ns}email-address' => 'user']));
['{http://sabredav.org/ns}email-address' => 'user@example.com',
'{DAV:}displayname' => 'User 12'], $test));
}

public function searchPrincipalsDataProvider() {
return [
[true, false, ['principals/users/user2', 'principals/users/user3']],
[true, true, ['principals/users/user2']],
[false, false, []],
[true, false, 'allof', ['principals/users/user3']],
[true, false, 'anyof', ['principals/users/user2', 'principals/users/user3', 'principals/users/user4']],
[true, true, 'allof', []],
[true, true, 'anyof', ['principals/users/user2', 'principals/users/user4']],
[false, false, 'allof', []],
[false, false, 'anyof', []],
];
}


Loading…
取消
儲存