diff options
author | Morris Jobke <hey@morrisjobke.de> | 2016-11-25 11:40:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-25 11:40:20 +0100 |
commit | 64fb0fb3dd02fe9fd5f4543e7248137b7e9fcbcd (patch) | |
tree | 03c70a33e480b312451a72a08cdb286fcfdb2216 /lib | |
parent | 1967b9112c237f8ed825d3fc664edf96e97f9514 (diff) | |
parent | 0de685c5629deb66984e536e5fd20f140228aa3f (diff) | |
download | nextcloud-server-64fb0fb3dd02fe9fd5f4543e7248137b7e9fcbcd.tar.gz nextcloud-server-64fb0fb3dd02fe9fd5f4543e7248137b7e9fcbcd.zip |
Merge pull request #2276 from nextcloud/update-email-address
Update email address
Diffstat (limited to 'lib')
-rw-r--r-- | lib/base.php | 6 | ||||
-rw-r--r-- | lib/private/Accounts/AccountManager.php | 16 | ||||
-rw-r--r-- | lib/private/Accounts/Hooks.php | 98 |
3 files changed, 115 insertions, 5 deletions
diff --git a/lib/base.php b/lib/base.php index 2f5517f4614..00e984d80c5 100644 --- a/lib/base.php +++ b/lib/base.php @@ -766,6 +766,7 @@ class OC { self::registerLogRotate(); self::registerEncryptionWrapper(); self::registerEncryptionHooks(); + self::registerAccountHooks(); self::registerSettingsHooks(); //make sure temporary files are cleaned up @@ -876,6 +877,11 @@ class OC { } } + private static function registerAccountHooks() { + $hookHandler = new \OC\Accounts\Hooks(\OC::$server->getLogger()); + \OCP\Util::connectHook('OC_User', 'changeUser', $hookHandler, 'changeUserHook'); + } + /** * register hooks for the cache */ diff --git a/lib/private/Accounts/AccountManager.php b/lib/private/Accounts/AccountManager.php index 6496a521326..3701421a20f 100644 --- a/lib/private/Accounts/AccountManager.php +++ b/lib/private/Accounts/AccountManager.php @@ -81,16 +81,22 @@ class AccountManager { */ public function updateUser(IUser $user, $data) { $userData = $this->getUser($user); + $updated = true; if (empty($userData)) { $this->insertNewUser($user, $data); - } else { + } elseif ($userData !== $data) { $this->updateExistingUser($user, $data); + } else { + // nothing needs to be done if new and old data set are the same + $updated = false; } - $this->eventDispatcher->dispatch( - 'OC\AccountManager::userUpdated', - new GenericEvent($user) - ); + if ($updated) { + $this->eventDispatcher->dispatch( + 'OC\AccountManager::userUpdated', + new GenericEvent($user) + ); + } } /** diff --git a/lib/private/Accounts/Hooks.php b/lib/private/Accounts/Hooks.php new file mode 100644 index 00000000000..38e7e20485b --- /dev/null +++ b/lib/private/Accounts/Hooks.php @@ -0,0 +1,98 @@ +<?php +/** + * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + + +namespace OC\Accounts; + +use OCP\ILogger; +use OCP\IUser; + +class Hooks { + + /** @var AccountManager */ + private $accountManager = null; + + /** @var ILogger */ + private $logger; + + /** + * Hooks constructor. + * + * @param ILogger $logger + */ + public function __construct(ILogger $logger) { + $this->logger = $logger; + } + + /** + * update accounts table if email address or display name was changed from outside + * + * @param array $params + */ + public function changeUserHook($params) { + + $accountManager = $this->getAccountManager(); + + /** @var IUser $user */ + $user = isset($params['user']) ? $params['user'] : null; + $feature = isset($params['feature']) ? $params['feature'] : null; + $newValue = isset($params['value']) ? $params['value'] : null; + + if (is_null($user) || is_null($feature) || is_null($newValue)) { + $this->logger->warning('Missing expected parameters in change user hook'); + return; + } + + $accountData = $accountManager->getUser($user); + + switch ($feature) { + case 'eMailAddress': + if ($accountData[AccountManager::PROPERTY_EMAIL]['value'] !== $newValue) { + $accountData[AccountManager::PROPERTY_EMAIL]['value'] = $newValue; + $accountManager->updateUser($user, $accountData); + } + break; + case 'displayName': + if ($accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $newValue) { + $accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $newValue; + $accountManager->updateUser($user, $accountData); + } + break; + } + + } + + /** + * return instance of accountManager + * + * @return AccountManager + */ + protected function getAccountManager() { + if (is_null($this->accountManager)) { + $this->accountManager = new AccountManager( + \OC::$server->getDatabaseConnection(), + \OC::$server->getEventDispatcher() + ); + } + return $this->accountManager; + } + +} |