Qual: Uniformize code

This commit is contained in:
Laurent Destailleur 2012-01-11 01:06:56 +01:00
parent 4bb5d68ca9
commit a93a9f75f2
2 changed files with 75 additions and 51 deletions

View File

@ -24,7 +24,7 @@
/** /**
* \file htdocs/user/class/user.class.php * \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"); 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 $rights; // Array of permissions user->rights->permx
var $all_permissions_are_loaded; /**< \private all_permissions_are_loaded */ 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 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 int $rid id du droit a ajouter
* @param string $allmodule Ajouter tous les droits du module allmodule * @param string $allmodule Ajouter tous les droits du module allmodule
* @param string $allperms Ajouter tous les droits du module allmodule, perms allperms * @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='') function addrights($rid,$allmodule='',$allperms='')
{ {
global $conf; global $conf;
dol_syslog("User::addrights $rid, $allmodule, $allperms"); dol_syslog(get_class($this)."::addrights $rid, $allmodule, $allperms");
$err=0; $err=0;
$whereforadd=''; $whereforadd='';
@ -336,9 +336,9 @@ class User extends CommonObject
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($result);
$nid = $obj->id; $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++; 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++; if (! $this->db->query($sql)) $err++;
$i++; $i++;
@ -369,7 +369,7 @@ class User extends CommonObject
* @param int $rid Id du droit a retirer * @param int $rid Id du droit a retirer
* @param string $allmodule Retirer tous les droits du module allmodule * @param string $allmodule Retirer tous les droits du module allmodule
* @param string $allperms Retirer tous les droits du module allmodule, perms allperms * @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='') function delrights($rid,$allmodule='',$allperms='')
{ {
@ -434,7 +434,7 @@ class User extends CommonObject
$nid = $obj->id; $nid = $obj->id;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights"; $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++; if (! $this->db->query($sql)) $err++;
$i++; $i++;
@ -469,7 +469,7 @@ class User extends CommonObject
dol_syslog(get_class($this)."::clearrights reset user->rights"); dol_syslog(get_class($this)."::clearrights reset user->rights");
$this->rights=''; $this->rights='';
$this->all_permissions_are_loaded=false; $this->all_permissions_are_loaded=false;
$this->tab_loaded=array(); $this->_tab_loaded=array();
} }
@ -483,7 +483,7 @@ class User extends CommonObject
{ {
global $conf; 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 // Le fichier de ce module est deja charge
return; return;
@ -507,15 +507,15 @@ class User extends CommonObject
$sql.= " AND r.perms IS NOT NULL"; $sql.= " AND r.perms IS NOT NULL";
if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'"; if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
dol_syslog(get_class($this).'::getRights sql='.$sql, LOG_DEBUG); dol_syslog(get_class($this).'::getrights sql='.$sql, LOG_DEBUG);
$result = $this->db->query($sql); $resql = $this->db->query($sql);
if ($result) if ($resql)
{ {
$num = $this->db->num_rows($result); $num = $this->db->num_rows($resql);
$i = 0; $i = 0;
while ($i < $num) while ($i < $num)
{ {
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($resql);
$module=$obj->module; $module=$obj->module;
$perms=$obj->perms; $perms=$obj->perms;
@ -540,7 +540,7 @@ class User extends CommonObject
} }
$i++; $i++;
} }
$this->db->free($result); $this->db->free($resql);
} }
// Maintenant les droits groupes // Maintenant les droits groupes
@ -556,32 +556,35 @@ class User extends CommonObject
$sql.= " AND gu.entity IN (0,".$conf->entity.")"; $sql.= " AND gu.entity IN (0,".$conf->entity.")";
if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'"; if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
dol_syslog(get_class($this).'::getRights sql='.$sql, LOG_DEBUG); dol_syslog(get_class($this).'::getrights sql='.$sql, LOG_DEBUG);
$result = $this->db->query($sql); $resql = $this->db->query($sql);
if ($result) if ($resql)
{ {
$num = $this->db->num_rows($result); $num = $this->db->num_rows($resql);
$i = 0; $i = 0;
while ($i < $num) 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 (dol_strlen($row[2]) > 0) if ($perms)
{ {
$this->rights->$row[0]->$row[1]->$row[2] = 1; if ($subperms)
{
$this->rights->$module->$perms->$subperms = 1;
} }
else else
{ {
$this->rights->$row[0]->$row[1] = 1; $this->rights->$module->$perms = 1;
} }
} }
$i++; $i++;
} }
$this->db->free($result); $this->db->free($resql);
} }
if (! $moduletag) if (! $moduletag)
@ -593,7 +596,7 @@ class User extends CommonObject
else else
{ {
// Si module defini, on le marque comme charge en cache // Si module defini, on le marque comme charge en cache
$this->tab_loaded[$moduletag]=1; $this->_tab_loaded[$moduletag]=1;
} }
} }

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (c) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (c) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (c) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (c) 2005-2011 Regis Houssin <regis@dolibarr.fr> * Copyright (c) 2005-2011 Regis Houssin <regis@dolibarr.fr>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -19,8 +19,7 @@
/** /**
* \file htdocs/user/class/usergroup.class.php * \file htdocs/user/class/usergroup.class.php
* \brief Fichier de la classe des groupes d'utilisateur * \brief File of class to manage user groups
* \author Rodolphe Qiedeville
*/ */
require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php"); 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 $datem; // Modification date of group
var $members=array(); // Array of users 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 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.= " FROM ".MAIN_DB_PREFIX."usergroup as g";
$sql.= " WHERE g.rowid = ".$this->id; $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); $result = $this->db->query($sql);
if ($result) if ($result)
{ {
@ -105,7 +106,7 @@ class UserGroup extends CommonObject
else else
{ {
$this->error=$this->db->lasterror(); $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; return -1;
} }
} }
@ -138,7 +139,7 @@ class UserGroup extends CommonObject
} }
$sql.= " ORDER BY g.nom"; $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); $result = $this->db->query($sql);
if ($result) if ($result)
{ {
@ -158,7 +159,7 @@ class UserGroup extends CommonObject
else else
{ {
$this->error=$this->db->lasterror(); $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; return -1;
} }
} }
@ -187,7 +188,7 @@ class UserGroup extends CommonObject
{ {
$sql.= " AND u.entity IN (0,".$conf->entity.")"; $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); $result = $this->db->query($sql);
if ($result) if ($result)
{ {
@ -207,7 +208,7 @@ class UserGroup extends CommonObject
else else
{ {
$this->error=$this->db->lasterror(); $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; return -1;
} }
} }
@ -224,6 +225,7 @@ class UserGroup extends CommonObject
{ {
global $conf; global $conf;
dol_syslog(get_class($this)."::addrights $rid, $allmodule, $allperms");
$err=0; $err=0;
$whereforadd=''; $whereforadd='';
@ -254,7 +256,7 @@ class UserGroup extends CommonObject
$whereforadd="id=".$rid; $whereforadd="id=".$rid;
// Ajout des droits induits // Ajout des droits induits
if ($subperms) $whereforadd.=" OR (module='$module' AND perms='$perms' AND (subperms='lire' OR subperms='read'))"; 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)"; 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 // Pour compatibilite, si lowid = 0, on est en mode ajout de tout
// TODO A virer quand sera gere par l'appelant // TODO A virer quand sera gere par l'appelant
@ -285,7 +287,7 @@ class UserGroup extends CommonObject
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($result);
$nid = $obj->id; $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++; if (! $this->db->query($sql)) $err++;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_rights (fk_usergroup, fk_id) VALUES ($this->id, $nid)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_rights (fk_usergroup, fk_id) VALUES ($this->id, $nid)";
if (! $this->db->query($sql)) $err++; if (! $this->db->query($sql)) $err++;
@ -385,7 +387,8 @@ class UserGroup extends CommonObject
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($result);
$nid = $obj->id; $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++; if (! $this->db->query($sql)) $err++;
$i++; $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) * @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 * @return int <0 if KO, >0 if OK
*/ */
function getrights($module='') function getrights($moduletag='')
{ {
global $conf; 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) if ($this->all_permissions_are_loaded)
{ {
// Si les permissions ont deja ete chargees, on quitte // 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 r.entity = ".$conf->entity;
$sql.= " AND u.fk_usergroup = ".$this->id; $sql.= " AND u.fk_usergroup = ".$this->id;
$sql.= " AND r.perms IS NOT NULL"; $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); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -442,31 +454,40 @@ class UserGroup extends CommonObject
$i = 0; $i = 0;
while ($i < $num) 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 (dol_strlen($row[2]) > 0) if ($perms)
{ {
$this->rights->$row[0]->$row[1]->$row[2] = 1; if ($subperms)
{
$this->rights->$module->$perms->$subperms = 1;
} }
else else
{ {
$this->rights->$row[0]->$row[1] = 1; $this->rights->$module->$perms = 1;
} }
} }
$i++; $i++;
} }
$this->db->free($resql);
} }
if ($module == '') if ($moduletag == '')
{ {
// Si module etait non defini, alors on a tout charge, on peut donc considerer // 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; $this->all_permissions_are_loaded=1;
} }
else
{
// Si module defini, on le marque comme charge en cache
$this->_tab_loaded[$moduletag]=1;
}
return 1; return 1;
} }