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,86 +1287,97 @@ 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);
if ($resqltochecklogin) {
$objtochecklogin = $this->db->fetch_object($resqltochecklogin);
if ($objtochecklogin && $objtochecklogin->nb > 0) {
$langs->load("errors");
$this->error = $langs->trans("ErrorLoginAlreadyExists", $this->login);
dol_syslog(get_class($this)."::create ".$this->error, LOG_DEBUG);
$this->db->rollback();
return -6;
}
$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 .= " 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);
dol_syslog(get_class($this)."::create", LOG_DEBUG); dol_syslog(get_class($this)."::create", LOG_DEBUG);
$resql = $this->db->query($sql); if ($result) {
if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."user");
$num = $this->db->num_rows($resql);
$this->db->free($resql);
if ($num) { // Set default rights
$this->error = 'ErrorLoginAlreadyExists'; if ($this->set_default_rights() < 0) {
dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING); $this->error = 'ErrorFailedToSetDefaultRightOfUser';
$this->db->rollback(); $this->db->rollback();
return -6; return -5;
} else { }
$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).")";
$result = $this->db->query($sql);
dol_syslog(get_class($this)."::create", LOG_DEBUG); if (!empty($conf->global->MAIN_DEFAULT_WAREHOUSE_USER) && !empty($conf->global->STOCK_USERSTOCK_AUTOCREATE)) {
if ($result) { require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."user"); $langs->load("stocks");
$entrepot = new Entrepot($this->db);
$entrepot->label = $langs->trans("PersonalStock", $this->getFullName($langs));
$entrepot->libelle = $entrepot->label; // For backward compatibility
$entrepot->description = $langs->trans("ThisWarehouseIsPersonalStock", $this->getFullName($langs));
$entrepot->statut = 1;
$entrepot->country_id = $mysoc->country_id;
$warehouseid = $entrepot->create($user);
// Set default rights $this->fk_warehouse = $warehouseid;
if ($this->set_default_rights() < 0) { }
$this->error = 'ErrorFailedToSetDefaultRightOfUser';
$this->db->rollback();
return -5;
}
if (!empty($conf->global->MAIN_DEFAULT_WAREHOUSE_USER) && !empty($conf->global->STOCK_USERSTOCK_AUTOCREATE)) { // Update minor fields
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php'; $result = $this->update($user, 1, 1);
$langs->load("stocks"); if ($result < 0) {
$entrepot = new Entrepot($this->db); $this->db->rollback();
$entrepot->label = $langs->trans("PersonalStock", $this->getFullName($langs)); return -4;
$entrepot->libelle = $entrepot->label; // For backward compatibility }
$entrepot->description = $langs->trans("ThisWarehouseIsPersonalStock", $this->getFullName($langs));
$entrepot->statut = 1;
$entrepot->country_id = $mysoc->country_id;
$warehouseid = $entrepot->create($user);
$this->fk_warehouse = $warehouseid; if (!$notrigger) {
} // Call trigger
$result = $this->call_trigger('USER_CREATE', $user);
// Update minor fields if ($result < 0) {
$result = $this->update($user, 1, 1); $error++;
if ($result < 0) {
$this->db->rollback();
return -4;
}
if (!$notrigger) {
// Call trigger
$result = $this->call_trigger('USER_CREATE', $user);
if ($result < 0) {
$error++;
}
// End call triggers
}
if (!$error) {
$this->db->commit();
return $this->id;
} else {
//$this->error=$interface->error;
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
$this->db->rollback();
return -3;
}
} else {
$this->error = $this->db->lasterror();
$this->db->rollback();
return -2;
} }
// End call triggers
}
if (!$error) {
$this->db->commit();
return $this->id;
} else {
//$this->error=$interface->error;
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
$this->db->rollback();
return -3;
} }
} else { } else {
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
$this->db->rollback(); $this->db->rollback();
return -1; return -2;
} }
} }
@ -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;
} }
} }