From a93a9f75f2051eb0aabdf4f25bb3bc71ba84a846 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 11 Jan 2012 01:06:56 +0100 Subject: [PATCH] Qual: Uniformize code --- htdocs/user/class/user.class.php | 59 ++++++++++++----------- htdocs/user/class/usergroup.class.php | 67 ++++++++++++++++++--------- 2 files changed, 75 insertions(+), 51 deletions(-) diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index d01fa38fe0f..bcb7bdd1913 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -24,7 +24,7 @@ /** * \file htdocs/user/class/user.class.php - * \brief Fichier de la classe utilisateur + * \brief File of class to manage users */ require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php"); @@ -86,7 +86,7 @@ class User extends CommonObject var $rights; // Array of permissions user->rights->permx var $all_permissions_are_loaded; /**< \private all_permissions_are_loaded */ - var $tab_loaded=array(); // Tableau pour signaler les permissions deja chargees + private $_tab_loaded=array(); // Array of cache of already loaded permissions var $oldcopy; // To contains a clone of this when we need to save old properties of object @@ -270,13 +270,13 @@ class User extends CommonObject * @param int $rid id du droit a ajouter * @param string $allmodule Ajouter tous les droits du module allmodule * @param string $allperms Ajouter tous les droits du module allmodule, perms allperms - * @return int > 0 si ok, < 0 si erreur + * @return int > 0 if OK, < 0 if KO */ function addrights($rid,$allmodule='',$allperms='') { global $conf; - dol_syslog("User::addrights $rid, $allmodule, $allperms"); + dol_syslog(get_class($this)."::addrights $rid, $allmodule, $allperms"); $err=0; $whereforadd=''; @@ -336,9 +336,9 @@ class User extends CommonObject $obj = $this->db->fetch_object($result); $nid = $obj->id; - $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = $this->id AND fk_id=$nid"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = ".$this->id." AND fk_id=".$nid; if (! $this->db->query($sql)) $err++; - $sql = "INSERT INTO ".MAIN_DB_PREFIX."user_rights (fk_user, fk_id) VALUES ($this->id, $nid)"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."user_rights (fk_user, fk_id) VALUES (".$this->id.", ".$nid.")"; if (! $this->db->query($sql)) $err++; $i++; @@ -369,7 +369,7 @@ class User extends CommonObject * @param int $rid Id du droit a retirer * @param string $allmodule Retirer tous les droits du module allmodule * @param string $allperms Retirer tous les droits du module allmodule, perms allperms - * @return int > 0 si ok, < 0 si erreur + * @return int > 0 if OK, < 0 if OK */ function delrights($rid,$allmodule='',$allperms='') { @@ -434,7 +434,7 @@ class User extends CommonObject $nid = $obj->id; $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights"; - $sql.= " WHERE fk_user = $this->id AND fk_id=$nid"; + $sql.= " WHERE fk_user = ".$this->id." AND fk_id=".$nid; if (! $this->db->query($sql)) $err++; $i++; @@ -469,7 +469,7 @@ class User extends CommonObject dol_syslog(get_class($this)."::clearrights reset user->rights"); $this->rights=''; $this->all_permissions_are_loaded=false; - $this->tab_loaded=array(); + $this->_tab_loaded=array(); } @@ -483,7 +483,7 @@ class User extends CommonObject { global $conf; - if ($moduletag && isset($this->tab_loaded[$moduletag]) && $this->tab_loaded[$moduletag]) + if ($moduletag && isset($this->_tab_loaded[$moduletag]) && $this->_tab_loaded[$moduletag]) { // Le fichier de ce module est deja charge return; @@ -507,15 +507,15 @@ class User extends CommonObject $sql.= " AND r.perms IS NOT NULL"; if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'"; - dol_syslog(get_class($this).'::getRights sql='.$sql, LOG_DEBUG); - $result = $this->db->query($sql); - if ($result) + dol_syslog(get_class($this).'::getrights sql='.$sql, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { - $num = $this->db->num_rows($result); + $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { - $obj = $this->db->fetch_object($result); + $obj = $this->db->fetch_object($resql); $module=$obj->module; $perms=$obj->perms; @@ -540,7 +540,7 @@ class User extends CommonObject } $i++; } - $this->db->free($result); + $this->db->free($resql); } // Maintenant les droits groupes @@ -556,32 +556,35 @@ class User extends CommonObject $sql.= " AND gu.entity IN (0,".$conf->entity.")"; if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'"; - dol_syslog(get_class($this).'::getRights sql='.$sql, LOG_DEBUG); - $result = $this->db->query($sql); - if ($result) + dol_syslog(get_class($this).'::getrights sql='.$sql, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { - $num = $this->db->num_rows($result); + $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { - $row = $this->db->fetch_row($result); + $obj = $this->db->fetch_object($resql); - if (dol_strlen($row[1]) > 0) + $module=$obj->module; + $perms=$obj->perms; + $subperms=$obj->subperms; + + if ($perms) { - - if (dol_strlen($row[2]) > 0) + if ($subperms) { - $this->rights->$row[0]->$row[1]->$row[2] = 1; + $this->rights->$module->$perms->$subperms = 1; } else { - $this->rights->$row[0]->$row[1] = 1; + $this->rights->$module->$perms = 1; } } $i++; } - $this->db->free($result); + $this->db->free($resql); } if (! $moduletag) @@ -593,7 +596,7 @@ class User extends CommonObject else { // Si module defini, on le marque comme charge en cache - $this->tab_loaded[$moduletag]=1; + $this->_tab_loaded[$moduletag]=1; } } diff --git a/htdocs/user/class/usergroup.class.php b/htdocs/user/class/usergroup.class.php index 653a28411c3..a1b95bec331 100644 --- a/htdocs/user/class/usergroup.class.php +++ b/htdocs/user/class/usergroup.class.php @@ -1,6 +1,6 @@ - * Copyright (c) 2005-2010 Laurent Destailleur + * Copyright (c) 2005-2012 Laurent Destailleur * Copyright (c) 2005-2011 Regis Houssin * * This program is free software; you can redistribute it and/or modify @@ -19,8 +19,7 @@ /** * \file htdocs/user/class/usergroup.class.php - * \brief Fichier de la classe des groupes d'utilisateur - * \author Rodolphe Qiedeville + * \brief File of class to manage user groups */ require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php"); @@ -46,6 +45,8 @@ class UserGroup extends CommonObject var $datem; // Modification date of group var $members=array(); // Array of users + private $_tab_loaded=array(); // Array of cache of already loaded permissions + var $oldcopy; // To contains a clone of this when we need to save old properties of object @@ -78,7 +79,7 @@ class UserGroup extends CommonObject $sql.= " FROM ".MAIN_DB_PREFIX."usergroup as g"; $sql.= " WHERE g.rowid = ".$this->id; - dol_syslog("Usergroup::fetch sql=".$sql); + dol_syslog(get_class($this)."::fetch sql=".$sql); $result = $this->db->query($sql); if ($result) { @@ -105,7 +106,7 @@ class UserGroup extends CommonObject else { $this->error=$this->db->lasterror(); - dol_syslog("UserGroup::Fetch ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::Fetch ".$this->error, LOG_ERR); return -1; } } @@ -138,7 +139,7 @@ class UserGroup extends CommonObject } $sql.= " ORDER BY g.nom"; - dol_syslog("UserGroup::listGroupsForUser sql=".$sql,LOG_DEBUG); + dol_syslog(get_class($this)."::listGroupsForUser sql=".$sql,LOG_DEBUG); $result = $this->db->query($sql); if ($result) { @@ -158,7 +159,7 @@ class UserGroup extends CommonObject else { $this->error=$this->db->lasterror(); - dol_syslog("UserGroup::listGroupsForUser ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::listGroupsForUser ".$this->error, LOG_ERR); return -1; } } @@ -187,7 +188,7 @@ class UserGroup extends CommonObject { $sql.= " AND u.entity IN (0,".$conf->entity.")"; } - dol_syslog("UserGroup::listUsersForGroup sql=".$sql,LOG_DEBUG); + dol_syslog(get_class($this)."::listUsersForGroup sql=".$sql,LOG_DEBUG); $result = $this->db->query($sql); if ($result) { @@ -207,7 +208,7 @@ class UserGroup extends CommonObject else { $this->error=$this->db->lasterror(); - dol_syslog("UserGroup::listUsersForGroup ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::listUsersForGroup ".$this->error, LOG_ERR); return -1; } } @@ -224,6 +225,7 @@ class UserGroup extends CommonObject { global $conf; + dol_syslog(get_class($this)."::addrights $rid, $allmodule, $allperms"); $err=0; $whereforadd=''; @@ -253,8 +255,8 @@ class UserGroup extends CommonObject // Where pour la liste des droits a ajouter $whereforadd="id=".$rid; // Ajout des droits induits - if ($subperms) $whereforadd.=" OR (module='$module' AND perms='$perms' AND (subperms='lire' OR subperms='read'))"; - if ($perms) $whereforadd.=" OR (module='$module' AND (perms='lire' OR perms='read') AND subperms IS NULL)"; + if ($subperms) $whereforadd.=" OR (module='$module' AND perms='$perms' AND (subperms='lire' OR subperms='read'))"; + else if ($perms) $whereforadd.=" OR (module='$module' AND (perms='lire' OR perms='read') AND subperms IS NULL)"; // Pour compatibilite, si lowid = 0, on est en mode ajout de tout // TODO A virer quand sera gere par l'appelant @@ -285,7 +287,7 @@ class UserGroup extends CommonObject $obj = $this->db->fetch_object($result); $nid = $obj->id; - $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights WHERE fk_usergroup = $this->id AND fk_id=$nid"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights WHERE fk_usergroup = $this->id AND fk_id=".$nid; if (! $this->db->query($sql)) $err++; $sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_rights (fk_usergroup, fk_id) VALUES ($this->id, $nid)"; if (! $this->db->query($sql)) $err++; @@ -385,7 +387,8 @@ class UserGroup extends CommonObject $obj = $this->db->fetch_object($result); $nid = $obj->id; - $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights WHERE fk_usergroup = $this->id AND fk_id=$nid"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights"; + $sql.= " WHERE fk_usergroup = $this->id AND fk_id=".$nid; if (! $this->db->query($sql)) $err++; $i++; @@ -416,10 +419,16 @@ class UserGroup extends CommonObject * @param string $module Nom du module dont il faut recuperer les droits ('' par defaut signifie tous les droits) * @return int <0 if KO, >0 if OK */ - function getrights($module='') + function getrights($moduletag='') { global $conf; + if ($moduletag && isset($this->_tab_loaded[$moduletag]) && $this->_tab_loaded[$moduletag]) + { + // Le fichier de ce module est deja charge + return; + } + if ($this->all_permissions_are_loaded) { // Si les permissions ont deja ete chargees, on quitte @@ -435,6 +444,9 @@ class UserGroup extends CommonObject $sql.= " AND r.entity = ".$conf->entity; $sql.= " AND u.fk_usergroup = ".$this->id; $sql.= " AND r.perms IS NOT NULL"; + if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'"; + + dol_syslog(get_class($this).'::getrights sql='.$sql, LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { @@ -442,31 +454,40 @@ class UserGroup extends CommonObject $i = 0; while ($i < $num) { - $row = $this->db->fetch_row($resql); + $obj = $this->db->fetch_object($resql); - if (dol_strlen($row[1]) > 0) + $module=$obj->module; + $perms=$obj->perms; + $subperms=$obj->subperms; + + if ($perms) { - - if (dol_strlen($row[2]) > 0) + if ($subperms) { - $this->rights->$row[0]->$row[1]->$row[2] = 1; + $this->rights->$module->$perms->$subperms = 1; } else { - $this->rights->$row[0]->$row[1] = 1; + $this->rights->$module->$perms = 1; } } $i++; } + $this->db->free($resql); } - if ($module == '') + if ($moduletag == '') { // Si module etait non defini, alors on a tout charge, on peut donc considerer - // que les droits sont en cache (car tous charges) pour cet instance de user + // que les droits sont en cache (car tous charges) pour cet instance de group $this->all_permissions_are_loaded=1; } + else + { + // Si module defini, on le marque comme charge en cache + $this->_tab_loaded[$moduletag]=1; + } return 1; } @@ -481,7 +502,7 @@ class UserGroup extends CommonObject global $user,$conf,$langs; $error=0; - + $this->db->begin(); $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights";