diff options
author | Marc Hefter <marchefter@march42.net> | 2023-02-26 20:15:01 +0100 |
---|---|---|
committer | Marc Hefter <marchefter@gmail.com> | 2023-04-06 08:20:28 +0200 |
commit | f812b8757689edf7a3c1f2751bbd106196e9fafb (patch) | |
tree | 202d3193a9e6f8b103325dbbffee51189c93f0f1 | |
parent | 5c4a05cfd68bb0397cb033f7c5d957ed6f9eabd0 (diff) | |
download | nextcloud-server-f812b8757689edf7a3c1f2751bbd106196e9fafb.tar.gz nextcloud-server-f812b8757689edf7a3c1f2751bbd106196e9fafb.zip |
handling updateProfile with array of values
using an array to buffer profile updates, like suggested by @come-nc
clean some code and remove unneccessary redundancy
added the Fediverse profile property
Co-Authored-By: Côme Chilliet <91878298+come-nc@users.noreply.github.com>
Signed-off-by: Marc Hefter <marchefter@gmail.com>
-rw-r--r-- | apps/user_ldap/js/wizard/wizardTabAdvanced.js | 13 | ||||
-rw-r--r-- | apps/user_ldap/lib/Configuration.php | 3 | ||||
-rw-r--r-- | apps/user_ldap/lib/Connection.php | 1 | ||||
-rw-r--r-- | apps/user_ldap/lib/User/Manager.php | 1 | ||||
-rw-r--r-- | apps/user_ldap/lib/User/User.php | 126 | ||||
-rw-r--r-- | apps/user_ldap/templates/settings.php | 1 |
6 files changed, 79 insertions, 66 deletions
diff --git a/apps/user_ldap/js/wizard/wizardTabAdvanced.js b/apps/user_ldap/js/wizard/wizardTabAdvanced.js index 031f2bf2a9d..170625ce896 100644 --- a/apps/user_ldap/js/wizard/wizardTabAdvanced.js +++ b/apps/user_ldap/js/wizard/wizardTabAdvanced.js @@ -159,6 +159,10 @@ OCA = OCA || {}; $element: $('#ldap_attr_biography'), setMethod: 'setBiographyAttribute' }, + ldap_attr_fediverse: { + $element: $('#ldap_attr_fediverse'), + setMethod: 'setFediverseAttribute' + }, ldap_profile_scope: { $element: $('#ldap_profile_scope'), setMethod: 'setProfileScope' @@ -477,6 +481,15 @@ OCA = OCA || {}; }, /** + * sets the attribute for the Nextcloud user profile fediverse + * + * @param {string} attribute + */ + setFediverseAttribute: function(attribute) { + this.setElementValue(this.managedItems.ldap_attr_fediverse.$element, attribute); + }, + + /** * sets the visibility scope for the Nextcloud user profile properties * * @param {string} scope diff --git a/apps/user_ldap/lib/Configuration.php b/apps/user_ldap/lib/Configuration.php index 2b42dd9992b..1eb6c7986e5 100644 --- a/apps/user_ldap/lib/Configuration.php +++ b/apps/user_ldap/lib/Configuration.php @@ -132,6 +132,7 @@ class Configuration { 'ldapAttributeRole' => null, 'ldapAttributeHeadline' => null, 'ldapAttributeBiography' => null, + 'ldapAttributeFediverse' => null, 'ldapProfileScope' => null, ]; @@ -487,6 +488,7 @@ class Configuration { 'ldap_attr_role' => '', 'ldap_attr_headline' => '', 'ldap_attr_biography' => '', + 'ldap_attr_fediverse' => '', 'ldap_profile_scope' => '', ]; } @@ -562,6 +564,7 @@ class Configuration { 'ldap_attr_role' => 'ldapAttributeRole', 'ldap_attr_headline' => 'ldapAttributeHeadline', 'ldap_attr_biography' => 'ldapAttributeBiography', + 'ldap_attr_fediverse' => 'ldapAttributeFediverse', 'ldap_profile_scope' => 'ldapProfileScope', ]; return $array; diff --git a/apps/user_ldap/lib/Connection.php b/apps/user_ldap/lib/Connection.php index 11aaaec13dd..8bf2904179a 100644 --- a/apps/user_ldap/lib/Connection.php +++ b/apps/user_ldap/lib/Connection.php @@ -81,6 +81,7 @@ use Psr\Log\LoggerInterface; * @property string ldapAttributeRole * @property string ldapAttributeHeadline * @property string ldapAttributeBiography + * @property string ldapAttributeFediverse * @property string ldapProfileScope */ class Connection extends LDAPUtility { diff --git a/apps/user_ldap/lib/User/Manager.php b/apps/user_ldap/lib/User/Manager.php index 8942563a0d5..d356a272f8c 100644 --- a/apps/user_ldap/lib/User/Manager.php +++ b/apps/user_ldap/lib/User/Manager.php @@ -161,6 +161,7 @@ class Manager { $this->access->getConnection()->ldapAttributeRole, $this->access->getConnection()->ldapAttributeHeadline, $this->access->getConnection()->ldapAttributeBiography, + $this->access->getConnection()->ldapAttributeFediverse, ]; $homeRule = (string)$this->access->getConnection()->homeFolderNamingRule; diff --git a/apps/user_ldap/lib/User/User.php b/apps/user_ldap/lib/User/User.php index 4807280dc40..93f7ff5c332 100644 --- a/apps/user_ldap/lib/User/User.php +++ b/apps/user_ldap/lib/User/User.php @@ -111,18 +111,6 @@ class User { public const USER_PREFKEY_FIRSTLOGIN = 'firstLoginAccomplished'; /** - * DB config keys for user profile - */ - public const USER_PREFKEY_PHONE = 'profile_phone'; - public const USER_PREFKEY_WEBSITE = 'profile_website'; - public const USER_PREFKEY_ADDRESS = 'profile_address'; - public const USER_PREFKEY_TWITTER = 'profile_twitter'; - public const USER_PREFKEY_ORGANISATION = 'profile_organisation'; - public const USER_PREFKEY_ROLE = 'profile_role'; - public const USER_PREFKEY_HEADLINE = 'profile_headline'; - public const USER_PREFKEY_BIOGRAPHY = 'profile_biography'; - - /** * @brief constructor, make sure the subclasses call this one! * @param string $username the internal username * @param string $dn the LDAP DN @@ -245,6 +233,12 @@ class User { } unset($attr); + /** + * Additions to User_LDAP, for writing the User Profile + * + * @var string|null $profileScope the configured scope of visibility + * @var array<string, string> $profileValues array of the LDAP data + */ //User profile visibility $profileScope = $this->connection->ldapProfileScope; if (empty($profileScope) || $profileScope === 'unset') { @@ -253,43 +247,54 @@ class User { //User Profile Field - Phone number $attr = strtolower($this->connection->ldapAttributePhone); if (isset($ldapEntry[$attr])) { - $this->updateProfile(self::USER_PREFKEY_PHONE, $ldapEntry[$attr][0], $profileScope); + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_PHONE] = $ldapEntry[$attr][0]; } //User Profile Field - website $attr = strtolower($this->connection->ldapAttributeWebsite); if (isset($ldapEntry[$attr])) { - $this->updateProfile(self::USER_PREFKEY_WEBSITE, $ldapEntry[$attr][0], $profileScope); + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_WEBSITE] = $ldapEntry[$attr][0]; } //User Profile Field - Address $attr = strtolower($this->connection->ldapAttributeAddress); if (isset($ldapEntry[$attr])) { - $this->updateProfile(self::USER_PREFKEY_ADDRESS, $ldapEntry[$attr][0], $profileScope); + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_ADDRESS] = $ldapEntry[$attr][0]; } //User Profile Field - Twitter $attr = strtolower($this->connection->ldapAttributeTwitter); if (isset($ldapEntry[$attr])) { - $this->updateProfile(self::USER_PREFKEY_TWITTER, $ldapEntry[$attr][0], $profileScope); + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_TWITTER] = $ldapEntry[$attr][0]; } //User Profile Field - organisation $attr = strtolower($this->connection->ldapAttributeOrganisation); if (isset($ldapEntry[$attr])) { - $this->updateProfile(self::USER_PREFKEY_ORGANISATION, $ldapEntry[$attr][0], $profileScope); + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_ORGANISATION] = $ldapEntry[$attr][0]; } //User Profile Field - role $attr = strtolower($this->connection->ldapAttributeRole); if (isset($ldapEntry[$attr])) { - $this->updateProfile(self::USER_PREFKEY_ROLE, $ldapEntry[$attr][0], $profileScope); + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_ROLE] = $ldapEntry[$attr][0]; } //User Profile Field - headline $attr = strtolower($this->connection->ldapAttributeHeadline); if (isset($ldapEntry[$attr])) { - $this->updateProfile(self::USER_PREFKEY_HEADLINE, $ldapEntry[$attr][0], $profileScope); + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_HEADLINE] = $ldapEntry[$attr][0]; } //User Profile Field - biography $attr = strtolower($this->connection->ldapAttributeBiography); if (isset($ldapEntry[$attr])) { - $this->updateProfile(self::USER_PREFKEY_BIOGRAPHY, $ldapEntry[$attr][0], $profileScope); + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_BIOGRAPHY] = $ldapEntry[$attr][0]; + } + //User Profile Field - fediverse + $attr = strtolower($this->connection->ldapAttributeFediverse); + if (isset($ldapEntry[$attr])) { + $profileValues[\OCP\Accounts\IAccountManager::PROPERTY_FEDIVERSE] = $ldapEntry[$attr][0]; + } + // Update user profile + if(0 < count($profileValues)) { + $this->updateProfile($profileValues, $profileScope); + unset($profileValues); } + unset($profileScope); unset($attr); //Avatar @@ -574,59 +579,48 @@ class User { } /** - * fetches values from LDAP and stores it as Nextcloud user value - * @param string $valueFromLDAP if known, to save an LDAP read request + * takes values from LDAP and stores it as Nextcloud user profile value + * + * @param array $profileValues associaive array of property keys and values from LDAP + * @param string|null $profileScope the scope of visibility to set + * @var string $property the array key (property name from AccountManager class) + * @var string $valueFromLDAP the value as read from LDAP + * @var string $propertyValue + * @var string $value + * @var string $currentValue */ - private function updateProfile(string $property, $valueFromLDAP, ?string $scope=null): void { - // check for valid property and set corresponding profile property - $profileProperty = 'INVALID'; - if (self::USER_PREFKEY_PHONE == $property) { - $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_PHONE; - } elseif (self::USER_PREFKEY_WEBSITE == $property) { - $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_WEBSITE; - } elseif (self::USER_PREFKEY_ADDRESS == $property) { - $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_ADDRESS; - } elseif (self::USER_PREFKEY_TWITTER == $property) { - $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_TWITTER; - } elseif (self::USER_PREFKEY_ORGANISATION == $property) { - $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_ORGANISATION; - } elseif (self::USER_PREFKEY_ROLE == $property) { - $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_ROLE; - } elseif (self::USER_PREFKEY_HEADLINE == $property) { - $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_HEADLINE; - } elseif (self::USER_PREFKEY_BIOGRAPHY == $property) { - $profileProperty = \OCP\Accounts\IAccountManager::PROPERTY_BIOGRAPHY; - } else { - // FIXME: throw exception for invalid property specified + private function updateProfile(array $profileValues, ?string $profileScope=null): void { + // check if user profile was refreshed before + if ($this->wasRefreshed('profile')) { return; } - // check if this property was refreshed before - if ($this->wasRefreshed($property)) { + // check if parameter array is empty + if(0 == count($profileValues)) { return; } - $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->setProfileProperty($profileProperty,$value,$scope,null); - } - // setScope(IAccountManager::SCOPE_FEDERATED); - // setVerified(IAccountManager::VERIFIED); + // fetch/prepare user + $user = $this->userManager->get($this->uid); + if (is_null($user)) { + return; + } + // loop through the properties and handle them + foreach($profileValues as $property => $valueFromLDAP) { + $this->logger->debug('user profile data ('.$property.') from LDAP '.$this->dn.' ='.((string)$valueFromLDAP), ['app' => 'user_ldap']); + // check and update profile properties + $propertyValue = [$valueFromLDAP]; + if ($propertyValue && isset($propertyValue[0])) { + $value = $propertyValue[0]; + try { + $currentValue = (string)$user->getProfilePropertyValue($property); + if ($currentValue !== $value) { + $user->setProfileProperty($property,$value,$scope,null); + $this->logger->debug('property updated: '.$property.'='.$value.' for user '.$this->getUsername().'', ['app' => 'user_ldap']); + } + } catch (PropertyDoesNotExistException $e) { + $this->logger->error('property does not exist: '.$property.' for user '.$this->getUsername().'', ['app' => 'user_ldap']); + return; } - } catch (PropertyDoesNotExistException $e) { - $this->logger->error('property does not exist: '.$profileProperty.' for user '.$this->getUsername().'', ['app' => 'user_ldap']); - return; } - $this->logger->debug('property updated: '.$profileProperty.'='.$value.' for user '.$this->getUsername().'', ['app' => 'user_ldap']); - return; - } else { - // FIXME: I decided, to leave profile untouched, if attribute gets removed from LDAP - return; } } diff --git a/apps/user_ldap/templates/settings.php b/apps/user_ldap/templates/settings.php index aee48f85ed0..c0ff2b6a866 100644 --- a/apps/user_ldap/templates/settings.php +++ b/apps/user_ldap/templates/settings.php @@ -130,6 +130,7 @@ style('user_ldap', 'settings'); <p><label for="ldap_attr_role"> <?php p($l->t('Role Field')); ?></label><input type="text" id="ldap_attr_role" name="ldap_attr_role" title="<?php p($l->t('User profile Role will be set from the specified attribute')); ?>" data-default="<?php p($_['ldap_attr_role_default']); ?>"></p> <p><label for="ldap_attr_headline"> <?php p($l->t('Headline Field')); ?></label><input type="text" id="ldap_attr_headline" name="ldap_attr_headline" title="<?php p($l->t('User profile Headline will be set from the specified attribute')); ?>" data-default="<?php p($_['ldap_attr_headline_default']); ?>"></p> <p><label for="ldap_attr_biography"> <?php p($l->t('Biography Field')); ?></label><input type="text" id="ldap_attr_biography" name="ldap_attr_biography" title="<?php p($l->t('User profile Biography will be set from the specified attribute')); ?>" data-default="<?php p($_['ldap_attr_biography_default']); ?>"></p> + <p><label for="ldap_attr_fediverse"> <?php p($l->t('Fediverse Field')); ?></label><input type="text" id="ldap_attr_fediverse" name="ldap_attr_fediverse" title="<?php p($l->t('User profile Fediverse will be set from the specified attribute')); ?>" data-default="<?php p($_['ldap_attr_fediverse_default']); ?>"></p> <p><label for="ldap_profile_scope"><?php p($l->t('Profile Scope'));?></label><select id="ldap_profile_scope" name="ldap_profile_scope" data-default="<?php p($_['ldap_profile_scope_default']); ?>" ><option value="unset"<?php if (! isset($_['ldap_profile_scope']) || ($_['ldap_profile_scope'] === 'unset')) p(' selected'); ?>>must be set by user</option><option value="v2-private"<?php if (isset($_['ldap_profile_scope']) && ($_['ldap_profile_scope'] === 'v2-private')) p(' selected'); ?>>private</option><option value="v2-local"<?php if (isset($_['ldap_profile_scope']) && ($_['ldap_profile_scope'] === 'v2-local')) p(' selected'); ?>>local</option><option value="v2-federated"<?php if (isset($_['ldap_profile_scope']) && ($_['ldap_profile_scope'] === 'v2-federated')) p(' selected'); ?>>federated</option><option value="v2-published"<?php if (isset($_['ldap_profile_scope']) && ($_['ldap_profile_scope'] === 'v2-published')) p(' selected'); ?>>published</option></select></p> </div> </div> |