[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/htdocs/ -> usergroup.class.php (source)

   1  <?php
   2  /* Copyright (c) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   3   * Copyright (c) 2005 Laurent Destailleur  <eldy@users.sourceforge.net>
   4   * 
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License
  16   * along with this program; if not, write to the Free Software
  17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18   *
  19   * $Id: usergroup.class.php,v 1.8 2005/04/01 22:31:01 eldy Exp $
  20   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/usergroup.class.php,v $
  21   */
  22  
  23  /**      
  24       \file       htdocs/usergroup.class.php
  25       \brief      Fichier de la classe des groupes d'utilisateur
  26       \author     Rodolphe Qiedeville
  27       \version    $Revision: 1.8 $
  28  */
  29  
  30  /**    
  31         \class      UserGroup
  32         \brief      Classe permettant la gestion des groupes d'utilisateur
  33  */
  34  
  35  class UserGroup
  36  {
  37      var $db;
  38      
  39      var $id;
  40      var $nom;
  41      var $note;
  42      var $datec;
  43      var $datem;
  44  
  45  
  46    /**
  47     *    \brief Constructeur de la classe
  48     *    \param  $DB         handler accès base de données
  49     *    \param  $id         id du groupe (0 par défaut)
  50     */
  51       
  52      function UserGroup($DB, $id=0)
  53      {
  54          $this->db = $DB;
  55          $this->id = $id;
  56          
  57          return 0;
  58      }
  59  
  60  
  61    /**
  62     *    \brief      Charge un objet user avec toutes ces caractéristiques
  63     *    \param      id      id du groupe à charger
  64     */
  65       
  66      function fetch($id)
  67      {
  68          $this->id = $id;
  69      
  70          $sql  = "SELECT g.rowid, g.nom, g.note, g.datec, tms as datem";
  71          $sql .= " FROM ".MAIN_DB_PREFIX."usergroup as g";
  72          $sql .= " WHERE g.rowid = ".$this->id;
  73      
  74          $result = $this->db->query($sql);
  75      
  76          if ($result)
  77          {
  78              if ($this->db->num_rows($result))
  79              {
  80                  $obj = $this->db->fetch_object($result);
  81      
  82                  $this->id = $obj->rowid;
  83                  $this->nom  = $obj->nom;
  84                  $this->note = $obj->note;
  85                  $this->datec = $obj->datec;
  86                  $this->datem = $obj->datem;
  87              }
  88              $this->db->free($result);
  89      
  90          }
  91          else
  92          {
  93              dolibarr_syslog("UserGroup::Fetch Erreur");
  94          }
  95      
  96      }
  97  
  98    
  99    /**
 100     *    \brief      Ajoute un droit a l'utilisateur
 101     *    \param      rid         id du droit à ajouter
 102     *    \param      allmodule   Ajouter tous les droits du module allmodule
 103     *    \param      allperms    Ajouter tous les droits du module allmodule, perms allperms
 104     *    \return     int         > 0 si ok, < 0 si erreur
 105     */
 106       
 107      function addrights($rid,$allmodule='',$allperms='')
 108      {
 109          $err=0;
 110          $whereforadd='';
 111          
 112          $this->db->begin();
 113  
 114          if ($rid) 
 115          {
 116              // Si on a demandé ajout d'un droit en particulier, on récupère
 117              // les caractéristiques (module, perms et subperms) de ce droit.
 118              $sql = "SELECT module, perms, subperms";
 119              $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
 120              $sql.= " WHERE ";
 121              $sql.=" id = '".$rid."'";
 122         
 123              $result=$this->db->query($sql);
 124              if ($result) {
 125                  $obj = $this->db->fetch_object($result);
 126                  $module=$obj->module;
 127                  $perms=$obj->perms;
 128                  $subperms=$obj->subperms;
 129              }
 130              else {
 131                  $err++;
 132                  dolibarr_print_error($this->db);
 133              }
 134  
 135              // Where pour la liste des droits à ajouter
 136              $whereforadd="id=".$rid;
 137              // Ajout des droits induits
 138              if ($subperms) $whereforadd.=" OR (module='$module' AND perms='$perms' AND subperms='lire')";
 139              if ($perms)    $whereforadd.=" OR (module='$module' AND perms='lire' AND subperms IS NULL)";
 140  
 141              // Pour compatibilité, si lowid = 0, on est en mode ajout de tout
 142              // \todo A virer quand sera géré par l'appelant
 143              if (substr($rid,-1,1) == 0) $whereforadd="module='$module'";
 144          }
 145          else {
 146              // Where pour la liste des droits à ajouter
 147              if ($allmodule) $whereforadd="module='$allmodule'";
 148              if ($allperms)  $whereforadd=" AND perms='$allperms'";
 149          }
 150  
 151          // Ajout des droits de la liste whereforadd
 152          if ($whereforadd)
 153          {
 154              //print "$module-$perms-$subperms";
 155              $sql = "SELECT id";
 156              $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
 157              $sql.= " WHERE $whereforadd";
 158              
 159              $result=$this->db->query($sql);
 160              if ($result)
 161              {
 162                  $num = $this->db->num_rows($result);
 163                  $i = 0;
 164                  while ($i < $num)
 165                  {
 166                      $obj = $this->db->fetch_object($result);
 167                      $nid = $obj->id;
 168         
 169                      $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights WHERE fk_usergroup = $this->id AND fk_id=$nid";
 170                      if (! $this->db->query($sql)) $err++;
 171                      $sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_rights (fk_usergroup, fk_id) VALUES ($this->id, $nid)";
 172                      if (! $this->db->query($sql)) $err++;
 173      
 174                      $i++;
 175                  }
 176              }
 177              else 
 178              {
 179                  $err++;
 180                  dolibarr_print_error($this->db);
 181              }
 182          }
 183      
 184          if ($err) {
 185              $this->db->rollback();
 186              return -$err;
 187          }
 188          else {
 189              $this->db->commit();
 190              return 1;
 191          }
 192          
 193      }
 194  
 195  
 196    /**
 197     *    \brief      Retire un droit a l'utilisateur
 198     *    \param      rid         id du droit à retirer
 199     *    \param      allmodule   Retirer tous les droits du module allmodule
 200     *    \param      allperms    Retirer tous les droits du module allmodule, perms allperms
 201     *    \return     int         > 0 si ok, < 0 si erreur
 202     */
 203       
 204      function delrights($rid,$allmodule='',$allperms='')
 205      {
 206          $err=0;
 207          $wherefordel='';
 208          
 209          $this->db->begin();
 210  
 211          if ($rid) 
 212          {
 213              // Si on a demandé supression d'un droit en particulier, on récupère
 214              // les caractéristiques module, perms et subperms de ce droit.
 215              $sql = "SELECT module, perms, subperms";
 216              $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
 217              $sql.= " WHERE ";
 218              $sql.=" id = '".$rid."'";
 219         
 220              $result=$this->db->query($sql);
 221              if ($result) {
 222                  $obj = $this->db->fetch_object($result);
 223                  $module=$obj->module;
 224                  $perms=$obj->perms;
 225                  $subperms=$obj->subperms;
 226              }
 227              else {
 228                  $err++;
 229                  dolibarr_print_error($this->db);
 230              }
 231  
 232              // Where pour la liste des droits à supprimer
 233              $wherefordel="id=".$rid;
 234              // Suppression des droits induits
 235              if ($subperms=='lire') $wherefordel.=" OR (module='$module' AND perms='$perms' AND subperms IS NOT NULL)";
 236              if ($perms=='lire')    $wherefordel.=" OR (module='$module')";
 237  
 238              // Pour compatibilité, si lowid = 0, on est en mode suppression de tout
 239              // \todo A virer quand sera géré par l'appelant
 240              if (substr($rid,-1,1) == 0) $wherefordel="module='$module'";
 241          }
 242          else {
 243              // Where pour la liste des droits à supprimer
 244              if ($allmodule) $wherefordel="module='$allmodule'";
 245              if ($allperms)  $wherefordel=" AND perms='$allperms'";
 246          }
 247  
 248          // Suppression des droits de la liste wherefordel
 249          if ($wherefordel)
 250          {
 251              //print "$module-$perms-$subperms";
 252              $sql = "SELECT id";
 253              $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
 254              $sql.= " WHERE $wherefordel";
 255  
 256              $result=$this->db->query($sql);
 257              if ($result)
 258              {
 259                  $num = $this->db->num_rows($result);
 260                  $i = 0;
 261                  while ($i < $num)
 262                  {
 263                      $obj = $this->db->fetch_object($result);
 264                      $nid = $obj->id;
 265         
 266                      $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights WHERE fk_usergroup = $this->id AND fk_id=$nid";
 267                      if (! $this->db->query($sql)) $err++;
 268      
 269                      $i++;
 270                  }
 271              }
 272              else 
 273              {
 274                  $err++;
 275                  dolibarr_print_error($this->db);
 276              }
 277          }
 278      
 279          if ($err) {
 280              $this->db->rollback();
 281              return -$err;
 282          }
 283          else {
 284              $this->db->commit();
 285              return 1;
 286          }
 287  
 288      }
 289      
 290  
 291    /**
 292     *    \brief      Charge dans l'objet group, la liste des permissions auquels le groupe a droit
 293     *    \param      module    nom du module dont il faut récupérer les droits ('' par defaut signifie tous les droits)
 294     */
 295       
 296    function getrights($module='')
 297      {
 298        if ($this->all_permissions_are_loaded)
 299        {
 300          // Si les permissions ont déja été chargées, on quitte
 301          return;
 302        }
 303  
 304        /*
 305         * Récupération des droits
 306         */
 307        $sql = "SELECT r.module, r.perms, r.subperms ";
 308        $sql .= " FROM ".MAIN_DB_PREFIX."usergroup_rights as u, ".MAIN_DB_PREFIX."rights_def as r";
 309        $sql .= " WHERE r.id = u.fk_id AND u.fk_usergroup= $this->id AND r.perms IS NOT NULL";
 310        if ($this->db->query($sql))
 311      {
 312        $num = $this->db->num_rows();
 313        $i = 0;
 314        while ($i < $num)
 315          {
 316            $row = $this->db->fetch_row();
 317  
 318            if (strlen($row[1]) > 0)
 319          {
 320  
 321            if (strlen($row[2]) > 0)
 322              {
 323                $this->rights->$row[0]->$row[1]->$row[2] = 1;
 324              }
 325            else
 326              {
 327                $this->rights->$row[0]->$row[1] = 1;
 328              }
 329  
 330          }
 331            $i++;
 332          }
 333      }
 334  
 335          if ($module == '')
 336          {
 337            // Si module etait non defini, alors on a tout chargé, on peut donc considérer
 338            // que les droits sont en cache (car tous chargés) pour cet instance de user
 339            $this->all_permissions_are_loaded=1;
 340          }
 341          
 342      }
 343  
 344    /**
 345     *        \brief      Efface un groupe de la base
 346     *        \return     < 0 si erreur, > 0 si ok
 347     */
 348       
 349    function delete()
 350    {
 351      $this->db->begin();
 352      
 353      $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights";
 354      $sql .= " WHERE fk_usergroup = ".$this->id;
 355      $this->db->query($sql);
 356  
 357      $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user";
 358      $sql .= " WHERE fk_usergroup = ".$this->id;
 359      $this->db->query($sql);
 360  
 361      $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup";
 362      $sql .= " WHERE rowid = ".$this->id;
 363      $result=$this->db->query($sql);
 364      if ($result) 
 365      {
 366          $this->db->commit();
 367          return 1;
 368      }
 369      else
 370      {
 371          $this->db->rollback();
 372          dolibarr_print_error($this->db);
 373          return -1;
 374      }
 375    }
 376  
 377    /**
 378     *        \brief      Crée un groupe en base
 379     *        \return     si erreur <0, si ok renvoie id groupe créé
 380     */
 381       
 382    function create()
 383    {
 384  
 385      $sql = "INSERT into ".MAIN_DB_PREFIX."usergroup (datec,nom)";
 386      $sql .= " VALUES(now(),'$this->nom')";
 387  
 388      $result=$this->db->query($sql);
 389      if ($result)
 390      {
 391          $table =  "".MAIN_DB_PREFIX."usergroup";
 392          $this->id = $this->db->last_insert_id($table);
 393  
 394          if ($this->update() < 0) return -2;
 395  
 396          return $this->id;
 397      }
 398      else
 399      {
 400          dolibarr_syslog("UserGroup::Create");
 401          return -1;
 402      }
 403    }
 404  
 405  
 406    /**
 407     *    \brief      Mise à jour en base d'un utilisateur
 408     *    \return     <0 si echec, >=0 si ok
 409     */
 410    function update()
 411      {
 412          $sql = "UPDATE ".MAIN_DB_PREFIX."usergroup SET ";
 413          $sql .= " nom = '$this->nom',";
 414          $sql .= " note = '$this->note'";
 415          $sql .= " WHERE rowid = ".$this->id;
 416  
 417          $result = $this->db->query($sql);
 418  
 419          if ($result)
 420          {
 421              if ($this->db->affected_rows())
 422              {
 423                  return 1;
 424              }
 425              return 0;
 426          }
 427          else
 428          {
 429              dolibarr_print_error($this->db);
 430              return -2;
 431          }
 432  
 433     }
 434     
 435  }
 436  
 437  ?>


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics