From 0708ec4243d24c512ee3b4ab285556fbb84387f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacobo=20Aragunde=20P=C3=A9rez?= Date: Wed, 8 Mar 2023 10:46:04 +0100 Subject: [PATCH] [#173,#496] Report better errors from LDAP user create DAO. The HybridUserDAO matches user data with the LDAP and keeps them in sync with the local database, to maintain the relations with other data tables. We modify the create operation in this DAO to return OperationResult, matching the new expectations of the upper layers. We change the Hybrid DAO to extend the Postgres DAO, so we can reuse the SQL operations. In this case, we call the create operation from the Postgres DAO after we do the corresponding LDAP checks. --- model/dao/UserDAO/HybridUserDAO.php | 37 ++++++++++------------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/model/dao/UserDAO/HybridUserDAO.php b/model/dao/UserDAO/HybridUserDAO.php index b2f99bf59..03a7d77cf 100644 --- a/model/dao/UserDAO/HybridUserDAO.php +++ b/model/dao/UserDAO/HybridUserDAO.php @@ -32,7 +32,7 @@ include_once(PHPREPORT_ROOT . '/util/SQLIncorrectTypeException.php'); include_once(PHPREPORT_ROOT . '/util/DBPostgres.php'); include_once(PHPREPORT_ROOT . '/model/vo/UserVO.php'); -include_once(PHPREPORT_ROOT . '/model/dao/UserDAO/UserDAO.php'); +include_once(PHPREPORT_ROOT . '/model/dao/UserDAO/PostgreSQLUserDAO.php'); include_once(PHPREPORT_ROOT . '/util/LDAPConnectionErrorException.php'); include_once(PHPREPORT_ROOT . '/util/LDAPInvalidOperationException.php'); include_once(PHPREPORT_ROOT . '/util/LDAPOperationErrorException.php'); @@ -42,10 +42,11 @@ /** DAO for Users in LDAP/PostgreSQL Hybrid * * This is the implementation for LDAP/PostgreSQL Hybrid of {@link UserDAO}. + * It extends PostgreSQLUserDAO so it can reuse the DB operations when needed. * * @see UserDAO, UserVO */ -class HybridUserDAO extends UserDAO{ +class HybridUserDAO extends PostgreSQLUserDAO { protected $ldapConnect; @@ -454,12 +455,9 @@ public function update(UserVO $userVO) { * The internal id of $userVO will be set after its creation. * * @param UserVO $userVO the {@link UserVO} with the data we want to insert on database. - * @return int the number of rows that have been affected (it should be 1). - * @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException} + * @return OperationResult the result {@link OperationResult} with information about operation status */ public function create(UserVO $userVO) { - $affectedRows = 0; - // check if a user with that login exists in LDAP if (!$sr=ldap_list($this->ldapConnect,"ou=People," . ConfigurationParametersManager::getParameter('LDAP_BASE'), @@ -470,29 +468,20 @@ public function create(UserVO $userVO) { return $affectedRows; } $ldapResult = ldap_get_entries($this->ldapConnect, $sr); - if($ldapResult["count"] == 0) - // user does not exist in LDAP - return $affectedRows; - - $sql = "INSERT INTO usr (login) VALUES(" . DBPostgres::checkStringNull($userVO->getLogin()) . ")"; - - $res = pg_query($this->connect, $sql); - - if ($res == NULL) - if (strpos(pg_last_error(), "unique_usr_login")) - throw new SQLUniqueViolationException(pg_last_error()); - else throw new SQLQueryErrorException(pg_last_error()); + if($ldapResult["count"] == 0) { + $result = new OperationResult(false); + $result->setResponseCode(400); + $result->setErrorNumber(1234); + $result->setMessage("Error creating user:\nLogin does not exist in LDAP."); + return $result; + } - // populate UserVO with newly created ID from DB - $userVO->setID(DBPostgres::getId($this->connect, "usr_id_seq")); + $result = parent::create($userVO); // populate UserVO with existing group assignation in LDAP $userVO->setGroups($this->getGroupsByLogin($userVO->getLogin())); - $affectedRows = pg_affected_rows($res); - - return $affectedRows; - + return $result; } /** User deleter for LDAP/PostgreSQL Hybrid.