Skip to content

Commit

Permalink
[FEATURE] make backend user realName configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
infabo committed Feb 2, 2024
1 parent 9a4493e commit aa6cf90
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
1 change: 0 additions & 1 deletion Classes/ResourceServer/GitLab.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ public function updateUserRecord(
$currentRecord,
[
'email' => $userData['email'],
'realName' => $userData['name'],
'username' => $this->getUsernameFromUser($user),
'usergroup' => $this->getUserGroupsForUser(
$this->gitlabDefaultGroups,
Expand Down
31 changes: 27 additions & 4 deletions Classes/Services/OAuth2LoginService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()),
]
);

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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%

0 comments on commit aa6cf90

Please sign in to comment.