summaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/lib
diff options
context:
space:
mode:
authorMarc Hefter <marchefter@march42.net>2023-02-26 20:15:01 +0100
committerMarc Hefter <marchefter@gmail.com>2023-04-06 08:20:28 +0200
commitf812b8757689edf7a3c1f2751bbd106196e9fafb (patch)
tree202d3193a9e6f8b103325dbbffee51189c93f0f1 /apps/user_ldap/lib
parent5c4a05cfd68bb0397cb033f7c5d957ed6f9eabd0 (diff)
downloadnextcloud-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>
Diffstat (limited to 'apps/user_ldap/lib')
-rw-r--r--apps/user_ldap/lib/Configuration.php3
-rw-r--r--apps/user_ldap/lib/Connection.php1
-rw-r--r--apps/user_ldap/lib/User/Manager.php1
-rw-r--r--apps/user_ldap/lib/User/User.php126
4 files changed, 65 insertions, 66 deletions
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;
}
}