Skip to content

Commit b80f83b

Browse files
authored
Merge pull request #12141 from nextcloud/backport/12054/stable14
[stable14] LDAP: announce display name changes so that addressbook picks it up
2 parents 500b5d8 + bdb8e37 commit b80f83b

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

apps/user_ldap/lib/User/User.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,23 @@ private function store($key, $value) {
414414
*
415415
* @param string $displayName
416416
* @param string $displayName2
417-
* @returns string the effective display name
417+
* @return string the effective display name
418418
*/
419419
public function composeAndStoreDisplayName($displayName, $displayName2 = '') {
420420
$displayName2 = (string)$displayName2;
421421
if($displayName2 !== '') {
422422
$displayName .= ' (' . $displayName2 . ')';
423423
}
424-
$this->store('displayName', $displayName);
424+
$oldName = $this->config->getUserValue($this->uid, 'user_ldap', 'displayName', null);
425+
if ($oldName !== $displayName) {
426+
$this->store('displayName', $displayName);
427+
$user = $this->userManager->get($this->getUsername());
428+
if (!empty($oldName) && $user instanceof \OC\User\User) {
429+
// if it was empty, it would be a new record, not a change emitting the trigger could
430+
// potentially cause a UniqueConstraintViolationException, depending on some factors.
431+
$user->triggerChange('displayName', $displayName);
432+
}
433+
}
425434
return $displayName;
426435
}
427436

apps/user_ldap/tests/User/UserTest.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,23 +998,58 @@ public function testGetHomePathConfiguredNotAvailableNotAllowed() {
998998

999999
public function displayNameProvider() {
10001000
return [
1001-
['Roland Deschain', '', 'Roland Deschain'],
1002-
['Roland Deschain', null, 'Roland Deschain'],
1003-
['Roland Deschain', '[email protected]', 'Roland Deschain ([email protected])'],
1001+
['Roland Deschain', '', 'Roland Deschain', false],
1002+
['Roland Deschain', '', 'Roland Deschain', true],
1003+
['Roland Deschain', null, 'Roland Deschain', false],
1004+
['Roland Deschain', '[email protected]', 'Roland Deschain ([email protected])', false],
1005+
['Roland Deschain', '[email protected]', 'Roland Deschain ([email protected])', true],
10041006
];
10051007
}
10061008

10071009
/**
10081010
* @dataProvider displayNameProvider
10091011
*/
1010-
public function testComposeAndStoreDisplayName($part1, $part2, $expected) {
1012+
public function testComposeAndStoreDisplayName($part1, $part2, $expected, $expectTriggerChange) {
10111013
$this->config->expects($this->once())
10121014
->method('setUserValue');
1015+
$oldName = $expectTriggerChange ? 'xxGunslingerxx' : null;
1016+
$this->config->expects($this->once())
1017+
->method('getUserValue')
1018+
->with($this->user->getUsername(), 'user_ldap', 'displayName', null)
1019+
->willReturn($oldName);
1020+
1021+
$ncUserObj = $this->createMock(\OC\User\User::class);
1022+
if ($expectTriggerChange) {
1023+
$ncUserObj->expects($this->once())
1024+
->method('triggerChange')
1025+
->with('displayName', $expected);
1026+
} else {
1027+
$ncUserObj->expects($this->never())
1028+
->method('triggerChange');
1029+
}
1030+
$this->userManager->expects($this->once())
1031+
->method('get')
1032+
->willReturn($ncUserObj);
10131033

10141034
$displayName = $this->user->composeAndStoreDisplayName($part1, $part2);
10151035
$this->assertSame($expected, $displayName);
10161036
}
10171037

1038+
public function testComposeAndStoreDisplayNameNoOverwrite() {
1039+
$displayName = 'Randall Flagg';
1040+
$this->config->expects($this->never())
1041+
->method('setUserValue');
1042+
$this->config->expects($this->once())
1043+
->method('getUserValue')
1044+
->willReturn($displayName);
1045+
1046+
$this->userManager->expects($this->never())
1047+
->method('get'); // Implicit: no triggerChange can be called
1048+
1049+
$composedDisplayName = $this->user->composeAndStoreDisplayName($displayName);
1050+
$this->assertSame($composedDisplayName, $displayName);
1051+
}
1052+
10181053
public function testHandlePasswordExpiryWarningDefaultPolicy() {
10191054
$this->connection->expects($this->any())
10201055
->method('__get')

0 commit comments

Comments
 (0)