2004-10-20 23:15:17 +02:00
< ? php
2007-01-04 09:51:10 +01:00
/* Copyright ( c ) 2002 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2004-08-07 20:47:54 +02:00
* Copyright ( c ) 2002 - 2003 Jean - Louis Bergamo < jlb @ j1b . org >
2012-01-07 15:01:30 +01:00
* Copyright ( c ) 2004 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2004-09-01 23:23:20 +02:00
* Copyright ( C ) 2004 Sebastien Di Cintio < sdicintio @ ressource - toi . org >
2004-10-01 15:25:02 +02:00
* Copyright ( C ) 2004 Benoit Mortier < benoit . mortier @ opensides . be >
2012-12-30 15:11:07 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2006-12-03 20:27:26 +01:00
* Copyright ( C ) 2005 Lionel Cousteix < etm_ltd @ tiscali . co . uk >
2011-08-19 09:22:17 +02:00
* Copyright ( C ) 2011 Herve Prot < herve . prot @ symeos . com >
2014-03-05 09:57:36 +01:00
* Copyright ( C ) 2013 - 2014 Philippe Grand < philippe . grand @ atoo - net . com >
2014-02-17 11:48:57 +01:00
* Copyright ( C ) 2013 Alexandre Spangaro < alexandre . spangaro @ gmail . com >
2006-11-20 03:10:47 +01:00
*
2002-04-30 12:51:35 +02:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2002-04-30 12:51:35 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-08-01 01:19:04 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2002-04-30 12:51:35 +02:00
*/
2005-03-15 18:17:58 +01:00
/**
2010-07-21 14:35:56 +02:00
* \file htdocs / user / class / user . class . php
2012-01-11 01:06:56 +01:00
* \brief File of class to manage users
2012-03-27 09:59:28 +02:00
* \ingroup core
2008-07-19 18:53:56 +02:00
*/
2004-08-07 20:47:54 +02:00
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2007-11-05 23:37:41 +01:00
2004-08-07 20:47:54 +02:00
2005-03-15 18:17:58 +01:00
/**
2012-04-01 20:31:39 +02:00
* Class to manage Dolibarr users
2008-07-19 18:53:56 +02:00
*/
2007-11-05 23:37:41 +01:00
class User extends CommonObject
2003-08-11 20:56:42 +02:00
{
2011-09-20 19:19:46 +02:00
public $element = 'user' ;
public $table_element = 'user' ;
protected $ismultientitymanaged = 1 ; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
2004-08-07 20:47:54 +02:00
2008-07-19 18:53:56 +02:00
var $id = 0 ;
2012-08-29 10:21:25 +02:00
var $ref ;
var $ref_ext ;
2007-11-13 21:17:52 +01:00
var $ldap_sid ;
var $search_sid ;
2011-09-24 01:46:14 +02:00
var $lastname ;
var $firstname ;
2007-11-13 21:17:52 +01:00
var $note ;
var $email ;
2013-11-07 13:17:08 +01:00
var $skype ;
2012-10-05 12:40:19 +02:00
var $job ;
2011-06-29 12:23:32 +02:00
var $signature ;
2007-11-13 21:17:52 +01:00
var $office_phone ;
var $office_fax ;
var $user_mobile ;
var $admin ;
var $login ;
2009-04-18 16:38:08 +02:00
var $entity ;
2007-11-13 21:17:52 +01:00
2009-01-21 15:09:42 +01:00
//! Clear password in memory
2007-11-13 21:17:52 +01:00
var $pass ;
2009-01-21 15:09:42 +01:00
//! Clear password in database (defined if DATABASE_PWD_ENCRYPTED=0)
2007-11-13 21:17:52 +01:00
var $pass_indatabase ;
2009-01-21 15:09:42 +01:00
//! Encrypted password in database (always defined)
2007-11-13 21:17:52 +01:00
var $pass_indatabase_crypted ;
var $datec ;
var $datem ;
2012-05-23 13:09:36 +02:00
2011-09-24 01:46:14 +02:00
//! If this is defined, it is an external user
2007-11-13 21:17:52 +01:00
var $societe_id ;
2012-05-23 13:09:36 +02:00
var $contact_id ;
2007-11-13 21:17:52 +01:00
var $fk_member ;
2013-02-11 20:45:36 +01:00
var $fk_user ;
2009-01-21 15:09:42 +01:00
2013-03-31 16:44:24 +02:00
var $clicktodial_url ;
2013-03-31 04:03:08 +02:00
var $clicktodial_login ;
var $clicktodial_password ;
var $clicktodial_poste ;
2007-11-13 21:17:52 +01:00
var $datelastlogin ;
var $datepreviouslogin ;
var $statut ;
2010-02-13 23:32:12 +01:00
var $photo ;
2007-11-13 21:17:52 +01:00
var $lang ;
2012-01-15 14:01:40 +01:00
var $rights ; // Array of permissions user->rights->permx
var $all_permissions_are_loaded ; /**< \private all_permissions_are_loaded */
2012-01-11 01:06:56 +01:00
private $_tab_loaded = array (); // Array of cache of already loaded permissions
2008-07-19 18:53:56 +02:00
2013-02-11 20:45:36 +01:00
var $conf ; // To store personal config
var $oldcopy ; // To contains a clone of this when we need to save old properties of object
2008-07-19 18:53:56 +02:00
2013-02-11 20:45:36 +01:00
var $users ; // To store all tree of users hierarchy
var $parentof ; // To store an array of all parents for all ids.
2013-08-25 06:53:59 +02:00
2013-06-25 11:14:00 +02:00
var $accountancy_code ; // Accountancy code in prevision of the complete accountancy module
2014-03-16 14:31:02 +01:00
var $thm ; // Average cost of employee
2008-07-19 18:53:56 +02:00
/**
2011-07-08 20:49:16 +02:00
* Constructor de la classe
2011-09-04 20:41:19 +02:00
*
2012-01-13 18:24:25 +01:00
* @ param DoliDb $db Database handler
2008-07-19 18:53:56 +02:00
*/
2012-01-13 18:24:25 +01:00
function __construct ( $db )
2008-07-19 18:53:56 +02:00
{
2012-01-13 18:24:25 +01:00
$this -> db = $db ;
2008-07-19 18:53:56 +02:00
// Preference utilisateur
$this -> liste_limit = 0 ;
2009-01-09 22:22:58 +01:00
$this -> clicktodial_loaded = 0 ;
2008-07-19 18:53:56 +02:00
$this -> all_permissions_are_loaded = 0 ;
$this -> admin = 0 ;
2012-03-27 09:59:28 +02:00
2013-01-19 14:32:37 +01:00
$this -> conf = new stdClass ();
$this -> rights = new stdClass ();
$this -> rights -> user = new stdClass ();
$this -> rights -> user -> user = new stdClass ();
$this -> rights -> user -> self = new stdClass ();
2008-07-19 18:53:56 +02:00
}
2007-04-13 18:11:44 +02:00
/**
2011-06-29 12:23:32 +02:00
* Load a user from database with its id or ref ( login )
2011-08-21 01:56:03 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param int $id Si defini , id a utiliser pour recherche
* @ param string $login Si defini , login a utiliser pour recherche
* @ param strinf $sid Si defini , sid a utiliser pour recherche
* @ param int $loadpersonalconf Also load personal conf of user ( in $user -> conf -> xxx )
* @ return int < 0 if KO , 0 not found , > 0 if OK
2008-07-19 18:53:56 +02:00
*/
2010-04-28 09:31:34 +02:00
function fetch ( $id = '' , $login = '' , $sid = '' , $loadpersonalconf = 1 )
2007-04-13 18:11:44 +02:00
{
2011-08-20 00:15:22 +02:00
global $conf , $user ;
2007-01-05 10:25:52 +01:00
2011-06-29 12:23:32 +02:00
// Clean parameters
2007-09-11 19:30:36 +02:00
$login = trim ( $login );
2008-07-19 18:53:56 +02:00
2011-06-29 12:23:32 +02:00
// Get user
2013-11-07 13:17:08 +01:00
$sql = " SELECT u.rowid, u.lastname, u.firstname, u.email, u.job, u.skype, u.signature, u.office_phone, u.office_fax, u.user_mobile, " ;
2013-04-11 09:15:41 +02:00
$sql .= " u.admin, u.login, u.note, " ;
2007-06-02 03:53:54 +02:00
$sql .= " u.pass, u.pass_crypted, u.pass_temp, " ;
2013-02-11 20:45:36 +01:00
$sql .= " u.fk_societe, u.fk_socpeople, u.fk_member, u.fk_user, u.ldap_sid, " ;
2009-04-18 16:38:08 +02:00
$sql .= " u.statut, u.lang, u.entity, " ;
2010-01-13 19:51:19 +01:00
$sql .= " u.datec as datec, " ;
$sql .= " u.tms as datem, " ;
$sql .= " u.datelastlogin as datel, " ;
2010-02-13 23:32:12 +01:00
$sql .= " u.datepreviouslogin as datep, " ;
2010-12-01 22:38:00 +01:00
$sql .= " u.photo as photo, " ;
2011-09-03 17:27:23 +02:00
$sql .= " u.openid as openid, " ;
2013-06-25 11:14:00 +02:00
$sql .= " u.accountancy_code, " ;
2014-03-16 14:31:02 +01:00
$sql .= " u.thm, " ;
2011-09-03 17:27:23 +02:00
$sql .= " u.ref_int, u.ref_ext " ;
2007-04-13 18:11:44 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
2011-08-20 18:59:14 +02:00
2013-02-16 21:06:15 +01:00
if (( empty ( $conf -> multicompany -> enabled ) || empty ( $conf -> multicompany -> transverse_mode )) && ( ! empty ( $user -> entity )))
2011-08-20 00:15:22 +02:00
{
2013-02-16 21:06:15 +01:00
$sql .= " WHERE u.entity IN (0, " . $conf -> entity . " ) " ;
2011-08-20 00:15:22 +02:00
}
else
{
2013-02-16 21:06:15 +01:00
$sql .= " WHERE u.entity IS NOT NULL " ;
2011-08-20 00:15:22 +02:00
}
2010-12-01 22:38:00 +01:00
2011-08-21 01:56:03 +02:00
if ( $sid ) // permet une recherche du user par son SID ActiveDirectory ou Samba
2007-06-01 22:15:30 +02:00
{
2011-02-12 23:34:37 +01:00
$sql .= " AND (u.ldap_sid = ' " . $sid . " ' OR u.login = ' " . $this -> db -> escape ( $login ) . " ') LIMIT 1 " ;
2007-06-01 22:15:30 +02:00
}
else if ( $login )
2007-04-13 18:11:44 +02:00
{
2011-02-12 08:02:56 +01:00
$sql .= " AND u.login = ' " . $this -> db -> escape ( $login ) . " ' " ;
2007-04-13 18:11:44 +02:00
}
else
{
2010-04-28 10:26:56 +02:00
$sql .= " AND u.rowid = " . $id ;
2007-04-13 18:11:44 +02:00
}
2008-07-19 18:53:56 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2007-04-13 18:11:44 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
if ( $obj )
{
2011-01-15 10:31:09 +01:00
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> rowid ;
2011-09-04 20:41:19 +02:00
2011-09-03 17:27:23 +02:00
$this -> ref_int = $obj -> ref_int ;
$this -> ref_ext = $obj -> ref_ext ;
2011-01-15 10:31:09 +01:00
$this -> ldap_sid = $obj -> ldap_sid ;
2013-03-09 20:34:38 +01:00
$this -> lastname = $obj -> lastname ;
2011-01-15 10:31:09 +01:00
$this -> firstname = $obj -> firstname ;
2008-07-19 18:53:56 +02:00
2012-10-05 12:40:19 +02:00
$this -> login = $obj -> login ;
2007-04-13 18:11:44 +02:00
$this -> pass_indatabase = $obj -> pass ;
2007-04-23 00:55:32 +02:00
$this -> pass_indatabase_crypted = $obj -> pass_crypted ;
2012-10-05 12:40:19 +02:00
$this -> pass = $obj -> pass ;
$this -> pass_temp = $obj -> pass_temp ;
$this -> office_phone = $obj -> office_phone ;
2007-04-13 18:11:44 +02:00
$this -> office_fax = $obj -> office_fax ;
$this -> user_mobile = $obj -> user_mobile ;
2012-10-05 12:40:19 +02:00
$this -> email = $obj -> email ;
2014-03-16 20:37:43 +01:00
$this -> skype = $obj -> skype ;
2012-10-05 12:40:19 +02:00
$this -> job = $obj -> job ;
$this -> signature = $obj -> signature ;
$this -> admin = $obj -> admin ;
$this -> note = $obj -> note ;
$this -> statut = $obj -> statut ;
$this -> photo = $obj -> photo ;
$this -> openid = $obj -> openid ;
$this -> lang = $obj -> lang ;
$this -> entity = $obj -> entity ;
2013-06-25 11:14:00 +02:00
$this -> accountancy_code = $obj -> accountancy_code ;
2014-03-16 20:37:43 +01:00
$this -> thm = $obj -> thm ;
2012-10-05 12:40:19 +02:00
$this -> datec = $this -> db -> jdate ( $obj -> datec );
$this -> datem = $this -> db -> jdate ( $obj -> datem );
$this -> datelastlogin = $this -> db -> jdate ( $obj -> datel );
$this -> datepreviouslogin = $this -> db -> jdate ( $obj -> datep );
2008-07-19 18:53:56 +02:00
2007-10-02 15:54:34 +02:00
$this -> societe_id = $obj -> fk_societe ;
$this -> contact_id = $obj -> fk_socpeople ;
$this -> fk_member = $obj -> fk_member ;
2013-02-11 20:45:36 +01:00
$this -> fk_user = $obj -> fk_user ;
2013-06-16 21:31:21 +02:00
2013-05-10 12:03:27 +02:00
// Retreive all extrafield for thirdparty
// fetch optionals attributes and labels
require_once ( DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' );
$extrafields = new ExtraFields ( $this -> db );
$extralabels = $extrafields -> fetch_name_optionals_label ( $this -> table_element , true );
2013-06-16 21:31:21 +02:00
$this -> fetch_optionals ( $this -> id , $extralabels );
2007-04-13 18:11:44 +02:00
$this -> db -> free ( $result );
}
else
{
$this -> error = " USERNOTFOUND " ;
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch user not found " , LOG_DEBUG );
2007-04-13 18:11:44 +02:00
$this -> db -> free ( $result );
2007-10-02 20:16:29 +02:00
return 0 ;
2008-07-19 18:53:56 +02:00
}
2007-04-13 18:11:44 +02:00
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
2008-07-19 18:53:56 +02:00
2007-12-19 11:24:18 +01:00
// Recupere parametrage global propre a l'utilisateur
2008-08-30 01:49:53 +02:00
if ( $loadpersonalconf )
2007-04-13 18:11:44 +02:00
{
$sql = " SELECT param, value FROM " . MAIN_DB_PREFIX . " user_param " ;
$sql .= " WHERE fk_user = " . $this -> id ;
2009-04-27 22:37:50 +02:00
$sql .= " AND entity = " . $conf -> entity ;
2014-06-13 01:34:39 +02:00
//dol_syslog(get_class($this).'::fetch load personalized conf', LOG_DEBUG);
2012-01-15 19:07:01 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2007-04-13 18:11:44 +02:00
{
2012-01-15 19:07:01 +01:00
$num = $this -> db -> num_rows ( $resql );
2007-04-13 18:11:44 +02:00
$i = 0 ;
while ( $i < $num )
{
2012-01-15 19:07:01 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2012-08-01 13:30:38 +02:00
$p = ( ! empty ( $obj -> param ) ? $obj -> param : '' );
if ( ! empty ( $p )) $this -> conf -> $p = $obj -> value ;
2007-04-13 18:11:44 +02:00
$i ++ ;
}
2012-01-15 19:07:01 +01:00
$this -> db -> free ( $resql );
2007-04-13 18:11:44 +02:00
}
else
{
$this -> error = $this -> db -> error ();
2008-08-30 01:49:53 +02:00
return - 2 ;
2007-04-13 18:11:44 +02:00
}
}
2008-07-19 18:53:56 +02:00
2007-04-13 18:11:44 +02:00
return 1 ;
}
2008-07-19 18:53:56 +02:00
/**
2011-09-20 19:34:15 +02:00
* Ajoute un droit a l ' utilisateur
2011-09-04 20:41:19 +02:00
*
2012-09-27 11:54:51 +02:00
* @ 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
* @ param int $entity Entity to use
* @ return int > 0 if OK , < 0 if KO
2008-07-19 18:53:56 +02:00
*/
2012-09-27 11:54:51 +02:00
function addrights ( $rid , $allmodule = '' , $allperms = '' , $entity = '' )
2008-07-19 18:53:56 +02:00
{
2009-04-27 22:37:50 +02:00
global $conf ;
2009-05-07 01:30:49 +02:00
2012-09-27 11:54:51 +02:00
$entity = ( ! empty ( $entity ) ? $entity : $conf -> entity );
dol_syslog ( get_class ( $this ) . " ::addrights $rid , $allmodule , $allperms , $entity " );
2008-07-19 18:53:56 +02:00
$err = 0 ;
$whereforadd = '' ;
$this -> db -> begin ();
2012-09-27 11:54:51 +02:00
if ( ! empty ( $rid ))
2008-07-19 18:53:56 +02:00
{
// Si on a demande ajout d'un droit en particulier, on recupere
// les caracteristiques (module, perms et subperms) de ce droit.
$sql = " SELECT module, perms, subperms " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " rights_def " ;
2012-10-20 11:10:36 +02:00
$sql .= " WHERE id = ' " . $this -> db -> escape ( $rid ) . " ' " ;
2012-09-27 11:54:51 +02:00
$sql .= " AND entity = " . $entity ;
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
if ( $result ) {
$obj = $this -> db -> fetch_object ( $result );
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
}
else {
$err ++ ;
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
}
// Where pour la liste des droits a ajouter
2012-10-20 11:10:36 +02:00
$whereforadd = " id= " . $this -> db -> escape ( $rid );
2008-07-19 18:53:56 +02:00
// Ajout des droits induits
2012-10-20 09:33:39 +02:00
if ( ! empty ( $subperms )) $whereforadd .= " OR (module=' $module ' AND perms=' $perms ' AND (subperms='lire' OR subperms='read')) " ;
else if ( ! empty ( $perms )) $whereforadd .= " OR (module=' $module ' AND (perms='lire' OR perms='read') AND subperms IS NULL) " ;
2008-07-19 18:53:56 +02:00
}
else {
// On a pas demande un droit en particulier mais une liste de droits
// sur la base d'un nom de module de de perms
// Where pour la liste des droits a ajouter
2012-10-20 11:10:36 +02:00
if ( ! empty ( $allmodule )) $whereforadd = " module=' " . $this -> db -> escape ( $allmodule ) . " ' " ;
if ( ! empty ( $allperms )) $whereforadd = " AND perms=' " . $this -> db -> escape ( $allperms ) . " ' " ;
2008-07-19 18:53:56 +02:00
}
// Ajout des droits trouves grace au critere whereforadd
2012-10-20 09:33:39 +02:00
if ( ! empty ( $whereforadd ))
2008-07-19 18:53:56 +02:00
{
//print "$module-$perms-$subperms";
$sql = " SELECT id " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " WHERE " . $whereforadd ;
2012-09-27 11:54:51 +02:00
$sql .= " AND entity = " . $entity ;
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $result );
$nid = $obj -> id ;
2012-01-11 01:06:56 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user_rights WHERE fk_user = " . $this -> id . " AND fk_id= " . $nid ;
2008-07-19 18:53:56 +02:00
if ( ! $this -> db -> query ( $sql )) $err ++ ;
2012-01-11 01:06:56 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " user_rights (fk_user, fk_id) VALUES ( " . $this -> id . " , " . $nid . " ) " ;
2008-07-19 18:53:56 +02:00
if ( ! $this -> db -> query ( $sql )) $err ++ ;
$i ++ ;
}
}
else
{
$err ++ ;
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
}
}
if ( $err ) {
$this -> db -> rollback ();
return - $err ;
}
else {
$this -> db -> commit ();
return 1 ;
}
}
2004-08-07 20:47:54 +02:00
2006-11-26 20:19:46 +01:00
/**
2011-09-20 19:34:15 +02:00
* Retire un droit a l ' utilisateur
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ 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
2012-09-27 11:54:51 +02:00
* @ param int $entity Entity to use
2012-01-11 01:06:56 +01:00
* @ return int > 0 if OK , < 0 if OK
2008-07-19 18:53:56 +02:00
*/
2012-09-27 11:54:51 +02:00
function delrights ( $rid , $allmodule = '' , $allperms = '' , $entity = '' )
2008-07-19 18:53:56 +02:00
{
2009-04-27 22:37:50 +02:00
global $conf ;
2009-05-07 01:30:49 +02:00
2008-07-19 18:53:56 +02:00
$err = 0 ;
$wherefordel = '' ;
2012-09-27 11:54:51 +02:00
$entity = ( ! empty ( $entity ) ? $entity : $conf -> entity );
2008-07-19 18:53:56 +02:00
$this -> db -> begin ();
2012-09-27 11:54:51 +02:00
if ( ! empty ( $rid ))
2008-07-19 18:53:56 +02:00
{
// Si on a demande supression d'un droit en particulier, on recupere
// les caracteristiques module, perms et subperms de ce droit.
$sql = " SELECT module, perms, subperms " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " rights_def " ;
2012-10-20 11:10:36 +02:00
$sql .= " WHERE id = ' " . $this -> db -> escape ( $rid ) . " ' " ;
2012-09-27 11:54:51 +02:00
$sql .= " AND entity = " . $entity ;
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
if ( $result ) {
$obj = $this -> db -> fetch_object ( $result );
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
}
else {
$err ++ ;
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
}
// Where pour la liste des droits a supprimer
2012-10-20 11:10:36 +02:00
$wherefordel = " id= " . $this -> db -> escape ( $rid );
2008-07-19 18:53:56 +02:00
// Suppression des droits induits
if ( $subperms == 'lire' || $subperms == 'read' ) $wherefordel .= " OR (module=' $module ' AND perms=' $perms ' AND subperms IS NOT NULL) " ;
if ( $perms == 'lire' || $perms == 'read' ) $wherefordel .= " OR (module=' $module ') " ;
}
else {
// On a demande suppression d'un droit sur la base d'un nom de module ou perms
// Where pour la liste des droits a supprimer
2012-10-20 11:10:36 +02:00
if ( ! empty ( $allmodule )) $wherefordel = " module=' " . $this -> db -> escape ( $allmodule ) . " ' " ;
if ( ! empty ( $allperms )) $wherefordel = " AND perms=' " . $this -> db -> escape ( $allperms ) . " ' " ;
2008-07-19 18:53:56 +02:00
}
// Suppression des droits selon critere defini dans wherefordel
2012-10-20 09:33:39 +02:00
if ( ! empty ( $wherefordel ))
2008-07-19 18:53:56 +02:00
{
//print "$module-$perms-$subperms";
$sql = " SELECT id " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " WHERE $wherefordel " ;
2012-09-27 11:54:51 +02:00
$sql .= " AND entity = " . $entity ;
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $result );
$nid = $obj -> id ;
2009-04-27 22:37:50 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user_rights " ;
2012-01-11 01:06:56 +01:00
$sql .= " WHERE fk_user = " . $this -> id . " AND fk_id= " . $nid ;
2008-07-19 18:53:56 +02:00
if ( ! $this -> db -> query ( $sql )) $err ++ ;
$i ++ ;
}
}
else
{
$err ++ ;
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
}
}
if ( $err ) {
$this -> db -> rollback ();
return - $err ;
}
else {
$this -> db -> commit ();
return 1 ;
}
}
/**
2011-09-20 19:34:15 +02:00
* Clear all permissions array of user
*
* @ return void
2014-02-17 11:48:57 +01:00
* @ see getrights
2008-07-19 18:53:56 +02:00
*/
2006-11-26 20:19:46 +01:00
function clearrights ()
2008-07-19 18:53:56 +02:00
{
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::clearrights reset user->rights " );
2006-11-26 20:19:46 +01:00
$this -> rights = '' ;
$this -> all_permissions_are_loaded = false ;
2012-01-11 01:06:56 +01:00
$this -> _tab_loaded = array ();
2006-11-26 20:19:46 +01:00
}
2007-02-28 04:13:34 +01:00
/**
2010-12-01 22:38:00 +01:00
* Load permissions granted to user into object user
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param string $moduletag Limit permission for a particular module ( '' by default means load all permissions )
* @ return void
2014-02-17 11:48:57 +01:00
* @ see clearrights
2009-09-10 23:47:22 +02:00
*/
2007-11-13 21:17:52 +01:00
function getrights ( $moduletag = '' )
2007-02-28 04:13:34 +01:00
{
2009-04-27 22:37:50 +02:00
global $conf ;
2009-05-07 01:30:49 +02:00
2012-01-11 01:06:56 +01:00
if ( $moduletag && isset ( $this -> _tab_loaded [ $moduletag ]) && $this -> _tab_loaded [ $moduletag ])
2008-07-19 18:53:56 +02:00
{
2007-12-19 11:24:18 +01:00
// Le fichier de ce module est deja charge
2007-11-13 21:17:52 +01:00
return ;
}
2007-02-28 04:13:34 +01:00
if ( $this -> all_permissions_are_loaded )
{
2007-12-19 11:24:18 +01:00
// Si les permissions ont deja ete charge pour ce user, on quitte
2007-02-28 04:13:34 +01:00
return ;
}
2008-07-19 18:53:56 +02:00
2007-12-19 11:24:18 +01:00
// Recuperation des droits utilisateurs + recuperation des droits groupes
2008-07-19 18:53:56 +02:00
2007-02-28 04:13:34 +01:00
// D'abord les droits utilisateurs
$sql = " SELECT r.module, r.perms, r.subperms " ;
2009-04-28 08:49:44 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user_rights as ur " ;
$sql .= " , " . MAIN_DB_PREFIX . " rights_def as r " ;
$sql .= " WHERE r.id = ur.fk_id " ;
2012-09-26 12:06:08 +02:00
$sql .= " AND r.entity IN (0, " . ( ! empty ( $conf -> multicompany -> enabled ) && ! empty ( $conf -> multicompany -> transverse_mode ) ? " 1, " : " " ) . $conf -> entity . " ) " ;
2009-04-28 08:49:44 +02:00
$sql .= " AND ur.fk_user= " . $this -> id ;
$sql .= " AND r.perms IS NOT NULL " ;
2011-02-12 08:02:56 +01:00
if ( $moduletag ) $sql .= " AND r.module = ' " . $this -> db -> escape ( $moduletag ) . " ' " ;
2008-07-19 18:53:56 +02:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::getrights' , LOG_DEBUG );
2012-01-11 01:06:56 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2007-02-28 04:13:34 +01:00
{
2012-01-11 01:06:56 +01:00
$num = $this -> db -> num_rows ( $resql );
2007-02-28 04:13:34 +01:00
$i = 0 ;
while ( $i < $num )
{
2012-01-11 01:06:56 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2007-02-28 04:13:34 +01:00
2007-10-24 01:28:13 +02:00
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
if ( $perms )
2007-02-28 04:13:34 +01:00
{
2013-01-19 14:32:37 +01:00
if ( ! isset ( $this -> rights ) || ! is_object ( $this -> rights )) $this -> rights = new stdClass (); // For avoid error
if ( ! isset ( $this -> rights -> $module ) || ! is_object ( $this -> rights -> $module )) $this -> rights -> $module = new stdClass ();
2007-10-24 01:28:13 +02:00
if ( $subperms )
2007-02-28 04:13:34 +01:00
{
2013-01-19 14:32:37 +01:00
if ( ! isset ( $this -> rights -> $module -> $perms ) || ! is_object ( $this -> rights -> $module -> $perms )) $this -> rights -> $module -> $perms = new stdClass ();
2012-04-14 15:04:46 +02:00
$this -> rights -> $module -> $perms -> $subperms = 1 ;
2007-02-28 04:13:34 +01:00
}
else
{
2007-10-24 01:28:13 +02:00
$this -> rights -> $module -> $perms = 1 ;
2007-02-28 04:13:34 +01:00
}
}
$i ++ ;
}
2012-01-11 01:06:56 +01:00
$this -> db -> free ( $resql );
2007-02-28 04:13:34 +01:00
}
// Maintenant les droits groupes
2007-11-13 21:17:52 +01:00
$sql = " SELECT r.module, r.perms, r.subperms " ;
2011-06-07 17:45:01 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " usergroup_rights as gr, " ;
$sql .= " " . MAIN_DB_PREFIX . " usergroup_user as gu, " ;
$sql .= " " . MAIN_DB_PREFIX . " rights_def as r " ;
$sql .= " WHERE r.id = gr.fk_id " ;
2012-09-26 12:06:08 +02:00
if ( ! empty ( $conf -> multicompany -> enabled ) && ! empty ( $conf -> multicompany -> transverse_mode )) {
$sql .= " AND gu.entity IN (0, " . $conf -> entity . " ) " ;
} else {
$sql .= " AND r.entity = " . $conf -> entity ;
}
2011-06-07 17:45:01 +02:00
$sql .= " AND gr.fk_usergroup = gu.fk_usergroup " ;
$sql .= " AND gu.fk_user = " . $this -> id ;
$sql .= " AND r.perms IS NOT NULL " ;
2011-02-12 08:02:56 +01:00
if ( $moduletag ) $sql .= " AND r.module = ' " . $this -> db -> escape ( $moduletag ) . " ' " ;
2007-02-28 04:13:34 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::getrights' , LOG_DEBUG );
2012-01-11 01:06:56 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2007-02-28 04:13:34 +01:00
{
2012-01-11 01:06:56 +01:00
$num = $this -> db -> num_rows ( $resql );
2007-02-28 04:13:34 +01:00
$i = 0 ;
while ( $i < $num )
{
2012-01-11 01:06:56 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2007-02-28 04:13:34 +01:00
2012-01-11 01:06:56 +01:00
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
2007-02-28 04:13:34 +01:00
2012-01-11 01:06:56 +01:00
if ( $perms )
{
2013-01-19 14:32:37 +01:00
if ( ! isset ( $this -> rights ) || ! is_object ( $this -> rights )) $this -> rights = new stdClass (); // For avoid error
if ( ! isset ( $this -> rights -> $module ) || ! is_object ( $this -> rights -> $module )) $this -> rights -> $module = new stdClass ();
2012-01-11 01:06:56 +01:00
if ( $subperms )
2007-02-28 04:13:34 +01:00
{
2013-01-19 14:32:37 +01:00
if ( ! isset ( $this -> rights -> $module -> $perms ) || ! is_object ( $this -> rights -> $module -> $perms )) $this -> rights -> $module -> $perms = new stdClass ();
2012-01-11 01:06:56 +01:00
$this -> rights -> $module -> $perms -> $subperms = 1 ;
2007-02-28 04:13:34 +01:00
}
else
{
2012-01-11 01:06:56 +01:00
$this -> rights -> $module -> $perms = 1 ;
2007-02-28 04:13:34 +01:00
}
}
$i ++ ;
}
2012-01-11 01:06:56 +01:00
$this -> db -> free ( $resql );
2007-02-28 04:13:34 +01:00
}
2012-04-28 17:59:08 +02:00
2012-04-06 12:14:16 +02:00
// For backward compatibility
if ( isset ( $this -> rights -> propale ))
{
$this -> rights -> propal = $this -> rights -> propale ;
}
2007-02-28 04:13:34 +01:00
2007-11-13 21:17:52 +01:00
if ( ! $moduletag )
2007-02-28 04:13:34 +01:00
{
2007-12-19 11:24:18 +01:00
// 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
2007-02-28 04:13:34 +01:00
$this -> all_permissions_are_loaded = 1 ;
}
else
{
2007-12-19 11:24:18 +01:00
// Si module defini, on le marque comme charge en cache
2012-01-11 01:06:56 +01:00
$this -> _tab_loaded [ $moduletag ] = 1 ;
2007-02-28 04:13:34 +01:00
}
}
2003-08-11 20:56:42 +02:00
2008-07-19 18:53:56 +02:00
/**
2011-09-20 19:34:15 +02:00
* Change status of a user
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param int $statut Status to set
* @ return int < 0 if KO , 0 if nothing is done , > 0 if OK
2008-07-19 18:53:56 +02:00
*/
2008-01-29 21:34:58 +01:00
function setstatus ( $statut )
{
2008-03-17 15:59:34 +01:00
global $conf , $langs , $user ;
2008-07-19 18:53:56 +02:00
2008-01-29 21:34:58 +01:00
$error = 0 ;
2008-07-19 18:53:56 +02:00
2008-03-20 22:22:35 +01:00
// Check parameters
2011-09-03 17:27:23 +02:00
if ( $this -> statut == $statut ) return 0 ;
else $this -> statut = $statut ;
2008-07-19 18:53:56 +02:00
2008-01-29 21:34:58 +01:00
$this -> db -> begin ();
2008-07-19 18:53:56 +02:00
2008-01-29 21:34:58 +01:00
// Desactive utilisateur
$sql = " UPDATE " . MAIN_DB_PREFIX . " user " ;
2011-09-03 17:27:23 +02:00
$sql .= " SET statut = " . $this -> statut ;
2008-01-29 21:34:58 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
$result = $this -> db -> query ( $sql );
2008-07-19 18:53:56 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::setstatus " , LOG_DEBUG );
2008-01-29 21:34:58 +01:00
if ( $result )
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'USER_ENABLEDISABLE' , $user );
2014-07-05 08:59:41 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2008-01-29 21:34:58 +01:00
}
2008-07-19 18:53:56 +02:00
2008-01-29 21:34:58 +01:00
if ( $error )
{
$this -> db -> rollback ();
return - $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
2008-07-19 18:53:56 +02:00
/**
2010-07-21 19:58:22 +02:00
* Delete the user
2011-09-04 20:41:19 +02:00
*
2010-07-21 19:58:22 +02:00
* @ return int < 0 if KO , > 0 if OK
2008-07-19 18:53:56 +02:00
*/
function delete ()
{
global $user , $conf , $langs ;
2011-12-05 19:41:38 +01:00
$error = 0 ;
2012-01-07 15:01:30 +01:00
2008-07-19 18:53:56 +02:00
$this -> db -> begin ();
2010-05-05 19:36:20 +02:00
$this -> fetch ( $this -> id );
2008-07-19 18:53:56 +02:00
2014-06-13 02:28:30 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
// Supprime droits
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user_rights WHERE fk_user = " . $this -> id ;
2014-06-13 02:28:30 +02:00
2012-12-02 13:51:33 +01:00
if ( ! $error && ! $this -> db -> query ( $sql ))
2008-07-19 18:53:56 +02:00
{
2012-12-02 13:51:33 +01:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2010-11-10 11:53:39 +01:00
}
2010-11-15 20:08:35 +01:00
2010-11-10 11:53:39 +01:00
// Remove group
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " usergroup_user WHERE fk_user = " . $this -> id ;
2012-12-02 13:51:33 +01:00
if ( ! $error && ! $this -> db -> query ( $sql ))
2010-11-10 11:53:39 +01:00
{
2012-12-05 11:18:45 +01:00
$error ++ ;
2012-12-02 13:51:33 +01:00
$this -> error = $this -> db -> lasterror ();
2008-07-19 18:53:56 +02:00
}
// Si contact, supprime lien
if ( $this -> contact_id )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " socpeople SET fk_user_creat = null WHERE rowid = " . $this -> contact_id ;
2012-12-02 13:51:33 +01:00
if ( ! $error && ! $this -> db -> query ( $sql ))
2008-07-19 18:53:56 +02:00
{
2012-12-02 13:51:33 +01:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2008-07-19 18:53:56 +02:00
}
}
2012-12-08 19:23:13 +01:00
// Remove extrafields
2012-12-08 14:23:52 +01:00
if (( ! $error ) && ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))) // For avoid conflicts if trigger used
2012-12-02 13:51:33 +01:00
{
2012-12-10 19:06:59 +01:00
$result = $this -> deleteExtraFields ();
if ( $result < 0 )
{
2012-12-02 13:51:33 +01:00
$error ++ ;
dol_syslog ( get_class ( $this ) . " ::delete error -4 " . $this -> error , LOG_ERR );
}
}
// Remove user
if ( ! $error )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user WHERE rowid = " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
2012-12-02 13:51:33 +01:00
if ( ! $this -> db -> query ( $sql ))
{
$error ++ ;
$this -> error = $this -> db -> lasterror ();
}
}
if ( ! $error )
2008-07-19 18:53:56 +02:00
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'USER_DELETE' , $user );
2014-07-05 08:59:41 +02:00
if ( $result < 0 )
{
2014-07-03 14:01:01 +02:00
$error ++ ;
$this -> db -> rollback ();
2014-07-05 08:59:41 +02:00
return - 1 ;
}
2014-07-03 14:01:01 +02:00
// End call triggers
2014-07-05 08:59:41 +02:00
2008-07-19 18:53:56 +02:00
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
/**
2010-11-15 20:08:35 +01:00
* Create a user into database
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param User $user Objet user qui demande la creation
* @ param int $notrigger 1 ne declenche pas les triggers , 0 sinon
* @ return int < 0 si KO , id compte cree si OK
2008-07-19 18:53:56 +02:00
*/
2008-04-07 17:32:24 +02:00
function create ( $user , $notrigger = 0 )
2007-09-09 22:10:20 +02:00
{
2008-04-07 17:32:24 +02:00
global $conf , $langs ;
2012-03-11 21:45:17 +01:00
global $mysoc ;
2008-07-19 18:53:56 +02:00
2009-05-07 01:30:49 +02:00
// Clean parameters
2007-09-09 22:10:20 +02:00
$this -> login = trim ( $this -> login );
2009-05-07 01:30:49 +02:00
if ( ! isset ( $this -> entity )) $this -> entity = $conf -> entity ; // If not defined, we use default value
2008-07-19 18:53:56 +02:00
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::create login= " . $this -> login . " , user= " . ( is_object ( $user ) ? $user -> id : '' ), LOG_DEBUG );
2008-07-19 18:53:56 +02:00
2010-01-08 18:33:30 +01:00
// Check parameters
if ( ! empty ( $conf -> global -> USER_MAIL_REQUIRED ) && ! isValidEMail ( $this -> email ))
{
$langs -> load ( " errors " );
$this -> error = $langs -> trans ( " ErrorBadEMail " , $this -> email );
return - 1 ;
}
2011-11-30 11:27:48 +01:00
$this -> datec = dol_now ();
2010-05-05 19:27:12 +02:00
2007-12-16 21:05:55 +01:00
$error = 0 ;
2007-09-09 22:10:20 +02:00
$this -> db -> begin ();
2008-07-19 18:53:56 +02:00
2007-09-09 22:10:20 +02:00
$sql = " SELECT login FROM " . MAIN_DB_PREFIX . " user " ;
2011-02-12 08:02:56 +01:00
$sql .= " WHERE login =' " . $this -> db -> escape ( $this -> login ) . " ' " ;
2014-06-09 12:34:10 +02:00
$sql .= " AND entity IN (0, " . $this -> db -> escape ( $conf -> entity ) . " ) " ;
2009-01-09 22:22:58 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2007-09-09 22:10:20 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$this -> db -> free ( $resql );
2008-08-30 01:49:53 +02:00
2007-09-09 22:10:20 +02:00
if ( $num )
{
2007-12-16 21:05:55 +01:00
$this -> error = 'ErrorLoginAlreadyExists' ;
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_WARNING );
2007-12-19 11:24:18 +01:00
$this -> db -> rollback ();
2007-09-09 22:10:20 +02:00
return - 6 ;
}
else
{
2009-04-27 22:37:50 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " user (datec,login,ldap_sid,entity) " ;
2014-06-09 12:34:10 +02:00
$sql .= " VALUES(' " . $this -> db -> idate ( $this -> datec ) . " ',' " . $this -> db -> escape ( $this -> login ) . " ',' " . $this -> ldap_sid . " ', " . $this -> db -> escape ( $this -> entity ) . " ) " ;
2007-09-09 22:10:20 +02:00
$result = $this -> db -> query ( $sql );
2008-07-19 18:53:56 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2007-09-09 22:10:20 +02:00
if ( $result )
{
2010-05-17 14:43:36 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " user " );
2008-08-30 01:49:53 +02:00
2007-09-09 22:10:20 +02:00
// Set default rights
if ( $this -> set_default_rights () < 0 )
{
2012-10-24 21:19:10 +02:00
$this -> error = 'ErrorFailedToSetDefaultRightOfUser' ;
2007-09-09 22:10:20 +02:00
$this -> db -> rollback ();
return - 5 ;
}
2008-08-30 01:49:53 +02:00
2007-09-09 22:10:20 +02:00
// Update minor fields
2007-10-22 01:04:06 +02:00
$result = $this -> update ( $user , 1 , 1 );
if ( $result < 0 )
2007-09-09 22:10:20 +02:00
{
2006-12-05 23:55:50 +01:00
$this -> db -> rollback ();
return - 4 ;
}
2007-09-09 22:10:20 +02:00
2010-06-02 00:22:09 +02:00
if ( ! empty ( $conf -> global -> STOCK_USERSTOCK_AUTOCREATE ))
2007-09-09 22:10:20 +02:00
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/entrepot.class.php' ;
2010-06-02 00:22:09 +02:00
$langs -> load ( " stocks " );
2007-09-09 22:10:20 +02:00
$entrepot = new Entrepot ( $this -> db );
2012-03-11 21:45:17 +01:00
$entrepot -> libelle = $langs -> trans ( " PersonalStock " , $this -> getFullName ( $langs ));
$entrepot -> description = $langs -> trans ( " ThisWarehouseIsPersonalStock " , $this -> getFullName ( $langs ));
2007-09-09 22:10:20 +02:00
$entrepot -> statut = 1 ;
2012-03-11 21:45:17 +01:00
$entrepot -> country_id = $mysoc -> country_id ;
2007-09-09 22:10:20 +02:00
$entrepot -> create ( $user );
}
2007-10-22 01:04:06 +02:00
2007-09-09 22:10:20 +02:00
if ( ! $notrigger )
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'USER_CREATE' , $user );
2014-07-05 08:59:41 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2007-07-25 19:46:47 +02:00
}
2008-08-30 01:49:53 +02:00
2006-12-05 23:55:50 +01:00
if ( ! $error )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
2014-07-03 14:01:01 +02:00
//$this->error=$interface->error;
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
2006-12-05 23:55:50 +01:00
$this -> db -> rollback ();
return - 3 ;
}
}
else
{
2008-12-15 02:04:32 +01:00
$this -> error = $this -> db -> lasterror ();
2006-12-05 23:55:50 +01:00
$this -> db -> rollback ();
return - 2 ;
}
}
}
2006-12-04 12:31:57 +01:00
else
2006-12-05 23:55:50 +01:00
{
2008-12-15 02:04:32 +01:00
$this -> error = $this -> db -> lasterror ();
2006-12-05 23:55:50 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
2004-08-07 20:47:54 +02:00
2005-09-26 21:37:20 +02:00
2007-09-09 22:10:20 +02:00
/**
2011-09-20 19:34:15 +02:00
* Create a user from a contact object . User will be internal but if contact is linked to a third party , user will be external
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param Contact $contact Object for source contact
* @ param string $login Login to force
* @ param string $password Password to force
* @ return int < 0 if error , if OK returns id of created user
2008-07-19 18:53:56 +02:00
*/
2011-01-29 18:41:26 +01:00
function create_from_contact ( $contact , $login = '' , $password = '' )
2007-09-09 22:10:20 +02:00
{
2010-11-08 18:34:38 +01:00
global $conf , $user , $langs ;
2008-07-19 18:53:56 +02:00
2011-12-05 19:41:38 +01:00
$error = 0 ;
2012-01-07 15:01:30 +01:00
2008-07-19 18:53:56 +02:00
// Positionne parametres
2012-01-13 11:25:48 +01:00
$this -> admin = 0 ;
2013-01-03 23:57:18 +01:00
$this -> lastname = $contact -> lastname ;
$this -> firstname = $contact -> firstname ;
2012-01-13 11:25:48 +01:00
$this -> email = $contact -> email ;
2014-06-09 12:34:10 +02:00
$this -> skype = $contact -> skype ;
2012-01-13 11:25:48 +01:00
$this -> office_phone = $contact -> phone_pro ;
$this -> office_fax = $contact -> fax ;
$this -> user_mobile = $contact -> phone_mobile ;
2013-03-23 17:40:18 +01:00
$this -> address = $contact -> address ;
2013-03-30 14:27:13 +01:00
$this -> zip = $contact -> zip ;
$this -> town = $contact -> town ;
2013-03-23 17:40:18 +01:00
$this -> state_id = $contact -> state_id ;
2013-03-30 14:27:13 +01:00
$this -> country_id = $contact -> country_id ;
2009-01-09 22:22:58 +01:00
2013-01-03 23:57:18 +01:00
if ( empty ( $login )) $login = strtolower ( substr ( $contact -> firstname , 0 , 4 )) . strtolower ( substr ( $contact -> lastname , 0 , 4 ));
2008-12-17 19:34:47 +01:00
$this -> login = $login ;
2008-07-19 18:53:56 +02:00
$this -> db -> begin ();
// Cree et positionne $this->id
$result = $this -> create ( $user );
if ( $result > 0 )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " user " ;
2008-08-11 01:46:44 +02:00
$sql .= " SET fk_socpeople= " . $contact -> id ;
2011-03-30 14:27:42 +02:00
if ( $contact -> socid ) $sql .= " , fk_societe= " . $contact -> socid ;
2008-07-19 18:53:56 +02:00
$sql .= " WHERE rowid= " . $this -> id ;
$resql = $this -> db -> query ( $sql );
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create_from_contact " , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
if ( $resql )
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'USER_CREATE_FROM_CONTACT' , $user );
2014-07-05 08:59:41 +02:00
if ( $result < 0 ) { $error ++ ; $this -> db -> rollback (); return - 1 ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2014-07-05 08:59:41 +02:00
2008-07-19 18:53:56 +02:00
$this -> db -> commit ();
return $this -> id ;
}
else
{
2008-08-11 01:46:44 +02:00
$this -> error = $this -> db -> error ();
2008-07-19 18:53:56 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
// $this->error deja positionne
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::create_from_contact - 0 " );
2008-07-19 18:53:56 +02:00
$this -> db -> rollback ();
return $result ;
}
}
/**
2011-09-20 19:34:15 +02:00
* Create a user into database from a member object
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param Adherent $member Object member source
* @ param string $login Login to force
* @ return int < 0 if KO , if OK , return id of created account
2008-07-19 18:53:56 +02:00
*/
2009-01-14 15:59:28 +01:00
function create_from_member ( $member , $login = '' )
2008-07-19 18:53:56 +02:00
{
global $conf , $user , $langs ;
// Positionne parametres
2009-01-14 15:59:28 +01:00
$this -> admin = 0 ;
2013-03-23 17:40:18 +01:00
$this -> lastname = $member -> lastname ;
$this -> firstname = $member -> firstname ;
$this -> email = $member -> email ;
$this -> fk_member = $member -> id ;
$this -> pass = $member -> pass ;
2013-03-30 14:27:13 +01:00
$this -> address = $member -> address ;
$this -> zip = $member -> zip ;
$this -> town = $member -> town ;
$this -> state_id = $member -> state_id ;
$this -> country_id = $member -> country_id ;
2007-04-27 00:01:10 +02:00
2012-04-30 16:08:34 +02:00
if ( empty ( $login )) $login = strtolower ( substr ( $member -> firstname , 0 , 4 )) . strtolower ( substr ( $member -> lastname , 0 , 4 ));
2009-01-14 15:59:28 +01:00
$this -> login = $login ;
2007-04-27 00:01:10 +02:00
2008-07-19 18:53:56 +02:00
$this -> db -> begin ();
2007-04-27 00:01:10 +02:00
2014-01-23 09:19:15 +01:00
// Create and set $this->id
2008-07-19 18:53:56 +02:00
$result = $this -> create ( $user );
if ( $result > 0 )
{
2014-01-23 09:19:15 +01:00
$newpass = $this -> setPassword ( $user , $this -> pass );
if ( is_numeric ( $newpass ) && $newpass < 0 ) $result =- 2 ;
2014-02-17 11:48:57 +01:00
2014-01-23 09:19:15 +01:00
if ( $result > 0 && $member -> fk_soc ) // If member is linked to a thirdparty
{
2013-01-12 15:16:38 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " user " ;
$sql .= " SET fk_societe= " . $member -> fk_soc ;
$sql .= " WHERE rowid= " . $this -> id ;
2013-02-11 20:45:36 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create_from_member " , LOG_DEBUG );
2013-01-12 15:16:38 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
2014-01-23 09:19:15 +01:00
$this -> error = $this -> db -> lasterror ();
2013-02-11 20:45:36 +01:00
2013-01-12 15:16:38 +01:00
$this -> db -> rollback ();
return - 1 ;
}
2008-07-19 18:53:56 +02:00
}
}
2014-01-23 09:19:15 +01:00
if ( $result > 0 )
{
$this -> db -> commit ();
return $this -> id ;
}
2008-07-19 18:53:56 +02:00
else
{
// $this->error deja positionne
$this -> db -> rollback ();
2014-01-23 09:19:15 +01:00
return - 2 ;
2008-07-19 18:53:56 +02:00
}
}
2007-04-27 00:01:10 +02:00
/**
2011-09-04 20:41:19 +02:00
* Affectation des permissions par defaut
*
* @ return Si erreur < 0 , si ok renvoi le nbre de droits par defaut positionnes
2008-07-19 18:53:56 +02:00
*/
2007-04-27 00:01:10 +02:00
function set_default_rights ()
2008-07-19 18:53:56 +02:00
{
2009-04-27 22:37:50 +02:00
global $conf ;
2009-05-07 01:30:49 +02:00
2009-04-27 22:37:50 +02:00
$sql = " SELECT id FROM " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " WHERE bydefault = 1 " ;
$sql .= " AND entity = " . $conf -> entity ;
2008-07-19 18:53:56 +02:00
2010-08-14 04:44:07 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2008-07-19 18:53:56 +02:00
{
2010-08-14 04:44:07 +02:00
$num = $this -> db -> num_rows ( $resql );
2008-07-19 18:53:56 +02:00
$i = 0 ;
$rd = array ();
while ( $i < $num )
{
2010-08-14 04:44:07 +02:00
$row = $this -> db -> fetch_row ( $resql );
2008-07-19 18:53:56 +02:00
$rd [ $i ] = $row [ 0 ];
$i ++ ;
}
2010-08-14 04:44:07 +02:00
$this -> db -> free ( $resql );
2008-07-19 18:53:56 +02:00
}
$i = 0 ;
while ( $i < $num )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user_rights WHERE fk_user = $this->id AND fk_id= $rd[$i] " ;
$result = $this -> db -> query ( $sql );
$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 ;
$i ++ ;
}
return $i ;
}
/**
2013-02-11 20:45:36 +01:00
* Update a user into database ( and also password if this -> pass is defined )
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param User $user User qui fait la mise a jour
* @ param int $notrigger 1 ne declenche pas les triggers , 0 sinon
* @ param int $nosyncmember 0 = Synchronize linked member ( standard info ), 1 = Do not synchronize linked member
* @ param int $nosyncmemberpass 0 = Synchronize linked member ( password ), 1 = Do not synchronize linked member
* @ return int < 0 si KO , >= 0 si OK
2008-07-19 18:53:56 +02:00
*/
2009-07-27 02:08:49 +02:00
function update ( $user , $notrigger = 0 , $nosyncmember = 0 , $nosyncmemberpass = 0 )
2008-07-19 18:53:56 +02:00
{
2013-01-25 19:12:54 +01:00
global $conf , $langs , $hookmanager ;
2008-07-19 18:53:56 +02:00
2007-10-21 22:40:24 +02:00
$nbrowsaffected = 0 ;
$error = 0 ;
2007-09-09 22:10:20 +02:00
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::update notrigger= " . $notrigger . " , nosyncmember= " . $nosyncmember . " , nosyncmemberpass= " . $nosyncmemberpass );
2008-07-19 18:53:56 +02:00
2009-05-07 01:30:49 +02:00
// Clean parameters
2011-09-03 19:28:10 +02:00
$this -> lastname = trim ( $this -> lastname );
$this -> firstname = trim ( $this -> firstname );
2008-07-19 18:53:56 +02:00
$this -> login = trim ( $this -> login );
$this -> pass = trim ( $this -> pass );
$this -> office_phone = trim ( $this -> office_phone );
$this -> office_fax = trim ( $this -> office_fax );
$this -> user_mobile = trim ( $this -> user_mobile );
$this -> email = trim ( $this -> email );
2013-11-07 13:17:08 +01:00
$this -> skype = trim ( $this -> skype );
2012-10-05 12:40:19 +02:00
$this -> job = trim ( $this -> job );
2011-09-24 01:46:14 +02:00
$this -> signature = trim ( $this -> signature );
2008-07-19 18:53:56 +02:00
$this -> note = trim ( $this -> note );
2011-01-12 03:43:48 +01:00
$this -> openid = trim ( empty ( $this -> openid ) ? '' : $this -> openid ); // Avoid warning
2008-07-19 18:53:56 +02:00
$this -> admin = $this -> admin ? $this -> admin : 0 ;
2013-06-05 16:12:07 +02:00
$this -> address = empty ( $this -> address ) ? '' : $this -> address ;
$this -> zip = empty ( $this -> zip ) ? '' : $this -> zip ;
$this -> town = empty ( $this -> town ) ? '' : $this -> town ;
2013-06-25 11:14:00 +02:00
$this -> accountancy_code = trim ( $this -> accountancy_code );
2013-06-16 21:31:21 +02:00
2010-01-08 18:33:30 +01:00
// Check parameters
if ( ! empty ( $conf -> global -> USER_MAIL_REQUIRED ) && ! isValidEMail ( $this -> email ))
{
$langs -> load ( " errors " );
$this -> error = $langs -> trans ( " ErrorBadEMail " , $this -> email );
return - 1 ;
}
2008-07-19 18:53:56 +02:00
$this -> db -> begin ();
// Mise a jour autres infos
$sql = " UPDATE " . MAIN_DB_PREFIX . " user SET " ;
2013-02-23 11:54:46 +01:00
$sql .= " lastname = ' " . $this -> db -> escape ( $this -> lastname ) . " ' " ;
2011-09-03 19:28:10 +02:00
$sql .= " , firstname = ' " . $this -> db -> escape ( $this -> firstname ) . " ' " ;
2011-02-12 08:02:56 +01:00
$sql .= " , login = ' " . $this -> db -> escape ( $this -> login ) . " ' " ;
2008-07-19 18:53:56 +02:00
$sql .= " , admin = " . $this -> admin ;
2013-03-23 17:40:18 +01:00
$sql .= " , address = ' " . $this -> db -> escape ( $this -> address ) . " ' " ;
$sql .= " , zip = ' " . $this -> db -> escape ( $this -> zip ) . " ' " ;
$sql .= " , town = ' " . $this -> db -> escape ( $this -> town ) . " ' " ;
2013-05-24 14:32:12 +02:00
$sql .= " , fk_state = " . (( ! empty ( $this -> state_id ) && $this -> state_id > 0 ) ? " ' " . $this -> db -> escape ( $this -> state_id ) . " ' " : " null " );
$sql .= " , fk_country = " . (( ! empty ( $this -> country_id ) && $this -> country_id > 0 ) ? " ' " . $this -> db -> escape ( $this -> country_id ) . " ' " : " null " );
2011-02-12 08:02:56 +01:00
$sql .= " , office_phone = ' " . $this -> db -> escape ( $this -> office_phone ) . " ' " ;
$sql .= " , office_fax = ' " . $this -> db -> escape ( $this -> office_fax ) . " ' " ;
$sql .= " , user_mobile = ' " . $this -> db -> escape ( $this -> user_mobile ) . " ' " ;
$sql .= " , email = ' " . $this -> db -> escape ( $this -> email ) . " ' " ;
2013-11-07 13:17:08 +01:00
$sql .= " , skype = ' " . $this -> db -> escape ( $this -> skype ) . " ' " ;
2012-10-05 12:40:19 +02:00
$sql .= " , job = ' " . $this -> db -> escape ( $this -> job ) . " ' " ;
$sql .= " , signature = ' " . $this -> db -> escape ( $this -> signature ) . " ' " ;
2013-06-25 11:14:00 +02:00
$sql .= " , accountancy_code = ' " . $this -> db -> escape ( $this -> accountancy_code ) . " ' " ;
2014-03-16 20:37:43 +01:00
$sql .= " , thm = " . ( isset ( $this -> thm ) ? $this -> thm : " null " ); // If not set, we use null
2011-02-12 08:02:56 +01:00
$sql .= " , note = ' " . $this -> db -> escape ( $this -> note ) . " ' " ;
$sql .= " , photo = " . ( $this -> photo ? " ' " . $this -> db -> escape ( $this -> photo ) . " ' " : " null " );
$sql .= " , openid = " . ( $this -> openid ? " ' " . $this -> db -> escape ( $this -> openid ) . " ' " : " null " );
2013-02-15 18:39:27 +01:00
$sql .= " , fk_user = " . ( $this -> fk_user > 0 ? " ' " . $this -> db -> escape ( $this -> fk_user ) . " ' " : " null " );
2011-05-16 21:56:43 +02:00
$sql .= " , entity = ' " . $this -> entity . " ' " ;
2008-07-19 18:53:56 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$nbrowsaffected += $this -> db -> affected_rows ( $resql );
2008-08-30 01:49:53 +02:00
2009-07-27 02:08:49 +02:00
// Update password
2008-07-19 18:53:56 +02:00
if ( $this -> pass )
{
if ( $this -> pass != $this -> pass_indatabase && $this -> pass != $this -> pass_indatabase_crypted )
{
// Si mot de passe saisi et different de celui en base
2009-07-27 02:08:49 +02:00
$result = $this -> setPassword ( $user , $this -> pass , 0 , $notrigger , $nosyncmemberpass );
2013-02-11 20:45:36 +01:00
if ( ! $nbrowsaffected ) $nbrowsaffected ++ ;
2008-07-19 18:53:56 +02:00
}
}
2008-08-30 01:49:53 +02:00
2009-05-07 01:30:49 +02:00
// If user is linked to a member, remove old link to this member
if ( $this -> fk_member > 0 )
2007-10-21 22:40:24 +02:00
{
2009-05-07 01:30:49 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " user SET fk_member = NULL where fk_member = " . $this -> fk_member ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2009-05-07 01:30:49 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) { $this -> error = $this -> db -> error (); $this -> db -> rollback (); return - 5 ; }
}
// Set link to user
$sql = " UPDATE " . MAIN_DB_PREFIX . " user SET fk_member = " . ( $this -> fk_member > 0 ? $this -> fk_member : 'null' ) . " where rowid = " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2009-05-07 01:30:49 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) { $this -> error = $this -> db -> error (); $this -> db -> rollback (); return - 5 ; }
if ( $nbrowsaffected ) // If something has changed in data
{
if ( $this -> fk_member > 0 && ! $nosyncmember )
2007-10-21 22:40:24 +02:00
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php' ;
2008-08-30 01:49:53 +02:00
2007-10-21 22:40:24 +02:00
// This user is linked with a member, so we also update members informations
// if this is an update.
2007-09-07 20:25:53 +02:00
$adh = new Adherent ( $this -> db );
$result = $adh -> fetch ( $this -> fk_member );
2008-08-30 01:49:53 +02:00
2007-09-09 22:10:20 +02:00
if ( $result >= 0 )
{
2012-04-30 16:08:34 +02:00
$adh -> firstname = $this -> firstname ;
$adh -> lastname = $this -> lastname ;
2007-09-09 22:10:20 +02:00
$adh -> login = $this -> login ;
$adh -> pass = $this -> pass ;
2009-05-07 01:30:49 +02:00
$adh -> societe = ( empty ( $adh -> societe ) && $this -> societe_id ? $this -> societe_id : $adh -> societe );
2007-09-09 22:10:20 +02:00
$adh -> email = $this -> email ;
2013-11-07 13:17:08 +01:00
$adh -> skype = $this -> skype ;
2007-09-09 22:10:20 +02:00
$adh -> phone = $this -> office_phone ;
$adh -> phone_mobile = $this -> user_mobile ;
2008-07-19 18:53:56 +02:00
2008-03-21 01:27:37 +01:00
$adh -> note = $this -> note ;
2007-09-09 22:10:20 +02:00
$adh -> user_id = $this -> id ;
$adh -> user_login = $this -> login ;
2009-05-07 01:30:49 +02:00
2007-09-09 22:10:20 +02:00
$result = $adh -> update ( $user , 0 , 1 );
2007-10-22 01:04:06 +02:00
if ( $result < 0 )
2007-09-09 22:10:20 +02:00
{
2007-10-22 01:04:06 +02:00
$this -> error = $luser -> error ;
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::update " . $this -> error , LOG_ERR );
2007-09-09 22:10:20 +02:00
$error ++ ;
}
}
else
2007-09-07 20:25:53 +02:00
{
$this -> error = $adh -> error ;
$error ++ ;
}
}
2011-09-03 18:44:34 +02:00
}
2011-09-04 20:41:19 +02:00
2012-12-02 11:03:23 +01:00
// Actions on extra fields (by external module or standard code)
$hookmanager -> initHooks ( array ( 'userdao' ));
$parameters = array ( 'socid' => $this -> id );
$reshook = $hookmanager -> executeHooks ( 'insertExtraFields' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( empty ( $reshook ))
{
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
}
else if ( $reshook < 0 ) $error ++ ;
2012-11-30 15:55:59 +01:00
2011-09-03 18:44:34 +02:00
if ( ! $error && ! $notrigger )
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'USER_MODIFY' , $user );
2014-07-05 08:59:41 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2008-07-19 18:53:56 +02:00
}
2007-09-07 20:25:53 +02:00
if ( ! $error )
{
$this -> db -> commit ();
2011-09-03 18:37:12 +02:00
return $nbrowsaffected ;
2007-09-07 20:25:53 +02:00
}
else
{
2011-09-03 18:37:12 +02:00
dol_syslog ( get_class ( $this ) . " ::update error= " . $this -> error , LOG_ERR );
2007-09-07 20:25:53 +02:00
$this -> db -> rollback ();
2011-09-03 18:37:12 +02:00
return - 1 ;
2007-09-07 20:25:53 +02:00
}
2008-07-19 18:53:56 +02:00
}
else
{
$this -> error = $this -> db -> lasterror ();
2011-09-03 18:37:12 +02:00
$this -> db -> rollback ();
return - 2 ;
2008-07-19 18:53:56 +02:00
}
2005-02-08 01:18:23 +01:00
2008-02-24 18:01:48 +01:00
}
2004-08-07 20:47:54 +02:00
2007-09-08 11:44:26 +02:00
/**
2013-02-11 20:45:36 +01:00
* Mise a jour en base de la date de derniere connexion d ' un utilisateur
2011-09-04 20:41:19 +02:00
* Fonction appelee lors d ' une nouvelle connexion
*
* @ return < 0 si echec , >= 0 si ok
2008-07-19 18:53:56 +02:00
*/
2008-02-24 18:01:48 +01:00
function update_last_login_date ()
2008-07-19 18:53:56 +02:00
{
2011-05-28 02:16:12 +02:00
$now = dol_now ();
2008-07-19 18:53:56 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " user SET " ;
$sql .= " datepreviouslogin = datelastlogin, " ;
2012-03-11 21:45:17 +01:00
$sql .= " datelastlogin = ' " . $this -> db -> idate ( $now ) . " ', " ;
2008-07-19 18:53:56 +02:00
$sql .= " tms = tms " ; // La date de derniere modif doit changer sauf pour la mise a jour de date de derniere connexion
$sql .= " WHERE rowid = " . $this -> id ;
2009-01-30 23:18:07 +01:00
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::update_last_login_date user->id= " . $this -> id . " " . $sql , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> datepreviouslogin = $this -> datelastlogin ;
$this -> datelastlogin = $now ;
return 1 ;
}
else
{
2011-09-24 01:46:14 +02:00
$this -> error = $this -> db -> lasterror () . ' sql=' . $sql ;
2008-07-19 18:53:56 +02:00
return - 1 ;
}
}
2006-11-20 03:10:47 +01:00
2006-04-24 23:28:24 +02:00
/**
2011-01-29 18:41:26 +01:00
* Change password of a user
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param User $user Object user of user making change
* @ param string $password New password in clear text ( to generate if not provided )
* @ param int $changelater 1 = Change password only after clicking on confirm email
* @ param int $notrigger 1 = Does not launch triggers
* @ param int $nosyncmember Do not synchronize linked member
* @ return string If OK return clear password , 0 if no change , < 0 if error
2008-07-19 18:53:56 +02:00
*/
function setPassword ( $user , $password = '' , $changelater = 0 , $notrigger = 0 , $nosyncmember = 0 )
{
global $conf , $langs ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
2011-09-20 19:19:46 +02:00
2007-10-22 01:04:06 +02:00
$error = 0 ;
2006-11-20 03:10:47 +01:00
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::setPassword user= " . $user -> id . " password= " . preg_replace ( '/./i' , '*' , $password ) . " changelater= " . $changelater . " notrigger= " . $notrigger . " nosyncmember= " . $nosyncmember , LOG_DEBUG );
2006-11-20 03:10:47 +01:00
2011-09-24 01:46:14 +02:00
// If new password not provided, we generate one
2008-07-19 18:53:56 +02:00
if ( ! $password )
{
2014-04-28 04:27:49 +02:00
$password = getRandomPassword ( false );
2008-07-19 18:53:56 +02:00
}
// Crypte avec md5
2011-09-06 08:53:52 +02:00
$password_crypted = dol_hash ( $password );
2006-11-25 22:39:58 +01:00
// Mise a jour
2007-06-02 03:53:54 +02:00
if ( ! $changelater )
2007-04-23 00:55:32 +02:00
{
2011-11-02 18:05:45 +01:00
if ( ! is_object ( $this -> oldcopy )) $this -> oldcopy = dol_clone ( $this );
2014-07-03 14:01:01 +02:00
$this -> db -> begin ();
2014-07-05 08:59:41 +02:00
2011-11-02 18:05:45 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " user " ;
2011-02-12 08:02:56 +01:00
$sql .= " SET pass_crypted = ' " . $this -> db -> escape ( $password_crypted ) . " ', " ;
2007-06-02 03:53:54 +02:00
$sql .= " pass_temp = null " ;
2007-12-31 13:32:30 +01:00
if ( ! empty ( $conf -> global -> DATABASE_PWD_ENCRYPTED ))
2007-06-02 03:53:54 +02:00
{
$sql .= " , pass = null " ;
}
else
{
2011-02-12 08:02:56 +01:00
$sql .= " , pass = ' " . $this -> db -> escape ( $password ) . " ' " ;
2007-06-02 03:53:54 +02:00
}
2008-07-19 18:53:56 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2007-06-02 03:53:54 +02:00
2014-07-02 21:29:07 +02:00
dol_syslog ( get_class ( $this ) . " ::setPassword " , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
2007-10-21 22:40:24 +02:00
if ( $this -> db -> affected_rows ( $result ))
2008-07-19 18:53:56 +02:00
{
$this -> pass = $password ;
$this -> pass_indatabase = $password ;
$this -> pass_indatabase_crypted = $password_crypted ;
2007-06-02 03:53:54 +02:00
2007-10-22 01:04:06 +02:00
if ( $this -> fk_member && ! $nosyncmember )
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php' ;
2008-07-19 18:53:56 +02:00
2007-10-22 01:04:06 +02:00
// This user is linked with a member, so we also update members informations
// if this is an update.
$adh = new Adherent ( $this -> db );
$result = $adh -> fetch ( $this -> fk_member );
2008-07-19 18:53:56 +02:00
2007-10-22 01:04:06 +02:00
if ( $result >= 0 )
{
2007-12-31 13:32:30 +01:00
$result = $adh -> setPassword ( $user , $this -> pass , 0 , 1 ); // Cryptage non gere dans module adherent
2007-10-22 01:04:06 +02:00
if ( $result < 0 )
{
$this -> error = $adh -> error ;
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::setPassword " . $this -> error , LOG_ERR );
2007-10-22 01:04:06 +02:00
$error ++ ;
}
}
else
{
$this -> error = $adh -> error ;
$error ++ ;
}
}
2011-01-12 03:43:48 +01:00
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::setPassword notrigger= " . $notrigger . " error= " . $error , LOG_DEBUG );
2007-10-22 01:04:06 +02:00
if ( ! $error && ! $notrigger )
2007-08-31 09:43:23 +02:00
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'USER_NEW_PASSWORD' , $user );
2014-07-05 08:59:41 +02:00
if ( $result < 0 ) { $error ++ ; $this -> db -> rollback (); return - 1 ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2007-08-31 09:43:23 +02:00
}
2014-07-05 08:59:41 +02:00
2014-07-03 14:01:01 +02:00
$this -> db -> commit ();
2008-07-19 18:53:56 +02:00
return $this -> pass ;
}
else
2007-10-21 22:40:24 +02:00
{
2014-07-03 14:01:01 +02:00
$this -> db -> rollback ();
2008-07-19 18:53:56 +02:00
return 0 ;
}
}
else
{
2014-07-03 14:01:01 +02:00
$this -> db -> rollback ();
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
return - 1 ;
}
2007-06-02 03:53:54 +02:00
}
else
{
// We store clear password in password temporary field.
// After receiving confirmation link, we will crypt it and store it in pass_crypted
$sql = " UPDATE " . MAIN_DB_PREFIX . " user " ;
2011-02-12 08:02:56 +01:00
$sql .= " SET pass_temp = ' " . $this -> db -> escape ( $password ) . " ' " ;
2008-07-19 18:53:56 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2007-09-01 00:06:14 +02:00
2014-07-02 21:29:07 +02:00
dol_syslog ( get_class ( $this ) . " ::setPassword " , LOG_DEBUG ); // No log
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
2007-06-02 03:53:54 +02:00
{
2008-07-19 18:53:56 +02:00
return $password ;
2007-06-02 03:53:54 +02:00
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
return - 3 ;
}
2007-04-23 00:55:32 +02:00
}
2008-07-19 18:53:56 +02:00
}
2002-12-18 19:02:06 +01:00
2007-06-02 03:53:54 +02:00
/**
2011-09-20 19:34:15 +02:00
* Envoie mot de passe par mail
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param User $user Object user de l 'utilisateur qui fait l' envoi
* @ param string $password Nouveau mot de passe
* @ param int $changelater 1 = Change password only after clicking on confirm email
* @ return int < 0 si erreur , > 0 si ok
2008-07-19 18:53:56 +02:00
*/
function send_password ( $user , $password = '' , $changelater = 0 )
{
global $conf , $langs ;
2011-12-05 19:41:38 +01:00
global $dolibarr_main_url_root ;
2012-01-07 15:01:30 +01:00
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
2005-02-04 12:08:01 +01:00
2008-07-19 18:53:56 +02:00
$msgishtml = 0 ;
2006-11-20 03:10:47 +01:00
2007-06-02 03:53:54 +02:00
// Define $msg
2008-07-19 18:53:56 +02:00
$mesg = '' ;
2008-08-30 01:49:53 +02:00
2011-01-30 13:40:45 +01:00
$outputlangs = new Translate ( " " , $conf );
2008-08-30 01:49:53 +02:00
if ( isset ( $this -> conf -> MAIN_LANG_DEFAULT )
&& $this -> conf -> MAIN_LANG_DEFAULT != 'auto' )
{ // If user has defined its own language (rare because in most cases, auto is used)
$outputlangs -> getDefaultLang ( $this -> conf -> MAIN_LANG_DEFAULT );
}
else
{ // If user has not defined its own language, we used current language
$outputlangs = $langs ;
}
2009-01-09 22:22:58 +01:00
2013-08-25 06:53:59 +02:00
$outputlangs -> load ( " main " );
$outputlangs -> load ( " errors " );
$outputlangs -> load ( " users " );
$outputlangs -> load ( " other " );
2014-03-15 14:23:41 +01:00
$subject = $outputlangs -> transnoentitiesnoconv ( " SubjectNewPassword " );
2013-08-25 06:53:59 +02:00
2012-12-12 14:46:13 +01:00
// Define $urlwithroot
2012-12-13 13:08:03 +01:00
//$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
//$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
$urlwithroot = DOL_MAIN_URL_ROOT ; // This is to use same domain name than current
2012-09-26 12:06:08 +02:00
2007-06-02 03:53:54 +02:00
if ( ! $changelater )
{
2013-08-25 06:53:59 +02:00
$mesg .= $outputlangs -> transnoentitiesnoconv ( " RequestToResetPasswordReceived " ) . " . \n " ;
$mesg .= $outputlangs -> transnoentitiesnoconv ( " NewKeyIs " ) . " : \n \n " ;
$mesg .= $outputlangs -> transnoentitiesnoconv ( " Login " ) . " = " . $this -> login . " \n " ;
$mesg .= $outputlangs -> transnoentitiesnoconv ( " Password " ) . " = " . $password . " \n \n " ;
2007-06-02 03:53:54 +02:00
$mesg .= " \n " ;
2012-12-12 14:46:13 +01:00
$url = $urlwithroot ;
2013-08-25 06:53:59 +02:00
$mesg .= $outputlangs -> transnoentitiesnoconv ( " ClickHereToGoTo " , $conf -> global -> MAIN_APPLICATION_TITLE ) . ': ' . $url . " \n \n " ;
2008-07-19 18:53:56 +02:00
$mesg .= " -- \n " ;
2013-08-25 06:53:59 +02:00
$mesg .= $user -> getFullName ( $outputlangs ); // Username that make then sending
2007-06-02 03:53:54 +02:00
}
else
{
2013-08-25 06:53:59 +02:00
$mesg .= $outputlangs -> transnoentitiesnoconv ( " RequestToResetPasswordReceived " ) . " \n " ;
$mesg .= $outputlangs -> transnoentitiesnoconv ( " NewKeyWillBe " ) . " : \n \n " ;
$mesg .= $outputlangs -> transnoentitiesnoconv ( " Login " ) . " = " . $this -> login . " \n " ;
$mesg .= $outputlangs -> transnoentitiesnoconv ( " Password " ) . " = " . $password . " \n \n " ;
2007-06-02 03:53:54 +02:00
$mesg .= " \n " ;
2013-08-25 06:53:59 +02:00
$mesg .= $outputlangs -> transnoentitiesnoconv ( " YouMustClickToChange " ) . " : \n " ;
2012-12-12 14:46:13 +01:00
$url = $urlwithroot . '/user/passwordforgotten.php?action=validatenewpassword&username=' . $this -> login . " &passwordmd5= " . dol_hash ( $password );
2008-07-19 18:53:56 +02:00
$mesg .= $url . " \n \n " ;
2013-08-25 06:53:59 +02:00
$mesg .= $outputlangs -> transnoentitiesnoconv ( " ForgetIfNothing " ) . " \n \n " ;
2012-04-01 20:31:39 +02:00
dol_syslog ( get_class ( $this ) . " ::send_password url= " . $url );
2007-06-02 03:53:54 +02:00
}
2012-04-02 18:29:08 +02:00
$mailfile = new CMailFile (
2012-03-27 09:59:28 +02:00
$subject ,
$this -> email ,
$conf -> notification -> email_from ,
$mesg ,
array (),
array (),
array (),
'' ,
'' ,
0 ,
$msgishtml
2012-05-09 00:28:02 +02:00
);
2005-07-02 00:09:12 +02:00
2008-07-19 18:53:56 +02:00
if ( $mailfile -> sendfile ())
{
return 1 ;
}
else
{
2011-09-24 01:46:14 +02:00
$langs -> trans ( " errors " );
2008-07-19 18:53:56 +02:00
$this -> error = $langs -> trans ( " ErrorFailedToSendPassword " ) . ' ' . $mailfile -> error ;
return - 1 ;
}
}
/**
2011-09-04 20:41:19 +02:00
* Renvoie la derniere erreur fonctionnelle de manipulation de l ' objet
*
* @ return string chaine erreur
2008-07-19 18:53:56 +02:00
*/
2006-06-07 00:54:23 +02:00
function error ()
2008-07-19 18:53:56 +02:00
{
return $this -> error ;
}
2005-02-26 19:34:14 +01:00
2006-11-20 03:10:47 +01:00
2008-07-19 18:53:56 +02:00
/**
2011-09-04 20:41:19 +02:00
* Read clicktodial information for user
*
* @ return < 0 if KO , > 0 if OK
2008-07-19 18:53:56 +02:00
*/
function fetch_clicktodial ()
{
2013-03-31 16:44:24 +02:00
$sql = " SELECT url, login, pass, poste " ;
2008-07-19 18:53:56 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user_clicktodial as u " ;
$sql .= " WHERE u.fk_user = " . $this -> id ;
2006-11-20 03:10:47 +01:00
2009-01-09 22:22:58 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2008-07-19 18:53:56 +02:00
{
2009-01-09 22:22:58 +01:00
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
2005-01-14 15:18:45 +01:00
2013-03-31 16:44:24 +02:00
$this -> clicktodial_url = $obj -> url ;
2009-01-09 22:22:58 +01:00
$this -> clicktodial_login = $obj -> login ;
$this -> clicktodial_password = $obj -> pass ;
$this -> clicktodial_poste = $obj -> poste ;
}
2005-01-14 15:18:45 +01:00
2009-01-09 22:22:58 +01:00
$this -> clicktodial_loaded = 1 ; // Data loaded (found or not)
2009-01-14 15:59:28 +01:00
2009-01-09 22:22:58 +01:00
$this -> db -> free ( $resql );
return 1 ;
2008-07-19 18:53:56 +02:00
}
else
{
2009-01-09 22:22:58 +01:00
$this -> error = $this -> db -> error ();
return - 1 ;
2008-07-19 18:53:56 +02:00
}
2005-01-14 15:18:45 +01:00
}
2005-02-26 19:34:14 +01:00
2008-07-19 18:53:56 +02:00
/**
2011-09-20 19:34:15 +02:00
* Update clicktodial info
*
* @ return void
2008-07-19 18:53:56 +02:00
*/
function update_clicktodial ()
{
2011-09-24 01:46:14 +02:00
$this -> db -> begin ();
2006-11-20 03:10:47 +01:00
2008-07-19 18:53:56 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user_clicktodial " ;
$sql .= " WHERE fk_user = " . $this -> id ;
2002-12-18 19:02:06 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::update_clicktodial' , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
2005-01-14 15:18:45 +01:00
2008-07-19 18:53:56 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " user_clicktodial " ;
2013-03-31 16:44:24 +02:00
$sql .= " (fk_user,url,login,pass,poste) " ;
2008-07-19 18:53:56 +02:00
$sql .= " VALUES ( " . $this -> id ;
2013-03-31 16:44:24 +02:00
$sql .= " , ' " . $this -> db -> escape ( $this -> clicktodial_url ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> clicktodial_login ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> clicktodial_password ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> clicktodial_poste ) . " ') " ;
2006-11-20 03:10:47 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::update_clicktodial' , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
2011-09-24 01:46:14 +02:00
$this -> db -> commit ();
2013-03-31 16:44:24 +02:00
return 1 ;
2008-07-19 18:53:56 +02:00
}
else
{
2011-09-24 01:46:14 +02:00
$this -> db -> rollback ();
2013-03-31 16:44:24 +02:00
$this -> error = $this -> db -> lasterror ();
2011-09-24 01:46:14 +02:00
return - 1 ;
2008-07-19 18:53:56 +02:00
}
2005-01-14 15:18:45 +01:00
}
2005-01-27 16:56:32 +01:00
2005-02-26 19:34:14 +01:00
2008-07-19 18:53:56 +02:00
/**
2011-09-20 19:34:15 +02:00
* Add user into a group
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param Group $group Id of group
* @ param int $entity Entity
* @ param int $notrigger Disable triggers
* @ return int < 0 if KO , > 0 if OK
2008-07-19 18:53:56 +02:00
*/
2011-06-07 17:45:01 +02:00
function SetInGroup ( $group , $entity , $notrigger = 0 )
2008-07-19 18:53:56 +02:00
{
2011-06-07 17:45:01 +02:00
global $conf , $langs , $user ;
2011-06-29 12:23:32 +02:00
2011-09-24 01:46:14 +02:00
$error = 0 ;
2011-05-28 02:16:12 +02:00
$this -> db -> begin ();
2005-01-27 16:56:32 +01:00
2008-07-19 18:53:56 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " usergroup_user " ;
2011-05-16 17:21:54 +02:00
$sql .= " WHERE fk_user = " . $this -> id ;
$sql .= " AND fk_usergroup = " . $group ;
2011-06-07 17:45:01 +02:00
$sql .= " AND entity = " . $entity ;
2006-11-20 03:10:47 +01:00
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
2005-01-27 16:56:32 +01:00
2011-05-16 17:21:54 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " usergroup_user (entity, fk_user, fk_usergroup) " ;
2011-06-07 17:45:01 +02:00
$sql .= " VALUES ( " . $entity . " , " . $this -> id . " , " . $group . " ) " ;
2006-11-20 03:10:47 +01:00
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
2011-06-07 17:45:01 +02:00
if ( $result )
{
if ( ! $error && ! $notrigger )
{
2012-05-11 12:47:15 +02:00
$this -> newgroupid = $group ;
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'USER_SETINGROUP' , $user );
2014-07-05 08:59:41 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2011-06-07 17:45:01 +02:00
}
2011-06-29 12:23:32 +02:00
2011-06-07 17:45:01 +02:00
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
else
{
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::SetInGroup " . $this -> error , LOG_ERR );
2011-06-07 17:45:01 +02:00
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
2008-07-19 18:53:56 +02:00
}
2005-01-27 16:56:32 +01:00
2006-08-07 04:20:57 +02:00
/**
2011-09-20 19:34:15 +02:00
* Remove a user from a group
2011-09-04 20:41:19 +02:00
*
2011-09-24 01:46:14 +02:00
* @ param Group $group Id of group
* @ param int $entity Entity
* @ param int $notrigger Disable triggers
* @ return int < 0 if KO , > 0 if OK
2006-08-07 04:20:57 +02:00
*/
2011-06-07 17:45:01 +02:00
function RemoveFromGroup ( $group , $entity , $notrigger = 0 )
2008-07-19 18:53:56 +02:00
{
2011-06-07 17:45:01 +02:00
global $conf , $langs , $user ;
2011-06-29 12:23:32 +02:00
2011-09-24 01:46:14 +02:00
$error = 0 ;
2005-01-27 16:56:32 +01:00
2011-09-24 01:46:14 +02:00
$this -> db -> begin ();
2011-05-28 02:16:12 +02:00
2011-09-24 01:46:14 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " usergroup_user " ;
2011-05-16 17:21:54 +02:00
$sql .= " WHERE fk_user = " . $this -> id ;
$sql .= " AND fk_usergroup = " . $group ;
2011-06-07 17:45:01 +02:00
$sql .= " AND entity = " . $entity ;
2006-11-20 03:10:47 +01:00
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
2011-06-07 17:45:01 +02:00
if ( $result )
{
if ( ! $error && ! $notrigger )
{
2012-05-11 12:47:15 +02:00
$this -> oldgroupid = $group ;
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'USER_REMOVEFROMGROUP' , $user );
2014-07-05 08:59:41 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2011-06-07 17:45:01 +02:00
}
2011-05-28 02:16:12 +02:00
2011-06-07 17:45:01 +02:00
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $interface -> error ;
2011-09-24 01:46:14 +02:00
dol_syslog ( get_class ( $this ) . " ::RemoveFromGroup " . $this -> error , LOG_ERR );
2011-06-07 17:45:01 +02:00
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
2008-07-19 18:53:56 +02:00
}
2006-08-07 04:20:57 +02:00
/**
2014-03-05 09:57:36 +01:00
* Return a link to the user card ( with optionaly the picto )
2013-02-23 15:26:39 +01:00
* Use this -> id , this -> lastname , this -> firstname
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param int $withpicto Include picto in link ( 0 = No picto , 1 = Inclut le picto dans le lien , 2 = Picto seul )
* @ param string $option On what the link point to
* @ return string String with URL
2006-08-07 04:20:57 +02:00
*/
function getNomUrl ( $withpicto = 0 , $option = '' )
{
global $langs ;
2006-11-20 03:10:47 +01:00
2006-08-07 04:20:57 +02:00
$result = '' ;
2006-11-20 03:10:47 +01:00
2006-08-07 04:20:57 +02:00
$lien = '<a href="' . DOL_URL_ROOT . '/user/fiche.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
2013-04-29 22:04:46 +02:00
if ( $withpicto )
2006-08-07 04:20:57 +02:00
{
2013-04-29 22:04:46 +02:00
$result .= ( $lien . img_object ( $langs -> trans ( " ShowUser " ), 'user' ) . $lienfin );
if ( $withpicto != 2 ) $result .= ' ' ;
2006-08-07 04:20:57 +02:00
}
2010-07-18 12:39:07 +02:00
$result .= $lien . $this -> getFullName ( $langs ) . $lienfin ;
2006-08-07 04:20:57 +02:00
return $result ;
2006-09-01 01:44:34 +02:00
}
2006-11-20 03:10:47 +01:00
2006-09-01 01:44:34 +02:00
/**
2011-09-20 19:34:15 +02:00
* Renvoie login clicable ( avec eventuellement le picto )
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param int $withpicto Inclut le picto dans le lien
* @ param string $option Sur quoi pointe le lien
* @ return string Chaine avec URL
2006-09-01 01:44:34 +02:00
*/
function getLoginUrl ( $withpicto = 0 , $option = '' )
{
global $langs ;
2006-11-20 03:10:47 +01:00
2006-09-01 01:44:34 +02:00
$result = '' ;
2006-11-20 03:10:47 +01:00
2006-09-01 01:44:34 +02:00
$lien = '<a href="' . DOL_URL_ROOT . '/user/fiche.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
if ( $option == 'xxx' )
{
$lien = '<a href="' . DOL_URL_ROOT . '/user/fiche.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
if ( $withpicto ) $result .= ( $lien . img_object ( $langs -> trans ( " ShowUser " ), 'user' ) . $lienfin . ' ' );
$result .= $lien . $this -> login . $lienfin ;
return $result ;
2006-09-02 03:17:50 +02:00
}
2006-11-20 03:10:47 +01:00
2006-09-02 03:17:50 +02:00
/**
2011-09-20 19:34:15 +02:00
* Retourne le libelle du statut d ' un user ( actif , inactif )
2011-09-04 20:41:19 +02:00
*
2013-02-20 21:16:31 +01:00
* @ param int $mode 0 = libelle long , 1 = libelle court , 2 = Picto + Libelle court , 3 = Picto , 4 = Picto + Libelle long , 5 = Libelle court + Picto
2011-09-20 19:34:15 +02:00
* @ return string Label of status
2006-09-02 03:17:50 +02:00
*/
function getLibStatut ( $mode = 0 )
{
return $this -> LibStatut ( $this -> statut , $mode );
}
/**
2011-09-20 19:34:15 +02:00
* Renvoi le libelle d ' un statut donne
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param int $statut Id statut
* @ param int $mode 0 = libelle long , 1 = libelle court , 2 = Picto + Libelle court , 3 = Picto , 4 = Picto + Libelle long , 5 = Libelle court + Picto
* @ return string Label of status
2006-09-02 03:17:50 +02:00
*/
function LibStatut ( $statut , $mode = 0 )
{
global $langs ;
$langs -> load ( 'users' );
if ( $mode == 0 )
{
$prefix = '' ;
if ( $statut == 1 ) return $langs -> trans ( 'Enabled' );
if ( $statut == 0 ) return $langs -> trans ( 'Disabled' );
}
if ( $mode == 1 )
{
if ( $statut == 1 ) return $langs -> trans ( 'Enabled' );
if ( $statut == 0 ) return $langs -> trans ( 'Disabled' );
}
if ( $mode == 2 )
{
if ( $statut == 1 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' ) . ' ' . $langs -> trans ( 'Enabled' );
if ( $statut == 0 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' ) . ' ' . $langs -> trans ( 'Disabled' );
}
if ( $mode == 3 )
{
if ( $statut == 1 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' );
if ( $statut == 0 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' );
}
if ( $mode == 4 )
{
if ( $statut == 1 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' ) . ' ' . $langs -> trans ( 'Enabled' );
if ( $statut == 0 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' ) . ' ' . $langs -> trans ( 'Disabled' );
}
if ( $mode == 5 )
{
if ( $statut == 1 ) return $langs -> trans ( 'Enabled' ) . ' ' . img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' );
if ( $statut == 0 ) return $langs -> trans ( 'Disabled' ) . ' ' . img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' );
}
2006-11-20 03:10:47 +01:00
}
2006-11-19 17:02:53 +01:00
2009-08-11 14:51:33 +02:00
/**
2011-05-01 12:48:43 +02:00
* Retourne chaine DN complete dans l 'annuaire LDAP pour l' objet
2011-09-04 20:41:19 +02:00
*
2014-06-20 13:07:15 +02:00
* @ param array $info Info array loaded by _load_ldap_info
2011-09-20 19:34:15 +02:00
* @ param int $mode 0 = Return full DN ( uid = qqq , ou = xxx , dc = aaa , dc = bbb )
* 1 =
* 2 = Return key only ( uid = qqq )
* @ return string DN
2008-07-19 18:53:56 +02:00
*/
2006-11-21 23:51:05 +01:00
function _load_ldap_dn ( $info , $mode = 0 )
2006-11-20 02:13:13 +01:00
{
2006-11-21 23:51:05 +01:00
global $conf ;
$dn = '' ;
if ( $mode == 0 ) $dn = $conf -> global -> LDAP_KEY_USERS . " = " . $info [ $conf -> global -> LDAP_KEY_USERS ] . " , " . $conf -> global -> LDAP_USER_DN ;
if ( $mode == 1 ) $dn = $conf -> global -> LDAP_USER_DN ;
if ( $mode == 2 ) $dn = $conf -> global -> LDAP_KEY_USERS . " = " . $info [ $conf -> global -> LDAP_KEY_USERS ];
return $dn ;
2006-11-20 02:13:13 +01:00
}
2009-08-11 22:16:58 +02:00
/**
2011-05-01 12:48:43 +02:00
* Initialize the info array ( array of LDAP values ) that will be used to call LDAP functions
2011-09-04 20:41:19 +02:00
*
2011-05-01 12:48:43 +02:00
* @ return array Tableau info des attributs
2008-07-19 18:53:56 +02:00
*/
2006-11-21 23:51:05 +01:00
function _load_ldap_info ()
2006-11-20 02:13:13 +01:00
{
global $conf , $langs ;
2006-11-20 03:10:47 +01:00
2006-11-21 23:51:05 +01:00
$info = array ();
2007-04-13 02:35:13 +02:00
// Object classes
2009-10-20 15:14:44 +02:00
$info [ " objectclass " ] = explode ( ',' , $conf -> global -> LDAP_USER_OBJECT_CLASS );
2008-07-19 18:53:56 +02:00
2010-07-18 12:39:07 +02:00
$this -> fullname = $this -> getFullName ( $langs );
2006-11-20 03:10:47 +01:00
// Champs
2012-09-15 11:21:22 +02:00
if ( $this -> fullname && ! empty ( $conf -> global -> LDAP_FIELD_FULLNAME )) $info [ $conf -> global -> LDAP_FIELD_FULLNAME ] = $this -> fullname ;
if ( $this -> lastname && ! empty ( $conf -> global -> LDAP_FIELD_NAME )) $info [ $conf -> global -> LDAP_FIELD_NAME ] = $this -> lastname ;
if ( $this -> firstname && ! empty ( $conf -> global -> LDAP_FIELD_FIRSTNAME )) $info [ $conf -> global -> LDAP_FIELD_FIRSTNAME ] = $this -> firstname ;
if ( $this -> login && ! empty ( $conf -> global -> LDAP_FIELD_LOGIN )) $info [ $conf -> global -> LDAP_FIELD_LOGIN ] = $this -> login ;
if ( $this -> login && ! empty ( $conf -> global -> LDAP_FIELD_LOGIN_SAMBA )) $info [ $conf -> global -> LDAP_FIELD_LOGIN_SAMBA ] = $this -> login ;
if ( $this -> pass && ! empty ( $conf -> global -> LDAP_FIELD_PASSWORD )) $info [ $conf -> global -> LDAP_FIELD_PASSWORD ] = $this -> pass ; // this->pass = mot de passe non crypte
if ( $this -> ldap_sid && ! empty ( $conf -> global -> LDAP_FIELD_SID )) $info [ $conf -> global -> LDAP_FIELD_SID ] = $this -> ldap_sid ;
2006-11-20 02:13:13 +01:00
if ( $this -> societe_id > 0 )
{
$soc = new Societe ( $this -> db );
$soc -> fetch ( $this -> societe_id );
2006-11-20 03:10:47 +01:00
2013-02-23 11:42:12 +01:00
$info [ " o " ] = $soc -> lastname ;
2006-11-20 02:13:13 +01:00
if ( $soc -> client == 1 ) $info [ " businessCategory " ] = " Customers " ;
if ( $soc -> client == 2 ) $info [ " businessCategory " ] = " Prospects " ;
if ( $soc -> fournisseur == 1 ) $info [ " businessCategory " ] = " Suppliers " ;
}
2012-09-15 11:21:22 +02:00
if ( $this -> address && ! empty ( $conf -> global -> LDAP_FIELD_ADDRESS )) $info [ $conf -> global -> LDAP_FIELD_ADDRESS ] = $this -> address ;
if ( $this -> zip && ! empty ( $conf -> global -> LDAP_FIELD_ZIP )) $info [ $conf -> global -> LDAP_FIELD_ZIP ] = $this -> zip ;
if ( $this -> town && ! empty ( $conf -> global -> LDAP_FIELD_TOWN )) $info [ $conf -> global -> LDAP_FIELD_TOWN ] = $this -> town ;
if ( $this -> office_phone && ! empty ( $conf -> global -> LDAP_FIELD_PHONE )) $info [ $conf -> global -> LDAP_FIELD_PHONE ] = $this -> office_phone ;
if ( $this -> user_mobile && ! empty ( $conf -> global -> LDAP_FIELD_MOBILE )) $info [ $conf -> global -> LDAP_FIELD_MOBILE ] = $this -> user_mobile ;
if ( $this -> office_fax && ! empty ( $conf -> global -> LDAP_FIELD_FAX )) $info [ $conf -> global -> LDAP_FIELD_FAX ] = $this -> office_fax ;
if ( $this -> note && ! empty ( $conf -> global -> LDAP_FIELD_DESCRIPTION )) $info [ $conf -> global -> LDAP_FIELD_DESCRIPTION ] = $this -> note ;
if ( $this -> email && ! empty ( $conf -> global -> LDAP_FIELD_MAIL )) $info [ $conf -> global -> LDAP_FIELD_MAIL ] = $this -> email ;
2013-11-07 13:17:08 +01:00
if ( $this -> skype && ! empty ( $conf -> global -> LDAP_FIELD_SKYPE )) $info [ $conf -> global -> LDAP_FIELD_SKYPE ] = $this -> skype ;
2006-11-20 03:10:47 +01:00
2006-11-20 02:13:13 +01:00
if ( $conf -> global -> LDAP_SERVER_TYPE == 'egroupware' )
{
$info [ " objectclass " ][ 4 ] = " phpgwContact " ; // compatibilite egroupware
2006-11-20 03:10:47 +01:00
2006-11-20 02:13:13 +01:00
$info [ 'uidnumber' ] = $this -> id ;
2006-11-20 03:10:47 +01:00
2006-11-20 02:13:13 +01:00
$info [ 'phpgwTz' ] = 0 ;
$info [ 'phpgwMailType' ] = 'INTERNET' ;
$info [ 'phpgwMailHomeType' ] = 'INTERNET' ;
2006-11-20 03:10:47 +01:00
2006-11-20 02:13:13 +01:00
$info [ " phpgwContactTypeId " ] = 'n' ;
$info [ " phpgwContactCatId " ] = 0 ;
$info [ " phpgwContactAccess " ] = " public " ;
2006-11-20 03:10:47 +01:00
2010-08-24 21:51:56 +02:00
if ( dol_strlen ( $this -> egroupware_id ) == 0 )
2006-11-20 02:13:13 +01:00
{
$this -> egroupware_id = 1 ;
}
2006-11-20 03:10:47 +01:00
2006-11-20 02:13:13 +01:00
$info [ " phpgwContactOwner " ] = $this -> egroupware_id ;
2006-11-20 03:10:47 +01:00
2006-11-20 02:13:13 +01:00
if ( $this -> email ) $info [ " rfc822Mailbox " ] = $this -> email ;
if ( $this -> phone_mobile ) $info [ " phpgwCellTelephoneNumber " ] = $this -> phone_mobile ;
}
return $info ;
}
2008-07-19 18:53:56 +02:00
2006-11-20 03:10:47 +01:00
2006-11-19 17:41:17 +01:00
/**
2011-09-24 01:46:14 +02:00
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
2006-11-19 17:41:17 +01:00
*/
function initAsSpecimen ()
{
global $user , $langs ;
2013-04-11 09:15:41 +02:00
$now = dol_now ();
2013-06-16 21:31:21 +02:00
2007-12-19 11:24:18 +01:00
// Initialise parametres
2006-11-19 17:41:17 +01:00
$this -> id = 0 ;
$this -> ref = 'SPECIMEN' ;
$this -> specimen = 1 ;
2012-05-11 12:10:47 +02:00
$this -> lastname = 'DOLIBARR' ;
$this -> firstname = 'SPECIMEN' ;
2006-11-19 17:41:17 +01:00
$this -> note = 'This is a note' ;
$this -> email = 'email@specimen.com' ;
2013-11-07 13:17:08 +01:00
$this -> skype = 'tom.hanson' ;
2007-09-07 20:25:53 +02:00
$this -> office_phone = '0999999999' ;
2006-11-19 17:41:17 +01:00
$this -> office_fax = '0999999998' ;
$this -> user_mobile = '0999999997' ;
$this -> admin = 0 ;
$this -> login = 'dolibspec' ;
$this -> pass = 'dolibspec' ;
2013-04-01 14:49:33 +02:00
//$this->pass_indatabase='dolibspec'; Set after a fetch
//$this->pass_indatabase_crypted='e80ca5a88c892b0aaaf7e154853bccab'; Set after a fetch
2013-04-11 09:15:41 +02:00
$this -> datec = $now ;
$this -> datem = $now ;
2006-11-19 17:41:17 +01:00
2013-04-11 09:15:41 +02:00
$this -> datelastlogin = $now ;
$this -> datepreviouslogin = $now ;
2006-11-19 17:41:17 +01:00
$this -> statut = 1 ;
2013-04-01 14:49:33 +02:00
//$this->societe_id = 1; For external users
//$this->contact_id = 1; For external users
$this -> entity = 1 ;
2006-11-20 03:10:47 +01:00
}
2006-12-03 20:29:41 +01:00
2008-07-19 18:53:56 +02:00
/**
2011-09-20 19:34:15 +02:00
* Load info of user object
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param int $id Id of user to load
* @ return void
2008-07-19 18:53:56 +02:00
*/
function info ( $id )
{
2010-01-13 19:51:19 +01:00
$sql = " SELECT u.rowid, u.login as ref, u.datec, " ;
$sql .= " u.tms as date_modification, u.entity " ;
2008-07-19 18:53:56 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
$sql .= " WHERE u.rowid = " . $id ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
if ( $this -> db -> num_rows ( $result ))
{
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
$this -> ref = ( ! $obj -> ref ) ? $obj -> rowid : $obj -> ref ;
2010-01-13 19:51:19 +01:00
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
$this -> date_modification = $this -> db -> jdate ( $obj -> date_modification );
2009-04-18 16:38:08 +02:00
$this -> entity = $obj -> entity ;
2008-07-19 18:53:56 +02:00
}
$this -> db -> free ( $result );
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
}
}
2007-09-01 00:06:14 +02:00
2010-02-21 22:21:12 +01:00
/**
2011-05-01 12:48:43 +02:00
* Return number of mass Emailing received by this contacts with its email
2011-09-04 20:41:19 +02:00
*
2011-05-01 12:48:43 +02:00
* @ return int Number of EMailings
2010-02-21 22:21:12 +01:00
*/
function getNbOfEMailings ()
{
$sql = " SELECT count(mc.email) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing_cibles as mc " ;
2011-02-12 08:02:56 +01:00
$sql .= " WHERE mc.email = ' " . $this -> db -> escape ( $this -> email ) . " ' " ;
2010-02-21 22:21:12 +01:00
$sql .= " AND mc.statut=1 " ; // -1 erreur, 0 non envoye, 1 envoye avec succes
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$nb = $obj -> nb ;
$this -> db -> free ( $resql );
return $nb ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2010-06-17 18:53:26 +02:00
2010-06-07 07:11:45 +02:00
/**
2011-09-20 19:34:15 +02:00
* Return number of existing users
2011-09-04 20:41:19 +02:00
*
2011-09-20 19:34:15 +02:00
* @ param string $limitTo Limit to 'active' or 'superadmin' users
2012-08-14 19:52:51 +02:00
* @ param bool $all Return for all entities
2011-09-20 19:34:15 +02:00
* @ return int Number of users
2010-06-07 07:11:45 +02:00
*/
2012-08-14 19:52:51 +02:00
function getNbOfUsers ( $limitTo = 'active' , $all = false )
2010-06-07 07:11:45 +02:00
{
global $conf ;
2010-06-17 18:53:26 +02:00
2010-06-07 07:11:45 +02:00
$sql = " SELECT count(rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user " ;
2011-05-16 21:56:43 +02:00
if ( $limitTo == 'superadmin' )
{
$sql .= " WHERE entity = 0 " ;
}
else
{
2012-08-14 19:52:51 +02:00
if ( $all ) $sql .= " WHERE entity > 0 " ; // all users except superadmins
2011-09-24 01:46:14 +02:00
else $sql .= " WHERE entity = " . $conf -> entity ;
if ( $limitTo == 'active' ) $sql .= " AND statut = 1 " ;
2011-05-16 21:56:43 +02:00
}
2010-06-17 18:53:26 +02:00
2010-06-07 07:11:45 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$nb = $obj -> nb ;
$this -> db -> free ( $resql );
return $nb ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2010-02-21 22:21:12 +01:00
2013-01-30 16:50:28 +01:00
/**
2013-04-04 13:58:33 +02:00
* Update user using data from the LDAP
*
* @ param ldapuser & $ldapuser Ladp User
*
* @ return int < 0 if KO , > 0 if OK
2013-01-30 16:50:28 +01:00
*/
2013-02-11 20:45:36 +01:00
function update_ldap2dolibarr ( & $ldapuser )
{
2013-04-04 13:58:33 +02:00
// TODO: Voir pourquoi le update met à jour avec toutes les valeurs vide (global $user écrase ?)
2013-01-30 16:50:28 +01:00
global $user , $conf ;
$this -> firstname = $ldapuser -> { $conf -> global -> LDAP_FIELD_FIRSTNAME };
$this -> lastname = $ldapuser -> { $conf -> global -> LDAP_FIELD_NAME };
$this -> login = $ldapuser -> { $conf -> global -> LDAP_FIELD_LOGIN };
$this -> pass = $ldapuser -> { $conf -> global -> LDAP_FIELD_PASSWORD };
$this -> pass_indatabase_crypted = $ldapuser -> { $conf -> global -> LDAP_FIELD_PASSWORD_CRYPTED };
$this -> office_phone = $ldapuser -> { $conf -> global -> LDAP_FIELD_PHONE };
$this -> user_mobile = $ldapuser -> { $conf -> global -> LDAP_FIELD_MOBILE };
$this -> office_fax = $ldapuser -> { $conf -> global -> LDAP_FIELD_FAX };
$this -> email = $ldapuser -> { $conf -> global -> LDAP_FIELD_MAIL };
2013-11-07 13:17:08 +01:00
$this -> skype = $ldapuser -> { $conf -> global -> LDAP_FIELD_SKYPE };
2013-01-30 16:50:28 +01:00
$this -> ldap_sid = $ldapuser -> { $conf -> global -> LDAP_FIELD_SID };
$this -> job = $ldapuser -> { $conf -> global -> LDAP_FIELD_TITLE };
$this -> note = $ldapuser -> { $conf -> global -> LDAP_FIELD_DESCRIPTION };
2013-02-11 20:45:36 +01:00
2013-01-30 16:50:28 +01:00
$result = $this -> update ( $user );
2013-02-11 20:45:36 +01:00
2013-01-30 16:50:28 +01:00
dol_syslog ( get_class ( $this ) . " ::update_ldap2dolibarr result= " . $result , LOG_DEBUG );
2013-02-11 20:45:36 +01:00
2013-01-30 16:50:28 +01:00
return $result ;
}
2013-02-11 20:45:36 +01:00
2013-03-30 14:27:13 +01:00
/**
* Return and array with all instanciated children users of current user
*
* @ return void
*/
function get_children ()
{
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " user " ;
$sql .= " WHERE fk_user = " . $this -> id ;
dol_syslog ( get_class ( $this ) . " ::get_children result= " . $result , LOG_DEBUG );
$res = $this -> db -> query ( $sql );
if ( $res )
{
$users = array ();
while ( $rec = $this -> db -> fetch_array ( $res ))
{
$user = new User ( $this -> db );
$user -> fetch ( $rec [ 'rowid' ]);
$users [] = $user ;
}
return $users ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
/**
* Load this -> parentof that is array ( id_son => id_parent , ... )
*
* @ return int < 0 if KO , > 0 if OK
*/
private function load_parentof ()
{
global $conf ;
$this -> parentof = array ();
// Load array[child]=parent
$sql = " SELECT fk_user as id_parent, rowid as id_son " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user " ;
2014-08-07 20:11:39 +02:00
$sql .= " WHERE fk_user <> 0 " ;
$sql .= " AND entity IN ( " . getEntity ( 'user' , 1 ) . " ) " ;
2013-03-30 14:27:13 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::load_parentof " , LOG_DEBUG );
2013-03-30 14:27:13 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> parentof [ $obj -> id_son ] = $obj -> id_parent ;
}
return 1 ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
/**
* Reconstruit l 'arborescence hierarchique des users sous la forme d' un tableau
* Renvoi un tableau de tableau ( 'id' , 'id_parent' , ... ) trie selon arbre et avec :
* id = id du user
* id_parent = id du user parent
* id_children = tableau des id enfant
* name = nom du user
* fullname = nom avec chemin complet du user
* fullpath = chemin complet compose des id
*
2014-08-11 10:50:08 +02:00
* @ param int $deleteafterid Removed all users including the leaf $deleteafterid ( and all its child ) in user tree .
* @ return array Array of users . this -> users and this -> parentof are set .
2013-03-30 14:27:13 +01:00
*/
2014-08-11 10:50:08 +02:00
function get_full_tree ( $deleteafterid = 0 )
2013-03-30 14:27:13 +01:00
{
2014-05-30 12:55:00 +02:00
global $conf , $user ;
2013-03-30 14:27:13 +01:00
$this -> users = array ();
// Init this->parentof that is array(id_son=>id_parent, ...)
$this -> load_parentof ();
2014-08-07 20:11:39 +02:00
2013-03-30 14:27:13 +01:00
// Init $this->users array
2014-05-30 12:55:00 +02:00
$sql = " SELECT DISTINCT u.rowid, u.firstname, u.lastname, u.fk_user, u.login, u.statut, u.entity " ; // Distinct reduce pb with old tables with duplicates
2013-03-30 14:27:13 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
2014-05-30 12:43:07 +02:00
if ( ! empty ( $conf -> multicompany -> enabled ) && $conf -> entity == 1 && ( ! empty ( $conf -> multicompany -> transverse_mode ) || ( ! empty ( $user -> admin ) && empty ( $user -> entity ))))
{
$sql .= " WHERE u.entity IS NOT NULL " ;
}
else
{
$sql .= " WHERE u.entity IN ( " . getEntity ( 'user' , 1 ) . " ) " ;
}
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::get_full_tree get user list " , LOG_DEBUG );
2013-03-30 14:27:13 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$i = 0 ;
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> users [ $obj -> rowid ][ 'rowid' ] = $obj -> rowid ;
$this -> users [ $obj -> rowid ][ 'id' ] = $obj -> rowid ;
$this -> users [ $obj -> rowid ][ 'fk_user' ] = $obj -> fk_user ;
$this -> users [ $obj -> rowid ][ 'firstname' ] = $obj -> firstname ;
$this -> users [ $obj -> rowid ][ 'lastname' ] = $obj -> lastname ;
2013-02-20 21:16:31 +01:00
$this -> users [ $obj -> rowid ][ 'login' ] = $obj -> login ;
$this -> users [ $obj -> rowid ][ 'statut' ] = $obj -> statut ;
2014-05-30 12:55:00 +02:00
$this -> users [ $obj -> rowid ][ 'entity' ] = $obj -> entity ;
2013-03-30 14:27:13 +01:00
$i ++ ;
}
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
// We add the fullpath property to each elements of first level (no parent exists)
dol_syslog ( get_class ( $this ) . " ::get_full_tree call to build_path_from_id_user " , LOG_DEBUG );
foreach ( $this -> users as $key => $val )
{
$this -> build_path_from_id_user ( $key , 0 ); // Process a branch from the root user key (this user has no parent)
}
2014-08-11 10:50:08 +02:00
// Exclude leaf including $deleteafterid from tree
if ( $deleteafterid )
2013-03-30 14:27:13 +01:00
{
2014-08-11 10:50:08 +02:00
//print "Look to discard user ".$deleteafterid."\n";
$keyfilter1 = '^' . $deleteafterid . '$' ;
$keyfilter2 = '_' . $deleteafterid . '$' ;
$keyfilter3 = '^' . $deleteafterid . '_' ;
$keyfilter4 = '_' . $deleteafterid . '_' ;
2013-03-30 14:27:13 +01:00
foreach ( $this -> users as $key => $val )
{
if ( preg_match ( '/' . $keyfilter1 . '/' , $val [ 'fullpath' ]) || preg_match ( '/' . $keyfilter2 . '/' , $val [ 'fullpath' ])
|| preg_match ( '/' . $keyfilter3 . '/' , $val [ 'fullpath' ]) || preg_match ( '/' . $keyfilter4 . '/' , $val [ 'fullpath' ]))
{
unset ( $this -> users [ $key ]);
}
}
}
dol_syslog ( get_class ( $this ) . " ::get_full_tree dol_sort_array " , LOG_DEBUG );
$this -> users = dol_sort_array ( $this -> users , 'fullname' , 'asc' , true , false );
2014-07-05 08:59:41 +02:00
//var_dump($this->users);
2013-03-30 14:27:13 +01:00
return $this -> users ;
}
/**
* For user id_user and its childs available in this -> users , define property fullpath and fullname
*
* @ param int $id_user id_user entry to update
* @ param int $protection Deep counter to avoid infinite loop
* @ return void
*/
function build_path_from_id_user ( $id_user , $protection = 1000 )
{
dol_syslog ( get_class ( $this ) . " ::build_path_from_id_user id_user= " . $id_user . " protection= " . $protection , LOG_DEBUG );
if ( ! empty ( $this -> users [ $id_user ][ 'fullpath' ]))
{
// Already defined
dol_syslog ( get_class ( $this ) . " ::build_path_from_id_user fullpath and fullname already defined " , LOG_WARNING );
return ;
}
// Define fullpath and fullname
$this -> users [ $id_user ][ 'fullpath' ] = '_' . $id_user ;
2014-08-07 20:11:39 +02:00
$this -> users [ $id_user ][ 'fullname' ] = $this -> users [ $id_user ][ 'lastname' ];
2013-03-30 14:27:13 +01:00
$i = 0 ; $cursor_user = $id_user ;
2014-08-11 10:50:08 +02:00
while (( empty ( $protection ) || $i < $protection ) && ! empty ( $this -> parentof [ $cursor_user ]))
2013-03-30 14:27:13 +01:00
{
$this -> users [ $id_user ][ 'fullpath' ] = '_' . $this -> parentof [ $cursor_user ] . $this -> users [ $id_user ][ 'fullpath' ];
2014-08-07 20:11:39 +02:00
$this -> users [ $id_user ][ 'fullname' ] = $this -> users [ $this -> parentof [ $cursor_user ]][ 'lastname' ] . ' >> ' . $this -> users [ $id_user ][ 'fullname' ];
2013-03-30 14:27:13 +01:00
$i ++ ; $cursor_user = $this -> parentof [ $cursor_user ];
}
// We count number of _ to have level
$this -> users [ $id_user ][ 'level' ] = dol_strlen ( preg_replace ( '/[^_]/i' , '' , $this -> users [ $id_user ][ 'fullpath' ]));
return ;
}
2002-04-30 12:51:35 +02:00
}
2002-12-18 19:03:28 +01:00