2005-01-27 16:55:40 +01:00
< ? php
2010-01-16 20:56:05 +01:00
/* Copyright ( c ) 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2013-09-09 03:43:59 +02:00
* Copyright ( c ) 2005 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( c ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2013-08-21 18:47:51 +02:00
* Copyright ( C ) 2012 Florian Henry < florian . henry @ open - concept . pro >
2014-06-17 10:15:53 +02:00
* Copyright ( C ) 2014 Juanjo Menent < jmenent @ 2 byte . es >
2014-09-22 16:31:58 +02:00
* Copyright ( C ) 2014 Alexis Algoud < alexis @ atm - consulting . fr >
2006-11-19 17:41:17 +01:00
*
2005-01-27 16:55:40 +01: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
2005-01-27 16:55:40 +01: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 />.
2005-01-27 16:55:40 +01:00
*/
2006-11-19 17:41:17 +01:00
/**
2010-07-21 14:35:56 +02:00
* \file htdocs / user / class / usergroup . class . php
2012-01-11 01:06:56 +01:00
* \brief File of class to manage user groups
2009-05-04 20:01:47 +02:00
*/
2005-01-27 16:55:40 +01:00
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2012-05-29 23:00:12 +02:00
if ( ! empty ( $conf -> ldap -> enabled )) require_once ( DOL_DOCUMENT_ROOT . " /core/class/ldap.class.php " );
2006-11-20 03:10:47 +01:00
2006-11-19 17:41:17 +01:00
/**
2013-09-09 03:43:59 +02:00
* Class to manage user groups
2009-05-04 20:01:47 +02:00
*/
2009-04-27 22:37:50 +02:00
class UserGroup extends CommonObject
2005-01-27 16:55:40 +01:00
{
2011-09-20 19:19:46 +02:00
public $element = 'usergroup' ;
public $table_element = 'usergroup' ;
protected $ismultientitymanaged = 1 ; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
2009-05-04 20:01:47 +02:00
var $id ; // Group id
2009-12-19 15:12:55 +01:00
var $entity ; // Entity of group
2009-05-04 20:01:47 +02:00
var $nom ; // Name of group
2014-10-04 17:20:17 +02:00
var $name ; // Name of group // deprecated
2011-06-07 17:45:01 +02:00
var $globalgroup ; // Global group
2009-05-04 20:01:47 +02:00
var $note ; // Note on group
var $datec ; // Creation date of group
var $datem ; // Modification date of group
2009-12-21 12:45:45 +01:00
var $members = array (); // Array of users
2009-05-04 20:01:47 +02:00
2012-01-11 01:06:56 +01:00
private $_tab_loaded = array (); // Array of cache of already loaded permissions
2009-08-12 01:42:21 +02:00
var $oldcopy ; // To contains a clone of this when we need to save old properties of object
2009-05-04 20:01:47 +02:00
/**
2011-07-08 20:49:16 +02:00
* Constructor de la classe
2011-09-04 20:41:19 +02:00
*
2012-05-11 12:47:15 +02:00
* @ param DoliDb $db Database handler
2009-05-04 20:01:47 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2009-05-04 20:01:47 +02:00
{
2012-05-11 12:47:15 +02:00
$this -> db = $db ;
2006-11-19 17:41:17 +01:00
2009-05-04 20:01:47 +02:00
return 0 ;
}
/**
2011-07-04 13:41:02 +02:00
* Charge un objet group avec toutes ces caracteristiques ( excpet -> members array )
2011-09-04 20:41:19 +02:00
*
2013-02-25 16:46:31 +01:00
* @ param int $id id du groupe a charger
2014-10-04 17:20:17 +02:00
* @ param string $groupname name du groupe a charger
2013-02-25 16:46:31 +01:00
* @ return int < 0 if KO , > 0 if OK
2009-05-04 20:01:47 +02:00
*/
2013-02-25 16:46:31 +01:00
function fetch ( $id = '' , $groupname = '' )
2009-05-04 20:01:47 +02:00
{
global $conf ;
2005-02-26 19:34:14 +01:00
2012-01-30 10:22:50 +01:00
$sql = " SELECT g.rowid, g.entity, g.nom as name, g.note, g.datec, g.tms as datem " ;
2009-05-04 20:01:47 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " usergroup as g " ;
2013-02-25 16:46:31 +01:00
if ( $groupname )
{
$sql .= " WHERE g.nom = ' " . $this -> db -> escape ( $groupname ) . " ' " ;
}
else
{
$sql .= " WHERE g.rowid = " . $id ;
}
2006-11-19 17:41:17 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2009-05-04 20:01:47 +02:00
$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 -> rowid ;
$this -> entity = $obj -> entity ;
2012-01-30 10:22:50 +01:00
$this -> name = $obj -> name ;
2014-10-04 17:20:17 +02:00
$this -> nom = $obj -> name ; // Deprecated
2009-05-04 20:01:47 +02:00
$this -> note = $obj -> note ;
$this -> datec = $obj -> datec ;
$this -> datem = $obj -> datem ;
2010-01-16 20:56:05 +01:00
$this -> members = $this -> listUsersForGroup ();
2014-09-22 16:31:58 +02:00
// Retreive all extrafield for group
// fetch optionals attributes and labels
dol_include_once ( '/core/class/extrafields.class.php' );
$extrafields = new ExtraFields ( $this -> db );
$extralabels = $extrafields -> fetch_name_optionals_label ( $this -> table_element , true );
$this -> fetch_optionals ( $this -> id , $extralabels );
2014-10-04 17:20:17 +02:00
2009-08-12 01:42:21 +02:00
// Sav current LDAP Current DN
//$this->ldap_dn = $this->_load_ldap_dn($this->_load_ldap_info(),0);
2009-05-04 20:01:47 +02:00
}
$this -> db -> free ( $result );
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2005-02-26 19:34:14 +01:00
2005-01-27 16:55:40 +01:00
2007-05-16 14:03:31 +02:00
/**
2011-06-07 17:45:01 +02:00
* Return array of groups objects for a particular user
2011-08-24 00:25:37 +02:00
*
2011-09-04 20:41:19 +02:00
* @ param int $userid User id to search
* @ return array Array of groups objects
2009-05-04 20:01:47 +02:00
*/
2011-06-07 17:45:01 +02:00
function listGroupsForUser ( $userid )
2008-08-28 20:02:57 +02:00
{
2011-08-21 02:20:43 +02:00
global $conf , $user ;
2011-07-04 13:41:02 +02:00
2009-05-04 20:01:47 +02:00
$ret = array ();
2011-06-07 17:45:01 +02:00
$sql = " SELECT g.rowid, ug.entity as usergroup_entity " ;
2009-05-04 20:01:47 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " usergroup as g, " ;
$sql .= " " . MAIN_DB_PREFIX . " usergroup_user as ug " ;
$sql .= " WHERE ug.fk_usergroup = g.rowid " ;
2011-06-07 17:45:01 +02:00
$sql .= " AND ug.fk_user = " . $userid ;
2011-08-21 12:01:36 +02:00
if ( ! empty ( $conf -> multicompany -> enabled ) && $conf -> entity == 1 && $user -> admin && ! $user -> entity )
2011-08-20 00:15:22 +02:00
{
$sql .= " AND g.entity IS NOT NULL " ;
}
else
{
$sql .= " AND g.entity IN (0, " . $conf -> entity . " ) " ;
}
2009-05-04 20:01:47 +02:00
$sql .= " ORDER BY g.nom " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::listGroupsForUser " , LOG_DEBUG );
2009-05-04 20:01:47 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
while ( $obj = $this -> db -> fetch_object ( $result ))
{
2012-07-07 15:11:58 +02:00
if ( ! array_key_exists ( $obj -> rowid , $ret ))
{
$newgroup = new UserGroup ( $this -> db );
$newgroup -> fetch ( $obj -> rowid );
$ret [ $obj -> rowid ] = $newgroup ;
}
$ret [ $obj -> rowid ] -> usergroup_entity [] = $obj -> usergroup_entity ;
2009-05-04 20:01:47 +02:00
}
2011-07-04 13:41:02 +02:00
2009-05-04 20:01:47 +02:00
$this -> db -> free ( $result );
2011-07-04 13:41:02 +02:00
2011-06-07 17:45:01 +02:00
return $ret ;
2009-05-04 20:01:47 +02:00
}
else
{
$this -> error = $this -> db -> lasterror ();
2008-08-28 20:02:57 +02:00
return - 1 ;
2009-05-04 20:01:47 +02:00
}
2008-08-28 20:02:57 +02:00
}
2009-05-04 20:01:47 +02:00
2010-01-16 20:56:05 +01:00
/**
2014-09-05 13:48:55 +02:00
* Return array of User objects for group this -> id ( or all if this -> id not defined )
2011-08-24 00:25:37 +02:00
*
2013-04-14 01:04:11 +02:00
* @ param string $excludefilter Filter to exclude
2014-03-12 12:57:26 +01:00
* @ param int $mode 0 = Return array of user instance , 1 = Return array of users id only
2015-01-17 15:11:12 +01:00
* @ return mixed Array of users or - 1 on error
2010-01-16 20:56:05 +01:00
*/
2014-03-12 12:57:26 +01:00
function listUsersForGroup ( $excludefilter = '' , $mode = 0 )
2010-01-16 20:56:05 +01:00
{
2011-08-21 02:20:43 +02:00
global $conf , $user ;
2011-07-04 13:41:02 +02:00
2010-01-16 20:56:05 +01:00
$ret = array ();
2013-04-14 01:04:11 +02:00
$sql = " SELECT u.rowid " ;
if ( ! empty ( $this -> id )) $sql .= " , ug.entity as usergroup_entity " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
if ( ! empty ( $this -> id )) $sql .= " , " . MAIN_DB_PREFIX . " usergroup_user as ug " ;
$sql .= " WHERE 1 = 1 " ;
if ( ! empty ( $this -> id )) $sql .= " AND ug.fk_user = u.rowid " ;
if ( ! empty ( $this -> id )) $sql .= " AND ug.fk_usergroup = " . $this -> id ;
2012-07-07 15:11:58 +02:00
if ( ! empty ( $conf -> multicompany -> enabled ) && $conf -> entity == 1 && $user -> admin && ! $user -> entity )
2011-08-20 00:15:22 +02:00
{
$sql .= " AND u.entity IS NOT NULL " ;
}
else
{
$sql .= " AND u.entity IN (0, " . $conf -> entity . " ) " ;
}
2013-04-14 01:04:11 +02:00
if ( ! empty ( $excludefilter )) $sql .= ' AND (' . $excludefilter . ')' ;
2012-07-07 15:11:58 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::listUsersForGroup " , LOG_DEBUG );
2013-04-14 01:04:11 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2010-01-16 20:56:05 +01:00
{
2013-04-14 01:04:11 +02:00
while ( $obj = $this -> db -> fetch_object ( $resql ))
2010-01-16 20:56:05 +01:00
{
2012-07-07 15:11:58 +02:00
if ( ! array_key_exists ( $obj -> rowid , $ret ))
{
2014-03-12 12:57:26 +01:00
if ( $mode != 1 )
{
$newuser = new User ( $this -> db );
$newuser -> fetch ( $obj -> rowid );
$ret [ $obj -> rowid ] = $newuser ;
}
else $ret [ $obj -> rowid ] = $obj -> rowid ;
2012-07-07 15:11:58 +02:00
}
2014-03-12 12:57:26 +01:00
if ( $mode != 1 && ! empty ( $obj -> usergroup_entity ))
2013-04-26 19:13:39 +02:00
{
$ret [ $obj -> rowid ] -> usergroup_entity [] = $obj -> usergroup_entity ;
}
2010-01-16 20:56:05 +01:00
}
2011-07-04 13:41:02 +02:00
2013-04-14 01:04:11 +02:00
$this -> db -> free ( $resql );
2011-07-04 13:41:02 +02:00
2011-06-07 17:45:01 +02:00
return $ret ;
2010-01-16 20:56:05 +01:00
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2009-05-04 20:01:47 +02:00
/**
2011-09-04 20:41:19 +02:00
* Ajoute un droit a l ' utilisateur
*
* @ param int $rid id du droit a ajouter
* @ param string $allmodule Ajouter tous les droits du module allmodule
* @ param string $allperms Ajouter tous les droits du module allmodule , perms allperms
* @ return int > 0 if OK , < 0 if KO
2009-05-04 20:01:47 +02:00
*/
function addrights ( $rid , $allmodule = '' , $allperms = '' )
2005-02-26 19:34:14 +01:00
{
2009-05-04 20:01:47 +02:00
global $conf ;
2012-01-11 01:06:56 +01:00
dol_syslog ( get_class ( $this ) . " ::addrights $rid , $allmodule , $allperms " );
2009-05-04 20:01:47 +02:00
$err = 0 ;
$whereforadd = '' ;
$this -> db -> begin ();
2012-10-20 09:40:50 +02:00
if ( ! empty ( $rid ))
2009-05-04 20:01:47 +02:00
{
2009-06-06 16:59:55 +02:00
// Si on a demande ajout d'un droit en particulier, on recupere
// les caracteristiques (module, perms et subperms) de ce droit.
2009-05-04 20:01:47 +02:00
$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 ) . " ' " ;
2009-05-04 20:01:47 +02:00
$sql .= " AND entity = " . $conf -> entity ;
$result = $this -> db -> query ( $sql );
if ( $result ) {
$obj = $this -> db -> fetch_object ( $result );
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
}
else {
$err ++ ;
dol_print_error ( $this -> db );
}
2009-06-06 16:59:55 +02:00
// Where pour la liste des droits a ajouter
2012-10-20 11:10:36 +02:00
$whereforadd = " id= " . $this -> db -> escape ( $rid );
2009-05-04 20:01:47 +02:00
// Ajout des droits induits
2012-01-11 01:06:56 +01:00
if ( $subperms ) $whereforadd .= " OR (module=' $module ' AND perms=' $perms ' AND (subperms='lire' OR subperms='read')) " ;
else if ( $perms ) $whereforadd .= " OR (module=' $module ' AND (perms='lire' OR perms='read') AND subperms IS NULL) " ;
2009-05-04 20:01:47 +02:00
2009-06-06 16:59:55 +02:00
// Pour compatibilite, si lowid = 0, on est en mode ajout de tout
// TODO A virer quand sera gere par l'appelant
2014-06-17 10:15:53 +02:00
//if (substr($rid,-1,1) == 0) $whereforadd="module='$module'";
2009-05-04 20:01:47 +02:00
}
else {
2009-06-06 16:59:55 +02:00
// 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 ) . " ' " ;
2009-05-04 20:01:47 +02:00
}
// Ajout des droits de la liste whereforadd
2012-10-20 09:40:50 +02:00
if ( ! empty ( $whereforadd ))
2009-05-04 20:01:47 +02:00
{
//print "$module-$perms-$subperms";
$sql = " SELECT id " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " WHERE $whereforadd " ;
$sql .= " AND entity = " . $conf -> entity ;
$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 . " usergroup_rights WHERE fk_usergroup = $this->id AND fk_id= " . $nid ;
2009-05-04 20:01:47 +02:00
if ( ! $this -> db -> query ( $sql )) $err ++ ;
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " usergroup_rights (fk_usergroup, fk_id) VALUES ( $this->id , $nid ) " ;
if ( ! $this -> db -> query ( $sql )) $err ++ ;
$i ++ ;
}
}
else
{
$err ++ ;
dol_print_error ( $this -> db );
}
}
if ( $err ) {
$this -> db -> rollback ();
return - $err ;
}
else {
$this -> db -> commit ();
return 1 ;
}
}
/**
2011-09-04 20:41:19 +02:00
* Retire un droit a l ' utilisateur
*
* @ param int $rid id du droit a retirer
* @ param string $allmodule Retirer tous les droits du module allmodule
* @ param string $allperms Retirer tous les droits du module allmodule , perms allperms
* @ return int > 0 if OK , < 0 if OK
2009-05-04 20:01:47 +02:00
*/
function delrights ( $rid , $allmodule = '' , $allperms = '' )
{
global $conf ;
$err = 0 ;
$wherefordel = '' ;
$this -> db -> begin ();
2012-10-20 09:40:50 +02:00
if ( ! empty ( $rid ))
2009-05-04 20:01:47 +02:00
{
2009-06-06 16:59:55 +02:00
// Si on a demande supression d'un droit en particulier, on recupere
// les caracteristiques module, perms et subperms de ce droit.
2009-05-04 20:01:47 +02:00
$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 ) . " ' " ;
2009-05-04 20:01:47 +02:00
$sql .= " AND entity = " . $conf -> entity ;
$result = $this -> db -> query ( $sql );
if ( $result ) {
$obj = $this -> db -> fetch_object ( $result );
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
}
else {
$err ++ ;
dol_print_error ( $this -> db );
}
2009-06-06 16:59:55 +02:00
// Where pour la liste des droits a supprimer
2012-10-20 11:10:36 +02:00
$wherefordel = " id= " . $this -> db -> escape ( $rid );
2009-05-04 20:01:47 +02:00
// Suppression des droits induits
2010-10-28 21:31:11 +02:00
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 ') " ;
2009-05-04 20:01:47 +02:00
2009-06-06 16:59:55 +02:00
// Pour compatibilite, si lowid = 0, on est en mode suppression de tout
2009-12-21 01:32:17 +01:00
// TODO A virer quand sera gere par l'appelant
2014-06-11 17:51:50 +02:00
//if (substr($rid,-1,1) == 0) $wherefordel="module='$module'";
2009-05-04 20:01:47 +02:00
}
else {
2009-06-06 16:59:55 +02:00
// 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 ) . " ' " ;
2009-05-04 20:01:47 +02:00
}
2005-02-26 19:34:14 +01:00
2009-05-04 20:01:47 +02:00
// Suppression des droits de la liste wherefordel
2012-10-20 09:40:50 +02:00
if ( ! empty ( $wherefordel ))
2005-02-26 19:34:14 +01:00
{
2009-05-04 20:01:47 +02:00
//print "$module-$perms-$subperms";
$sql = " SELECT id " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " WHERE $wherefordel " ;
$sql .= " AND entity = " . $conf -> entity ;
$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 . " usergroup_rights " ;
$sql .= " WHERE fk_usergroup = $this->id AND fk_id= " . $nid ;
2009-05-04 20:01:47 +02:00
if ( ! $this -> db -> query ( $sql )) $err ++ ;
2005-02-26 19:34:14 +01:00
2009-05-04 20:01:47 +02:00
$i ++ ;
}
}
else
{
$err ++ ;
dol_print_error ( $this -> db );
}
}
2005-02-26 19:34:14 +01:00
2009-05-04 20:01:47 +02:00
if ( $err ) {
$this -> db -> rollback ();
return - $err ;
}
else {
$this -> db -> commit ();
return 1 ;
2005-02-26 19:34:14 +01:00
}
2009-05-04 20:01:47 +02:00
2005-02-26 19:34:14 +01:00
}
2006-11-19 17:41:17 +01:00
2009-05-04 20:01:47 +02:00
/**
2011-09-04 20:41:19 +02:00
* Charge dans l ' objet group , la liste des permissions auquels le groupe a droit
*
2012-03-09 09:38:31 +01:00
* @ param string $moduletag Name of module we want permissions ( '' means all )
* @ return int < 0 if KO , > 0 if OK
2009-05-04 20:01:47 +02:00
*/
2012-01-11 01:06:56 +01:00
function getrights ( $moduletag = '' )
2009-05-04 20:01:47 +02:00
{
global $conf ;
2012-01-11 01:06:56 +01:00
if ( $moduletag && isset ( $this -> _tab_loaded [ $moduletag ]) && $this -> _tab_loaded [ $moduletag ])
{
// Le fichier de ce module est deja charge
return ;
}
2012-07-10 13:20:53 +02:00
if ( ! empty ( $this -> all_permissions_are_loaded ))
2009-05-04 20:01:47 +02:00
{
2009-06-06 16:59:55 +02:00
// Si les permissions ont deja ete chargees, on quitte
2009-05-04 20:01:47 +02:00
return ;
}
/*
2009-06-06 16:59:55 +02:00
* Recuperation des droits
2009-05-04 20:01:47 +02:00
*/
$sql = " SELECT r.module, r.perms, r.subperms " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " usergroup_rights as u, " . MAIN_DB_PREFIX . " rights_def as r " ;
$sql .= " WHERE r.id = u.fk_id " ;
$sql .= " AND r.entity = " . $conf -> entity ;
$sql .= " AND u.fk_usergroup = " . $this -> id ;
$sql .= " AND r.perms IS NOT NULL " ;
2012-01-11 01:06:56 +01:00
if ( $moduletag ) $sql .= " AND r.module = ' " . $this -> db -> escape ( $moduletag ) . " ' " ;
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::getrights' , LOG_DEBUG );
2010-08-09 18:07:24 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2009-05-04 20:01:47 +02:00
{
2010-08-09 18:07:24 +02:00
$num = $this -> db -> num_rows ( $resql );
2009-12-21 01:32:17 +01:00
$i = 0 ;
while ( $i < $num )
{
2012-01-11 01:06:56 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2009-12-21 01:32:17 +01:00
2012-01-11 01:06:56 +01:00
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
2009-12-21 01:32:17 +01:00
2012-01-11 01:06:56 +01:00
if ( $perms )
{
2013-01-19 14:32:37 +01:00
if ( ! isset ( $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 )
2009-12-21 01:32:17 +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 ;
2009-12-21 01:32:17 +01:00
}
else
{
2012-01-11 01:06:56 +01:00
$this -> rights -> $module -> $perms = 1 ;
2009-12-21 01:32:17 +01:00
}
}
2011-08-24 00:25:37 +02:00
2009-12-21 01:32:17 +01:00
$i ++ ;
}
2012-01-11 01:06:56 +01:00
$this -> db -> free ( $resql );
2009-05-04 20:01:47 +02:00
}
2012-01-11 01:06:56 +01:00
if ( $moduletag == '' )
2009-05-04 20:01:47 +02:00
{
2009-06-06 16:59:55 +02:00
// Si module etait non defini, alors on a tout charge, on peut donc considerer
2012-01-11 01:06:56 +01:00
// que les droits sont en cache (car tous charges) pour cet instance de group
2009-05-04 20:01:47 +02:00
$this -> all_permissions_are_loaded = 1 ;
}
2012-01-11 01:06:56 +01:00
else
{
// Si module defini, on le marque comme charge en cache
$this -> _tab_loaded [ $moduletag ] = 1 ;
}
2009-05-04 20:01:47 +02:00
2011-09-04 20:41:19 +02:00
return 1 ;
2009-05-04 20:01:47 +02:00
}
2005-01-27 16:55:40 +01:00
2006-11-20 03:10:47 +01:00
/**
2011-09-04 20:41:19 +02:00
* Efface un groupe de la base
*
* @ return < 0 if KO , > 0 if OK
2009-05-04 20:01:47 +02:00
*/
2006-11-20 03:10:47 +01:00
function delete ()
{
2009-05-04 20:01:47 +02:00
global $user , $conf , $langs ;
2006-11-20 03:10:47 +01:00
2011-12-05 19:41:38 +01:00
$error = 0 ;
2012-01-11 01:06:56 +01:00
2006-11-20 03:10:47 +01:00
$this -> db -> begin ();
2009-05-04 20:01:47 +02:00
2006-11-20 03:10:47 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " usergroup_rights " ;
$sql .= " WHERE fk_usergroup = " . $this -> id ;
$this -> db -> query ( $sql );
2009-05-04 20:01:47 +02:00
2006-11-20 03:10:47 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " usergroup_user " ;
$sql .= " WHERE fk_usergroup = " . $this -> id ;
$this -> db -> query ( $sql );
2009-05-04 20:01:47 +02:00
2014-09-22 16:31:58 +02:00
// Remove extrafields
if (( ! $error ) && ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))) // For avoid conflicts if trigger used
{
$result = $this -> deleteExtraFields ();
if ( $result < 0 )
{
$error ++ ;
dol_syslog ( get_class ( $this ) . " ::delete error -4 " . $this -> error , LOG_ERR );
}
}
2006-11-20 03:10:47 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " usergroup " ;
$sql .= " WHERE rowid = " . $this -> id ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'GROUP_DELETE' , $user );
2014-09-05 13:48:55 +02:00
if ( $result < 0 ) { $error ++ ; $this -> db -> rollback (); return - 1 ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2009-05-04 20:01:47 +02:00
2006-11-20 03:10:47 +01:00
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2006-11-20 03:10:47 +01:00
return - 1 ;
}
}
2005-01-27 16:55:40 +01:00
2006-11-20 02:44:27 +01:00
/**
2011-06-07 17:45:01 +02:00
* Create group into database
2011-09-04 20:41:19 +02:00
*
* @ param int $notrigger 0 = triggers enabled , 1 = triggers disabled
* @ return int < 0 if KO , >= 0 if OK
2009-05-04 20:01:47 +02:00
*/
2011-06-07 17:45:01 +02:00
function create ( $notrigger = 0 )
2006-11-20 02:44:27 +01:00
{
2014-09-22 16:31:58 +02:00
global $user , $conf , $langs , $hookmanager ;
2009-05-04 20:01:47 +02:00
2011-12-05 19:41:38 +01:00
$error = 0 ;
2010-05-05 19:42:01 +02:00
$now = dol_now ();
2011-08-24 00:25:37 +02:00
2013-09-09 03:43:59 +02:00
if ( ! isset ( $this -> entity )) $this -> entity = $conf -> entity ; // If not defined, we use default value
$entity = $this -> entity ;
if ( ! empty ( $conf -> multicompany -> enabled ) && $conf -> entity == 1 ) $entity = $this -> entity ;
2010-05-05 19:42:01 +02:00
2014-07-03 14:01:01 +02:00
$this -> db -> begin ();
2014-09-05 13:48:55 +02:00
2011-06-07 17:45:01 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " usergroup ( " ;
$sql .= " datec " ;
$sql .= " , nom " ;
$sql .= " , entity " ;
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> idate ( $now ) . " ' " ;
$sql .= " ,' " . $this -> db -> escape ( $this -> nom ) . " ' " ;
2014-06-09 12:34:10 +02:00
$sql .= " , " . $this -> db -> escape ( $entity );
2011-06-07 17:45:01 +02:00
$sql .= " ) " ;
2009-05-04 20:01:47 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2006-11-20 02:44:27 +01:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
2010-05-05 19:42:01 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " usergroup " );
2009-05-04 20:01:47 +02:00
2006-11-20 02:44:27 +01:00
if ( $this -> update ( 1 ) < 0 ) return - 2 ;
2011-07-04 13:41:02 +02:00
2014-11-15 19:04:21 +01:00
$action = 'create' ;
2014-10-04 17:20:17 +02:00
2014-09-22 16:31:58 +02:00
// Actions on extra fields (by external module or standard code)
2015-04-12 04:01:28 +02:00
// TODO le hook fait double emploi avec le trigger !!
2014-09-22 16:31:58 +02:00
$hookmanager -> initHooks ( array ( 'groupdao' ));
$parameters = array ();
$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 ++ ;
2014-10-04 17:20:17 +02:00
2014-11-15 19:04:21 +01:00
if ( ! $error && ! $notrigger )
2011-06-07 17:45:01 +02:00
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'GROUP_CREATE' , $user );
2014-09-05 13:48:55 +02:00
if ( $result < 0 ) { $error ++ ; $this -> db -> rollback (); return - 1 ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2011-06-07 17:45:01 +02:00
}
2009-05-04 20:01:47 +02:00
2014-09-22 16:31:58 +02:00
if ( $error > 0 ) { $error ++ ; $this -> db -> rollback (); return - 1 ; }
else $this -> db -> commit ();
2014-10-04 17:20:17 +02:00
2006-11-20 02:44:27 +01:00
return $this -> id ;
}
else
{
2014-07-03 14:01:01 +02:00
$this -> db -> rollback ();
2010-05-05 19:42:01 +02:00
$this -> error = $this -> db -> lasterror ();
2006-11-20 02:44:27 +01:00
return - 1 ;
}
}
2005-01-27 16:55:40 +01:00
2006-11-20 02:44:27 +01:00
/**
2011-05-28 13:50:51 +02:00
* Update group into database
2011-09-04 20:41:19 +02:00
*
* @ param int $notrigger 0 = triggers enabled , 1 = triggers disabled
* @ return int < 0 if KO , >= 0 if OK
2009-05-04 20:01:47 +02:00
*/
function update ( $notrigger = 0 )
{
2014-09-22 16:31:58 +02:00
global $user , $conf , $langs , $hookmanager ;
2005-02-08 01:18:23 +01:00
2009-08-12 01:42:21 +02:00
$error = 0 ;
2011-08-24 00:25:37 +02:00
2011-08-20 00:15:22 +02:00
$entity = $conf -> entity ;
2011-08-21 12:01:36 +02:00
if ( ! empty ( $conf -> multicompany -> enabled ) && $conf -> entity == 1 )
2011-08-20 00:15:22 +02:00
{
$entity = $this -> entity ;
}
2009-08-12 01:42:21 +02:00
2014-07-03 14:01:01 +02:00
$this -> db -> begin ();
2014-09-05 13:48:55 +02:00
2009-05-04 20:01:47 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " usergroup SET " ;
2014-10-04 17:20:17 +02:00
$sql .= " nom = ' " . $this -> db -> escape ( $this -> name ) . " ' " ;
2014-06-09 12:34:10 +02:00
$sql .= " , entity = " . $this -> db -> escape ( $entity );
2011-08-20 00:15:22 +02:00
$sql .= " , note = ' " . $this -> db -> escape ( $this -> note ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> id ;
2009-05-04 20:01:47 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2009-08-12 01:42:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2009-05-04 20:01:47 +02:00
{
2014-11-15 19:04:21 +01:00
$action = 'update' ;
2014-10-04 17:20:17 +02:00
2014-09-22 16:31:58 +02:00
// Actions on extra fields (by external module or standard code)
2015-04-12 04:01:28 +02:00
// TODO le hook fait double emploi avec le trigger !!
2014-09-22 16:31:58 +02:00
$hookmanager -> initHooks ( array ( 'groupdao' ));
$parameters = array ();
$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 ++ ;
2014-10-04 17:20:17 +02:00
2014-11-15 19:04:21 +01:00
if ( ! $error && ! $notrigger )
2009-05-04 20:01:47 +02:00
{
2014-07-03 14:01:01 +02:00
// Call trigger
$result = $this -> call_trigger ( 'GROUP_MODIFY' , $user );
2014-09-05 13:48:55 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-03 14:01:01 +02:00
// End call triggers
2009-05-04 20:01:47 +02:00
}
2010-01-16 20:56:05 +01:00
2014-09-05 13:48:55 +02:00
if ( ! $error )
2014-07-03 14:01:01 +02:00
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return - $error ;
}
2009-05-04 20:01:47 +02:00
}
else
{
2014-07-03 14:01:01 +02:00
$this -> db -> rollback ();
2009-05-04 20:01:47 +02:00
dol_print_error ( $this -> db );
2010-01-16 20:56:05 +01:00
return - 1 ;
2009-05-04 20:01:47 +02:00
}
2006-11-20 02:13:13 +01:00
}
2006-11-19 17:41:17 +01:00
2009-08-11 14:51:33 +02:00
/**
2011-09-04 20:41:19 +02:00
* Retourne chaine DN complete dans l 'annuaire LDAP pour l' objet
*
2014-06-20 13:07:15 +02:00
* @ param array $info Info array loaded by _load_ldap_info
2011-09-04 20:41:19 +02:00
* @ param int $mode 0 = Return full DN ( uid = qqq , ou = xxx , dc = aaa , dc = bbb )
* 1 = Return DN without key inside ( ou = xxx , dc = aaa , dc = bbb )
* 2 = Return key only ( uid = qqq )
* @ return string DN
2009-05-04 20:01:47 +02:00
*/
2006-11-21 23:51:05 +01:00
function _load_ldap_dn ( $info , $mode = 0 )
2006-11-19 17:41:17 +01:00
{
2006-11-21 23:51:05 +01:00
global $conf ;
$dn = '' ;
2009-08-11 22:16:58 +02:00
if ( $mode == 0 ) $dn = $conf -> global -> LDAP_KEY_GROUPS . " = " . $info [ $conf -> global -> LDAP_KEY_GROUPS ] . " , " . $conf -> global -> LDAP_GROUP_DN ;
2006-11-21 23:51:05 +01:00
if ( $mode == 1 ) $dn = $conf -> global -> LDAP_GROUP_DN ;
if ( $mode == 2 ) $dn = $conf -> global -> LDAP_KEY_GROUPS . " = " . $info [ $conf -> global -> LDAP_KEY_GROUPS ];
return $dn ;
2006-11-19 17:41:17 +01:00
}
2006-11-20 02:13:13 +01:00
2009-08-11 14:51:33 +02:00
/**
2011-09-04 20:41:19 +02:00
* Initialize the info array ( array of LDAP values ) that will be used to call LDAP functions
*
* @ return array Tableau info des attributs
2009-05-04 20:01:47 +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-21 23:51:05 +01:00
$info = array ();
2009-05-04 20:01:47 +02:00
2007-04-13 02:35:13 +02:00
// Object classes
2009-10-20 15:14:44 +02:00
$info [ " objectclass " ] = explode ( ',' , $conf -> global -> LDAP_GROUP_OBJECT_CLASS );
2009-05-04 20:01:47 +02:00
2006-11-20 03:10:47 +01:00
// Champs
2014-10-04 17:20:17 +02:00
if ( $this -> name && ! empty ( $conf -> global -> LDAP_GROUP_FIELD_FULLNAME )) $info [ $conf -> global -> LDAP_GROUP_FIELD_FULLNAME ] = $this -> name ;
//if ($this->name && ! empty($conf->global->LDAP_GROUP_FIELD_NAME)) $info[$conf->global->LDAP_GROUP_FIELD_NAME] = $this->name;
2012-09-15 11:21:22 +02:00
if ( $this -> note && ! empty ( $conf -> global -> LDAP_GROUP_FIELD_DESCRIPTION )) $info [ $conf -> global -> LDAP_GROUP_FIELD_DESCRIPTION ] = $this -> note ;
2012-09-15 10:01:35 +02:00
if ( ! empty ( $conf -> global -> LDAP_GROUP_FIELD_GROUPMEMBERS ))
2009-12-21 01:50:46 +01:00
{
2009-12-21 12:12:57 +01:00
$valueofldapfield = array ();
2012-05-11 12:10:47 +02:00
foreach ( $this -> members as $key => $val ) // This is array of users for group into dolibarr database.
2009-12-21 01:50:46 +01:00
{
$muser = new User ( $this -> db );
2012-05-11 12:10:47 +02:00
$muser -> fetch ( $val -> id );
if ( $conf -> global -> LDAP_KEY_USERS == 'cn' ) $ldapuserid = $muser -> getFullName ( $langs );
elseif ( $conf -> global -> LDAP_KEY_USERS == 'sn' ) $ldapuserid = $muser -> lastname ;
elseif ( $conf -> global -> LDAP_KEY_USERS == 'uid' ) $ldapuserid = $muser -> login ;
2009-12-21 12:12:57 +01:00
$valueofldapfield [] = $conf -> global -> LDAP_KEY_USERS . '=' . $ldapuserid . ',' . $conf -> global -> LDAP_USER_DN ;
2009-12-21 01:50:46 +01:00
}
2009-12-21 13:28:21 +01:00
$info [ $conf -> global -> LDAP_GROUP_FIELD_GROUPMEMBERS ] = ( ! empty ( $valueofldapfield ) ? $valueofldapfield : '' );
2009-12-21 01:50:46 +01:00
}
2006-11-20 02:13:13 +01:00
return $info ;
}
2006-11-19 17:41:17 +01:00
/**
2011-09-20 19:19:46 +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 ()
{
2009-12-19 15:12:55 +01:00
global $conf , $user , $langs ;
2006-11-19 17:41:17 +01:00
2009-06-06 16:59:55 +02:00
// Initialise parametres
2006-11-19 17:41:17 +01:00
$this -> id = 0 ;
$this -> ref = 'SPECIMEN' ;
$this -> specimen = 1 ;
2014-10-04 17:20:17 +02:00
$this -> name = 'DOLIBARR GROUP SPECIMEN' ;
2006-11-19 17:41:17 +01:00
$this -> note = 'This is a note' ;
2009-05-04 20:01:47 +02:00
$this -> datec = time ();
$this -> datem = time ();
2009-12-21 01:32:17 +01:00
$this -> members = array ( $user -> id ); // Members of this group is just me
2006-11-19 17:41:17 +01:00
}
2005-01-27 16:55:40 +01:00
}