Skip to content

Implement ISetDisplayNameBackend in the user backend #1052

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
28 changes: 4 additions & 24 deletions lib/Service/ProvisioningService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use InvalidArgumentException;
use OC\Accounts\AccountManager;
use OCA\UserOIDC\AppInfo\Application;
use OCA\UserOIDC\Db\UserMapper;
use OCA\UserOIDC\Event\AttributeMappedEvent;
use OCP\Accounts\IAccountManager;
Expand Down Expand Up @@ -176,29 +175,10 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo
if ($event->hasValue() && $event->getValue() !== null && $event->getValue() !== '') {
$oidcGssUserData[$displaynameAttribute] = $event->getValue();
$newDisplayName = $event->getValue();
if ($existingLocalUser === null) {
$oldDisplayName = $backendUser->getDisplayName();
if ($newDisplayName !== $oldDisplayName) {
$backendUser->setDisplayName($newDisplayName);
$this->userMapper->update($backendUser);
}
// 2 reasons why we should update the display name: It does not match the one
// - of our backend
// - returned by the user manager (outdated one before the fix in https://github.com/nextcloud/user_oidc/pull/530)
if ($newDisplayName !== $oldDisplayName || $newDisplayName !== $user->getDisplayName()) {
$this->eventDispatcher->dispatchTyped(new UserChangedEvent($user, 'displayName', $newDisplayName, $oldDisplayName));
}
} else {
$oldDisplayName = $user->getDisplayName();
if ($newDisplayName !== $oldDisplayName) {
$user->setDisplayName($newDisplayName);
if ($user->getBackendClassName() === Application::APP_ID) {
$backendUser = $this->userMapper->getOrCreate($providerId, $user->getUID());
$backendUser->setDisplayName($newDisplayName);
$this->userMapper->update($backendUser);
}
$this->eventDispatcher->dispatchTyped(new UserChangedEvent($user, 'displayName', $newDisplayName, $oldDisplayName));
}
$oldDisplayName = $user->getDisplayName();
if ($newDisplayName !== $oldDisplayName) {
$user->setDisplayName($newDisplayName);
$this->eventDispatcher->dispatchTyped(new UserChangedEvent($user, 'displayName', $newDisplayName, $oldDisplayName));
}
}

Expand Down
14 changes: 13 additions & 1 deletion lib/User/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
use OCP\User\Backend\ICustomLogout;
use OCP\User\Backend\IGetDisplayNameBackend;
use OCP\User\Backend\IPasswordConfirmationBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
use Psr\Log\LoggerInterface;

class Backend extends ABackend implements IPasswordConfirmationBackend, IGetDisplayNameBackend, IApacheBackend, ICustomLogout, ICountUsersBackend {
class Backend extends ABackend implements IPasswordConfirmationBackend, IGetDisplayNameBackend, ISetDisplayNameBackend, IApacheBackend, ICustomLogout, ICountUsersBackend {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would mean that users can change the displayname also trough the Nextcloud profile page, no?

Since the IdP has authority over the display name that is used, we should not implement this interface I think.

Copy link
Member

@solracsf solracsf May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm that displayName can be changed trough the Nextcloud profile page when this is implemented (manually patched NC v30.0.10 with App v7.2.0).

private $tokenValidators = [
SelfEncodedValidator::class,
UserInfoValidator::class,
Expand Down Expand Up @@ -91,6 +92,17 @@ public function userExists($uid): bool {
return $this->userMapper->userExists($uid);
}

public function setDisplayName(string $uid, string $displayName): bool {
try {
$user = $this->userMapper->getUser($uid);
$user->setDisplayName($displayName);
$this->userMapper->update($user);
return true;
} catch (DoesNotExistException $e) {
return false;
}
}

public function getDisplayName($uid): string {
try {
$user = $this->userMapper->getUser($uid);
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Service/ProvisioningServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function testProvisionUserAutoProvisioning(): void {
->method('get')
->willReturn($user);

$backendUser->expects(self::once())
$user->expects(self::once())
->method('setDisplayName')
->with($name);
$user->expects(self::once())
Expand Down Expand Up @@ -225,7 +225,7 @@ public function testProvisionUserInvalidProperties(): void {
->method('get')
->willReturn($user);

$backendUser->expects(self::once())
$user->expects(self::once())
->method('setDisplayName')
->with($name);
$user->expects(self::once())
Expand Down Expand Up @@ -257,7 +257,7 @@ public function testProvisionUserInvalidProperties(): void {
->method('getProperty')
->with('twitter')
->willReturn($property);


$this->accountManager->expects(self::once())
->method('getAccount')
Expand Down
Loading