Skip to content

Commit 740d685

Browse files
authored
Merge pull request #1046 from nextcloud/enh/987/map-language
Map the user language
2 parents d1bdb8f + 4f5989d commit 740d685

6 files changed

Lines changed: 43 additions & 0 deletions

File tree

lib/Command/UpsertProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ class UpsertProvider extends Base {
5959
'shortcut' => null, 'mode' => InputOption::VALUE_REQUIRED, 'setting_key' => ProviderService::SETTING_EXTRA_CLAIMS,
6060
'description' => 'Extra claims to request when getting tokens',
6161
],
62+
'mapping-language' => [
63+
'shortcut' => null, 'mode' => InputOption::VALUE_REQUIRED, 'setting_key' => ProviderService::SETTING_MAPPING_LANGUAGE,
64+
'description' => 'Attribute mapping of the account language',
65+
],
6266
'mapping-website' => [
6367
'shortcut' => null, 'mode' => InputOption::VALUE_REQUIRED, 'setting_key' => ProviderService::SETTING_MAPPING_WEBSITE,
6468
'description' => 'Attribute mapping of the website',

lib/Service/ProviderService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ProviderService {
2626
public const SETTING_MAPPING_EMAIL = 'mappingEmail';
2727
public const SETTING_MAPPING_QUOTA = 'mappingQuota';
2828
public const SETTING_MAPPING_GROUPS = 'mappingGroups';
29+
public const SETTING_MAPPING_LANGUAGE = 'mappingLanguage';
2930
public const SETTING_MAPPING_ADDRESS = 'mappingAddress';
3031
public const SETTING_MAPPING_STREETADDRESS = 'mappingStreetaddress';
3132
public const SETTING_MAPPING_POSTALCODE = 'mappingPostalcode';
@@ -140,6 +141,7 @@ private function getSupportedSettings(): array {
140141
self::SETTING_MAPPING_QUOTA,
141142
self::SETTING_MAPPING_UID,
142143
self::SETTING_MAPPING_GROUPS,
144+
self::SETTING_MAPPING_LANGUAGE,
143145
self::SETTING_MAPPING_ADDRESS,
144146
self::SETTING_MAPPING_STREETADDRESS,
145147
self::SETTING_MAPPING_POSTALCODE,

lib/Service/ProvisioningService.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use OCP\ISession;
2525
use OCP\IUser;
2626
use OCP\IUserManager;
27+
use OCP\L10N\IFactory;
2728
use OCP\User\Events\UserChangedEvent;
2829
use Psr\Log\LoggerInterface;
2930
use Throwable;
@@ -43,6 +44,7 @@ public function __construct(
4344
private IAvatarManager $avatarManager,
4445
private IConfig $config,
4546
private ISession $session,
47+
private IFactory $l10nFactory,
4648
) {
4749
}
4850

@@ -77,6 +79,9 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo
7779
$quotaAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_QUOTA, 'quota');
7880
$quota = $idTokenPayload->{$quotaAttribute} ?? null;
7981

82+
$languageAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_LANGUAGE, 'language');
83+
$language = $idTokenPayload->{$languageAttribute} ?? null;
84+
8085
$genderAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_GENDER, 'gender');
8186
$gender = $idTokenPayload->{$genderAttribute} ?? null;
8287

@@ -236,6 +241,19 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo
236241
$account->setProperty('phone', $event->getValue(), $defaultScopes[IAccountManager::PROPERTY_PHONE] ?? $fallbackScope, '1', '');
237242
}
238243

244+
$event = new AttributeMappedEvent(ProviderService::SETTING_MAPPING_LANGUAGE, $idTokenPayload, $language);
245+
$this->eventDispatcher->dispatchTyped($event);
246+
$this->logger->debug('Language mapping event dispatched');
247+
if ($event->hasValue()) {
248+
$language = $event->getValue();
249+
$languagesCodes = $this->l10nFactory->findAvailableLanguages();
250+
if (in_array($language, $languagesCodes, true) || $language === 'en') {
251+
$this->config->setUserValue($user->getUID(), 'core', 'lang', $language);
252+
} else {
253+
$this->logger->debug('Invalid language in ID token', ['language' => $language]);
254+
}
255+
}
256+
239257
$addressParts = null;
240258
if (is_object($address)) {
241259
// Update the location/address

src/components/SettingsForm.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@
129129
type="text"
130130
placeholder="phone_number">
131131
</p>
132+
<p>
133+
<label for="mapping-language">{{ t('user_oidc', 'Language mapping') }}</label>
134+
<input id="mapping-language"
135+
v-model="localProvider.settings.mappingLanguage"
136+
type="text"
137+
placeholder="language">
138+
</p>
132139
<p>
133140
<label for="mapping-role">{{ t('user_oidc', 'Role/Title mapping') }}</label>
134141
<input id="mapping-role"

tests/unit/Service/ProviderServiceTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function testGetProvidersWithSettings() {
6565
'mappingQuota' => '1',
6666
'mappingUid' => '1',
6767
'mappingGroups' => '1',
68+
'mappingLanguage' => '1',
6869
'mappingAddress' => '1',
6970
'mappingStreetaddress' => '1',
7071
'mappingPostalcode' => '1',
@@ -105,6 +106,7 @@ public function testGetProvidersWithSettings() {
105106
'mappingQuota' => '1',
106107
'mappingUid' => '1',
107108
'mappingGroups' => '1',
109+
'mappingLanguage' => '1',
108110
'mappingAddress' => '1',
109111
'mappingStreetaddress' => '1',
110112
'mappingPostalcode' => '1',
@@ -149,6 +151,7 @@ public function testSetSettings() {
149151
'extraClaims' => 'claim1 claim2',
150152
'providerBasedId' => false,
151153
'groupProvisioning' => true,
154+
'mappingLanguage' => 'language',
152155
'mappingAddress' => 'address',
153156
'mappingStreetaddress' => 'street_address',
154157
'mappingPostalcode' => 'postal_code',
@@ -176,6 +179,7 @@ public function testSetSettings() {
176179
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_QUOTA, '', '1g'],
177180
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_UID, '', 'uid'],
178181
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_GROUPS, '', 'groups'],
182+
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_LANGUAGE, '', 'language'],
179183
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_ADDRESS, '', 'address'],
180184
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_STREETADDRESS, '', 'street_address'],
181185
[Application::APP_ID, 'provider-1-' . ProviderService::SETTING_MAPPING_POSTALCODE, '', 'postal_code'],

tests/unit/Service/ProvisioningServiceTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCP\ISession;
2222
use OCP\IUser;
2323
use OCP\IUserManager;
24+
use OCP\L10N\IFactory;
2425
use PHPUnit\Framework\MockObject\MockObject;
2526
use PHPUnit\Framework\TestCase;
2627
use Psr\Log\LoggerInterface;
@@ -64,6 +65,10 @@ class ProvisioningServiceTest extends TestCase {
6465

6566
/** @var ISession | MockObject */
6667
private $session;
68+
/**
69+
* @var IFactory | MockObject
70+
*/
71+
private $l10nFactory;
6772

6873
public function setUp(): void {
6974
parent::setUp();
@@ -80,6 +85,7 @@ public function setUp(): void {
8085
$this->avatarManager = $this->createMock(IAvatarManager::class);
8186
$this->config = $this->createMock(IConfig::class);
8287
$this->session = $this->createMock(ISession::class);
88+
$this->l10nFactory = $this->createMock(IFactory::class);
8389

8490
$this->provisioningService = new ProvisioningService(
8591
$this->idService,
@@ -94,6 +100,7 @@ public function setUp(): void {
94100
$this->avatarManager,
95101
$this->config,
96102
$this->session,
103+
$this->l10nFactory,
97104
);
98105
}
99106

@@ -119,6 +126,7 @@ public function testProvisionUserAutoProvisioning(): void {
119126
[$providerId, ProviderService::SETTING_MAPPING_DISPLAYNAME, 'name', 'name'],
120127
[$providerId, ProviderService::SETTING_MAPPING_QUOTA, 'quota', 'quota'],
121128
[$providerId, ProviderService::SETTING_GROUP_PROVISIONING, '0', '0'],
129+
[$providerId, ProviderService::SETTING_MAPPING_LANGUAGE, 'language', 'language'],
122130
[$providerId, ProviderService::SETTING_MAPPING_ADDRESS, 'address', 'address'],
123131
[$providerId, ProviderService::SETTING_MAPPING_STREETADDRESS, 'street_address', 'street_address'],
124132
[$providerId, ProviderService::SETTING_MAPPING_POSTALCODE, 'postal_code', 'postal_code'],

0 commit comments

Comments
 (0)