Hooks.php 2.6 KB

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