1
0

Hooks.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Accounts;
  26. use OCP\Accounts\IAccountManager;
  27. use OCP\Accounts\PropertyDoesNotExistException;
  28. use OCP\EventDispatcher\Event;
  29. use OCP\EventDispatcher\IEventListener;
  30. use OCP\IUser;
  31. use OCP\User\Events\UserChangedEvent;
  32. use Psr\Log\LoggerInterface;
  33. /**
  34. * @template-implements IEventListener<UserChangedEvent>
  35. */
  36. class Hooks implements IEventListener {
  37. public function __construct(
  38. private LoggerInterface $logger,
  39. private IAccountManager $accountManager,
  40. ) {
  41. }
  42. /**
  43. * update accounts table if email address or display name was changed from outside
  44. */
  45. public function changeUserHook(IUser $user, string $feature, $newValue): void {
  46. $account = $this->accountManager->getAccount($user);
  47. try {
  48. switch ($feature) {
  49. case 'eMailAddress':
  50. $property = $account->getProperty(IAccountManager::PROPERTY_EMAIL);
  51. break;
  52. case 'displayName':
  53. $property = $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
  54. break;
  55. }
  56. } catch (PropertyDoesNotExistException $e) {
  57. $this->logger->debug($e->getMessage(), ['exception' => $e]);
  58. return;
  59. }
  60. if (isset($property) && $property->getValue() !== (string)$newValue) {
  61. $property->setValue($newValue);
  62. $this->accountManager->updateAccount($account);
  63. }
  64. }
  65. public function handle(Event $event): void {
  66. if (!$event instanceof UserChangedEvent) {
  67. return;
  68. }
  69. $this->changeUserHook($event->getUser(), $event->getFeature(), $event->getValue());
  70. }
  71. }