Skip to content

Commit 4c22a9f

Browse files
committed
feat(provisioning): add locale mapping
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 956a873 commit 4c22a9f

6 files changed

Lines changed: 38 additions & 0 deletions

File tree

lib/Command/UpsertProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class UpsertProvider extends Base {
6363
'shortcut' => null, 'mode' => InputOption::VALUE_REQUIRED, 'setting_key' => ProviderService::SETTING_MAPPING_LANGUAGE,
6464
'description' => 'Attribute mapping of the account language',
6565
],
66+
'mapping-locale' => [
67+
'shortcut' => null, 'mode' => InputOption::VALUE_REQUIRED, 'setting_key' => ProviderService::SETTING_MAPPING_LOCALE,
68+
'description' => 'Attribute mapping of the account locale',
69+
],
6670
'mapping-website' => [
6771
'shortcut' => null, 'mode' => InputOption::VALUE_REQUIRED, 'setting_key' => ProviderService::SETTING_MAPPING_WEBSITE,
6872
'description' => 'Attribute mapping of the website',

lib/Service/ProviderService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ProviderService {
2828
public const SETTING_MAPPING_QUOTA = 'mappingQuota';
2929
public const SETTING_MAPPING_GROUPS = 'mappingGroups';
3030
public const SETTING_MAPPING_LANGUAGE = 'mappingLanguage';
31+
public const SETTING_MAPPING_LOCALE = 'mappingLocale';
3132
public const SETTING_MAPPING_ADDRESS = 'mappingAddress';
3233
public const SETTING_MAPPING_STREETADDRESS = 'mappingStreetaddress';
3334
public const SETTING_MAPPING_POSTALCODE = 'mappingPostalcode';
@@ -147,6 +148,7 @@ private function getSupportedSettings(): array {
147148
self::SETTING_MAPPING_UID,
148149
self::SETTING_MAPPING_GROUPS,
149150
self::SETTING_MAPPING_LANGUAGE,
151+
self::SETTING_MAPPING_LOCALE,
150152
self::SETTING_MAPPING_ADDRESS,
151153
self::SETTING_MAPPING_STREETADDRESS,
152154
self::SETTING_MAPPING_POSTALCODE,

lib/Service/ProvisioningService.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo
141141
$languageAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_LANGUAGE, 'language');
142142
$language = $this->getClaimValue($idTokenPayload, $languageAttribute, $providerId);//$idTokenPayload->{$languageAttribute} ?? null;
143143

144+
$localeAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_LOCALE, 'locale');
145+
$locale = $this->getClaimValue($idTokenPayload, $localeAttribute, $providerId);
146+
144147
$genderAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_GENDER, 'gender');
145148
$gender = $this->getClaimValue($idTokenPayload, $genderAttribute, $providerId);//$idTokenPayload->{$genderAttribute} ?? null;
146149

@@ -303,6 +306,22 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo
303306
}
304307
}
305308

309+
$event = new AttributeMappedEvent(ProviderService::SETTING_MAPPING_LOCALE, $idTokenPayload, $locale);
310+
$this->eventDispatcher->dispatchTyped($event);
311+
$this->logger->debug('Locale mapping event dispatched');
312+
if ($event->hasValue()) {
313+
$locale = $event->getValue();
314+
$locales = $this->l10nFactory->findAvailableLocales();
315+
$localeCodes = array_map(static function ($l) {
316+
return $l['code'];
317+
}, $locales);
318+
if (in_array($locale, $localeCodes, true) || $locale === 'en') {
319+
$this->config->setUserValue($user->getUID(), 'core', 'locale', $locale);
320+
} else {
321+
$this->logger->debug('Invalid locale in ID token', ['locale' => $locale]);
322+
}
323+
}
324+
306325
$event = new AttributeMappedEvent(ProviderService::SETTING_MAPPING_LANGUAGE, $idTokenPayload, $language);
307326
$this->eventDispatcher->dispatchTyped($event);
308327
$this->logger->debug('Language mapping event dispatched');

src/components/SettingsForm.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@
164164
type="text"
165165
placeholder="language">
166166
</p>
167+
<p>
168+
<label for="mapping-locale">{{ t('user_oidc', 'Locale mapping') }}</label>
169+
<input id="mapping-locale"
170+
v-model="localProvider.settings.mappingLocale"
171+
type="text"
172+
placeholder="locale">
173+
</p>
167174
<p>
168175
<label for="mapping-role">{{ t('user_oidc', 'Role/Title mapping') }}</label>
169176
<input id="mapping-role"

tests/unit/Service/ProviderServiceTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function testGetProvidersWithSettings() {
6868
'mappingUid' => '1',
6969
'mappingGroups' => '1',
7070
'mappingLanguage' => '1',
71+
'mappingLocale' => '1',
7172
'mappingAddress' => '1',
7273
'mappingStreetaddress' => '1',
7374
'mappingPostalcode' => '1',
@@ -113,6 +114,7 @@ public function testGetProvidersWithSettings() {
113114
'mappingUid' => '1',
114115
'mappingGroups' => '1',
115116
'mappingLanguage' => '1',
117+
'mappingLocale' => '1',
116118
'mappingAddress' => '1',
117119
'mappingStreetaddress' => '1',
118120
'mappingPostalcode' => '1',
@@ -161,6 +163,7 @@ public function testSetSettings() {
161163
'providerBasedId' => false,
162164
'groupProvisioning' => true,
163165
'mappingLanguage' => 'language',
166+
'mappingLocale' => 'locale',
164167
'mappingAddress' => 'address',
165168
'mappingStreetaddress' => 'street_address',
166169
'mappingPostalcode' => 'postal_code',
@@ -192,6 +195,7 @@ public function testSetSettings() {
192195
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_UID, '', 'uid'],
193196
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_GROUPS, '', 'groups'],
194197
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_LANGUAGE, '', 'language'],
198+
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_LOCALE, '', 'locale'],
195199
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_ADDRESS, '', 'address'],
196200
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_STREETADDRESS, '', 'street_address'],
197201
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_POSTALCODE, '', 'postal_code'],

tests/unit/Service/ProvisioningServiceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public function testProvisionUserAutoProvisioning(): void {
127127
[$providerId, ProviderService::SETTING_MAPPING_QUOTA, 'quota', 'quota'],
128128
[$providerId, ProviderService::SETTING_GROUP_PROVISIONING, '0', '0'],
129129
[$providerId, ProviderService::SETTING_MAPPING_LANGUAGE, 'language', 'language'],
130+
[$providerId, ProviderService::SETTING_MAPPING_LOCALE, 'locale', 'locale'],
130131
[$providerId, ProviderService::SETTING_MAPPING_ADDRESS, 'address', 'address'],
131132
[$providerId, ProviderService::SETTING_MAPPING_STREETADDRESS, 'street_address', 'street_address'],
132133
[$providerId, ProviderService::SETTING_MAPPING_POSTALCODE, 'postal_code', 'postal_code'],
@@ -201,6 +202,7 @@ public function testProvisionUserInvalidProperties(): void {
201202
[$providerId, ProviderService::SETTING_MAPPING_QUOTA, 'quota', 'quota'],
202203
[$providerId, ProviderService::SETTING_GROUP_PROVISIONING, '0', '0'],
203204
[$providerId, ProviderService::SETTING_MAPPING_LANGUAGE, 'language', 'language'],
205+
[$providerId, ProviderService::SETTING_MAPPING_LOCALE, 'locale', 'locale'],
204206
[$providerId, ProviderService::SETTING_MAPPING_ADDRESS, 'address', 'address'],
205207
[$providerId, ProviderService::SETTING_MAPPING_STREETADDRESS, 'street_address', 'street_address'],
206208
[$providerId, ProviderService::SETTING_MAPPING_POSTALCODE, 'postal_code', 'postal_code'],

0 commit comments

Comments
 (0)