From 30afaa56ab873c32dd80dff92cd498eba82f54fc Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Thu, 16 Jan 2020 15:28:48 +0200 Subject: [PATCH] LDAP bug fixes --- .../Espo/Core/Utils/Authentication/LDAP.php | 26 +++++++++++++------ .../metadata/authenticationMethods/LDAP.json | 5 ++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/application/Espo/Core/Utils/Authentication/LDAP.php b/application/Espo/Core/Utils/Authentication/LDAP.php index 49ba779f05..545c7ebe00 100644 --- a/application/Espo/Core/Utils/Authentication/LDAP.php +++ b/application/Espo/Core/Utils/Authentication/LDAP.php @@ -41,6 +41,8 @@ class LDAP extends Espo private $ldapClient; + protected $injections = []; + /** * User field name => option name (LDAP attribute) * @@ -82,6 +84,16 @@ class LDAP extends Espo $this->utils = new LDAP\Utils($config); } + public function inject($name, $object) + { + $this->injections[$name] = $object; + } + + protected function getInjection($name) + { + return $this->injections[$name]; + } + protected function getUtils() { return $this->utils; @@ -104,14 +116,14 @@ class LDAP extends Espo public function login(string $username, $password, ?AuthToken $authToken = null, array $params = [], $request = null) { - if (!$password) return; - $isPortal = !empty($params['isPortal']); if ($authToken) { return $this->loginByToken($username, $authToken); } + if (!$password || $username == '**logout') return; + if ($isPortal) { $useLdapAuthForPortalUser = $this->getUtils()->getOption('portalUserLdapAuth'); if (!$useLdapAuthForPortalUser) { @@ -271,20 +283,18 @@ class LDAP extends Espo $data[$fieldName] = $fieldValue; } - $entityManager = $this->entityManager; + $entityManager = $this->getEntityManager(); - $systemUser = $entityManager->getRepository('User')->get('system'); + $systemUser = $entityManager->getEntity('User', 'system'); if (!$systemUser) { throw new Error("System user is not found"); } - $systemUser->set('isAdmin', true); - $systemUser->set('ipAddress', $_SERVER['REMOTE_ADDR']); + $this->getInjection('container')->setUser($systemUser); $entityManager->setUser($systemUser); $user = $entityManager->getEntity('User'); $user->set($data); - - $this->getEntityManager()->saveEntity($user); + $entityManager->saveEntity($user); return $this->getEntityManager()->getEntity('User', $user->id); } diff --git a/application/Espo/Resources/metadata/authenticationMethods/LDAP.json b/application/Espo/Resources/metadata/authenticationMethods/LDAP.json index b2a1318e93..789f490ea3 100644 --- a/application/Espo/Resources/metadata/authenticationMethods/LDAP.json +++ b/application/Espo/Resources/metadata/authenticationMethods/LDAP.json @@ -259,5 +259,6 @@ } } } - } -} \ No newline at end of file + }, + "dependencyList": ["container"] +}