2016-11-23 15:19:06 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-11-23 15:19:06 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-11-23 15:19:06 -05:00
|
|
|
*/
|
|
|
|
|
namespace OC\Accounts;
|
|
|
|
|
|
2020-12-01 08:33:22 -05:00
|
|
|
use OCP\Accounts\IAccountManager;
|
2021-06-15 13:32:39 -04:00
|
|
|
use OCP\Accounts\PropertyDoesNotExistException;
|
|
|
|
|
use OCP\EventDispatcher\Event;
|
|
|
|
|
use OCP\EventDispatcher\IEventListener;
|
2016-11-23 15:19:06 -05:00
|
|
|
use OCP\IUser;
|
2021-06-15 13:32:39 -04:00
|
|
|
use OCP\User\Events\UserChangedEvent;
|
2020-10-13 10:33:53 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-11-23 15:19:06 -05:00
|
|
|
|
2022-04-19 07:05:38 -04:00
|
|
|
/**
|
|
|
|
|
* @template-implements IEventListener<UserChangedEvent>
|
|
|
|
|
*/
|
2021-06-15 13:32:39 -04:00
|
|
|
class Hooks implements IEventListener {
|
2023-06-24 05:16:40 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private LoggerInterface $logger,
|
|
|
|
|
private IAccountManager $accountManager,
|
|
|
|
|
) {
|
2016-11-23 17:57:20 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 15:19:06 -05:00
|
|
|
/**
|
|
|
|
|
* update accounts table if email address or display name was changed from outside
|
|
|
|
|
*/
|
2021-06-15 13:32:39 -04:00
|
|
|
public function changeUserHook(IUser $user, string $feature, $newValue): void {
|
|
|
|
|
$account = $this->accountManager->getAccount($user);
|
2016-11-23 17:57:20 -05:00
|
|
|
|
2021-06-15 13:32:39 -04:00
|
|
|
try {
|
|
|
|
|
switch ($feature) {
|
|
|
|
|
case 'eMailAddress':
|
|
|
|
|
$property = $account->getProperty(IAccountManager::PROPERTY_EMAIL);
|
|
|
|
|
break;
|
|
|
|
|
case 'displayName':
|
|
|
|
|
$property = $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} catch (PropertyDoesNotExistException $e) {
|
|
|
|
|
$this->logger->debug($e->getMessage(), ['exception' => $e]);
|
2016-11-23 17:57:20 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 13:32:39 -04:00
|
|
|
if (isset($property) && $property->getValue() !== (string)$newValue) {
|
|
|
|
|
$property->setValue($newValue);
|
|
|
|
|
$this->accountManager->updateAccount($account);
|
2016-11-23 15:19:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 13:32:39 -04:00
|
|
|
public function handle(Event $event): void {
|
2021-06-22 06:59:34 -04:00
|
|
|
if (!$event instanceof UserChangedEvent) {
|
2021-06-15 13:32:39 -04:00
|
|
|
return;
|
2016-11-23 15:19:06 -05:00
|
|
|
}
|
2021-06-15 13:32:39 -04:00
|
|
|
$this->changeUserHook($event->getUser(), $event->getFeature(), $event->getValue());
|
2016-11-23 15:19:06 -05:00
|
|
|
}
|
|
|
|
|
}
|