diff --git a/application/Espo/Core/Utils/Auth.php b/application/Espo/Core/Utils/Auth.php index 20c6f3ea96..6f50701e4c 100644 --- a/application/Espo/Core/Utils/Auth.php +++ b/application/Espo/Core/Utils/Auth.php @@ -28,9 +28,22 @@ class Auth { protected $container; + protected $authentication; + + protected $config; + + protected $entityManager; + public function __construct(\Espo\Core\Container $container) { $this->container = $container; + + $this->entityManager = $this->container->get('entityManager'); + $this->config = $this->container->get('config'); + + $authenticationMethod = $this->config->get('authenticationMethod', 'Espo'); + $authenticationClassName = "\\Espo\\Core\\Utils\\Authentication\\" . $authenticationMethod; + $this->authentication = new $authenticationClassName($this->config, $this->entityManager); } public function useNoAuth($isAdmin = false) @@ -49,24 +62,14 @@ class Auth public function login($username, $password) { - $GLOBALS['log']->debug('AUTH: Try to authenticate'); + $GLOBALS['log']->debug('AUTH: Try to authenticate'); - $hash = md5($password); - - $entityManager = $this->container->get('entityManager'); + $entityManager = $this->entityManager; $authToken = $entityManager->getRepository('AuthToken')->where(array('token' => $password))->findOne(); - if ($authToken) { - $hash = $authToken->get('hash'); - } - - $user = $entityManager->getRepository('User')->findOne(array( - 'whereClause' => array( - 'userName' => $username, - 'password' => $hash - ), - )); - + + $user = $this->authentication->login($username, $password, $authToken); + if ($user) { $entityManager->setUser($user); $this->container->setUser($user); diff --git a/application/Espo/Core/Utils/Authentication/Espo.php b/application/Espo/Core/Utils/Authentication/Espo.php new file mode 100644 index 0000000000..08b4a539f3 --- /dev/null +++ b/application/Espo/Core/Utils/Authentication/Espo.php @@ -0,0 +1,60 @@ +config = $config; + $this->entityManager = $entityManager; + } + + + public function login($username, $password, \Espo\Entities\AuthToken $authToken = null) + { + if ($authToken) { + $hash = $authToken->get('hash'); + } else { + $hash = md5($password); + } + + $user = $this->entityManager->getRepository('User')->findOne(array( + 'whereClause' => array( + 'userName' => $username, + 'password' => $hash + ), + )); + + return $user; + } +} + diff --git a/application/Espo/Core/defaults/config.php b/application/Espo/Core/defaults/config.php index db0f76e341..f96b465022 100644 --- a/application/Espo/Core/defaults/config.php +++ b/application/Espo/Core/defaults/config.php @@ -74,6 +74,7 @@ return array ( 'isRotate' => true, /** rotate log files every day */ 'maxRotateFiles' => 30, /** max number of rotate files */ ), + 'authenticationMethod' => 'Espo', 'globalSearchEntityList' => array ( 0 => 'Account',