2016-11-23 15:19:06 -05:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
|
|
|
|
|
*
|
2017-11-06 09:56:42 -05:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2019-12-19 07:16:31 -05:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-12-16 08:54:15 -05:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 09:56:42 -05:00
|
|
|
*
|
2016-11-23 15:19:06 -05:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2021-06-04 15:52:51 -04:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-11-23 15:19:06 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
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 {
|
|
|
|
|
/** @var IAccountManager */
|
2020-10-13 10:33:53 -04:00
|
|
|
private $accountManager;
|
|
|
|
|
/** @var LoggerInterface */
|
2016-11-23 17:57:20 -05:00
|
|
|
private $logger;
|
|
|
|
|
|
2021-06-15 13:32:39 -04:00
|
|
|
public function __construct(LoggerInterface $logger, IAccountManager $accountManager) {
|
2016-11-23 17:57:20 -05:00
|
|
|
$this->logger = $logger;
|
2021-06-15 13:32:39 -04:00
|
|
|
$this->accountManager = $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
|
|
|
}
|
|
|
|
|
}
|