diff options
author | Marc Hefter <marchefter@march42.net> | 2022-05-17 12:49:38 +0200 |
---|---|---|
committer | Marc Hefter <marchefter@gmail.com> | 2023-04-06 08:20:21 +0200 |
commit | 2315c177261a03a2047f9ba3d11dd96da4fef840 (patch) | |
tree | f53852d74d9232945fc38cb9d191a6f9db0f2171 | |
parent | 7fa3c674de904b140575112f471c5753aa03a89c (diff) | |
download | nextcloud-server-2315c177261a03a2047f9ba3d11dd96da4fef840.tar.gz nextcloud-server-2315c177261a03a2047f9ba3d11dd96da4fef840.zip |
feature addition: [user_ldap] update user profile from LDAP
Signed-off-by: Marc Hefter <marchefter@march42.net>
Signed-off-by: Marc Hefter <marchefter@gmail.com>
-rw-r--r-- | apps/user_ldap/lib/User/User.php | 42 | ||||
-rw-r--r-- | lib/private/User/LazyUser.php | 10 | ||||
-rw-r--r-- | lib/private/User/User.php | 41 | ||||
-rw-r--r-- | lib/public/IUser.php | 23 |
4 files changed, 96 insertions, 20 deletions
diff --git a/apps/user_ldap/lib/User/User.php b/apps/user_ldap/lib/User/User.php index e9437d61ab3..043f3b2d273 100644 --- a/apps/user_ldap/lib/User/User.php +++ b/apps/user_ldap/lib/User/User.php @@ -36,9 +36,12 @@ use OCA\User_LDAP\Access; use OCA\User_LDAP\Connection; use OCA\User_LDAP\Exceptions\AttributeNotSet; use OCA\User_LDAP\FilesystemHelper; +use OC\Accounts\AccountManager; use OCP\Accounts\IAccountManager; +use OCP\Accounts\PropertyDoesNotExistException; use OCP\IAvatarManager; use OCP\IConfig; +use OCP\IDBConnection; use OCP\ILogger; use OCP\Image; use OCP\IUser; @@ -575,28 +578,12 @@ class User { return $quotaValue === 'none' || $quotaValue === 'default' || \OC_Helper::computerFileSize($quotaValue) !== false; } -/* user profile settings and LDAP attributes - * *** - * interface IAccountManager - * public const PROPERTY_PHONE = 'phone'; - * public const PROPERTY_EMAIL = 'email'; - * public const PROPERTY_WEBSITE = 'website'; - * public const PROPERTY_ADDRESS = 'address'; - * public const PROPERTY_TWITTER = 'twitter'; - * public const PROPERTY_ORGANISATION = 'organisation'; - * public const PROPERTY_ROLE = 'role'; - * public const PROPERTY_HEADLINE = 'headline'; - * public const PROPERTY_BIOGRAPHY = 'biography'; - * public const PROPERTY_PROFILE_ENABLED = 'profile_enabled'; - * public function getAccount(IUser $user): IAccount; - * public function updateAccount(IAccount $account): void; - */ /** * fetches values from LDAP and stores it as Nextcloud user value * @param string $valueFromLDAP if known, to save an LDAP read request * @return null */ - public function updateProfile(string $property, $valueFromLDAP = null) { + private function updateProfile(string $property, $valueFromLDAP) { // check for valid property and set corresponding profile property $profileProperty = 'INVALID'; if (self::USER_PREFKEY_PHONE == $property) { @@ -616,10 +603,9 @@ class User { } elseif (self::USER_PREFKEY_BIOGRAPHY == $property) { $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_BIOGRAPHY; } else { - // TODO: throw exception for invalid property specified + // FIXME: throw exception for invalid property specified return; } - $this->logger->info('user profile data from LDAP '.$this->dn.' ('.$profileProperty.')', ['app' => 'user_ldap']); // check if this property was refreshed before if ($this->wasRefreshed($property)) { return; @@ -628,12 +614,28 @@ class User { //$propertyValue = (string)$valueFromLDAP; $propertyValue = [$valueFromLDAP]; } + $this->logger->debug('user profile data ('.$profileProperty.') from LDAP '.$this->dn.' ='.((string)$valueFromLDAP), ['app' => 'user_ldap']); if ($propertyValue && isset($propertyValue[0])) { $value = $propertyValue[0]; + try { + $user = $this->userManager->get($this->uid); + if (!is_null($user)) { + $currentValue = (string)$user->getProfilePropertyValue($profileProperty); + if ($currentValue !== $value) { + $user->setProfilePropertyValue($profileProperty,$value); + } + // setScope(IAccountManager::SCOPE_FEDERATED); + // setVerified(IAccountManager::VERIFIED); + } + } catch (PropertyDoesNotExistException $e) { + $this->logger->error('property does not exist: '.$profileProperty.' for user '.$userName.'', ['app' => 'user_ldap']); + return; + } + $this->logger->debug('property updated: '.$profileProperty.'='.$propertyValue.' for user '.$userName.'', ['app' => 'user_ldap']); $this->config->setUserValue($this->getUsername(), 'user_ldap', $property, $value); - // TODO: update user profile data; call \OCP\Accounts\IAccount::setProperty return $value; } else { + // FIXME: I decided, to leave profile untouched, if attribute gets removed from LDAP $this->config->deleteUserValue($this->getUsername(), 'user_ldap', $property); return ''; } diff --git a/lib/private/User/LazyUser.php b/lib/private/User/LazyUser.php index 096578b8f37..577c937ee51 100644 --- a/lib/private/User/LazyUser.php +++ b/lib/private/User/LazyUser.php @@ -4,6 +4,8 @@ declare(strict_types=1); /** * @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl> * + * @author Marc Hefter <marchefter@march42.net> + * * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify @@ -145,4 +147,12 @@ class LazyUser implements IUser { public function setQuota($quota) { $this->getUser()->setQuota($quota); } + + public function getProfilePropertyValue(string $property): ?string { + return $this->getUser()->getProfilePropertyValue($property); + } + + public function setProfilePropertyValue(string $property, $value) { + $this->getUser()->setProfilePropertyValue($property, $value); + } } diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 2d80dbc7adf..d4f7effcf1d 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -12,6 +12,7 @@ * @author Julius Härtl <jus@bitgrid.net> * @author Leon Klingele <leon@struktur.de> * @author Lukas Reschke <lukas@statuscode.ch> + * @author Marc Hefter <marchefter@march42.net> * @author Morris Jobke <hey@morrisjobke.de> * @author Robin Appelman <robin@icewind.nl> * @author Roeland Jago Douma <roeland@famdouma.nl> @@ -588,4 +589,44 @@ class User implements IUser { $this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]); } } + + /** + * @param string $property name of the AccountProperty + * @return string|null AccountProperty value + * @throws InvalidArgumentException when the property name is invalid or null + */ + public function getProfilePropertyValue($property): ?string { + if ($property === null) { + throw new InvalidArgumentException('Property can not be null.'); + } + // FIXME: check $property if it's one of the IAccountManager::PROPERTY_* public constants + + // FIXME: I need to get the AccountProperty value to return + //return $this->config->getUserValue($this->uid, 'user_ldap', $property, null); + $this->ensureAccountManager(); + $account = $this->accountManager->getAccount($this); + $property = $account->getProperty($property); + return $property->getValue(); + } + + /** + * @param string $property name of the AccountProperty + * @param string $value AccountProperty value + * @return void + * @throws InvalidArgumentException when the property name is invalid or null + */ + public function setProfilePropertyValue($property, $value) { + if ($property === null) { + throw new InvalidArgumentException('Property can not be null.'); + } + // FIXME: check $property if it's one of the IAccountManager::PROPERTY_* public constants + $this->ensureAccountManager(); + $account = $this->accountManager->getAccount($this); + $property = $account->getProperty($property); + $property->setValue($value); + //$property->setScope(IAccountManager::SCOPE_FEDERATED); + //$property->setVerified(IAccountManager::VERIFIED); + $this->accountManager->updateAccount($account); + return; + } } diff --git a/lib/public/IUser.php b/lib/public/IUser.php index 3a7e6ab1f11..fc732b47757 100644 --- a/lib/public/IUser.php +++ b/lib/public/IUser.php @@ -5,6 +5,7 @@ * @author Arthur Schiwon <blizzz@arthur-schiwon.de> * @author John Molakvoæ <skjnldsv@protonmail.com> * @author Lukas Reschke <lukas@statuscode.ch> + * @author Marc Hefter <marchefter@march42.net> * @author Morris Jobke <hey@morrisjobke.de> * @author Robin Appelman <robin@icewind.nl> * @author Roeland Jago Douma <roeland@famdouma.nl> @@ -270,4 +271,26 @@ interface IUser { * @since 9.0.0 */ public function setQuota($quota); + + /** + * get users' profile property value. + * + * @param string $property name see IAccountManager::PROPERTY_* + * @return string AccountProperty value + * @throws InvalidArgumentException when the property name is invalid or null + * @since 25.0.0 + */ + public function getProfilePropertyValue(string $property): ?string; + + /** + * set users' profile property value. + * remove property, if null + * + * @param string $property name from IAccountManager::PROPERTY_* + * @param string $value AccountProperty value + * @return void + * @throws InvalidArgumentException when the property name is invalid or null + * @since 25.0.0 + */ + public function setProfilePropertyValue(string $property, $value); } |