EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
OIDC user info populator (#3686)
Yurii Kuznietsovf66555b85b14
diff --git a/application/Espo/Binding.php b/application/Espo/Binding.phpindex e54ac2cb29..0740e659ce 100644--- a/application/Espo/Binding.php+++ b/application/Espo/Binding.php@@ -298,6 +298,11 @@ class Binding implements BindingProcessor'Espo\\Core\\Authentication\\Oidc\\UserProvider\\DefaultUserProvider');+ $binder->bindImplementation(+ 'Espo\\Core\\Authentication\\Oidc\\UserProvider\\UserInfoPopulator',+ 'Espo\\Core\\Authentication\\Oidc\\UserProvider\\DefaultUserInfoPopulator'+ );+$binder->bindImplementation('Espo\\Core\\Mail\\Importer\\ParentFinder','Espo\\Core\\Mail\\Importer\\DefaultParentFinder'diff --git a/application/Espo/Core/Authentication/Oidc/UserProvider/DefaultUserInfoPopulator.php b/application/Espo/Core/Authentication/Oidc/UserProvider/DefaultUserInfoPopulator.phpnew file mode 100644index 0000000000..d43f30c955--- /dev/null+++ b/application/Espo/Core/Authentication/Oidc/UserProvider/DefaultUserInfoPopulator.php@@ -0,0 +1,62 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Authentication\Oidc\UserProvider;++use Espo\Entities\User;++class DefaultUserInfoPopulator implements UserInfoPopulator+{+ public function populate(UserInfo $userInfo, User $user): void+ {+ $values = $this->getUserDataFromToken($userInfo);++ $user->setMultiple($values);+ }++ /**+ * @return array<string, mixed>+ */+ private function getUserDataFromToken(UserInfo $userInfo): array+ {+ return [+ 'emailAddress' => $userInfo->get('email'),+ 'phoneNumber' => $userInfo->get('phone_number'),+ 'emailAddressData' => null,+ 'phoneNumberData' => null,+ 'firstName' => $userInfo->get('given_name'),+ 'lastName' => $userInfo->get('family_name'),+ 'middle_name' => $userInfo->get('middle_name'),+ 'gender' =>+ in_array($userInfo->get('gender'), ['male', 'female']) ?+ ucfirst($userInfo->get('gender') ?? '') :+ null,+ ];+ }+}diff --git a/application/Espo/Core/Authentication/Oidc/UserProvider/Sync.php b/application/Espo/Core/Authentication/Oidc/UserProvider/Sync.phpindex f3d5d03bbe..8b2d79dd0f 100644--- a/application/Espo/Core/Authentication/Oidc/UserProvider/Sync.php+++ b/application/Espo/Core/Authentication/Oidc/UserProvider/Sync.php@@ -50,6 +50,7 @@ class Syncprivate PasswordHash $passwordHash,private AclCacheClearer $aclCacheClearer,private ApplicationState $applicationState,+ private UserInfoPopulator $userInfoPopulator,) {}public function createUser(UserInfo $userInfo): User@@ -67,7 +68,7 @@ class Sync'password' => $this->passwordHash->hash(Util::generatePassword(10, 4, 2, true)),]);- $user->set($this->getUserDataFromToken($userInfo));+ $this->userInfoPopulator->populate($userInfo, $user);$user->set($this->getUserTeamsDataFromToken($userInfo));if ($this->applicationState->isPortal()) {@@ -93,7 +94,7 @@ class Sync}if ($this->configDataProvider->sync()) {- $user->set($this->getUserDataFromToken($payload));+ $this->userInfoPopulator->populate($payload, $user);}$clearAclCache = false;@@ -113,26 +114,6 @@ class Sync}}- /**- * @return array<string, mixed>- */- private function getUserDataFromToken(UserInfo $userInfo): array- {- return [- 'emailAddress' => $userInfo->get('email'),- 'phoneNumber' => $userInfo->get('phone_number'),- 'emailAddressData' => null,- 'phoneNumberData' => null,- 'firstName' => $userInfo->get('given_name'),- 'lastName' => $userInfo->get('family_name'),- 'middle_name' => $userInfo->get('middle_name'),- 'gender' =>- in_array($userInfo->get('gender'), ['male', 'female']) ?- ucfirst($userInfo->get('gender') ?? '') :- null,- ];- }-/*** @return array<string, mixed>*/diff --git a/application/Espo/Core/Authentication/Oidc/UserProvider/UserInfoPopulator.php b/application/Espo/Core/Authentication/Oidc/UserProvider/UserInfoPopulator.phpnew file mode 100644index 0000000000..d9027547a3--- /dev/null+++ b/application/Espo/Core/Authentication/Oidc/UserProvider/UserInfoPopulator.php@@ -0,0 +1,40 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Authentication\Oidc\UserProvider;++use Espo\Entities\User;++/**+ * @since 10.0.0+ */+interface UserInfoPopulator+{+ public function populate(UserInfo $userInfo, User $user): void;+}diff --git a/tests/unit/Espo/Core/Authentication/Logins/Oidc/SyncTest.php b/tests/unit/Espo/Core/Authentication/Logins/Oidc/SyncTest.phpindex 137e1caa58..32209aba51 100644--- a/tests/unit/Espo/Core/Authentication/Logins/Oidc/SyncTest.php+++ b/tests/unit/Espo/Core/Authentication/Logins/Oidc/SyncTest.php@@ -32,6 +32,7 @@ namespace tests\unit\Espo\Core\Authentication\Logins\Oidc;use Espo\Core\Acl\Cache\Clearer;use Espo\Core\ApplicationState;use Espo\Core\Authentication\Oidc\ConfigDataProvider;+use Espo\Core\Authentication\Oidc\UserProvider\DefaultUserInfoPopulator;use Espo\Core\Authentication\Oidc\UserProvider\Sync;use Espo\Core\Authentication\Oidc\UserProvider\UsernameValidator;use Espo\Core\Authentication\Oidc\UserProvider\UserRepository;@@ -50,6 +51,8 @@ class SyncTest extends TestCase$this->config = $this->createMock(Config::class);$configDataProvider = $this->createMock(ConfigDataProvider::class);+ $populator = new DefaultUserInfoPopulator();+$this->sync = new Sync($this->createMock(UsernameValidator::class),$this->config,@@ -57,7 +60,8 @@ class SyncTest extends TestCase$this->createMock(UserRepository::class),$this->createMock(PasswordHash::class),$this->createMock(Clearer::class),- $this->createMock(ApplicationState::class)+ $this->createMock(ApplicationState::class),+ $populator,);}