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
3 changes: 1 addition & 2 deletions apps/user_ldap/lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,7 @@ public function applyUserDisplayName(string $uid, string $displayName, string $d
if ($user === null) {
return;
}
$composedDisplayName = $user->composeAndStoreDisplayName($displayName, $displayName2);
$this->cacheUserDisplayName($uid, $composedDisplayName);
$user->composeAndStoreDisplayName($displayName, $displayName2);
}

public function cacheUserDisplayName(string $ocName, string $displayName): void {
Expand Down
25 changes: 19 additions & 6 deletions apps/user_ldap/lib/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ public function processAttributes(array $ldapEntry): void {
$displayName2 = (string)$ldapEntry[$attr][0];
}
if ($displayName !== '') {
$composedDisplayName = $this->composeAndStoreDisplayName($displayName, $displayName2);
$this->access->cacheUserDisplayName($this->getUsername(), $composedDisplayName);
$this->composeAndStoreDisplayName($displayName, $displayName2);
}
unset($attr);

Expand Down Expand Up @@ -446,19 +445,33 @@ public function composeAndStoreDisplayName(string $displayName, string $displayN
if ($displayName2 !== '') {
$displayName .= ' (' . $displayName2 . ')';
}
$hasStoredName = $this->userConfig->hasKey($this->uid, 'user_ldap', 'displayName');
$oldName = $this->userConfig->getValueString($this->uid, 'user_ldap', 'displayName', '');
// Listeners of the change may read the display name right away, e.g. to regenerate the avatar
$this->access->cacheUserDisplayName($this->getUsername(), $displayName);
if ($oldName !== $displayName) {
$this->store('displayName', $displayName);
$user = $this->userManager->get($this->getUsername());
if (!empty($oldName) && $user instanceof \OC\User\User) {
// if it was empty, it would be a new record, not a change emitting the trigger could
// potentially cause a UniqueConstraintViolationException, depending on some factors.
$user->triggerChange('displayName', $displayName, $oldName);
// Without a stored value this is a new record, not a change. Emitting the trigger could
// cause a UniqueConstraintViolationException while the account is being created.
// An empty stored value means the user was known before, displayed with its user id.
if ($hasStoredName && $user instanceof \OC\User\User) {
$user->triggerChange('displayName', $displayName, $oldName !== '' ? $oldName : $user->getUID());
}
}
return $displayName;
}

/**
* Remembers that the user exists without a display name in LDAP, so that
* a display name set later is announced as a change.
*/
public function markDisplayNameMissing(): void {
if (!$this->userConfig->hasKey($this->uid, 'user_ldap', 'displayName')) {
$this->store('displayName', '');
}
}

public function fetchStoredDisplayName(): string {
return $this->userConfig->getValueString($this->uid, 'user_ldap', 'displayName', '');
}
Expand Down
4 changes: 3 additions & 1 deletion apps/user_ldap/lib/User_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,20 +456,22 @@ private function getDisplayNameFromLdap(string $uid): string {
$this->access->username2dn($uid),
$this->access->connection->ldapUserDisplayName);

$user = $this->access->userManager->get($uid);
if ($displayName && (count($displayName) > 0)) {
$displayName = $displayName[0];

if (is_array($displayName2)) {
$displayName2 = count($displayName2) > 0 ? $displayName2[0] : '';
}

$user = $this->access->userManager->get($uid);
if ($user instanceof User) {
return $user->composeAndStoreDisplayName($displayName, (string)$displayName2);
}
if ($user instanceof OfflineUser) {
return $user->getDisplayName();
}
} elseif ($user instanceof User) {
$user->markDisplayNameMissing();
}

return '';
Expand Down
65 changes: 57 additions & 8 deletions apps/user_ldap/tests/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1048,28 +1048,49 @@ public function testGetHomePathConfiguredNotAvailableNotAllowed(): void {

public static function displayNameProvider(): array {
return [
['Roland Deschain', '', 'Roland Deschain', false],
['Roland Deschain', '', 'Roland Deschain', true],
['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', false],
['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', true],
// no stored display name: new record, no change announced
['Roland Deschain', '', 'Roland Deschain', false, '', null],
['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', false, '', null],
// stored display name differs: change announced
['Roland Deschain', '', 'Roland Deschain', true, 'xxGunslingerxx', 'xxGunslingerxx'],
['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', true, 'xxGunslingerxx', 'xxGunslingerxx'],
// user was known without display name: change announced, old value is the user id
['Roland Deschain', '', 'Roland Deschain', true, '', 'alice'],
['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', true, '', 'alice'],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'displayNameProvider')]
public function testComposeAndStoreDisplayName(string $part1, string $part2, string $expected, bool $expectTriggerChange): void {
public function testComposeAndStoreDisplayName(string $part1, string $part2, string $expected, bool $hasStoredName, string $oldName, ?string $expectedOldValue): void {
$this->userConfig->expects($this->once())
->method('setValueString');
$oldName = $expectTriggerChange ? 'xxGunslingerxx' : '';
$this->userConfig->expects($this->once())
->method('hasKey')
->with($this->user->getUsername(), 'user_ldap', 'displayName')
->willReturn($hasStoredName);
$this->userConfig->expects($this->once())
->method('getValueString')
->with($this->user->getUsername(), 'user_ldap', 'displayName', '')
->willReturn($oldName);

$calls = [];
$this->access->expects($this->once())
->method('cacheUserDisplayName')
->with($this->user->getUsername(), $expected)
->willReturnCallback(function () use (&$calls): void {
$calls[] = 'cache';
});

$ncUserObj = $this->createMock(\OC\User\User::class);
if ($expectTriggerChange) {
$ncUserObj->method('getUID')
->willReturn('alice');
if ($expectedOldValue !== null) {
$ncUserObj->expects($this->once())
->method('triggerChange')
->with('displayName', $expected);
->with('displayName', $expected, $expectedOldValue)
->willReturnCallback(function () use (&$calls): void {
$calls[] = 'trigger';
});
} else {
$ncUserObj->expects($this->never())
->method('triggerChange');
Expand All @@ -1080,12 +1101,17 @@ public function testComposeAndStoreDisplayName(string $part1, string $part2, str

$displayName = $this->user->composeAndStoreDisplayName($part1, $part2);
$this->assertSame($expected, $displayName);
// the cache must hold the new name before listeners of the change run
$this->assertSame($expectedOldValue !== null ? ['cache', 'trigger'] : ['cache'], $calls);
}

public function testComposeAndStoreDisplayNameNoOverwrite(): void {
$displayName = 'Randall Flagg';
$this->userConfig->expects($this->never())
->method('setValueString');
$this->access->expects($this->once())
->method('cacheUserDisplayName')
->with($this->user->getUsername(), $displayName);
$this->userConfig->expects($this->once())
->method('getValueString')
->willReturn($displayName);
Expand All @@ -1096,4 +1122,27 @@ public function testComposeAndStoreDisplayNameNoOverwrite(): void {
$composedDisplayName = $this->user->composeAndStoreDisplayName($displayName);
$this->assertSame($composedDisplayName, $displayName);
}

public function testMarkDisplayNameMissing(): void {
$this->userConfig->expects($this->once())
->method('hasKey')
->with($this->user->getUsername(), 'user_ldap', 'displayName')
->willReturn(false);
$this->userConfig->expects($this->once())
->method('setValueString')
->with($this->user->getUsername(), 'user_ldap', 'displayName', '');

$this->user->markDisplayNameMissing();
}

public function testMarkDisplayNameMissingKeepsStoredName(): void {
$this->userConfig->expects($this->once())
->method('hasKey')
->with($this->user->getUsername(), 'user_ldap', 'displayName')
->willReturn(true);
$this->userConfig->expects($this->never())
->method('setValueString');

$this->user->markDisplayNameMissing();
}
}
2 changes: 2 additions & 0 deletions apps/user_ldap/tests/User_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,8 @@ public function testGetDisplayName(): void {
$user2 = $this->createMock(User::class);
$user2->expects($this->never())
->method('composeAndStoreDisplayName');
$user2->expects($this->once())
->method('markDisplayNameMissing');
$user2->expects($this->any())
->method('getDN')
->willReturn('another DN');
Expand Down
5 changes: 4 additions & 1 deletion lib/private/Accounts/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public function changeUserHook(IUser $user, string $feature, $newValue): void {
return;
}

if (isset($property) && $property->getValue() !== (string)$newValue) {
// The account already carries the display name of the user backend, so it
// cannot tell whether the stored one is outdated. updateAccount() compares
// against the stored data and only writes on changes.
if (isset($property) && ($feature === 'displayName' || $property->getValue() !== (string)$newValue)) {
$property->setValue($newValue);
$this->accountManager->updateAccount($account);
}
Expand Down
42 changes: 36 additions & 6 deletions tests/lib/Accounts/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $
$this->accountManager->expects($this->atLeastOnce())->method('getAccount')->willReturn($account);
if ($setEmail) {
$property = $this->createMock(IAccountProperty::class);
$property->expects($this->atLeastOnce())
->method('getValue')
->willReturn($data[IAccountManager::PROPERTY_EMAIL]['value']);
$property->expects($this->atLeastOnce())
->method('setValue')
->with($params['value']);
Expand All @@ -76,9 +73,6 @@ public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $
->with($account);
} elseif ($setDisplayName) {
$property = $this->createMock(IAccountProperty::class);
$property->expects($this->atLeastOnce())
->method('getValue')
->willReturn($data[IAccountManager::PROPERTY_DISPLAYNAME]['value']);
$property->expects($this->atLeastOnce())
->method('setValue')
->with($params['value']);
Expand All @@ -100,6 +94,42 @@ public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $
$this->hooks->changeUserHook($params['user'], $params['feature'], $params['value']);
}

public function testChangeUserHookPersistsDisplayNameAlreadyOnAccount(): void {
$property = $this->createMock(IAccountProperty::class);
$property->expects($this->once())
->method('setValue')
->with('newDisplayName');
$account = $this->createMock(IAccount::class);
$account->method('getProperty')
->with(IAccountManager::PROPERTY_DISPLAYNAME)
->willReturn($property);
$this->accountManager->method('getAccount')
->willReturn($account);
$this->accountManager->expects($this->once())
->method('updateAccount')
->with($account);

$this->hooks->changeUserHook($this->createMock(IUser::class), 'displayName', 'newDisplayName');
}

public function testChangeUserHookSkipsEmailAlreadyOnAccount(): void {
$property = $this->createMock(IAccountProperty::class);
$property->method('getValue')
->willReturn('mail@example.com');
$property->expects($this->never())
->method('setValue');
$account = $this->createMock(IAccount::class);
$account->method('getProperty')
->with(IAccountManager::PROPERTY_EMAIL)
->willReturn($property);
$this->accountManager->method('getAccount')
->willReturn($account);
$this->accountManager->expects($this->never())
->method('updateAccount');

$this->hooks->changeUserHook($this->createMock(IUser::class), 'eMailAddress', 'mail@example.com');
}

public static function dataTestChangeUserHook(): array {
return [
[
Expand Down
Loading