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.

94 lines
2.6 KiB

3 years ago
  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\ILogger;
  26. use OCP\IUser;
  27. class Hooks {
  28. /** @var AccountManager */
  29. private $accountManager = null;
  30. /** @var ILogger */
  31. private $logger;
  32. /**
  33. * Hooks constructor.
  34. *
  35. * @param ILogger $logger
  36. */
  37. public function __construct(ILogger $logger) {
  38. $this->logger = $logger;
  39. }
  40. /**
  41. * update accounts table if email address or display name was changed from outside
  42. *
  43. * @param array $params
  44. */
  45. public function changeUserHook($params) {
  46. $accountManager = $this->getAccountManager();
  47. /** @var IUser $user */
  48. $user = isset($params['user']) ? $params['user'] : null;
  49. $feature = isset($params['feature']) ? $params['feature'] : null;
  50. $newValue = isset($params['value']) ? $params['value'] : null;
  51. if (is_null($user) || is_null($feature) || is_null($newValue)) {
  52. $this->logger->warning('Missing expected parameters in change user hook');
  53. return;
  54. }
  55. $accountData = $accountManager->getUser($user);
  56. switch ($feature) {
  57. case 'eMailAddress':
  58. if ($accountData[AccountManager::PROPERTY_EMAIL]['value'] !== $newValue) {
  59. $accountData[AccountManager::PROPERTY_EMAIL]['value'] = $newValue;
  60. $accountManager->updateUser($user, $accountData);
  61. }
  62. break;
  63. case 'displayName':
  64. if ($accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $newValue) {
  65. $accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $newValue;
  66. $accountManager->updateUser($user, $accountData);
  67. }
  68. break;
  69. }
  70. }
  71. /**
  72. * return instance of accountManager
  73. *
  74. * @return AccountManager
  75. */
  76. protected function getAccountManager() {
  77. if ($this->accountManager === null) {
  78. $this->accountManager = \OC::$server->query(AccountManager::class);
  79. }
  80. return $this->accountManager;
  81. }
  82. }