[ 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/ -> user.class.php (source)

   1  <?php
   2  /* Copyright (c) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   3   * Copyright (c) 2002-2003 Jean-Louis Bergamo   <jlb@j1b.org>
   4   * Copyright (c) 2004-2005 Laurent Destailleur  <eldy@users.sourceforge.net>
   5   * Copyright (C) 2004      Sebastien Di Cintio  <sdicintio@ressource-toi.org>
   6   * Copyright (C) 2004      Benoit Mortier       <benoit.mortier@opensides.be>
   7   * Copyright (C) 2005      Regis Houssin        <regis.houssin@cap-networks.com>
   8   * Copyright (C) 2005      Lionel COUSTEIX      <etm_ltd@tiscali.co.uk>
   9   * 
  10   * This program is free software; you can redistribute it and/or modify
  11   * it under the terms of the GNU General Public License as published by
  12   * the Free Software Foundation; either version 2 of the License, or
  13   * (at your option) any later version.
  14   *
  15   * This program is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with this program; if not, write to the Free Software
  22   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23   *
  24   * $Id: user.class.php,v 1.76.2.1 2006/01/04 19:02:49 eldy Exp $
  25   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/user.class.php,v $
  26   */
  27  
  28  /**
  29          \file       htdocs/user.class.php
  30            \brief      Fichier de la classe utilisateur
  31            \author     Rodolphe Qiedeville
  32            \author     Jean-Louis Bergamo
  33            \author     Laurent Destailleur
  34            \author     Sebastien Di Cintio
  35            \author     Benoit Mortier
  36            \author     Regis Houssin
  37            \version    $Revision: 1.76.2.1 $
  38  */
  39  
  40  
  41  
  42  /**
  43          \class      User
  44          \brief      Classe permettant la gestion d'un utilisateur
  45  */
  46  
  47  class User
  48  {
  49    var $db;
  50      
  51    var $id;
  52    var $fullname;
  53    var $nom;
  54    var $prenom;
  55    var $note;
  56    var $code;
  57    var $email;
  58    var $office_tel;
  59    var $office_fax;
  60    var $user_mobile;
  61    var $admin;
  62    var $login;
  63    var $pass;
  64    var $lang;
  65    var $datec;
  66    var $datem;
  67    var $societe_id;
  68    var $webcal_login;
  69    var $datelastaccess;
  70    
  71    var $error;
  72    var $userpref_limite_liste;
  73    var $all_permissions_are_loaded;         /**< \private all_permissions_are_loaded */
  74  
  75  
  76    /**
  77     *    \brief Constructeur de la classe
  78     *    \param  $DB         handler accès base de données
  79     *    \param  $id         id de l'utilisateur (0 par défaut)
  80     */
  81       
  82    function User($DB, $id=0)
  83      {
  84  
  85        $this->db = $DB;
  86        $this->id = $id;
  87  
  88        // Preference utilisateur
  89        $this->liste_limit = 0;
  90        $this->clicktodial_enabled = 0;
  91            
  92        $this->all_permissions_are_loaded = 0;
  93  
  94        return 1;
  95    }
  96  
  97  
  98    /**
  99     *    \brief      Charge un objet user avec toutes ces caractéristiques depuis un id ou login
 100     *    \param      login       Si défini, login a utiliser pour recherche
 101     */
 102       
 103    function fetch($login='')
 104      {
 105          // Recupere utilisateur
 106          $sql = "SELECT u.rowid, u.name, u.firstname, u.email, u.office_phone, u.office_fax, u.user_mobile, u.code, u.admin, u.login, u.pass, u.webcal_login, u.note,";
 107          $sql.= " u.fk_societe, u.fk_socpeople, ";
 108          $sql.= " ".$this->db->pdate("u.datec")." as datec, ".$this->db->pdate("u.tms")." as datem,";
 109          $sql.= " ".$this->db->pdate("u.datelastaccess")." as datel";
 110          $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
 111          if ($login)
 112          {
 113              $sql .= " WHERE u.login = '$login'";
 114          }
 115          else
 116          {
 117              $sql .= " WHERE u.rowid = ".$this->id;
 118          }
 119          
 120          $result = $this->db->query($sql);
 121          if ($result)
 122          {
 123              $obj = $this->db->fetch_object($result);
 124              if ($obj)
 125              {
 126                  $this->id = $obj->rowid;
 127                  $this->nom = stripslashes($obj->name);
 128                  $this->prenom = stripslashes($obj->firstname);
 129          
 130                  $this->fullname = $this->prenom . ' ' . $this->nom;
 131                  $this->code = $obj->code;
 132                  $this->login = $obj->login;
 133                  $this->pass  = $obj->pass;
 134                   $this->office_phone  = $obj->office_phone;
 135                   $this->office_fax  = $obj->office_fax;
 136                   $this->user_mobile  = $obj->user_mobile;
 137                  $this->email = $obj->email;
 138                  $this->admin = $obj->admin;
 139                  $this->contact_id = $obj->fk_socpeople;
 140                  $this->note = stripslashes($obj->note);
 141                  $this->lang = 'fr_FR';    // \todo Gérer la langue par défaut d'un utilisateur Dolibarr
 142  
 143                  $this->datec  = $obj->datec;
 144                  $this->datem  = $obj->datem;
 145                  $this->datelastaccess = $obj->datel;
 146          
 147                  $this->webcal_login = $obj->webcal_login;
 148                  $this->societe_id = $obj->fk_societe;
 149              }
 150              $this->db->free($result);
 151          
 152          }
 153          else
 154          {
 155              dolibarr_print_error($this->db);
 156          }
 157          
 158          // Recupere parametrage global propre à l'utilisateur
 159          // \todo a stocker/recupérer en session pour eviter ce select a chaque page
 160          $sql = "SELECT param, value FROM ".MAIN_DB_PREFIX."user_param";
 161          $sql.= " WHERE fk_user = ".$this->id;
 162          $sql.= " AND page = ''";
 163          $result=$this->db->query($sql);
 164          if ($result)
 165          {
 166              $num = $this->db->num_rows($result);
 167              $i = 0;
 168              while ($i < $num)
 169              {
 170                  $obj = $this->db->fetch_object($result);
 171                  $p=$obj->param;
 172                  $this->conf->$p = $obj->value;
 173                  $i++;
 174              }
 175              $this->db->free($result);
 176          }
 177          else
 178          {
 179              dolibarr_print_error($this->db);
 180          }
 181  
 182          // Recupere parametrage propre à la page et à l'utilisateur
 183          // \todo SCRIPT_URL non defini sur tous serveurs 
 184          // Paramétrage par page desactivé pour l'instant
 185          if (1==2 && isset($_SERVER['SCRIPT_URL']))
 186          {
 187              $sql = "SELECT param, value FROM ".MAIN_DB_PREFIX."user_param";
 188              $sql.= " WHERE fk_user = ".$this->id;
 189              $sql.= " AND page='".$_SERVER['SCRIPT_URL']."'";
 190              $result=$this->db->query($sql);
 191              if ($result)
 192              {
 193                  $num = $this->db->num_rows($result);
 194                  $i = 0;
 195                  $page_param_url = '';
 196                  $this->page_param = array();
 197                  while ($i < $num)
 198                  {
 199                      $obj = $this->db->fetch_object($result);
 200                      $this->page_param[$obj->param] = $obj->value;
 201                      $page_param_url .= $obj->param."=".$obj->value."&amp;";
 202                      $i++;
 203                  }
 204                  $this->page_param_url = $page_param_url;
 205                  $this->db->free($result);
 206              }
 207              else
 208              {
 209                  dolibarr_print_error($this->db);
 210              }
 211          }
 212      }
 213    
 214    /**
 215     *    \brief      Ajoute un droit a l'utilisateur
 216     *    \param      rid         id du droit à ajouter
 217     *    \param      allmodule   Ajouter tous les droits du module allmodule
 218     *    \param      allperms    Ajouter tous les droits du module allmodule, perms allperms
 219     *    \return     int         > 0 si ok, < 0 si erreur
 220     */
 221       
 222      function addrights($rid,$allmodule='',$allperms='')
 223      {
 224          dolibarr_syslog("User::addrights $rid, $allmodule, $allperms");
 225          $err=0;
 226          $whereforadd='';
 227          
 228          $this->db->begin();
 229  
 230          if ($rid) 
 231          {
 232              // Si on a demandé ajout d'un droit en particulier, on récupère
 233              // les caractéristiques (module, perms et subperms) de ce droit.
 234              $sql = "SELECT module, perms, subperms";
 235              $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
 236              $sql.= " WHERE ";
 237              $sql.=" id = '".$rid."'";
 238         
 239              $result=$this->db->query($sql);
 240              if ($result) {
 241                  $obj = $this->db->fetch_object($result);
 242                  $module=$obj->module;
 243                  $perms=$obj->perms;
 244                  $subperms=$obj->subperms;
 245              }
 246              else {
 247                  $err++;
 248                  dolibarr_print_error($this->db);
 249              }
 250  
 251              // Where pour la liste des droits à ajouter
 252              $whereforadd="id=".$rid;
 253              // Ajout des droits induits
 254              if ($subperms) $whereforadd.=" OR (module='$module' AND perms='$perms' AND subperms='lire')";
 255              if ($perms)    $whereforadd.=" OR (module='$module' AND perms='lire' AND subperms IS NULL)";
 256          }
 257          else {
 258              // Where pour la liste des droits à ajouter
 259              if ($allmodule) $whereforadd="module='$allmodule'";
 260              if ($allperms)  $whereforadd=" AND perms='$allperms'";
 261          }
 262  
 263          // Ajout des droits de la liste whereforadd
 264          if ($whereforadd)
 265          {
 266              //print "$module-$perms-$subperms";
 267              $sql = "SELECT id";
 268              $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
 269              $sql.= " WHERE $whereforadd";
 270              
 271              $result=$this->db->query($sql);
 272              if ($result)
 273              {
 274                  $num = $this->db->num_rows($result);
 275                  $i = 0;
 276                  while ($i < $num)
 277                  {
 278                      $obj = $this->db->fetch_object($result);
 279                      $nid = $obj->id;
 280         
 281                      $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = $this->id AND fk_id=$nid";
 282                      if (! $this->db->query($sql)) $err++;
 283                      $sql = "INSERT INTO ".MAIN_DB_PREFIX."user_rights (fk_user, fk_id) VALUES ($this->id, $nid)";
 284                      if (! $this->db->query($sql)) $err++;
 285      
 286                      $i++;
 287                  }
 288              }
 289              else 
 290              {
 291                  $err++;
 292                  dolibarr_print_error($this->db);
 293              }
 294          }
 295      
 296          if ($err) {
 297              $this->db->rollback();
 298              return -$err;
 299          }
 300          else {
 301              $this->db->commit();
 302              return 1;
 303          }
 304          
 305      }
 306  
 307  
 308    /**
 309     *    \brief      Retire un droit a l'utilisateur
 310     *    \param      rid         id du droit à retirer
 311     *    \param      allmodule   Retirer tous les droits du module allmodule
 312     *    \param      allperms    Retirer tous les droits du module allmodule, perms allperms
 313     *    \return     int         > 0 si ok, < 0 si erreur
 314     */
 315       
 316      function delrights($rid,$allmodule='',$allperms='')
 317      {
 318          $err=0;
 319          $wherefordel='';
 320          
 321          $this->db->begin();
 322  
 323          if ($rid) 
 324          {
 325              // Si on a demandé supression d'un droit en particulier, on récupère
 326              // les caractéristiques module, perms et subperms de ce droit.
 327              $sql = "SELECT module, perms, subperms";
 328              $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
 329              $sql.= " WHERE ";
 330              $sql.=" id = '".$rid."'";
 331         
 332              $result=$this->db->query($sql);
 333              if ($result) {
 334                  $obj = $this->db->fetch_object($result);
 335                  $module=$obj->module;
 336                  $perms=$obj->perms;
 337                  $subperms=$obj->subperms;
 338              }
 339              else {
 340                  $err++;
 341                  dolibarr_print_error($this->db);
 342              }
 343  
 344              // Where pour la liste des droits à supprimer
 345              $wherefordel="id=".$rid;
 346              // Suppression des droits induits
 347              if ($subperms=='lire') $wherefordel.=" OR (module='$module' AND perms='$perms' AND subperms IS NOT NULL)";
 348              if ($perms=='lire')    $wherefordel.=" OR (module='$module')";
 349          }
 350          else {
 351              // Where pour la liste des droits à supprimer
 352              if ($allmodule) $wherefordel="module='$allmodule'";
 353              if ($allperms)  $wherefordel=" AND perms='$allperms'";
 354          }
 355  
 356          // Suppression des droits de la liste wherefordel
 357          if ($wherefordel)
 358          {
 359              //print "$module-$perms-$subperms";
 360              $sql = "SELECT id";
 361              $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
 362              $sql.= " WHERE $wherefordel";
 363  
 364              $result=$this->db->query($sql);
 365              if ($result)
 366              {
 367                  $num = $this->db->num_rows($result);
 368                  $i = 0;
 369                  while ($i < $num)
 370                  {
 371                      $obj = $this->db->fetch_object($result);
 372                      $nid = $obj->id;
 373         
 374                      $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = $this->id AND fk_id=$nid";
 375                      if (! $this->db->query($sql)) $err++;
 376      
 377                      $i++;
 378                  }
 379              }
 380              else 
 381              {
 382                  $err++;
 383                  dolibarr_print_error($this->db);
 384              }
 385          }
 386      
 387          if ($err) {
 388              $this->db->rollback();
 389              return -$err;
 390          }
 391          else {
 392              $this->db->commit();
 393              return 1;
 394          }
 395  
 396      }
 397  
 398    /**
 399     *    \brief      Charge dans l'objet user, la liste des permissions auxquelles l'utilisateur a droit
 400     *    \param      module    nom du module dont il faut récupérer les droits ('' par defaut signifie tous les droits)
 401     */
 402       
 403    function getrights($module='')
 404      {
 405          if ($this->all_permissions_are_loaded)
 406          {
 407              // Si les permissions ont déja été chargé pour ce user, on quitte
 408              return;
 409          }
 410          
 411          // Récupération des droits utilisateurs + récupération des droits groupes
 412  
 413          // D'abord les droits utilisateurs
 414          $sql = "SELECT r.module, r.perms, r.subperms";
 415          $sql .= " FROM ".MAIN_DB_PREFIX."user_rights as ur, ".MAIN_DB_PREFIX."rights_def as r";
 416          $sql .= " WHERE r.id = ur.fk_id AND ur.fk_user= ".$this->id." AND r.perms IS NOT NULL";
 417  
 418          $result = $this->db->query($sql);
 419          if ($result)
 420          {
 421              $num = $this->db->num_rows($result);
 422              $i = 0;
 423              while ($i < $num)
 424              {
 425                  $row = $this->db->fetch_row($result);
 426          
 427                  if (strlen($row[1]) > 0)
 428                  {
 429          
 430                      if (strlen($row[2]) > 0)
 431                      {
 432                          $this->rights->$row[0]->$row[1]->$row[2] = 1;
 433                      }
 434                      else
 435                      {
 436                          $this->rights->$row[0]->$row[1] = 1;
 437                      }
 438          
 439                  }
 440                  $i++;
 441              }
 442             $this->db->free($result);
 443          }
 444          
 445          // Maintenant les droits groupes
 446          $sql  = " SELECT r.module, r.perms, r.subperms";
 447          $sql .= " FROM ".MAIN_DB_PREFIX."usergroup_rights as gr, ".MAIN_DB_PREFIX."usergroup_user as gu, ".MAIN_DB_PREFIX."rights_def as r";
 448          $sql .= " WHERE r.id = gr.fk_id AND gr.fk_usergroup = gu.fk_usergroup AND gu.fk_user = ".$this->id." AND r.perms IS NOT NULL";
 449          
 450          $result = $this->db->query($sql);
 451          if ($result)
 452          {
 453              $num = $this->db->num_rows($result);
 454              $i = 0;
 455              while ($i < $num)
 456              {
 457                  $row = $this->db->fetch_row($result);
 458          
 459                  if (strlen($row[1]) > 0)
 460                  {
 461          
 462                      if (strlen($row[2]) > 0)
 463                      {
 464                          $this->rights->$row[0]->$row[1]->$row[2] = 1;
 465                      }
 466                      else
 467                      {
 468                          $this->rights->$row[0]->$row[1] = 1;
 469                      }
 470          
 471                  }
 472                  $i++;
 473              }
 474             $this->db->free($result);
 475          }
 476          
 477          if ($module == '')
 478          {
 479              // Si module etait non defini, alors on a tout chargé, on peut donc considérer
 480              // que les droits sont en cache (car tous chargés) pour cet instance de user
 481              $this->all_permissions_are_loaded=1;
 482          }
 483          
 484      }
 485  
 486  
 487      /**
 488       *      \brief      Désactive un utilisateur
 489       *      \return     int     <0 si ko, >0 si ok
 490       */
 491      function disable()
 492      {
 493          $error=0;
 494          
 495          // Désactive utilisateur
 496          $sql = "UPDATE ".MAIN_DB_PREFIX."user";
 497          $sql.= " SET login = NULL";
 498          $sql.= " WHERE rowid = ".$this->id;
 499          $result = $this->db->query($sql);
 500  
 501          if ($result)
 502          {
 503              // Appel des triggers
 504              include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php");
 505              $interface=new Interfaces($this->db);
 506              $result=$interface->run_triggers('USER_DISABLE',$this,$user,$lang,$conf);
 507              if ($result < 0) $error++;
 508              // Fin appel triggers
 509          }
 510  
 511          if ($error)
 512          {
 513              return -$error;
 514          }
 515          else
 516          {
 517              return 1;
 518          }
 519      }
 520    
 521    
 522      /**
 523       *    \brief      Supprime complètement un utilisateur
 524       */
 525      function delete()
 526      {
 527          // Supprime droits
 528          $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = $this->id";
 529          if ($this->db->query($sql))
 530          {
 531      
 532          }
 533      
 534          // Si contact, supprime lien
 535          if ($this->contact_id)
 536          {
 537              $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET fk_user = null WHERE idp = $this->contact_id";
 538              if ($this->db->query($sql))
 539              {
 540      
 541              }
 542          }
 543      
 544          // Supprime utilisateur
 545          $sql = "DELETE FROM ".MAIN_DB_PREFIX."user WHERE rowid = $this->id";
 546          $result = $this->db->query($sql);
 547      }
 548    
 549  
 550      /**
 551       *      \brief      Crée un utilisateur en base
 552       *      \return     int         si erreur <0, si ok renvoie id compte créé
 553       */
 554      function create()
 555      {
 556          global $langs;
 557      
 558          // Nettoyage parametres
 559          $this->login = trim($this->login);
 560          
 561          $this->db->begin();
 562  
 563          $sql = "SELECT login FROM ".MAIN_DB_PREFIX."user WHERE login ='".addslashes($this->login)."'";
 564          $resql=$this->db->query($sql);
 565          if ($resql)
 566          {
 567              $num = $this->db->num_rows($resql);
 568              $this->db->free($resql);
 569      
 570              if ($num)
 571              {
 572                  $this->error = $langs->trans("ErrorLoginAlreadyExists");
 573                  return -5;
 574              }
 575              else
 576              {
 577                  $sql = "INSERT INTO ".MAIN_DB_PREFIX."user (datec,login) VALUES(now(),'".addslashes($this->login)."')";
 578                  $result=$this->db->query($sql);
 579      
 580                  if ($result)
 581                  {
 582                      $table =  "".MAIN_DB_PREFIX."user";
 583                      $this->id = $this->db->last_insert_id($table);
 584      
 585                      // Set default rights
 586                      if ($this->set_default_rights() < 0) 
 587                      {
 588                          $this->db->rollback();
 589          
 590                          $this->error=$this->db->error();
 591                          return -4;
 592                      }
 593                      
 594                      // Update minor fields
 595                      if ($this->update() < 0)
 596                      {
 597                          $this->db->rollback();
 598          
 599                          $this->error=$this->db->error();
 600                          return -5;
 601                      }
 602      
 603                      // Appel des triggers
 604                      include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php");
 605                      $interface=new Interfaces($this->db);
 606                      $result=$interface->run_triggers('USER_CREATE',$this,$user,$lang,$conf);
 607                      if ($result < 0) $error++;
 608                      // Fin appel triggers
 609      
 610                      if (! $error)
 611                      {
 612                          $this->db->commit();
 613          
 614                          return $this->id;
 615                      }
 616                      else
 617                      {
 618                          $this->db->rollback();
 619          
 620                          $this->error=$interface->error;
 621                          return -3;
 622                      }
 623                  }
 624                  else
 625                  {
 626                      $this->db->rollback();
 627                      $this->error=$this->db->error();
 628                      return -2;
 629                  }
 630              }
 631          }
 632          else
 633          {
 634              $this->db->rollback();
 635              $this->error=$this->db->error();
 636              return -1;
 637          }
 638      }
 639  
 640  
 641      /**
 642       *      \brief      Créé en base un utilisateur depuis l'objet contact
 643       *      \param      contact     Objet du contact source
 644       *      \return     int         si erreur <0, si ok renvoie id compte créé
 645       */
 646      function create_from_contact($contact)
 647      {
 648          global $langs;
 649      
 650          // Positionne paramètres
 651          $this->nom = $contact->nom;
 652          $this->prenom = $contact->prenom;
 653  
 654          $this->login = strtolower(substr($contact->prenom, 0, 3)) . strtolower(substr($contact->nom, 0, 3));
 655          $this->admin = 0;
 656  
 657          $this->email = $contact->email;
 658      
 659          $this->db->begin();
 660      
 661          // Crée et positionne $this->id
 662          $result=$this->create();
 663          
 664          if ($result > 0)
 665          {
 666              $sql = "UPDATE ".MAIN_DB_PREFIX."user";
 667              $sql.= " SET fk_socpeople=".$contact->id.", fk_societe=".$contact->societeid;
 668              $sql.= " WHERE rowid=".$this->id;
 669              $resql=$this->db->query($sql);
 670  
 671              if ($resql)
 672              {
 673                  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
 674                  $sql.= " SET fk_user = ".$this->id;
 675                  $sql.= " WHERE idp = ".$contact->id;
 676                  $resql=$this->db->query($sql);
 677          
 678                  if ($resql)
 679                  {
 680                      $this->db->commit();
 681                      return $this->id;
 682                  }
 683                  else
 684                  {
 685                      $this->error=$this->db->error()." - $sql";
 686                      dolibarr_syslog("User::create_from_contact - 20 - ".$this->error);
 687  
 688                      $this->db->rollback();
 689                      return -2;
 690                  }                        
 691              }
 692              else
 693              {
 694                  $this->error=$this->db->error()." - $sql";
 695                  dolibarr_syslog("User::create_from_contact - 10 - ".$this->error);
 696  
 697                  $this->db->rollback();
 698                  return -1;
 699              }
 700          }
 701          else
 702          {
 703              // $this->error deja positionné
 704              dolibarr_syslog("User::create_from_contact - 0");
 705  
 706              $this->db->rollback();
 707              return $result;
 708          }
 709      
 710      }
 711  
 712    /**
 713     *    \brief      Affectation des permissions par défaut
 714     *    \return     si erreur <0, si ok renvoi le nbre de droits par defaut positionnés
 715     */
 716       
 717    function set_default_rights()
 718      {
 719          $sql = "SELECT id FROM ".MAIN_DB_PREFIX."rights_def WHERE bydefault = 1";
 720          
 721          if ($this->db->query($sql))
 722          {
 723              $num = $this->db->num_rows();
 724              $i = 0;
 725              $rd = array();
 726              while ($i < $num)
 727              {
 728                  $row = $this->db->fetch_row($i);
 729                  $rd[$i] = $row[0];
 730                  $i++;
 731              }
 732              $this->db->free();
 733          }
 734          $i = 0;
 735          while ($i < $num)
 736          {
 737          
 738              $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = $this->id AND fk_id=$rd[$i]";
 739              $result=$this->db->query($sql);
 740          
 741              $sql = "INSERT INTO ".MAIN_DB_PREFIX."user_rights (fk_user, fk_id) VALUES ($this->id, $rd[$i])";
 742              $result=$this->db->query($sql);
 743              if (! $result) return -1;
 744              $i++;
 745          }
 746          
 747          return $i;
 748      }
 749  
 750    /**
 751     *    \brief      Mise à jour en base d'un utilisateur
 752     *    \param      create      1 si update durant le create, 0 sinon
 753     *    \return     int         <0 si echec, >=0 si ok
 754     */
 755    function update($create=0)
 756      {
 757          global $langs;
 758          
 759          // Nettoyage parametres
 760          $this->nom=trim($this->nom);
 761          $this->prenom=trim($this->prenom);
 762          $this->login=trim($this->login);
 763          $this->pass=trim($this->pass);
 764          $this->email=trim($this->email);
 765          $this->note=trim($this->note);
 766          
 767          $error=0;
 768          
 769          if (!strlen($this->code)) $this->code = $this->login;
 770  
 771          $sql = "UPDATE ".MAIN_DB_PREFIX."user SET ";
 772          $sql .= " name = '".addslashes($this->nom)."'";
 773          $sql .= ", firstname = '".addslashes($this->prenom)."'";
 774          $sql .= ", login = '".addslashes($this->login)."'";
 775          if ($this->pass) $sql .= ", pass = '".addslashes($this->pass)."'";
 776          $sql .= ", admin = $this->admin";
 777          $sql .= ", office_phone = '$this->office_phone'";
 778          $sql .= ", office_fax = '$this->office_fax'";
 779          $sql .= ", user_mobile = '$this->user_mobile'";
 780          $sql .= ", email = '".addslashes($this->email)."'";
 781          $sql .= ", webcal_login = '$this->webcal_login'";
 782          $sql .= ", code = '$this->code'";
 783          $sql .= ", note = '".addslashes($this->note)."'";
 784          $sql .= " WHERE rowid = ".$this->id;
 785  
 786          $result = $this->db->query($sql);
 787          if ($result)
 788          {
 789              if ($this->db->affected_rows())
 790              {
 791                  if (! $create)
 792                  {
 793                      // Appel des triggers
 794                      include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php");
 795                      $interface=new Interfaces($this->db);
 796                      $result=$interface->run_triggers('USER_MODIFY',$this,$user,$lang,$conf);
 797                      if ($result < 0) $error++;
 798                      // Fin appel triggers
 799                  }
 800                  
 801                  return 1;
 802              }
 803              return 0;
 804          }
 805          else
 806          {
 807              $this->error=$this->db->error();
 808              return -1;
 809          }
 810  
 811     }
 812  
 813  
 814    /**
 815     *    \brief      Mise à jour en base de la date de deniere connexion d'un utilisateur
 816     *    \return     <0 si echec, >=0 si ok
 817     */
 818    function update_last_login_date()
 819      {
 820          dolibarr_syslog ("Mise a jour date derniere connexion pour user->id=".$this->id);
 821  
 822          $now=time();
 823          
 824          $sql = "UPDATE ".MAIN_DB_PREFIX."user";
 825          $sql.= " SET datelastaccess = ".$this->db->idate($now).",";
 826          $sql.= " tms = tms";    // La date de derniere modif doit changer sauf pour la mise a jour de date de derniere connexion
 827          $sql.= " WHERE rowid = ".$this->id;
 828          $resql = $this->db->query($sql);
 829          if ($resql)
 830          {
 831              $this->datelastaccess=$now;
 832              return 1;
 833          }
 834          else
 835          {
 836              $this->error=$this->db->error();
 837              return -1;
 838          }
 839     }
 840     
 841     
 842    /**
 843     *    \brief     Change le mot de passe d'un utilisateur
 844     *    \param     user             Object user de l'utilisateur qui fait la modification
 845     *    \param     password         Nouveau mot de passe (généré par defaut si non communiqué)
 846     *    \param     isencrypted      0 ou 1 si il faut crypter le mot de passe en base (0 par défaut)
 847     *    \return    string           mot de passe, < 0 si erreur
 848     */
 849      function password($user, $password='', $isencrypted = 0)
 850      {
 851          global $langs;
 852          $longueurmotdepasse=8;
 853          
 854          if (! $password)
 855          {
 856              $password =  strtolower(substr(md5(uniqid(rand())),0,$longueurmotdepasse));
 857          }
 858      
 859          if ($isencrypted)
 860          {
 861              $sqlpass = crypt($password, "CRYPT_STD_DES");
 862          }
 863          else
 864          {
 865              $sqlpass = $password;
 866          }
 867          $this->pass=$password;
 868          $sql = "UPDATE ".MAIN_DB_PREFIX."user SET pass = '".addslashes($sqlpass)."'";
 869          $sql.= " WHERE rowid = ".$this->id;
 870      
 871          $result = $this->db->query($sql);
 872          if ($result)
 873          {
 874              if ($this->db->affected_rows())
 875              {
 876                  return $this->pass;
 877              }
 878              else {
 879                  return -2;
 880              }
 881          }
 882          else
 883          {
 884              dolibarr_print_error($this->db);
 885              return -1;
 886          }
 887      }
 888  
 889  
 890    /**
 891     *    \brief     Envoie mot de passe par mail
 892     *    \param     user             Object user de l'utilisateur qui fait l'envoi
 893     *    \param     password         Nouveau mot de passe
 894     *    \return    int              < 0 si erreur, > 0 si ok
 895     */
 896      function send_password($user, $password='')
 897      {
 898          global $langs;
 899          
 900          require_once  DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php";
 901  
 902          $subject = $langs->trans("SubjectNewPassword");
 903          
 904          $mesg .= "Bonjour,\n\n";
 905          $mesg .= "Votre mot de passe pour accéder à Dolibarr a été changé :\n\n";
 906          $mesg .= $langs->trans("Login")." : $this->login\n";
 907          $mesg .= $langs->trans("Password")." : $password\n\n";
 908  
 909          $mesg .= "Adresse : http://".$_SERVER["HTTP_HOST"].DOL_URL_ROOT;
 910          $mesg .= "\n\n";
 911          $mesg .= "--\n";
 912          $mesg.= $user->fullname;
 913  
 914          $mailfile = new CMailFile($subject,$this->email,$conf->email_from,$mesg,array(),array(),array());
 915  
 916          if ($mailfile->sendfile())
 917          {
 918              return 1;
 919          }
 920          else
 921          {
 922              $this->error=$langs->trans("ErrorFailedToSendPassword");
 923              return -1;
 924          }
 925      }
 926  
 927    /**
 928     * \brief     Renvoie la dernière erreur fonctionnelle de manipulation de l'objet
 929     * \return    string      chaine erreur
 930     */
 931       
 932    function error()
 933      {
 934        return $this->error;
 935      }
 936  
 937    
 938    /**
 939     *    \brief      Lecture des infos de click to dial
 940     */
 941    function fetch_clicktodial()
 942      {
 943        
 944        $sql = "SELECT login, pass, poste FROM ".MAIN_DB_PREFIX."user_clicktodial as u";
 945        $sql .= " WHERE u.fk_user = ".$this->id;
 946        
 947        $result = $this->db->query($sql);
 948  
 949        if ($result) 
 950      {
 951        if ($this->db->num_rows()) 
 952          {
 953            $obj = $this->db->fetch_object();
 954  
 955            $this->clicktodial_login = $obj->login;
 956            $this->clicktodial_password = $obj->pass;
 957            $this->clicktodial_poste = $obj->poste;
 958  
 959            if (strlen(trim($this->clicktodial_login)) && 
 960            strlen(trim($this->clicktodial_password)) && 
 961            strlen(trim($this->clicktodial_poste)))
 962          {
 963            $this->clicktodial_enabled = 1;
 964          }
 965  
 966          }
 967  
 968        $this->db->free();
 969      }
 970        else
 971      {
 972        print $this->db->error();
 973      }
 974      }
 975  
 976    /**
 977     *    \brief      Mise à jour des infos de click to dial
 978     */
 979    function update_clicktodial()
 980      {
 981        
 982        $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_clicktodial";
 983        $sql .= " WHERE fk_user = ".$this->id;
 984  
 985        $result = $this->db->query($sql);
 986  
 987        $sql = "INSERT INTO ".MAIN_DB_PREFIX."user_clicktodial";
 988        $sql .= " (fk_user,login,pass,poste)";
 989        $sql .= " VALUES (".$this->id;
 990        $sql .= ", '". $this->clicktodial_login ."'";
 991        $sql .= ", '". $this->clicktodial_password ."'";
 992        $sql .= ", '". $this->clicktodial_poste."')";
 993        
 994        $result = $this->db->query($sql);
 995  
 996        if ($result) 
 997      {
 998        return 0;
 999      }
1000        else
1001      {
1002        print $this->db->error();
1003      }
1004      }
1005  
1006  
1007    /**
1008     *    \brief      Ajoute l'utilisateur dans un groupe
1009     *    \param      group       id du groupe
1010     */
1011  
1012    function SetInGroup($group)
1013      {
1014  
1015        $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user";
1016        $sql .= " WHERE fk_user  = ".$this->id;
1017        $sql .= " AND fk_usergroup = ".$group;
1018    
1019        $result = $this->db->query($sql);
1020  
1021        $sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_user (fk_user, fk_usergroup)";
1022        $sql .= " VALUES (".$this->id.",".$group.")";
1023          
1024        $result = $this->db->query($sql);
1025      }
1026  
1027    /**
1028     *    \brief      Retire l'utilisateur d'un groupe
1029     *    \param      group       id du groupe
1030     */
1031  
1032    function RemoveFromGroup($group)
1033      {
1034  
1035        $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user";
1036        $sql .= " WHERE fk_user  = ".$this->id;
1037        $sql .= " AND fk_usergroup = ".$group;
1038    
1039        $result = $this->db->query($sql);
1040      }
1041  }
1042  
1043  ?>


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