From c404d474733b1c6bd27bd2cf91d0f8cf6c5e8d6d Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Fri, 17 Jan 2020 14:30:21 +0200 Subject: [PATCH] LDAP fixes --- .../Espo/Core/Utils/Authentication/LDAP.php | 36 +++++++++++++------ .../Espo/Resources/i18n/en_US/User.json | 3 +- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/application/Espo/Core/Utils/Authentication/LDAP.php b/application/Espo/Core/Utils/Authentication/LDAP.php index 545c7ebe00..35fa27272c 100644 --- a/application/Espo/Core/Utils/Authentication/LDAP.php +++ b/application/Espo/Core/Utils/Authentication/LDAP.php @@ -99,6 +99,22 @@ class LDAP extends Espo return $this->utils; } + protected function getContainer() + { + return $this->getInjection('container'); + } + + protected function useSystemUser() + { + $systemUser = $this->getEntityManager()->getEntity('User', 'system'); + if (!$systemUser) { + throw new Error("System user is not found."); + } + + $this->getContainer()->setUser($systemUser); + $this->getEntityManager()->setUser($systemUser); + } + protected function getLdapClient() { if (!isset($this->ldapClient)) { @@ -183,7 +199,12 @@ class LDAP extends Espo ] ]); - if (!isset($user) && $this->getUtils()->getOption('createEspoUser')) { + if (!isset($user)) { + if (!$this->getUtils()->getOption('createEspoUser')) { + $this->useSystemUser(); + throw new Error($this->getContainer()->get('language')->translate('ldapUserInEspoNotFound', 'messages', 'User')); + } + $userData = $ldapClient->getEntry($userDn); $user = $this->createUser($userData, $isPortal); } @@ -283,18 +304,11 @@ class LDAP extends Espo $data[$fieldName] = $fieldValue; } - $entityManager = $this->getEntityManager(); + $this->useSystemUser(); - $systemUser = $entityManager->getEntity('User', 'system'); - if (!$systemUser) { - throw new Error("System user is not found"); - } - $this->getInjection('container')->setUser($systemUser); - $entityManager->setUser($systemUser); - - $user = $entityManager->getEntity('User'); + $user = $this->getEntityManager()->getEntity('User'); $user->set($data); - $entityManager->saveEntity($user); + $this->getEntityManager()->saveEntity($user); return $this->getEntityManager()->getEntity('User', $user->id); } diff --git a/application/Espo/Resources/i18n/en_US/User.json b/application/Espo/Resources/i18n/en_US/User.json index 4f85f0d9c1..56ee8dec10 100644 --- a/application/Espo/Resources/i18n/en_US/User.json +++ b/application/Espo/Resources/i18n/en_US/User.json @@ -109,7 +109,8 @@ "enterTotpCode": "Enter a code from your authenticator app.", "verifyTotpCode": "Scan the QR-code with your mobile authenticator app. If you have a trouble with scanning, you can enter the secret manually. After that you will see a 6-digit code in your application. Enter this code in the field below.", "generateAndSendNewPassword": "A new password will be generated and sent to the user's email address.", - "security2FaResetConfimation": "Are you sure you want to reset the current 2FA settings?" + "security2FaResetConfimation": "Are you sure you want to reset the current 2FA settings?", + "ldapUserInEspoNotFound": "User is not found in EspoCRM. Contact your administrator to create the user." }, "options": { "gender": {