Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement principal property search for calendar user address set property #14315

Merged
merged 1 commit into from
Mar 21, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof')
$stmt->closeCursor();
break;

case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set':
// If you add support for more search properties that qualify as a user-address,
// please also add them to the array below
$results[] = $this->searchPrincipals($this->principalPrefix, [
'{http://sabredav.org/ns}email-address' => $value,
], 'anyof');
break;

default:
$results[] = [];
break;
Expand Down
10 changes: 10 additions & 0 deletions apps/dav/lib/Connector/Sabre/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ protected function searchUserPrincipals(array $searchProperties, $test = 'allof'
}, []);
break;

case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set':
// If you add support for more search properties that qualify as a user-address,
// please also add them to the array below
$results[] = $this->searchUserPrincipals([
// In theory this should also search for principal:principals/users/...
// but that's used internally only anyway and i don't know of any client querying that
'{http://sabredav.org/ns}email-address' => $value,
], 'anyof');
break;

default:
$results[] = [];
break;
Expand Down
7 changes: 7 additions & 0 deletions apps/dav/lib/DAV/GroupPrincipalBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof')
}, []);
break;

case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set':
// If you add support for more search properties that qualify as a user-address,
// please also add them to the array below
$results[] = $this->searchPrincipals(self::PRINCIPAL_PREFIX, [
], 'anyof');
break;

default:
$results[] = [];
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,125 @@ public function dataSearchPrincipals() {
];
}

public function testSearchPrincipalsByCalendarUserAddressSet() {
$user = $this->createMock(IUser::class);
$this->userSession->expects($this->exactly(2))
->method('getUser')
->with()
->will($this->returnValue($user));
$this->groupManager->expects($this->exactly(2))
->method('getUserGroupIds')
->with($user)
->will($this->returnValue(['group1', 'group2']));

$queryBuilder1 = $this->createMock(IQueryBuilder::class);
$stmt1 = $this->createMock(\Doctrine\DBAL\Driver\Statement::class);
$expr1 = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class);

$this->dbConnection->expects($this->at(0))
->method('getQueryBuilder')
->with()
->will($this->returnValue($queryBuilder1));
$this->dbConnection->expects($this->at(1))
->method('escapeLikeParameter')
->with('foo')
->will($this->returnValue('escapedFoo'));

$queryBuilder1->method('expr')
->will($this->returnValue($expr1));

$expr1->method('iLike')
->will($this->returnValueMap([
['email', 'createNamedParameter-1', null, 'ILIKE_CLAUSE_1'],
]));

$queryBuilder1->method('expr')
->will($this->returnValue($expr1));

$queryBuilder1->method('createNamedParameter')
->will($this->returnValueMap([
['%escapedFoo%', \PDO::PARAM_STR, null, 'createNamedParameter-1'],
]));

$queryBuilder1->expects($this->at(0))
->method('select')
->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
->will($this->returnValue($queryBuilder1));
$queryBuilder1->expects($this->at(1))
->method('from')
->with($this->expectedDbTable)
->will($this->returnValue($queryBuilder1));
$queryBuilder1->expects($this->at(4))
->method('where')
->with('ILIKE_CLAUSE_1')
->will($this->returnValue($queryBuilder1));
$queryBuilder1->expects($this->at(5))
->method('execute')
->with()
->will($this->returnValue($stmt1));

$stmt1->expects($this->at(0))
->method('fetch')
->with(\PDO::FETCH_ASSOC)
->will($this->returnValue([
'id' => 0,
'backend_id' => 'db',
'resource_id' => '1',
'email' => '1',
'displayname' => 'Resource 1',
'group_restrictions' => null,
]));
$stmt1->expects($this->at(1))
->method('fetch')
->with(\PDO::FETCH_ASSOC)
->will($this->returnValue([
'id' => 1,
'backend_id' => 'db',
'resource_id' => '2',
'email' => '2',
'displayname' => 'Resource 2',
'group_restrictions' => '',
]));
$stmt1->expects($this->at(2))
->method('fetch')
->with(\PDO::FETCH_ASSOC)
->will($this->returnValue([
'id' => 2,
'backend_id' => 'db',
'resource_id' => '3',
'email' => '3',
'displayname' => 'Resource 3',
'group_restrictions' => '["group3"]',
]));
$stmt1->expects($this->at(3))
->method('fetch')
->with(\PDO::FETCH_ASSOC)
->will($this->returnValue([
'id' => 99,
'backend_id' => 'db',
'resource_id' => '99',
'email' => '99',
'displayname' => 'Resource 99',
'group_restrictions' => '["group1", "group2"]',
]));
$stmt1->expects($this->at(4))
->method('fetch')
->with(\PDO::FETCH_ASSOC)
->will($this->returnValue(null));

$actual = $this->principalBackend->searchPrincipals($this->principalPrefix, [
'{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => 'foo',
]);

$this->assertEquals(
str_replace('%prefix%', $this->principalPrefix, [
'%prefix%/db-1',
'%prefix%/db-2',
'%prefix%/db-99',
]),
$actual);
}

public function testSearchPrincipalsEmptySearchProperties() {
$this->userSession->expects($this->never())
->method('getUser');
Expand Down
26 changes: 26 additions & 0 deletions apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,32 @@ public function searchPrincipalsDataProvider() {
];
}

public function testSearchPrincipalByCalendarUserAddressSet() {
$this->shareManager->expects($this->exactly(2))
->method('shareAPIEnabled')
->will($this->returnValue(true));

$this->shareManager->expects($this->exactly(2))
->method('shareWithGroupMembersOnly')
->will($this->returnValue(false));

$user2 = $this->createMock(IUser::class);
$user2->method('getUID')->will($this->returnValue('user2'));
$user3 = $this->createMock(IUser::class);
$user3->method('getUID')->will($this->returnValue('user3'));

$this->userManager->expects($this->at(0))
->method('getByEmail')
->with('[email protected]')
->will($this->returnValue([$user2, $user3]));

$this->assertEquals([
'principals/users/user2',
'principals/users/user3',
], $this->connector->searchPrincipals('principals/users',
['{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => '[email protected]']));
}

public function testFindByUriSharingApiDisabled() {
$this->shareManager->expects($this->once())
->method('shareApiEnabled')
Expand Down