Skip to content

EspoCRM – Open Source CRM Application

Email address, Phone Number setting to null to set another primary

Yuriiefc0dea90e13
diff --git a/application/Espo/Core/Authentication/Oidc/UserProvider/DefaultUserInfoPopulator.php b/application/Espo/Core/Authentication/Oidc/UserProvider/DefaultUserInfoPopulator.php
index d43f30c955..5e479395fd 100644
--- a/application/Espo/Core/Authentication/Oidc/UserProvider/DefaultUserInfoPopulator.php
+++ b/application/Espo/Core/Authentication/Oidc/UserProvider/DefaultUserInfoPopulator.php
@@ -48,8 +48,6 @@ class DefaultUserInfoPopulator implements UserInfoPopulator
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'),
diff --git a/application/Espo/Core/FieldProcessing/EmailAddress/Saver.php b/application/Espo/Core/FieldProcessing/EmailAddress/Saver.php
index 944b792f1a..eaba3b5de8 100644
--- a/application/Espo/Core/FieldProcessing/EmailAddress/Saver.php
+++ b/application/Espo/Core/FieldProcessing/EmailAddress/Saver.php
@@ -35,6 +35,7 @@ use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Core\ORM\Type\FieldType;
use Espo\Entities\EmailAddress;
use Espo\ORM\Name\Attribute;
+use Espo\ORM\Repository\RDBRelation;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\ORM\Entity;
use Espo\Core\ApplicationState;
@@ -419,21 +420,7 @@ class Saver implements SaverInterface
return;
}
- $fetchedAddress = $entity->getFetched(self::ATTR_EMAIL_ADDRESS);
-
- if (!$fetchedAddress) {
- return;
- }
-
- $emailAddressOld = $this->getByAddress($fetchedAddress);
-
- if (!$emailAddressOld) {
- return;
- }
-
- $this->entityManager
- ->getRelation($entity, self::LINK_EMAIL_ADDRESSES)
- ->unrelate($emailAddressOld, [SaveOption::SKIP_HOOKS => true]);
+ $this->storePrimaryEmpty($entity);
}
private function storePrimaryNotEmpty(Entity $entity, string $emailAddressValue): void
@@ -580,4 +567,32 @@ class Saver implements SaverInterface
$this->loader->process($entity, LoaderParams::create());
$entity->setFetched(self::ATTR_EMAIL_ADDRESS_DATA, $previous);
}
+
+ private function storePrimaryEmpty(Entity $entity): void
+ {
+ $fetchedAddress = $entity->getFetched(self::ATTR_EMAIL_ADDRESS);
+
+ if (!$fetchedAddress) {
+ return;
+ }
+
+ /** @var RDBRelation<EmailAddress> $relation */
+ $relation = $this->entityManager->getRelation($entity, self::LINK_EMAIL_ADDRESSES);
+
+ $emailAddressOld = $this->getByAddress($fetchedAddress);
+
+ if ($emailAddressOld) {
+ $relation->unrelate($emailAddressOld, [SaveOption::SKIP_HOOKS => true]);
+ }
+
+ $one = $relation->findOne();
+
+ if (!$one) {
+ return;
+ }
+
+ $one->set(EmailAddress::FIELD_PRIMARY, true);
+
+ $this->entityManager->saveEntity($one);
+ }
}
diff --git a/application/Espo/Core/FieldProcessing/PhoneNumber/Saver.php b/application/Espo/Core/FieldProcessing/PhoneNumber/Saver.php
index 67ab9e3969..0a197b509b 100644
--- a/application/Espo/Core/FieldProcessing/PhoneNumber/Saver.php
+++ b/application/Espo/Core/FieldProcessing/PhoneNumber/Saver.php
@@ -32,8 +32,10 @@ namespace Espo\Core\FieldProcessing\PhoneNumber;
use Espo\Core\FieldProcessing\Loader\Params as LoeaderParams;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Core\ORM\Type\FieldType;
+use Espo\Entities\EmailAddress;
use Espo\Entities\PhoneNumber;
use Espo\ORM\Name\Attribute;
+use Espo\ORM\Repository\RDBRelation;
use Espo\Repositories\PhoneNumber as PhoneNumberRepository;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
@@ -427,21 +429,7 @@ class Saver implements SaverInterface
return;
}
- $fetchedNumber = $entity->getFetched(self::ATTR_PHONE_NUMBER);
-
- if (!$fetchedNumber) {
- return;
- }
-
- $phoneNumberOld = $this->getByNumber($fetchedNumber);
-
- if (!$phoneNumberOld) {
- return;
- }
-
- $entityRepository
- ->getRelation($entity, 'phoneNumbers')
- ->unrelate($phoneNumberOld, [SaveOption::SKIP_HOOKS => true]);
+ $this->storePrimaryEmpty($entity);
}
private function getByNumber(string $number): ?PhoneNumber
@@ -591,4 +579,32 @@ class Saver implements SaverInterface
$this->loader->process($entity, LoeaderParams::create());
$entity->setFetched(self::ATTR_PHONE_NUMBER_DATA, $previous);
}
+
+ private function storePrimaryEmpty(Entity $entity): void
+ {
+ $fetchedNumber = $entity->getFetched(self::ATTR_PHONE_NUMBER);
+
+ if (!$fetchedNumber) {
+ return;
+ }
+
+ /** @var RDBRelation<PhoneNumber> $relation */
+ $relation = $this->entityManager->getRelation($entity, 'phoneNumbers');
+
+ $phoneNumberOld = $this->getByNumber($fetchedNumber);
+
+ if ($phoneNumberOld) {
+ $relation->unrelate($phoneNumberOld, [SaveOption::SKIP_HOOKS => true]);
+ }
+
+ $one = $relation->findOne();
+
+ if (!$one) {
+ return;
+ }
+
+ $one->set(EmailAddress::FIELD_PRIMARY, true);
+
+ $this->entityManager->saveEntity($one);
+ }
}
diff --git a/application/Espo/Entities/EmailAddress.php b/application/Espo/Entities/EmailAddress.php
index 940668e952..682231fd38 100644
--- a/application/Espo/Entities/EmailAddress.php
+++ b/application/Espo/Entities/EmailAddress.php
@@ -40,6 +40,8 @@ class EmailAddress extends Entity
public const RELATION_ENTITY_EMAIL_ADDRESS = 'EntityEmailAddress';
+ public const string FIELD_PRIMARY = 'primary';
+
/**
* @param string $value
* @return void
diff --git a/application/Espo/Entities/PhoneNumber.php b/application/Espo/Entities/PhoneNumber.php
index 9c1855d2c9..e8b355d92f 100644
--- a/application/Espo/Entities/PhoneNumber.php
+++ b/application/Espo/Entities/PhoneNumber.php
@@ -40,6 +40,8 @@ class PhoneNumber extends Entity
public const RELATION_ENTITY_PHONE_NUMBER = 'EntityPhoneNumber';
+ public const string FIELD_PRIMARY = 'primary';
+
/**
* @param string $value
* @return void
diff --git a/tests/integration/Espo/Core/Authentication/Oidc/UserInfoPopulatorTest.php b/tests/integration/Espo/Core/Authentication/Oidc/UserInfoPopulatorTest.php
new file mode 100644
index 0000000000..6d7d9d63f6
--- /dev/null
+++ b/tests/integration/Espo/Core/Authentication/Oidc/UserInfoPopulatorTest.php
@@ -0,0 +1,113 @@
+<?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 integration\Espo\Core\Authentication\Oidc;
+
+use Espo\Core\Authentication\Oidc\UserProvider\DefaultUserInfoPopulator;
+use Espo\Core\Authentication\Oidc\UserProvider\UserInfo;
+use Espo\Core\Field\EmailAddress;
+use Espo\Core\Field\EmailAddressGroup;
+use Espo\Core\Field\PhoneNumber;
+use Espo\Core\Field\PhoneNumberGroup;
+use Espo\Entities\User;
+use tests\integration\Core\BaseTestCase;
+
+class UserInfoPopulatorTest extends BaseTestCase
+{
+ public function testPopulate(): void
+ {
+ $em = $this->getEntityManager();
+
+ $user = $em->getRDBRepositoryByClass(User::class)->getNew();
+
+ $user
+ ->setUserName('name')
+ ->setPhoneNumberGroup(
+ PhoneNumberGroup::create([
+ PhoneNumber::create('+100'),
+ PhoneNumber::create('+200'),
+ ])
+ )
+ ->setEmailAddressGroup(
+ EmailAddressGroup::create([
+ EmailAddress::create('[email protected]'),
+ EmailAddress::create('[email protected]'),
+ ]),
+ );
+
+ $em->saveEntity($user);
+
+ $userInfo = $this->createMock(UserInfo::class);
+ $userInfo
+ ->method('get')
+ ->willReturnMap([
+ ['email', '[email protected]'],
+ ['phone_number', '+300'],
+ ]);
+
+ $populator = $this->getInjectableFactory()->create(DefaultUserInfoPopulator::class);
+
+ //
+
+ $populator->populate($userInfo, $user);
+
+ $em->saveEntity($user);
+ $em->refreshEntity($user);
+
+ $this->assertCount(2, $user->getEmailAddressGroup()->getList());
+ $this->assertCount(2, $user->getPhoneNumberGroup()->getList());
+
+ $this->assertEquals('[email protected]', $user->getEmailAddress());
+ $this->assertEquals('+300', $user->getPhoneNumber());
+
+ $this->assertEquals('[email protected]', $user->getEmailAddressGroup()->getList()[1]->getAddress());
+ $this->assertEquals('+200', $user->getPhoneNumberGroup()->getList()[1]->getNumber());
+
+ //
+
+ $userInfo = $this->createMock(UserInfo::class);
+ $userInfo
+ ->method('get')
+ ->willReturnMap([
+ ['email', null],
+ ['phone_number', null],
+ ]);
+
+ $populator->populate($userInfo, $user);
+
+ $em->saveEntity($user);
+ $em->refreshEntity($user);
+
+ $this->assertNull($user->getEmailAddress());
+ $this->assertNull($user->getPhoneNumber());
+
+ $this->assertCount(1, $user->getEmailAddressGroup()->getList());
+ $this->assertCount(1, $user->getPhoneNumberGroup()->getList());
+ }
+}