From aa6cf90d971b42d1d65a80c89334c23924d2e252 Mon Sep 17 00:00:00 2001 From: Ingo Fabbri Date: Thu, 1 Feb 2024 20:10:26 +0100 Subject: [PATCH] [FEATURE] make backend user realName configurable resolves #51 --- Classes/ResourceServer/GitLab.php | 1 - Classes/Services/OAuth2LoginService.php | 31 +++++++++++++++++++++---- ext_conf_template.txt | 3 +++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Classes/ResourceServer/GitLab.php b/Classes/ResourceServer/GitLab.php index 2f5bd91..0365f54 100644 --- a/Classes/ResourceServer/GitLab.php +++ b/Classes/ResourceServer/GitLab.php @@ -294,7 +294,6 @@ public function updateUserRecord( $currentRecord, [ 'email' => $userData['email'], - 'realName' => $userData['name'], 'username' => $this->getUsernameFromUser($user), 'usergroup' => $this->getUserGroupsForUser( $this->gitlabDefaultGroups, diff --git a/Classes/Services/OAuth2LoginService.php b/Classes/Services/OAuth2LoginService.php index 165d166..dd8004a 100644 --- a/Classes/Services/OAuth2LoginService.php +++ b/Classes/Services/OAuth2LoginService.php @@ -16,6 +16,7 @@ use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication; use TYPO3\CMS\Core\Authentication\LoginType; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; +use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\QueryBuilder; @@ -26,7 +27,6 @@ use TYPO3\CMS\Core\Security\RequestToken; use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory; class OAuth2LoginService extends AbstractAuthenticationService implements LoggerAwareInterface { @@ -287,7 +287,8 @@ protected function createUser(ResourceOwnerInterface $user): void 'starttime' => 0, 'endtime' => 0, 'oauth_identifier' => $this->resourceServer->getOAuthIdentifier($user), - 'password' => $saltingInstance->getHashedPassword(md5(uniqid())) + 'password' => $saltingInstance->getHashedPassword(md5(uniqid())), + 'realName' => $this->generateRealName($this->extensionConfig['backendUserRealNameFormat'] ?: '', $user->toArray()), ]; $expirationDate = null; //$this->resourceServer->userExpiresAt($user); @@ -316,7 +317,8 @@ protected function updateFoundUser(ResourceOwnerInterface $user, array $record): 'disable' => 0, 'starttime' => 0, 'endtime' => 0, - 'oauth_identifier' => $this->resourceServer->getOAuthIdentifier($user) + 'oauth_identifier' => $this->resourceServer->getOAuthIdentifier($user), + 'realName' => $this->generateRealName($this->extensionConfig['backendUserRealNameFormat'] ?: '', $user->toArray()), ] ); @@ -331,7 +333,7 @@ protected function updateFoundUser(ResourceOwnerInterface $user, array $record): $record = $this->resourceServer->updateUserRecord($user, $record, $this->authInfo); } else { - $record = array_merge($record, [ 'oauth_identifier' => $this->resourceServer->getOAuthIdentifier($user) ]); + $record = array_merge($record, ['oauth_identifier' => $this->resourceServer->getOAuthIdentifier($user)]); } // update user record @@ -371,4 +373,25 @@ protected function getQueryBuilderForTable(string $table): QueryBuilder ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); return $queryBuilder; } + + private function generateRealName(string $format, array $userdata): string + { + $values = []; + + if (preg_match_all('/%(\w+)%/', $format, $matches) === 0) { + return ''; + } + + foreach ($matches[0] as $placeholder) { + $key = trim($placeholder, '%'); + + if (isset($userdata[$key]) && $userdata[$key] !== '') { + $values[$placeholder] = $userdata[$key]; + } else { + $format = str_replace([$placeholder, '()'], '', $format); + } + } + + return str_replace(array_keys($values), $values, $format); + } } diff --git a/ext_conf_template.txt b/ext_conf_template.txt index 44ff543..2fb8d34 100644 --- a/ext_conf_template.txt +++ b/ext_conf_template.txt @@ -3,3 +3,6 @@ enableBackendLogin = 0 # cat=gitlab; type=boolean; label=override existing user overrideUser = 0 + +# cat=gitlab; type=string; label=Backend user "realName" format string. Any gitlab user attribute possible. Enclose in %. Example: %name% (%id%). Optional groups are enclosed within braces. Example: %name% (%id%) {%work_information%} +backendUserRealNameFormat = %name%