Fix: La description d'un groupe n'était pas sauvegardé ni restitué.

Qual: Création des user et group au sein de transactions
This commit is contained in:
Laurent Destailleur 2005-02-08 00:18:23 +00:00
parent 8aaf21c6dd
commit 303453ac92
4 changed files with 198 additions and 161 deletions

View File

@ -571,7 +571,7 @@ class User
if ($result) if ($result)
{ {
if ($this->db->num_rows()) if ($this->db->num_rows($result))
{ {
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($result);
$this->id = $obj->rowid; $this->id = $obj->rowid;
@ -663,56 +663,55 @@ class User
} }
/** /**
* \brief Crée en base un utilisateur * \brief Crée un utilisateur en base
* \return si erreur <0, si ok renvoie id compte créé
*/ */
function create() function create()
{ {
global $langs; global $langs;
$sql = "SELECT login FROM ".MAIN_DB_PREFIX."user WHERE login ='$this->login';"; $sql = "SELECT login FROM ".MAIN_DB_PREFIX."user WHERE login ='".$this->login."';";
//$sql = "SELECT login FROM ".MAIN_DB_PREFIX."user WHERE login ='$this->email';"; $result=$this->db->query($sql);
if ($this->db->query($sql)) if ($result)
{ {
$num = $this->db->num_rows(); $num = $this->db->num_rows($result);
$this->db->free(); $this->db->free($result);
if ($num)
{
$this->error = $langs->trans("ErrorLoginAlreadyExists");
return -5;
}
else
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."user (datec,login,email) VALUES(now(),'$this->login','$this->email');";
$result=$this->db->query($sql);
if ($num) if ($result)
{ {
$this->error = $langs->trans("ErrorLoginAlreadyExists"); $table = "".MAIN_DB_PREFIX."user";
return 0; $this->id = $this->db->last_insert_id($table);
}
else if ($this->set_default_rights() < 0) return -4;
{
$sql = "insert into ".MAIN_DB_PREFIX."user (datec,login,email) if ($this->update() < 0) return -3;
values(now(),'$this->login','$this->email');";
if ($this->db->query($sql)) return $this->id;
{ }
/*if ($this->db->affected_rows()) else
{ {
$this->id = $this->db->last_insert_id(); dolibarr_print_error($this->db);
$this->update(); return -2;
$this->set_default_rights(); }
return $this->id; }
}*/ // ce code pose probleme en postgres il est remplace par le bloc ci dessous }
// fonctionne autant en postgres que mysql else
$table = "".MAIN_DB_PREFIX."user"; {
$this->id = $this->db->last_insert_id($table); dolibarr_print_error($this->db);
$this->set_default_rights(); return -1;
$this->update(); }
return $this->id; }
}
else
{
dolibarr_print_error($this->db);
}
}
}
else
{
dolibarr_print_error($this->db);
}
} //fin function
/** /**
* \brief Créé en base un utilisateur depuis l'objetc contact * \brief Créé en base un utilisateur depuis l'objetc contact
@ -776,100 +775,83 @@ class User
} }
/** /**
* \brief Affectation des permissions par défaut * \brief Affectation des permissions par défaut
* * \return si erreur <0, si ok renvoi le nbre de droits par defaut positionnés
*/ */
function set_default_rights() function set_default_rights()
{ {
$sql = "SELECT id FROM ".MAIN_DB_PREFIX."rights_def WHERE bydefault = 1"; $sql = "SELECT id FROM ".MAIN_DB_PREFIX."rights_def WHERE bydefault = 1";
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
$num = $this->db->num_rows(); $num = $this->db->num_rows();
$i = 0; $i = 0;
$rd = array(); $rd = array();
while ($i < $num) while ($i < $num)
{ {
$row = $this->db->fetch_row($i); $row = $this->db->fetch_row($i);
$rd[$i] = $row[0]; $rd[$i] = $row[0];
$i++; $i++;
} }
$this->db->free(); $this->db->free();
} }
$i = 0; $i = 0;
while ($i < $num) while ($i < $num)
{ {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = $this->id AND fk_id=$rd[$i]"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = $this->id AND fk_id=$rd[$i]";
$result=$this->db->query($sql);
$this->db->query($sql);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."user_rights (fk_user, fk_id) VALUES ($this->id, $rd[$i])";
$sql = "INSERT INTO ".MAIN_DB_PREFIX."user_rights (fk_user, fk_id) VALUES ($this->id, $rd[$i])"; $result=$this->db->query($sql);
if (! $result) return -1;
if ($this->db->query($sql)) $i++;
{ }
}
$i++; return $i;
}
} }
/** /**
* \brief Mise à jour en base d'un utilisateur * \brief Mise à jour en base d'un utilisateur
* \return <0 si echec, >0 si ok * \return <0 si echec, >=0 si ok
*/ */
function update() function update()
{ {
global $langs; global $langs;
$sql = "SELECT login FROM ".MAIN_DB_PREFIX."user WHERE login ='$this->login' AND rowid <> $this->id;"; if (!strlen($this->code))
$this->code = $this->login;
if ($this->db->query($sql)) $sql = "UPDATE ".MAIN_DB_PREFIX."user SET ";
{ $sql .= " name = '$this->nom'";
$num = $this->db->num_rows(); $sql .= ", firstname = '$this->prenom'";
$this->db->free(); $sql .= ", login = '$this->login'";
$sql .= ", email = '$this->email'";
$sql .= ", admin = $this->admin";
$sql .= ", webcal_login = '$this->webcal_login'";
$sql .= ", code = '$this->code'";
$sql .= ", note = '$this->note'";
$sql .= " WHERE rowid = ".$this->id;
if ($num)
{
$this->error = $langs->trans("ErrorLoginAlreadyExists");
return -1;
}
else
{
if (!strlen($this->code))
$this->code = $this->login;
$sql = "UPDATE ".MAIN_DB_PREFIX."user SET "; $result = $this->db->query($sql);
$sql .= " name = '$this->nom'";
$sql .= ", firstname = '$this->prenom'"; if ($result)
$sql .= ", login = '$this->login'"; {
$sql .= ", email = '$this->email'"; if ($this->db->affected_rows())
$sql .= ", admin = $this->admin"; {
$sql .= ", webcal_login = '$this->webcal_login'"; return 1;
$sql .= ", code = '$this->code'"; }
$sql .= ", note = '$this->note'"; return 0;
$sql .= " WHERE rowid = $this->id;"; }
else
{
$result = $this->db->query($sql); dolibarr_print_error($this->db);
return -2;
if ($result) }
{
if ($this->db->affected_rows()) }
{
return 1;
}
}
else
{
dolibarr_print_error($this->db);
}
}
}
}
/** /**
* \brief Change le mot de passe d'un utilisateur et l'envoie par mail * \brief Change le mot de passe d'un utilisateur et l'envoie par mail

View File

@ -64,6 +64,7 @@ if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == "yes")
} }
} }
/** /**
* Action ajout user * Action ajout user
*/ */
@ -78,6 +79,7 @@ if ($_POST["action"] == 'add' && $user->admin)
$message='<div class="error">'.$langs->trans("LoginNotDefined").'</div>'; $message='<div class="error">'.$langs->trans("LoginNotDefined").'</div>';
$action="create"; // Go back to create page $action="create"; // Go back to create page
} }
if (! $message) { if (! $message) {
$edituser = new User($db,0); $edituser = new User($db,0);
@ -89,20 +91,29 @@ if ($_POST["action"] == 'add' && $user->admin)
$edituser->admin = trim($_POST["admin"]); $edituser->admin = trim($_POST["admin"]);
$edituser->webcal_login = trim($_POST["webcal_login"]); $edituser->webcal_login = trim($_POST["webcal_login"]);
$db->begin();
$id = $edituser->create(); $id = $edituser->create();
if ($id) { if ($id > 0)
if (isset($_POST['password']) && trim($_POST['password'])) {
{ if (isset($_POST['password']) && trim($_POST['password']))
$edituser->password($user,trim($_POST['password']),$conf->password_encrypted); {
} $edituser->password($user,trim($_POST['password']),$conf->password_encrypted);
}
Header("Location: fiche.php?id=$id");
$db->commit();
Header("Location: fiche.php?id=$id");
} }
else { else
$message='<div class="error">'.$langs->trans("ErrorLoginAlreadyExists",$edituser->login).'</div>'; {
$action="create"; // Go back to create page $db->rollback();
$message='<div class="error">'.$langs->trans("ErrorLoginAlreadyExists",$edituser->login).'</div>';
$action="create"; // Go back to create page
} }
} }
} }
@ -187,7 +198,7 @@ if ($action == 'create')
print "<br>"; print "<br>";
if ($message) { print $message."<br>"; } if ($message) { print $message."<br>"; }
print '<form action="fiche.php" method="post" name="createuser>'; print '<form action="fiche.php" method="post" name="createuser">';
print '<input type="hidden" name="action" value="add">'; print '<input type="hidden" name="action" value="add">';
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';

View File

@ -21,9 +21,9 @@
*/ */
/** /**
\file htdocs/user/group/fiche.php \file htdocs/user/group/fiche.php
\brief Onglet groupes utilisateurs \brief Onglet groupes utilisateurs
\version $Revision$ \version $Revision$
*/ */
@ -51,16 +51,22 @@ if ($_POST["action"] == 'add' && $user->admin)
$editgroup->nom = trim($_POST["nom"]); $editgroup->nom = trim($_POST["nom"]);
$editgroup->note = trim($_POST["note"]); $editgroup->note = trim($_POST["note"]);
$result = $editgroup->create(); $db->begin();
$id = $editgroup->create();
if ($result == 0) if ($id > 0)
{ {
Header("Location: fiche.php?id=".$editgroup->id); $db->commit();
Header("Location: fiche.php?id=".$editgroup->id);
} }
else else
{ {
$message='<div class="error">'.$langs->trans("ErrorGroupAlreadyExists",$editgroup->nom).'</div>'; $db->rollback();
$action="create"; // Go back to create page
$message='<div class="error">'.$langs->trans("ErrorGroupAlreadyExists",$editgroup->nom).'</div>';
$action="create"; // Go back to create page
} }
} }
} }
@ -224,7 +230,7 @@ else
$result = $db->query($sql); $result = $db->query($sql);
if ($result) if ($result)
{ {
$num = $db->num_rows(); $num = $db->num_rows($result);
$i = 0; $i = 0;
print "<br>"; print "<br>";
@ -238,7 +244,7 @@ else
$var=True; $var=True;
while ($i < $num) while ($i < $num)
{ {
$obj = $db->fetch_object(); $obj = $db->fetch_object($result);
$var=!$var; $var=!$var;
print "<tr $bc[$var]>"; print "<tr $bc[$var]>";
@ -263,8 +269,11 @@ else
$i++; $i++;
} }
print "</table>"; print "</table>";
$db->free(); print "<br>";
$db->free($result);
} }
print '</div>';
} }
} }

View File

@ -65,7 +65,7 @@ class UserGroup
{ {
$this->id = $id; $this->id = $id;
$sql = "SELECT g.rowid, g.nom FROM ".MAIN_DB_PREFIX."usergroup as g"; $sql = "SELECT g.rowid, g.nom, g.note FROM ".MAIN_DB_PREFIX."usergroup as g";
$sql .= " WHERE g.rowid = ".$this->id; $sql .= " WHERE g.rowid = ".$this->id;
@ -73,15 +73,16 @@ class UserGroup
if ($result) if ($result)
{ {
if ($this->db->num_rows()) if ($this->db->num_rows($result))
{ {
$obj = $this->db->fetch_object(); $obj = $this->db->fetch_object($result);
$this->id = $obj->rowid; $this->id = $obj->rowid;
$this->nom = stripslashes($obj->nom); $this->nom = $obj->nom;
$this->note = $obj->note;
} }
$this->db->free(); $this->db->free($result);
} }
else else
@ -108,7 +109,8 @@ class UserGroup
} }
/** /**
* \brief Crée un groupe en base * \brief Crée un groupe en base
* \return si erreur <0, si ok renvoie id groupe créé
*/ */
function create() function create()
@ -117,19 +119,52 @@ class UserGroup
$sql = "INSERT into ".MAIN_DB_PREFIX."usergroup (datec,nom)"; $sql = "INSERT into ".MAIN_DB_PREFIX."usergroup (datec,nom)";
$sql .= " VALUES(now(),'$this->nom')"; $sql .= " VALUES(now(),'$this->nom')";
if ($this->db->query($sql)) $result=$this->db->query($sql);
{ if ($result)
$this->id = $this->db->last_insert_id(); {
return 0; $table = "".MAIN_DB_PREFIX."usergroup";
} $this->id = $this->db->last_insert_id($table);
if ($this->update() < 0) return -2;
return $this->id;
}
else else
{ {
dolibarr_syslog("UserGroup::Create"); dolibarr_syslog("UserGroup::Create");
return -1; return -1;
} }
} }
/**
* \brief Mise à jour en base d'un utilisateur
* \return <0 si echec, >=0 si ok
*/
function update()
{
$sql = "UPDATE ".MAIN_DB_PREFIX."usergroup SET ";
$sql .= " note = '$this->note'";
$sql .= " WHERE rowid = ".$this->id;
$result = $this->db->query($sql);
if ($result)
{
if ($this->db->affected_rows())
{
return 1;
}
return 0;
}
else
{
dolibarr_print_error($this->db);
return -2;
}
}
} }
?> ?>