Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] make backend user realName configurable #52

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 string-able gitlab user attribute possible. Enclose in %. Example: "%name% - %id%". Optional groups are enclosed within braces. Example: "%name% - %id% (%work_information%)"
backendUserRealNameFormat = %name%