Fix regression. Missing rollback.

This commit is contained in:
Laurent Destailleur 2021-06-13 19:05:48 +02:00
parent 95ee0f630d
commit 67e00e5cf5

View File

@ -1287,22 +1287,39 @@ class User extends CommonObject
$error = 0; $error = 0;
$this->db->begin(); $this->db->begin();
$sql = "SELECT login FROM ".MAIN_DB_PREFIX."user"; // Check if login already exists in same entity or into entity 0.
$sql .= " WHERE login ='".$this->db->escape($this->login)."'"; if ($this->login) {
$sql .= " AND entity IN (0, ".$this->db->escape($conf->entity).")"; $sqltochecklogin = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."user WHERE entity IN (".$this->db->sanitize((int) $this->entity).", 0) AND login = '".$this->db->escape($this->login)."'";
$resqltochecklogin = $this->db->query($sqltochecklogin);
dol_syslog(get_class($this)."::create", LOG_DEBUG); if ($resqltochecklogin) {
$resql = $this->db->query($sql); $objtochecklogin = $this->db->fetch_object($resqltochecklogin);
if ($resql) { if ($objtochecklogin && $objtochecklogin->nb > 0) {
$num = $this->db->num_rows($resql); $langs->load("errors");
$this->db->free($resql); $this->error = $langs->trans("ErrorLoginAlreadyExists", $this->login);
dol_syslog(get_class($this)."::create ".$this->error, LOG_DEBUG);
if ($num) {
$this->error = 'ErrorLoginAlreadyExists';
dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
$this->db->rollback(); $this->db->rollback();
return -6; return -6;
} else { }
$this->db->free($resqltochecklogin);
}
}
if ($this->email !== '') {
$sqltochecklogin = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."user WHERE entity IN (".$this->db->sanitize((int) $this->entity).", 0) AND email = '".$this->db->escape($this->email)."'";
$resqltochecklogin = $this->db->query($sqltochecklogin);
if ($resqltochecklogin) {
$objtochecklogin = $this->db->fetch_object($resqltochecklogin);
if ($objtochecklogin && $objtochecklogin->nb > 0) {
$langs->load("errors");
$this->error = $langs->trans("ErrorEmailAlreadyExists", $this->email);
dol_syslog(get_class($this)."::create ".$this->error, LOG_DEBUG);
$this->db->rollback();
return -6;
}
$this->db->free($resqltochecklogin);
}
}
// Insert into database
$sql = "INSERT INTO ".MAIN_DB_PREFIX."user (datec, login, ldap_sid, entity)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."user (datec, login, ldap_sid, entity)";
$sql .= " VALUES('".$this->db->idate($this->datec)."','".$this->db->escape($this->login)."','".$this->db->escape($this->ldap_sid)."',".$this->db->escape($this->entity).")"; $sql .= " VALUES('".$this->db->idate($this->datec)."','".$this->db->escape($this->login)."','".$this->db->escape($this->ldap_sid)."',".$this->db->escape($this->entity).")";
$result = $this->db->query($sql); $result = $this->db->query($sql);
@ -1363,12 +1380,6 @@ class User extends CommonObject
return -2; return -2;
} }
} }
} else {
$this->error = $this->db->lasterror();
$this->db->rollback();
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@ -1591,12 +1602,12 @@ class User extends CommonObject
/** /**
* Update a user into database (and also password if this->pass is defined) * Update a user into database (and also password if this->pass is defined)
* *
* @param User $user User qui fait la mise a jour * @param User $user User making update
* @param int $notrigger 1 ne declenche pas les triggers, 0 sinon * @param int $notrigger 1=do not execute triggers, 0 by default
* @param int $nosyncmember 0=Synchronize linked member (standard info), 1=Do not synchronize linked member * @param int $nosyncmember 0=Synchronize linked member (standard info), 1=Do not synchronize linked member
* @param int $nosyncmemberpass 0=Synchronize linked member (password), 1=Do not synchronize linked member * @param int $nosyncmemberpass 0=Synchronize linked member (password), 1=Do not synchronize linked member
* @param int $nosynccontact 0=Synchronize linked contact, 1=Do not synchronize linked contact * @param int $nosynccontact 0=Synchronize linked contact, 1=Do not synchronize linked contact
* @return int <0 si KO, >=0 si OK * @return int <0 if KO, >=0 if OK
*/ */
public function update($user, $notrigger = 0, $nosyncmember = 0, $nosyncmemberpass = 0, $nosynccontact = 0) public function update($user, $notrigger = 0, $nosyncmember = 0, $nosyncmemberpass = 0, $nosynccontact = 0)
{ {
@ -1663,7 +1674,7 @@ class User extends CommonObject
$this->db->begin(); $this->db->begin();
// Check if login already exists in same entity or into entity 0. // Check if login already exists in same entity or into entity 0.
if ($this->oldcopy->login != $this->login) { if (!empty($this->oldcopy) && $this->oldcopy->login != $this->login) {
$sqltochecklogin = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."user WHERE entity IN (".$this->db->sanitize((int) $this->entity).", 0) AND login = '".$this->db->escape($this->login)."'"; $sqltochecklogin = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."user WHERE entity IN (".$this->db->sanitize((int) $this->entity).", 0) AND login = '".$this->db->escape($this->login)."'";
$resqltochecklogin = $this->db->query($sqltochecklogin); $resqltochecklogin = $this->db->query($sqltochecklogin);
if ($resqltochecklogin) { if ($resqltochecklogin) {
@ -1671,11 +1682,13 @@ class User extends CommonObject
if ($objtochecklogin && $objtochecklogin->nb > 0) { if ($objtochecklogin && $objtochecklogin->nb > 0) {
$langs->load("errors"); $langs->load("errors");
$this->error = $langs->trans("ErrorLoginAlreadyExists", $this->login); $this->error = $langs->trans("ErrorLoginAlreadyExists", $this->login);
dol_syslog(get_class($this)."::create ".$this->error, LOG_DEBUG);
$this->db->rollback();
return -1; return -1;
} }
} }
} }
if ($this->email !== '' && $this->oldcopy->email != $this->email) { if (!empty($this->oldcopy) && $this->email !== '' && $this->oldcopy->email != $this->email) {
$sqltochecklogin = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."user WHERE entity IN (".$this->db->sanitize((int) $this->entity).", 0) AND email = '".$this->db->escape($this->email)."'"; $sqltochecklogin = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."user WHERE entity IN (".$this->db->sanitize((int) $this->entity).", 0) AND email = '".$this->db->escape($this->email)."'";
$resqltochecklogin = $this->db->query($sqltochecklogin); $resqltochecklogin = $this->db->query($sqltochecklogin);
if ($resqltochecklogin) { if ($resqltochecklogin) {
@ -1683,6 +1696,8 @@ class User extends CommonObject
if ($objtochecklogin && $objtochecklogin->nb > 0) { if ($objtochecklogin && $objtochecklogin->nb > 0) {
$langs->load("errors"); $langs->load("errors");
$this->error = $langs->trans("ErrorEmailAlreadyExists", $this->email); $this->error = $langs->trans("ErrorEmailAlreadyExists", $this->email);
dol_syslog(get_class($this)."::create ".$this->error, LOG_DEBUG);
$this->db->rollback();
return -1; return -1;
} }
} }