From c404d474733b1c6bd27bd2cf91d0f8cf6c5e8d6d Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Fri, 17 Jan 2020 14:30:21 +0200 Subject: [PATCH 1/2] 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": { From 6699e66c29a86278d21869a99c0f000bcbefa677 Mon Sep 17 00:00:00 2001 From: beppe9000 <6625123+beppe9000@users.noreply.github.com> Date: Tue, 21 Jan 2020 09:24:04 +0100 Subject: [PATCH 2/2] Increase logging of many-to-many relation operations and log invalid foreach parameter error for mid keys (#1579) closes #1577 --- .../Core/Utils/Database/Schema/Converter.php | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/application/Espo/Core/Utils/Database/Schema/Converter.php b/application/Espo/Core/Utils/Database/Schema/Converter.php index a2510953a3..a9e6b6294f 100644 --- a/application/Espo/Core/Utils/Database/Schema/Converter.php +++ b/application/Espo/Core/Utils/Database/Schema/Converter.php @@ -282,6 +282,9 @@ class Converter protected function prepareManyMany($entityName, $relationParams, $tables) { $tableName = Util::toUnderScore($relationParams['relationName']); + $GLOBALS['log']->debug('DBAL: prepareManyMany invoked for ' . $entityName, [ + 'tableName' => $tableName, 'parameters' => $relationParams + ]); if ($this->getSchema()->hasTable($tableName)) { $GLOBALS['log']->debug('DBAL: Table ['.$tableName.'] exists.'); @@ -297,17 +300,22 @@ class Converter //add midKeys to a schema $uniqueIndex = array(); - foreach($relationParams['midKeys'] as $index => $midKey) { - - $columnName = Util::toUnderScore($midKey); - $table->addColumn($columnName, $this->idParams['dbType'], $this->getDbFieldParams(array( - 'type' => 'foreignId', - 'len' => $this->idParams['len'], - ))); - $table->addIndex(array($columnName), SchemaUtils::generateIndexName($columnName)); - - $uniqueIndex[] = $columnName; - } + if (empty($relationParams['midKeys'])) { + $GLOBALS['log']->debug('REBUILD: midKeys are empty!', [ + 'scope' => $entityName, 'tableName' => $tableName, + 'parameters' => $relationParams + ]); + } else { + foreach($relationParams['midKeys'] as $index => $midKey) { + $columnName = Util::toUnderScore($midKey); + $table->addColumn($columnName, $this->idParams['dbType'], $this->getDbFieldParams(array( + 'type' => 'foreignId', + 'len' => $this->idParams['len'], + ))); + $table->addIndex(array($columnName), SchemaUtils::generateIndexName($columnName)); + $uniqueIndex[] = $columnName; + } + } //END: add midKeys to a schema //add additionalColumns