Hooks.php 2.7 KB

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