Hooks.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /** @var IAccountManager */
  38. private $accountManager;
  39. /** @var LoggerInterface */
  40. private $logger;
  41. public function __construct(LoggerInterface $logger, IAccountManager $accountManager) {
  42. $this->logger = $logger;
  43. $this->accountManager = $accountManager;
  44. }
  45. /**
  46. * update accounts table if email address or display name was changed from outside
  47. */
  48. public function changeUserHook(IUser $user, string $feature, $newValue): void {
  49. $account = $this->accountManager->getAccount($user);
  50. try {
  51. switch ($feature) {
  52. case 'eMailAddress':
  53. $property = $account->getProperty(IAccountManager::PROPERTY_EMAIL);
  54. break;
  55. case 'displayName':
  56. $property = $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
  57. break;
  58. }
  59. } catch (PropertyDoesNotExistException $e) {
  60. $this->logger->debug($e->getMessage(), ['exception' => $e]);
  61. return;
  62. }
  63. if (isset($property) && $property->getValue() !== (string)$newValue) {
  64. $property->setValue($newValue);
  65. $this->accountManager->updateAccount($account);
  66. }
  67. }
  68. public function handle(Event $event): void {
  69. if (!$event instanceof UserChangedEvent) {
  70. return;
  71. }
  72. $this->changeUserHook($event->getUser(), $event->getFeature(), $event->getValue());
  73. }
  74. }