You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB

  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Accounts;
  25. use OCP\IUser;
  26. use Psr\Log\LoggerInterface;
  27. class Hooks {
  28. /** @var AccountManager|null */
  29. private $accountManager;
  30. /** @var LoggerInterface */
  31. private $logger;
  32. public function __construct(LoggerInterface $logger) {
  33. $this->logger = $logger;
  34. }
  35. /**
  36. * update accounts table if email address or display name was changed from outside
  37. *
  38. * @param array $params
  39. */
  40. public function changeUserHook($params) {
  41. $accountManager = $this->getAccountManager();
  42. /** @var IUser $user */
  43. $user = isset($params['user']) ? $params['user'] : null;
  44. $feature = isset($params['feature']) ? $params['feature'] : null;
  45. $newValue = isset($params['value']) ? $params['value'] : null;
  46. if (is_null($user) || is_null($feature) || is_null($newValue)) {
  47. $this->logger->warning('Missing expected parameters in change user hook');
  48. return;
  49. }
  50. $accountData = $accountManager->getUser($user);
  51. switch ($feature) {
  52. case 'eMailAddress':
  53. if ($accountData[AccountManager::PROPERTY_EMAIL]['value'] !== $newValue) {
  54. $accountData[AccountManager::PROPERTY_EMAIL]['value'] = $newValue;
  55. $accountManager->updateUser($user, $accountData);
  56. }
  57. break;
  58. case 'displayName':
  59. if ($accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $newValue) {
  60. $accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $newValue;
  61. $accountManager->updateUser($user, $accountData);
  62. }
  63. break;
  64. }
  65. }
  66. /**
  67. * return instance of accountManager
  68. *
  69. * @return AccountManager
  70. */
  71. protected function getAccountManager(): AccountManager {
  72. if ($this->accountManager === null) {
  73. $this->accountManager = \OC::$server->query(AccountManager::class);
  74. }
  75. return $this->accountManager;
  76. }
  77. }