2005-01-27 16:55:40 +01:00
< ? php
2024-10-26 13:02:00 +02:00
/* Copyright ( c ) 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( c ) 2005 - 2018 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( c ) 2005 - 2018 Regis Houssin < regis . houssin @ inodbox . com >
* Copyright ( C ) 2012 Florian Henry < florian . henry @ open - concept . pro >
* Copyright ( C ) 2014 Juanjo Menent < jmenent @ 2 byte . es >
* Copyright ( C ) 2014 Alexis Algoud < alexis @ atm - consulting . fr >
* Copyright ( C ) 2018 Nicolas ZABOURI < info @ inovea - conseil . com >
2019-09-11 23:45:02 +02:00
* Copyright ( C ) 2019 Abbes Bahfir < dolipar @ dolipar . org >
2024-10-26 13:02:00 +02:00
* Copyright ( C ) 2023 - 2024 Frédéric France < frederic . france @ free . fr >
2025-02-03 11:24:16 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// 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' ;
2023-02-03 09:29:02 +01:00
if ( isModEnabled ( 'ldap' )) {
2021-02-26 13:18:40 +01:00
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
{
2018-08-23 18:52:47 +02:00
/**
* @ var string ID to identify managed object
*/
2020-04-10 10:59:32 +02:00
public $element = 'usergroup' ;
2018-09-02 20:37:12 +02:00
2018-08-22 19:56:24 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2020-04-10 10:59:32 +02:00
public $table_element = 'usergroup' ;
2018-09-02 20:37:12 +02:00
2019-10-30 10:13:57 +01:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
2020-04-10 10:59:32 +02:00
public $picto = 'group' ;
2017-06-06 07:57:03 +02:00
2018-08-31 18:27:16 +02:00
/**
* @ var int Entity of group
*/
public $entity ;
2018-02-06 19:16:15 +01:00
2015-04-23 23:21:06 +02:00
/**
2018-09-20 17:26:39 +02:00
* @ var string
2024-11-06 23:57:45 +01:00
* @ deprecated Use $name
2019-04-04 18:33:12 +02:00
* @ see $name
2015-04-23 23:21:06 +02:00
*/
2018-09-20 17:26:39 +02:00
public $nom ;
/**
* @ var string name
*/
2020-04-10 10:59:32 +02:00
public $name ; // Name of group
2018-09-20 17:26:39 +02:00
2024-11-04 23:53:20 +01:00
/**
2024-11-06 23:57:45 +01:00
* @ var int < 0 , 1 > global group Does not seem to be used
2024-11-04 23:53:20 +01:00
*/
2020-04-10 10:59:32 +02:00
public $globalgroup ; // Global group
2018-09-20 17:26:39 +02:00
2024-04-22 14:50:25 +02:00
/**
2024-04-22 15:17:38 +02:00
* @ var array < int > Entity in table llx_user_group
2024-04-23 22:36:13 +02:00
* @ deprecated Seems not used .
2024-04-22 14:50:25 +02:00
*/
public $usergroup_entity ;
2019-03-22 09:38:04 +01:00
/**
2020-10-31 14:32:18 +01:00
* Date creation record ( datec )
*
2024-11-06 23:57:45 +01:00
* @ var int
2020-10-31 14:32:18 +01:00
*/
public $datec ;
2018-09-20 17:26:39 +02:00
2020-12-05 23:53:55 +01:00
/**
* @ var string Description
*/
public $note ;
2018-09-20 17:26:39 +02:00
2023-07-27 17:51:41 +02:00
/**
2024-10-26 13:02:00 +02:00
* @ var User [] Array of users
2023-07-27 17:51:41 +02:00
*/
2024-10-26 13:02:00 +02:00
public $members = array ();
2009-05-04 20:01:47 +02:00
2024-10-26 13:02:00 +02:00
/**
* @ var int Number of rights granted to the user
*/
public $nb_rights ;
2017-12-15 15:15:14 +01:00
2024-10-26 13:02:00 +02:00
/**
* @ var int Number of users in the group
*/
public $nb_users ;
2023-08-04 03:56:02 +02:00
2024-10-26 13:02:00 +02:00
/**
* @ var stdClass Permissions of the group
*/
public $rights ;
/**
* @ var array < string , int > Cache array of already loaded permissions
*/
2020-04-10 10:59:32 +02:00
private $_tab_loaded = array (); // Array of cache of already loaded permissions
2012-01-11 01:06:56 +01:00
2023-02-06 23:00:42 +01:00
/**
* @ var int all_permissions_are_loaded
*/
public $all_permissions_are_loaded ;
2020-04-13 15:38:50 +02:00
public $fields = array (
2024-03-11 22:28:39 +01:00
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'visible' => - 2 , 'notnull' => 1 , 'index' => 1 , 'position' => 1 , 'comment' => 'Id' ),
2024-03-18 19:51:57 +01:00
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'enabled' => 1 , 'visible' => 0 , 'notnull' => 1 , 'default' => '1' , 'index' => 1 , 'position' => 5 ),
2024-03-11 22:28:39 +01:00
'nom' => array ( 'type' => 'varchar(180)' , 'label' => 'Name' , 'enabled' => 1 , 'visible' => 1 , 'notnull' => 1 , 'showoncombobox' => 1 , 'index' => 1 , 'position' => 10 , 'searchall' => 1 , 'comment' => 'Group name' ),
'note' => array ( 'type' => 'html' , 'label' => 'Description' , 'enabled' => 1 , 'visible' => 1 , 'position' => 20 , 'notnull' => - 1 , 'searchall' => 1 ),
'datec' => array ( 'type' => 'datetime' , 'label' => 'DateCreation' , 'enabled' => 1 , 'visible' => - 2 , 'position' => 50 , 'notnull' => 1 ,),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' => - 2 , 'position' => 60 , 'notnull' => 1 ,),
'model_pdf' => array ( 'type' => 'varchar(255)' , 'label' => 'ModelPDF' , 'enabled' => 1 , 'visible' => 0 , 'position' => 100 ),
2020-04-13 15:38:50 +02:00
);
2020-04-13 19:03:48 +02:00
/**
2020-12-05 23:53:55 +01:00
* @ var string Field with ID of parent key if this field has a parent
2020-04-13 19:03:48 +02:00
*/
public $fk_element = 'fk_usergroup' ;
/**
2024-03-11 22:28:39 +01:00
* @ var array < string , array < string >> List of child tables . To test if we can delete object .
2020-04-13 19:03:48 +02:00
*/
2020-04-23 18:55:10 +02:00
protected $childtables = array ();
2020-04-13 19:03:48 +02:00
/**
2024-03-11 22:28:39 +01:00
* @ var string [] List of child tables . To know object to delete on cascade .
2020-04-13 19:03:48 +02:00
*/
2020-04-23 18:55:10 +02:00
protected $childtablesoncascade = array ( 'usergroup_rights' , 'usergroup_user' );
2020-04-13 19:03:48 +02:00
2009-05-04 20:01:47 +02:00
/**
2024-01-12 17:18:52 +01:00
* Class constructor
2020-10-31 14:32:18 +01:00
*
2024-01-13 15:50:02 +01:00
* @ param DoliDB $db Database handler
2020-10-31 14:32:18 +01:00
*/
public function __construct ( $db )
{
$this -> db = $db ;
2024-05-05 00:34:19 +02:00
$this -> ismultientitymanaged = 1 ;
2020-10-31 14:32:18 +01:00
$this -> nb_rights = 0 ;
}
2009-05-04 20:01:47 +02:00
/**
2024-09-02 10:21:24 +02:00
* Load a group object with all properties ( except -> members array that is array of users in group )
2011-09-04 20:41:19 +02:00
*
2017-09-15 23:21:42 +02:00
* @ param int $id Id of group to load
* @ param string $groupname Name of group to load
* @ param boolean $load_members Load all members of the group
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2009-05-04 20:01:47 +02:00
*/
2023-11-21 00:00:06 +01:00
public function fetch ( $id = 0 , $groupname = '' , $load_members = false )
2009-05-04 20:01:47 +02:00
{
2020-04-13 19:03:48 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2021-02-26 13:18:40 +01:00
if ( ! empty ( $groupname )) {
2020-04-13 19:03:48 +02:00
$result = $this -> fetchCommon ( 0 , '' , ' AND nom = \'' . $this -> db -> escape ( $groupname ) . '\'' );
2020-05-21 15:05:19 +02:00
} else {
2020-04-13 19:03:48 +02:00
$result = $this -> fetchCommon ( $id );
2013-02-25 16:46:31 +01:00
}
2006-11-19 17:41:17 +01:00
2020-10-31 14:32:18 +01:00
$this -> name = $this -> nom ; // For compatibility with field name
2020-04-19 14:19:34 +02:00
2021-02-26 13:18:40 +01:00
if ( $result ) {
if ( $load_members ) {
2024-09-02 10:21:24 +02:00
$excludefilter = '' ;
$this -> members = $this -> listUsersForGroup ( $excludefilter , 0 ); // This make a request to get list of users but may also do subrequest to fetch each users on some versions
2009-05-04 20:01:47 +02:00
}
2020-04-13 19:03:48 +02:00
2009-05-04 20:01:47 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> lasterror ();
2009-05-04 20:01:47 +02:00
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
/**
2019-02-25 20:35:59 +01:00
* Return array of groups objects for a particular user
2011-08-24 00:25:37 +02:00
*
2024-09-18 03:27:25 +02:00
* @ param int $userid User id to search
* @ param boolean $load_members Load all members of the group
* @ return array < int , UserGroup >| int <- 1 , - 1 > Array of groups objects
2009-05-04 20:01:47 +02:00
*/
2019-02-25 20:35:59 +01:00
public function listGroupsForUser ( $userid , $load_members = true )
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
2020-04-10 10:59:32 +02:00
$ret = array ();
2009-05-04 20:01:47 +02:00
2011-06-07 17:45:01 +02:00
$sql = " SELECT g.rowid, ug.entity as usergroup_entity " ;
2022-01-27 10:00:26 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " usergroup as g, " ;
$sql .= " " . $this -> db -> prefix () . " usergroup_user as ug " ;
2020-04-10 10:59:32 +02:00
$sql .= " WHERE ug.fk_usergroup = g.rowid " ;
2021-06-09 15:36:47 +02:00
$sql .= " AND ug.fk_user = " . (( int ) $userid );
2022-08-28 14:41:03 +02:00
if ( isModEnabled ( 'multicompany' ) && $conf -> entity == 1 && $user -> admin && ! $user -> entity ) {
2020-04-10 10:59:32 +02:00
$sql .= " AND g.entity IS NOT NULL " ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$sql .= " AND g.entity IN (0, " . $conf -> entity . " ) " ;
2011-08-20 00:15:22 +02:00
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY g.nom " ;
2009-05-04 20:01:47 +02:00
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 );
2021-02-26 13:18:40 +01:00
if ( $result ) {
while ( $obj = $this -> db -> fetch_object ( $result )) {
if ( ! array_key_exists ( $obj -> rowid , $ret )) {
2020-04-10 10:59:32 +02:00
$newgroup = new UserGroup ( $this -> db );
2017-09-15 12:58:13 +02:00
$newgroup -> fetch ( $obj -> rowid , '' , $load_members );
2020-04-10 10:59:32 +02:00
$ret [ $obj -> rowid ] = $newgroup ;
2012-07-07 15:11:58 +02:00
}
2024-03-31 18:28:57 +02:00
if ( ! is_array ( $ret [ $obj -> rowid ] -> usergroup_entity )) {
$ret [ $obj -> rowid ] -> usergroup_entity = array ();
}
2024-04-22 14:50:25 +02:00
// $ret[$obj->rowid] is instance of UserGroup
2024-04-22 15:17:38 +02:00
$ret [ $obj -> rowid ] -> usergroup_entity [] = ( int ) $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 ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$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
*
2024-09-18 03:27:25 +02:00
* @ param string $excludefilter Filter to exclude . Do not use here a string coming from user input .
* @ param int < 0 , 1 > $mode 0 = Return array of user instance , 1 = Return array of users id only
* @ return array < int , User >| array < int , int >| int <- 1 , - 1 > Array of users or - 1 on error
2010-01-16 20:56:05 +01:00
*/
2019-02-25 20:35:59 +01:00
public 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
2020-04-10 10:59:32 +02:00
$ret = array ();
2010-01-16 20:56:05 +01:00
2023-06-04 03:41:23 +02:00
$sql = " SELECT u.rowid, u.login, u.lastname, u.firstname, u.photo, u.fk_soc, u.entity, u.employee, u.email, u.statut as status " ;
2021-02-26 13:18:40 +01:00
if ( ! empty ( $this -> id )) {
$sql .= " , ug.entity as usergroup_entity " ;
}
2022-01-27 10:00:26 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " user as u " ;
2021-02-26 13:18:40 +01:00
if ( ! empty ( $this -> id )) {
2022-01-27 10:00:26 +01:00
$sql .= " , " . $this -> db -> prefix () . " usergroup_user as ug " ;
2021-02-26 13:18:40 +01:00
}
2020-04-10 10:59:32 +02:00
$sql .= " WHERE 1 = 1 " ;
2021-02-26 13:18:40 +01:00
if ( ! empty ( $this -> id )) {
$sql .= " AND ug.fk_user = u.rowid " ;
}
if ( ! empty ( $this -> id )) {
2021-08-23 19:33:24 +02:00
$sql .= " AND ug.fk_usergroup = " . (( int ) $this -> id );
2021-02-26 13:18:40 +01:00
}
2022-08-28 14:41:03 +02:00
if ( isModEnabled ( 'multicompany' ) && $conf -> entity == 1 && $user -> admin && ! $user -> entity ) {
2020-04-10 10:59:32 +02:00
$sql .= " AND u.entity IS NOT NULL " ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$sql .= " AND u.entity IN (0, " . $conf -> entity . " ) " ;
2011-08-20 00:15:22 +02:00
}
2021-02-26 13:18:40 +01: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 );
2023-01-31 05:01:30 +01:00
2021-02-26 13:18:40 +01:00
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
if ( ! array_key_exists ( $obj -> rowid , $ret )) {
if ( $mode != 1 ) {
2020-04-10 10:59:32 +02:00
$newuser = new User ( $this -> db );
2023-01-31 05:01:30 +01:00
//$newuser->fetch($obj->rowid); // We are inside a loop, no subrequests inside a loop
$newuser -> id = $obj -> rowid ;
$newuser -> login = $obj -> login ;
$newuser -> photo = $obj -> photo ;
$newuser -> lastname = $obj -> lastname ;
$newuser -> firstname = $obj -> firstname ;
$newuser -> email = $obj -> email ;
$newuser -> socid = $obj -> fk_soc ;
$newuser -> entity = $obj -> entity ;
$newuser -> employee = $obj -> employee ;
2023-06-04 03:41:23 +02:00
$newuser -> status = $obj -> status ;
2023-01-31 05:01:30 +01:00
2020-04-10 10:59:32 +02:00
$ret [ $obj -> rowid ] = $newuser ;
2021-02-26 13:18:40 +01:00
} else {
$ret [ $obj -> rowid ] = $obj -> rowid ;
}
2012-07-07 15:11:58 +02:00
}
2021-02-26 13:18:40 +01:00
if ( $mode != 1 && ! empty ( $obj -> usergroup_entity )) {
2024-04-22 14:50:25 +02:00
// $ret[$obj->rowid] is instance of User
2024-03-31 18:28:57 +02:00
if ( ! is_array ( $ret [ $obj -> rowid ] -> usergroup_entity )) {
$ret [ $obj -> rowid ] -> usergroup_entity = array ();
}
2024-04-22 15:17:38 +02:00
$ret [ $obj -> rowid ] -> usergroup_entity [] = ( int ) $obj -> usergroup_entity ;
2013-04-26 19:13:39 +02:00
}
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 ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> lasterror ();
2010-01-16 20:56:05 +01:00
return - 1 ;
}
}
2009-05-04 20:01:47 +02:00
/**
2017-02-28 11:48:46 +01:00
* Add a permission to a group
2011-09-04 20:41:19 +02:00
*
2017-06-06 07:57:03 +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
2009-05-04 20:01:47 +02:00
*/
2019-02-25 20:35:59 +01:00
public function addrights ( $rid , $allmodule = '' , $allperms = '' , $entity = 0 )
2005-02-26 19:34:14 +01:00
{
2017-02-28 11:48:46 +01:00
global $conf , $user , $langs ;
2009-05-04 20:01:47 +02:00
2020-04-10 10:59:32 +02:00
$entity = ( ! empty ( $entity ) ? $entity : $conf -> entity );
2017-06-06 07:57:03 +02:00
dol_syslog ( get_class ( $this ) . " ::addrights $rid , $allmodule , $allperms , $entity " );
2020-04-10 10:59:32 +02:00
$error = 0 ;
$whereforadd = '' ;
2009-05-04 20:01:47 +02:00
$this -> db -> begin ();
2021-02-26 13:18:40 +01:00
if ( ! empty ( $rid )) {
2020-11-07 15:57:38 +01:00
$module = $perms = $subperms = '' ;
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 " ;
2022-01-27 10:00:26 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " rights_def " ;
2020-11-07 15:28:10 +01:00
$sql .= " WHERE id = " . (( int ) $rid );
$sql .= " AND entity = " . (( int ) $entity );
2009-05-04 20:01:47 +02:00
2020-04-10 10:59:32 +02:00
$result = $this -> db -> query ( $sql );
2009-05-04 20:01:47 +02:00
if ( $result ) {
$obj = $this -> db -> fetch_object ( $result );
2020-11-07 15:28:10 +01:00
if ( $obj ) {
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-02-28 11:48:46 +01:00
$error ++ ;
2009-05-04 20:01:47 +02:00
dol_print_error ( $this -> db );
}
2009-06-06 16:59:55 +02:00
// Where pour la liste des droits a ajouter
2020-11-07 15:28:10 +01:00
$whereforadd = " id= " . (( int ) $rid );
2020-11-07 15:57:38 +01:00
// Find also rights that are herited to add them too
2021-02-26 13:18:40 +01:00
if ( $subperms ) {
$whereforadd .= " OR (module=' " . $this -> db -> escape ( $module ) . " ' AND perms=' " . $this -> db -> escape ( $perms ) . " ' AND (subperms='lire' OR subperms='read')) " ;
} elseif ( $perms ) {
$whereforadd .= " OR (module=' " . $this -> db -> escape ( $module ) . " ' AND (perms='lire' OR perms='read') AND subperms IS NULL) " ;
}
2020-05-21 15:05:19 +02:00
} else {
2009-06-06 16:59:55 +02:00
// Where pour la liste des droits a ajouter
2021-02-26 13:18:40 +01:00
if ( ! empty ( $allmodule )) {
if ( $allmodule == 'allmodules' ) {
2020-04-10 10:59:32 +02:00
$whereforadd = 'allmodules' ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$whereforadd = " module=' " . $this -> db -> escape ( $allmodule ) . " ' " ;
2021-02-26 13:18:40 +01:00
if ( ! empty ( $allperms )) {
$whereforadd .= " AND perms=' " . $this -> db -> escape ( $allperms ) . " ' " ;
}
2018-07-03 21:46:20 +02:00
}
}
2009-05-04 20:01:47 +02:00
}
2020-11-07 15:28:10 +01:00
// Add permission of the list $whereforadd
2021-02-26 13:18:40 +01:00
if ( ! empty ( $whereforadd )) {
2009-05-04 20:01:47 +02:00
//print "$module-$perms-$subperms";
$sql = " SELECT id " ;
2022-01-27 10:00:26 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " rights_def " ;
2021-06-09 15:36:47 +02:00
$sql .= " WHERE entity = " . (( int ) $entity );
2020-04-10 10:59:32 +02:00
if ( ! empty ( $whereforadd ) && $whereforadd != 'allmodules' ) {
$sql .= " AND " . $whereforadd ;
2018-07-03 21:46:20 +02:00
}
2009-05-04 20:01:47 +02:00
2020-04-10 10:59:32 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 13:18:40 +01:00
if ( $result ) {
2009-05-04 20:01:47 +02:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
2021-02-26 13:18:40 +01:00
while ( $i < $num ) {
2009-05-04 20:01:47 +02:00
$obj = $this -> db -> fetch_object ( $result );
$nid = $obj -> id ;
2022-01-27 10:00:26 +01:00
$sql = " DELETE FROM " . $this -> db -> prefix () . " usergroup_rights WHERE fk_usergroup = " . (( int ) $this -> id ) . " AND fk_id= " . (( int ) $nid ) . " AND entity = " . (( int ) $entity );
2021-02-26 13:18:40 +01:00
if ( ! $this -> db -> query ( $sql )) {
$error ++ ;
}
2022-01-27 10:00:26 +01:00
$sql = " INSERT INTO " . $this -> db -> prefix () . " usergroup_rights (entity, fk_usergroup, fk_id) VALUES ( " . (( int ) $entity ) . " , " . (( int ) $this -> id ) . " , " . (( int ) $nid ) . " ) " ;
2021-02-26 13:18:40 +01:00
if ( ! $this -> db -> query ( $sql )) {
$error ++ ;
}
2009-05-04 20:01:47 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-02-28 11:48:46 +01:00
$error ++ ;
2009-05-04 20:01:47 +02:00
dol_print_error ( $this -> db );
}
2017-05-24 11:27:27 +02:00
2021-02-26 13:18:40 +01:00
if ( ! $error ) {
2017-12-08 12:38:22 +01:00
$langs -> load ( " other " );
2024-03-11 22:28:39 +01:00
$this -> context = array ( 'audit' => $langs -> trans ( " PermissionsAdd " ) . ( $rid ? ' (id=' . $rid . ')' : '' ));
2017-05-24 11:27:27 +02:00
2020-10-31 14:32:18 +01:00
// Call trigger
$result = $this -> call_trigger ( 'USERGROUP_MODIFY' , $user );
2021-02-26 13:18:40 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-10-31 14:32:18 +01:00
// End call triggers
2017-05-24 11:27:27 +02:00
}
2009-05-04 20:01:47 +02:00
}
2017-02-28 11:48:46 +01:00
if ( $error ) {
2009-05-04 20:01:47 +02:00
$this -> db -> rollback ();
2017-02-28 11:48:46 +01:00
return - $error ;
2020-05-21 15:05:19 +02:00
} else {
2009-05-04 20:01:47 +02:00
$this -> db -> commit ();
return 1 ;
}
}
/**
2017-02-28 11:48:46 +01:00
* Remove a permission from group
2011-09-04 20:41:19 +02:00
*
2017-06-06 07:57:03 +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
* @ param int $entity Entity to use
* @ return int > 0 if OK , < 0 if OK
2009-05-04 20:01:47 +02:00
*/
2019-02-25 20:35:59 +01:00
public function delrights ( $rid , $allmodule = '' , $allperms = '' , $entity = 0 )
2009-05-04 20:01:47 +02:00
{
2017-02-28 11:48:46 +01:00
global $conf , $user , $langs ;
2009-05-04 20:01:47 +02:00
2020-04-10 10:59:32 +02:00
$error = 0 ;
$wherefordel = '' ;
2009-05-04 20:01:47 +02:00
2020-04-10 10:59:32 +02:00
$entity = ( ! empty ( $entity ) ? $entity : $conf -> entity );
2017-06-06 07:57:03 +02:00
2009-05-04 20:01:47 +02:00
$this -> db -> begin ();
2021-02-26 13:18:40 +01:00
if ( ! empty ( $rid )) {
2020-11-07 19:39:51 +01:00
$module = $perms = $subperms = '' ;
2024-01-12 17:18:52 +01:00
// Si on a demande suppression d'un droit en particulier, on recupere
2009-06-06 16:59:55 +02:00
// les caracteristiques module, perms et subperms de ce droit.
2009-05-04 20:01:47 +02:00
$sql = " SELECT module, perms, subperms " ;
2022-01-27 10:00:26 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " rights_def " ;
2021-06-09 15:36:47 +02:00
$sql .= " WHERE id = " . (( int ) $rid );
$sql .= " AND entity = " . (( int ) $entity );
2009-05-04 20:01:47 +02:00
2020-04-10 10:59:32 +02:00
$result = $this -> db -> query ( $sql );
2009-05-04 20:01:47 +02:00
if ( $result ) {
$obj = $this -> db -> fetch_object ( $result );
2020-11-07 19:25:04 +01:00
if ( $obj ) {
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-02-28 11:48:46 +01:00
$error ++ ;
2009-05-04 20:01:47 +02:00
dol_print_error ( $this -> db );
}
2021-06-09 15:36:47 +02:00
// Where for the list of permissions to delete
$wherefordel = " id = " . (( int ) $rid );
2009-05-04 20:01:47 +02:00
// Suppression des droits induits
2021-02-26 13:18:40 +01:00
if ( $subperms == 'lire' || $subperms == 'read' ) {
$wherefordel .= " OR (module=' " . $this -> db -> escape ( $module ) . " ' AND perms=' " . $this -> db -> escape ( $perms ) . " ' AND subperms IS NOT NULL) " ;
}
if ( $perms == 'lire' || $perms == 'read' ) {
$wherefordel .= " OR (module=' " . $this -> db -> escape ( $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
2024-01-12 17:18:52 +01:00
// TODO To remove when this will be implemented by the caller
2014-06-11 17:51:50 +02:00
//if (substr($rid,-1,1) == 0) $wherefordel="module='$module'";
2019-02-25 20:35:59 +01:00
} else {
2020-11-07 19:25:04 +01:00
// Add permission of the list $wherefordel
2021-02-26 13:18:40 +01:00
if ( ! empty ( $allmodule )) {
if ( $allmodule == 'allmodules' ) {
2020-04-10 10:59:32 +02:00
$wherefordel = 'allmodules' ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$wherefordel = " module=' " . $this -> db -> escape ( $allmodule ) . " ' " ;
2021-02-26 13:18:40 +01:00
if ( ! empty ( $allperms )) {
$wherefordel .= " AND perms=' " . $this -> db -> escape ( $allperms ) . " ' " ;
}
2018-07-03 21:46:20 +02:00
}
}
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
2021-02-26 13:18:40 +01:00
if ( ! empty ( $wherefordel )) {
2009-05-04 20:01:47 +02:00
//print "$module-$perms-$subperms";
$sql = " SELECT id " ;
2022-01-27 10:00:26 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " rights_def " ;
2021-08-23 19:33:24 +02:00
$sql .= " WHERE entity = " . (( int ) $entity );
2020-04-10 10:59:32 +02:00
if ( ! empty ( $wherefordel ) && $wherefordel != 'allmodules' ) {
$sql .= " AND " . $wherefordel ;
2018-07-03 21:46:20 +02:00
}
2009-05-04 20:01:47 +02:00
2020-04-10 10:59:32 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 13:18:40 +01:00
if ( $result ) {
2009-05-04 20:01:47 +02:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
2021-02-26 13:18:40 +01:00
while ( $i < $num ) {
2020-11-07 19:39:51 +01:00
$nid = 0 ;
2009-05-04 20:01:47 +02:00
$obj = $this -> db -> fetch_object ( $result );
2020-11-07 19:39:51 +01:00
if ( $obj ) {
$nid = $obj -> id ;
}
2009-05-04 20:01:47 +02:00
2022-01-27 10:00:26 +01:00
$sql = " DELETE FROM " . $this -> db -> prefix () . " usergroup_rights " ;
2021-04-24 20:18:11 +02:00
$sql .= " WHERE fk_usergroup = $this->id AND fk_id= " . (( int ) $nid );
2021-08-23 19:33:24 +02:00
$sql .= " AND entity = " . (( int ) $entity );
2021-02-26 13:18:40 +01:00
if ( ! $this -> db -> query ( $sql )) {
$error ++ ;
}
2005-02-26 19:34:14 +01:00
2009-05-04 20:01:47 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-02-28 11:48:46 +01:00
$error ++ ;
2009-05-04 20:01:47 +02:00
dol_print_error ( $this -> db );
}
2017-05-24 11:27:27 +02:00
2021-02-26 13:18:40 +01:00
if ( ! $error ) {
2017-12-08 12:38:22 +01:00
$langs -> load ( " other " );
2024-03-11 22:28:39 +01:00
$this -> context = array ( 'audit' => $langs -> trans ( " PermissionsDelete " ) . ( $rid ? ' (id=' . $rid . ')' : '' ));
2017-05-24 11:27:27 +02:00
2020-10-31 14:32:18 +01:00
// Call trigger
$result = $this -> call_trigger ( 'USERGROUP_MODIFY' , $user );
2021-02-26 13:18:40 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-10-31 14:32:18 +01:00
// End call triggers
2017-02-28 11:48:46 +01:00
}
2009-05-04 20:01:47 +02:00
}
2005-02-26 19:34:14 +01:00
2017-02-28 11:48:46 +01:00
if ( $error ) {
2009-05-04 20:01:47 +02:00
$this -> db -> rollback ();
2017-02-28 11:48:46 +01:00
return - $error ;
2020-05-21 15:05:19 +02:00
} else {
2009-05-04 20:01:47 +02:00
$this -> db -> commit ();
return 1 ;
2005-02-26 19:34:14 +01:00
}
}
2009-05-04 20:01:47 +02:00
/**
2024-01-12 17:18:52 +01:00
* Load the list of permissions for the user into the group object
2011-09-04 20:41:19 +02:00
*
2012-03-09 09:38:31 +01:00
* @ param string $moduletag Name of module we want permissions ( '' means all )
2024-01-12 17:18:52 +01:00
* @ return int Return integer < 0 if KO , >= 0 if OK
2024-09-07 18:03:43 +02:00
* @ deprecated
* TODO Remove this method . It has a name conflict with getRights () in CommonObject and was replaced in v20 with loadRights ()
*
* @ phpstan - ignore - next - line
2009-05-04 20:01:47 +02:00
*/
2019-02-25 20:35:59 +01:00
public function getrights ( $moduletag = '' )
2024-09-07 18:03:43 +02:00
{
2024-09-10 00:57:02 +02:00
return $this -> loadRights ( $moduletag );
2024-09-07 18:03:43 +02:00
}
/**
* Load the list of permissions for the user into the group object
*
* @ param string $moduletag Name of module we want permissions ( '' means all )
* @ return int Return integer < 0 if KO , >= 0 if OK
*/
public function loadRights ( $moduletag = '' )
2009-05-04 20:01:47 +02:00
{
global $conf ;
2021-02-26 13:18:40 +01:00
if ( $moduletag && isset ( $this -> _tab_loaded [ $moduletag ]) && $this -> _tab_loaded [ $moduletag ]) {
2019-06-18 20:15:04 +02:00
// Rights for this module are already loaded, so we leave
2023-01-04 18:34:54 +01:00
return 0 ;
2012-01-11 01:06:56 +01:00
}
2021-02-26 13:18:40 +01:00
if ( ! empty ( $this -> all_permissions_are_loaded )) {
2019-06-18 20:15:04 +02:00
// We already loaded all rights for this group, so we leave
2023-01-04 18:34:54 +01:00
return 0 ;
2009-05-04 20:01:47 +02:00
}
2024-09-07 18:03:43 +02:00
// Load permission from group
2009-05-04 20:01:47 +02:00
$sql = " SELECT r.module, r.perms, r.subperms " ;
2022-01-27 10:00:26 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " usergroup_rights as u, " . $this -> db -> prefix () . " rights_def as r " ;
2020-04-10 10:59:32 +02:00
$sql .= " WHERE r.id = u.fk_id " ;
2021-08-23 19:33:24 +02:00
$sql .= " AND r.entity = " . (( int ) $conf -> entity );
$sql .= " AND u.entity = " . (( int ) $conf -> entity );
$sql .= " AND u.fk_usergroup = " . (( int ) $this -> id );
2020-04-10 10:59:32 +02:00
$sql .= " AND r.perms IS NOT NULL " ;
2021-02-26 13:18:40 +01:00
if ( $moduletag ) {
$sql .= " AND r.module = ' " . $this -> db -> escape ( $moduletag ) . " ' " ;
}
2012-01-11 01:06:56 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::getrights' , LOG_DEBUG );
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 13:18:40 +01:00
if ( $resql ) {
2010-08-09 18:07:24 +02:00
$num = $this -> db -> num_rows ( $resql );
2009-12-21 01:32:17 +01:00
$i = 0 ;
2021-02-26 13:18:40 +01:00
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
2020-11-07 19:39:51 +01:00
if ( $obj ) {
$module = $obj -> module ;
$perms = $obj -> perms ;
$subperms = $obj -> subperms ;
2009-12-21 01:32:17 +01:00
2021-02-26 13:18:40 +01:00
if ( $perms ) {
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 ();
}
if ( $subperms ) {
if ( ! isset ( $this -> rights -> $module -> $perms ) || ! is_object ( $this -> rights -> $module -> $perms )) {
$this -> rights -> $module -> $perms = new stdClass ();
}
if ( empty ( $this -> rights -> $module -> $perms -> $subperms )) {
$this -> nb_rights ++ ;
}
2020-11-07 19:39:51 +01:00
$this -> rights -> $module -> $perms -> $subperms = 1 ;
} else {
2021-02-26 13:18:40 +01:00
if ( empty ( $this -> rights -> $module -> $perms )) {
$this -> nb_rights ++ ;
}
2020-11-07 19:39:51 +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
}
2021-02-26 13:18:40 +01:00
if ( $moduletag == '' ) {
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
2020-04-10 10:59:32 +02:00
$this -> all_permissions_are_loaded = 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-06-18 20:15:04 +02:00
// If module defined, we flag it as loaded into cache
2020-10-31 14:32:18 +01:00
$this -> _tab_loaded [ $moduletag ] = 1 ;
2012-01-11 01:06:56 +01:00
}
2009-05-04 20:01:47 +02:00
2020-10-31 14:32:18 +01: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
/**
2018-12-22 17:34:27 +01:00
* Delete a group
2011-09-04 20:41:19 +02:00
*
2018-12-22 17:34:27 +01:00
* @ param User $user User that delete
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2009-05-04 20:01:47 +02:00
*/
2019-02-25 20:35:59 +01:00
public function delete ( User $user )
2006-11-20 03:10:47 +01:00
{
2020-04-13 19:03:48 +02:00
return $this -> deleteCommon ( $user );
2006-11-20 03:10:47 +01:00
}
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
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 if KO , >= 0 if OK
2009-05-04 20:01:47 +02:00
*/
2019-02-25 20:35:59 +01:00
public function create ( $notrigger = 0 )
2006-11-20 02:44:27 +01:00
{
2020-04-13 19:03:48 +02:00
global $user , $conf ;
2009-05-04 20:01:47 +02:00
2020-04-13 19:03:48 +02:00
$this -> datec = dol_now ();
2020-09-07 10:34:44 +02:00
if ( ! empty ( $this -> name )) {
$this -> nom = $this -> name ; // Field for 'name' is called 'nom' in database
2020-04-21 16:14:38 +02:00
}
2011-08-24 00:25:37 +02:00
2021-02-26 13:18:40 +01:00
if ( ! isset ( $this -> entity )) {
$this -> entity = $conf -> entity ; // If not defined, we use default value
}
2010-05-05 19:42:01 +02:00
2020-04-13 19:03:48 +02:00
return $this -> createCommon ( $user , $notrigger );
2006-11-20 02:44:27 +01:00
}
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
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 if KO , >= 0 if OK
2009-05-04 20:01:47 +02:00
*/
2019-02-25 20:35:59 +01:00
public function update ( $notrigger = 0 )
2009-05-04 20:01:47 +02:00
{
2020-04-13 19:03:48 +02:00
global $user , $conf ;
2011-08-24 00:25:37 +02:00
2020-09-07 10:34:44 +02:00
if ( ! empty ( $this -> name )) {
$this -> nom = $this -> name ; // Field for 'name' is called 'nom' in database
2011-08-20 00:15:22 +02:00
}
2009-08-12 01:42:21 +02:00
2020-04-13 19:03:48 +02:00
return $this -> updateCommon ( $user , $notrigger );
2006-11-20 02:13:13 +01:00
}
2006-11-19 17:41:17 +01:00
2023-06-04 02:59:28 +02:00
/**
* Return full name ( civility + ' ' + name + ' ' + lastname )
*
* @ param Translate $langs Language object for translation of civility ( used only if option is 1 )
* @ param int $option 0 = No option , 1 = Add civility
* @ param int $nameorder - 1 = Auto , 0 = Lastname + Firstname , 1 = Firstname + Lastname , 2 = Firstname , 3 = Firstname if defined else lastname , 4 = Lastname , 5 = Lastname if defined else firstname
* @ param int $maxlen Maximum length
* @ return string String with full name
*/
public function getFullName ( $langs , $option = 0 , $nameorder = - 1 , $maxlen = 0 )
{
//print "lastname=".$this->lastname." name=".$this->name." nom=".$this->nom."<br>\n";
$lastname = $this -> lastname ;
$firstname = $this -> firstname ;
if ( empty ( $lastname )) {
$lastname = ( isset ( $this -> lastname ) ? $this -> lastname : ( isset ( $this -> name ) ? $this -> name : ( isset ( $this -> nom ) ? $this -> nom : ( isset ( $this -> societe ) ? $this -> societe : ( isset ( $this -> company ) ? $this -> company : '' )))));
}
$ret = '' ;
if ( ! empty ( $option ) && ! empty ( $this -> civility_code )) {
if ( $langs -> transnoentitiesnoconv ( " Civility " . $this -> civility_code ) != " Civility " . $this -> civility_code ) {
$ret .= $langs -> transnoentitiesnoconv ( " Civility " . $this -> civility_code ) . ' ' ;
} else {
$ret .= $this -> civility_code . ' ' ;
}
}
$ret .= dolGetFirstLastname ( $firstname , $lastname , $nameorder );
return dol_trunc ( $ret , $maxlen );
}
2017-05-09 10:19:39 +02:00
/**
2023-04-08 15:08:55 +02:00
* Return the label of the status
2017-05-09 10:19:39 +02:00
*
2023-04-08 15:08:55 +02:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto , 6 = Long label + Picto
2017-05-09 10:19:39 +02:00
* @ return string Label of status
*/
2019-02-25 20:35:59 +01:00
public function getLibStatut ( $mode = 0 )
2017-05-09 10:19:39 +02:00
{
2020-10-31 14:32:18 +01:00
return $this -> LibStatut ( 0 , $mode );
2017-05-09 10:19:39 +02:00
}
2017-06-06 07:57:03 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-05-09 10:19:39 +02:00
/**
2023-04-08 15:08:55 +02:00
* Return the label of a given status
2017-05-09 10:19:39 +02:00
*
2023-04-08 15:08:55 +02:00
* @ param int $status Id status
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto , 6 = Long label + Picto
* @ return string Label of status
2017-05-09 10:19:39 +02:00
*/
2019-11-01 23:58:14 +01:00
public function LibStatut ( $status , $mode = 0 )
2017-05-09 10:19:39 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
global $langs ;
$langs -> load ( 'users' );
return '' ;
2017-05-09 10:19:39 +02:00
}
2017-06-06 07:57:03 +02:00
2023-02-03 09:29:02 +01:00
/**
* getTooltipContentArray
2024-09-23 03:24:19 +02:00
* @ param array < string , mixed > $params params to construct tooltip data
2023-02-03 09:29:02 +01:00
* @ since v18
2024-09-23 03:24:19 +02:00
* @ return array { picto ? : string , ref ? : string , refsupplier ? : string , label ? : string , date ? : string , date_echeance ? : string , amountht ? : string , total_ht ? : string , totaltva ? : string , amountlt1 ? : string , amountlt2 ? : string , amountrevenustamp ? : string , totalttc ? : string } | array { optimize : string }
2023-02-03 09:29:02 +01:00
*/
public function getTooltipContentArray ( $params )
{
global $conf , $langs , $menumanager ;
$option = $params [ 'option' ] ? ? '' ;
$datas = [];
2023-11-27 12:08:48 +01:00
if ( getDolGlobalString ( 'MAIN_OPTIMIZEFORTEXTBROWSER' )) {
2023-02-03 09:29:02 +01:00
$langs -> load ( " users " );
return [ 'optimize' => $langs -> trans ( " ShowGroup " )];
}
$datas [ 'divopen' ] = '<div class="centpercent">' ;
$datas [ 'picto' ] = img_picto ( '' , 'group' ) . ' <u>' . $langs -> trans ( " Group " ) . '</u><br>' ;
$datas [ 'name' ] = '<b>' . $langs -> trans ( 'Name' ) . ':</b> ' . $this -> name ;
$datas [ 'description' ] = '<br><b>' . $langs -> trans ( " Description " ) . ':</b> ' . $this -> note ;
$datas [ 'divclose' ] = '</div>' ;
return $datas ;
}
2018-02-06 19:16:15 +01:00
/**
2024-01-12 17:18:52 +01:00
* Return a link to the user card ( with optionally the picto )
2019-02-25 20:35:59 +01:00
* Use this -> id , this -> lastname , this -> firstname
2018-02-06 19:16:15 +01:00
*
2019-02-25 20:35:59 +01:00
* @ param int $withpicto Include picto in link ( 0 = No picto , 1 = Include picto into link , 2 = Only picto , - 1 = Include photo into link , - 2 = Only picto photo , - 3 = Only photo very small )
2021-07-03 18:09:48 +02:00
* @ param string $option On what the link point to ( 'nolink' , 'permissions' )
2018-02-06 19:16:15 +01:00
* @ param integer $notooltip 1 = Disable tooltip on picto and name
* @ param string $morecss Add more css on link
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
* @ return string String with URL
*/
2019-02-25 20:35:59 +01:00
public function getNomUrl ( $withpicto = 0 , $option = '' , $notooltip = 0 , $morecss = '' , $save_lastsearch_value = - 1 )
2018-02-06 19:16:15 +01:00
{
global $langs , $conf , $db , $hookmanager ;
2023-11-27 12:08:48 +01:00
if ( getDolGlobalString ( 'MAIN_OPTIMIZEFORTEXTBROWSER' ) && $withpicto ) {
2021-02-26 13:18:40 +01:00
$withpicto = 0 ;
}
2018-02-06 19:16:15 +01:00
2023-02-06 23:00:42 +01:00
$result = '' ;
$params = [
'id' => $this -> id ,
'objecttype' => $this -> element ,
'option' => $option ,
];
$classfortooltip = 'classfortooltip' ;
$dataparams = '' ;
if ( getDolGlobalInt ( 'MAIN_ENABLE_AJAX_TOOLTIP' )) {
$classfortooltip = 'classforajaxtooltip' ;
2023-04-03 19:51:40 +02:00
$dataparams = ' data-params="' . dol_escape_htmltag ( json_encode ( $params )) . '"' ;
$label = '' ;
} else {
$label = implode ( $this -> getTooltipContentArray ( $params ));
2023-02-06 23:00:42 +01:00
}
2018-02-06 19:16:15 +01:00
2021-07-03 18:09:48 +02:00
if ( $option == 'permissions' ) {
$url = DOL_URL_ROOT . '/user/group/perms.php?id=' . $this -> id ;
} else {
$url = DOL_URL_ROOT . '/user/group/card.php?id=' . $this -> id ;
}
2018-02-06 19:16:15 +01:00
2021-02-26 13:18:40 +01:00
if ( $option != 'nolink' ) {
2018-02-06 19:16:15 +01:00
// Add param to save lastsearch_values or not
2020-04-10 10:59:32 +02:00
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2023-09-10 17:41:22 +02:00
if ( $save_lastsearch_value == - 1 && isset ( $_SERVER [ " PHP_SELF " ]) && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) {
2021-02-26 13:18:40 +01:00
$add_save_lastsearch_values = 1 ;
}
if ( $add_save_lastsearch_values ) {
$url .= '&save_lastsearch_values=1' ;
}
2018-02-06 19:16:15 +01:00
}
2020-04-10 10:59:32 +02:00
$linkclose = " " ;
2021-02-26 13:18:40 +01:00
if ( empty ( $notooltip )) {
2023-11-27 12:08:48 +01:00
if ( getDolGlobalString ( 'MAIN_OPTIMIZEFORTEXTBROWSER' )) {
2018-02-06 19:16:15 +01:00
$langs -> load ( " users " );
2020-04-10 10:59:32 +02:00
$label = $langs -> trans ( " ShowGroup " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 , 1 ) . '"' ;
2018-02-06 19:16:15 +01:00
}
2025-01-09 02:01:49 +01:00
$linkclose .= ( $label ? ' title="' . dolPrintHTMLForAttribute ( $label ) . '"' : ' title="tocomplete"' );
2023-02-04 21:22:30 +01:00
$linkclose .= $dataparams . ' class="' . $classfortooltip . ( $morecss ? ' ' . $morecss : '' ) . '"' ;
2018-02-06 19:16:15 +01:00
}
$linkstart = '<a href="' . $url . '"' ;
2020-04-10 10:59:32 +02:00
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2018-02-06 19:16:15 +01:00
$result = $linkstart ;
2021-02-26 13:18:40 +01:00
if ( $withpicto ) {
2023-09-17 12:04:54 +02:00
$result .= img_object (( $notooltip ? '' : $label ), ( $this -> picto ? $this -> picto : 'generic' ), ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . '"' ), 0 , 0 , $notooltip ? 0 : 1 );
2021-02-26 13:18:40 +01:00
}
if ( $withpicto != 2 ) {
$result .= $this -> name ;
}
2018-02-06 19:16:15 +01:00
$result .= $linkend ;
2018-03-15 20:46:55 +01:00
global $action ;
$hookmanager -> initHooks ( array ( 'groupdao' ));
2024-03-11 22:28:39 +01:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2020-04-10 10:59:32 +02:00
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-26 13:18:40 +01:00
if ( $reshook > 0 ) {
$result = $hookmanager -> resPrint ;
} else {
$result .= $hookmanager -> resPrint ;
}
2018-03-15 20:46:55 +01:00
2018-02-06 19:16:15 +01:00
return $result ;
}
2019-05-10 16:12:16 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2019-05-10 16:37:03 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
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
*
2024-11-06 23:57:45 +01:00
* @ param array < string , mixed > $info Info array loaded by _load_ldap_info
* @ param int < 0 , 2 > $mode 0 = Return full DN ( uid = qqq , ou = xxx , dc = aaa , dc = bbb )
2011-09-04 20:41:19 +02:00
* 1 = Return DN without key inside ( ou = xxx , dc = aaa , dc = bbb )
* 2 = Return key only ( uid = qqq )
2024-11-06 23:57:45 +01:00
* @ return string DN
2009-05-04 20:01:47 +02:00
*/
2020-10-31 14:32:18 +01:00
public function _load_ldap_dn ( $info , $mode = 0 )
2006-11-19 17:41:17 +01:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2006-11-21 23:51:05 +01:00
global $conf ;
2020-04-10 10:59:32 +02:00
$dn = '' ;
2021-02-26 13:18:40 +01:00
if ( $mode == 0 ) {
2023-10-15 18:41:39 +02:00
$dn = getDolGlobalString ( 'LDAP_KEY_GROUPS' ) . " = " . $info [ getDolGlobalString ( 'LDAP_KEY_GROUPS' )] . " , " . getDolGlobalString ( 'LDAP_GROUP_DN' );
2021-02-26 13:18:40 +01:00
}
if ( $mode == 1 ) {
2024-01-05 04:18:53 +01:00
$dn = getDolGlobalString ( 'LDAP_GROUP_DN' );
2021-02-26 13:18:40 +01:00
}
if ( $mode == 2 ) {
2023-10-15 18:41:39 +02:00
$dn = getDolGlobalString ( 'LDAP_KEY_GROUPS' ) . " = " . $info [ getDolGlobalString ( 'LDAP_KEY_GROUPS' )];
2021-02-26 13:18:40 +01:00
}
2006-11-21 23:51:05 +01:00
return $dn ;
2006-11-19 17:41:17 +01:00
}
2006-11-20 02:13:13 +01:00
2019-05-10 16:12:16 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2019-05-10 16:37:03 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
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
*
2024-11-06 23:57:45 +01:00
* @ return array < string , mixed > Tableau info des attributes
2009-05-04 20:01:47 +02:00
*/
2020-10-31 14:32:18 +01:00
public function _load_ldap_info ()
2006-11-20 02:13:13 +01:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-06-18 20:01:28 +02:00
global $conf ;
2017-07-13 23:52:02 +02:00
2020-04-10 10:59:32 +02:00
$info = array ();
2009-05-04 20:01:47 +02:00
2007-04-13 02:35:13 +02:00
// Object classes
2023-12-13 15:20:53 +01:00
$info [ " objectclass " ] = explode ( ',' , getDolGlobalString ( 'LDAP_GROUP_OBJECT_CLASS' ));
2009-05-04 20:01:47 +02:00
2006-11-20 03:10:47 +01:00
// Champs
2023-11-27 12:08:48 +01:00
if ( $this -> name && getDolGlobalString ( 'LDAP_GROUP_FIELD_FULLNAME' )) {
2023-10-15 18:41:39 +02:00
$info [ getDolGlobalString ( 'LDAP_GROUP_FIELD_FULLNAME' )] = $this -> name ;
2021-02-26 13:18:40 +01:00
}
2022-08-31 22:14:20 +02:00
//if ($this->name && !empty($conf->global->LDAP_GROUP_FIELD_NAME)) $info[$conf->global->LDAP_GROUP_FIELD_NAME] = $this->name;
2023-11-27 12:08:48 +01:00
if ( $this -> note && getDolGlobalString ( 'LDAP_GROUP_FIELD_DESCRIPTION' )) {
2023-10-15 18:41:39 +02:00
$info [ getDolGlobalString ( 'LDAP_GROUP_FIELD_DESCRIPTION' )] = dol_string_nohtmltag ( $this -> note , 2 );
2021-02-26 13:18:40 +01:00
}
2023-11-27 12:08:48 +01:00
if ( getDolGlobalString ( 'LDAP_GROUP_FIELD_GROUPMEMBERS' )) {
2020-04-10 10:59:32 +02:00
$valueofldapfield = array ();
2021-02-26 13:18:40 +01:00
foreach ( $this -> members as $key => $val ) { // This is array of users for group into dolibarr database.
2020-04-10 10:59:32 +02:00
$muser = new User ( $this -> db );
2012-05-11 12:10:47 +02:00
$muser -> fetch ( $val -> id );
2016-06-16 23:39:59 +02:00
$info2 = $muser -> _load_ldap_info ();
2017-05-24 11:27:27 +02:00
$valueofldapfield [] = $muser -> _load_ldap_dn ( $info2 );
2009-12-21 01:50:46 +01:00
}
2023-10-15 18:41:39 +02:00
$info [ getDolGlobalString ( 'LDAP_GROUP_FIELD_GROUPMEMBERS' )] = ( ! empty ( $valueofldapfield ) ? $valueofldapfield : '' );
2021-05-17 11:29:51 +02:00
}
2023-11-27 12:08:48 +01:00
if ( getDolGlobalString ( 'LDAP_GROUP_FIELD_GROUPID' )) {
2023-10-15 18:41:39 +02:00
$info [ getDolGlobalString ( 'LDAP_GROUP_FIELD_GROUPID' )] = $this -> id ;
2021-05-17 11:29:51 +02:00
}
2006-11-20 02:13:13 +01:00
return $info ;
}
2006-11-19 17:41:17 +01:00
/**
2020-10-31 14:32:18 +01:00
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
2024-03-02 16:38:35 +01:00
* @ return int
2020-10-31 14:32:18 +01:00
*/
public function initAsSpecimen ()
2006-11-19 17:41:17 +01:00
{
2009-12-19 15:12:55 +01:00
global $conf , $user , $langs ;
2006-11-19 17:41:17 +01:00
2024-01-12 17:18:52 +01:00
// Initialise parameters
2020-04-10 10:59:32 +02:00
$this -> id = 0 ;
2006-11-19 17:41:17 +01:00
$this -> ref = 'SPECIMEN' ;
2020-04-10 10:59:32 +02:00
$this -> specimen = 1 ;
2006-11-19 17:41:17 +01:00
2020-04-10 10:59:32 +02:00
$this -> name = 'DOLIBARR GROUP SPECIMEN' ;
$this -> note = 'This is a note' ;
$this -> datec = time ();
2023-04-03 12:22:05 +02:00
$this -> tms = time ();
2017-05-24 11:27:27 +02:00
2020-10-31 14:32:18 +01:00
// Members of this group is just me
$this -> members = array (
$user -> id => $user
);
2024-03-02 16:38:35 +01:00
return 1 ;
2020-10-31 14:32:18 +01:00
}
2017-06-06 07:57:03 +02:00
2017-03-08 11:34:21 +01:00
/**
* Create a document onto disk according to template module .
*
* @ param string $modele Force model to use ( '' to not force )
* @ param Translate $outputlangs Object langs to use for output
2024-11-06 23:57:45 +01:00
* @ param int < 0 , 1 > $hidedetails Hide details of lines
* @ param int < 0 , 1 > $hidedesc Hide description
* @ param int < 0 , 1 > $hideref Hide ref
* @ param ? array < string , mixed > $moreparams Array to provide more information
2017-03-08 11:34:21 +01:00
* @ return int 0 if KO , 1 if OK
*/
2019-01-27 15:20:16 +01:00
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 , $moreparams = null )
2017-03-08 11:34:21 +01:00
{
2020-04-10 10:59:32 +02:00
global $conf , $user , $langs ;
2017-03-08 11:34:21 +01:00
$langs -> load ( " user " );
// Positionne le modele sur le nom du modele a utiliser
2021-02-26 13:18:40 +01:00
if ( ! dol_strlen ( $modele )) {
2023-11-27 12:08:48 +01:00
if ( getDolGlobalString ( 'USERGROUP_ADDON_PDF' )) {
2024-01-05 04:18:53 +01:00
$modele = getDolGlobalString ( 'USERGROUP_ADDON_PDF' );
2020-05-21 15:05:19 +02:00
} else {
2017-03-08 11:34:21 +01:00
$modele = 'grass' ;
}
}
$modelpath = " core/modules/usergroup/doc/ " ;
2018-06-15 09:47:28 +02:00
return $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams );
2017-03-08 11:34:21 +01:00
}
2022-12-02 15:10:54 +01:00
2023-12-04 13:53:48 +01:00
/**
2024-08-07 01:20:43 +02:00
* Return clickable link of object ( with eventually picto )
2022-12-02 15:10:54 +01:00
*
2025-02-03 11:24:16 +01:00
* @ param string $option Where point the link ( 0 => main card , 1 , 2 => shipment , 'nolink' => No link )
* @ param ? array < string , mixed > $arraydata Array of data
* @ return string HTML Code for Kanban thumb .
2022-12-02 15:10:54 +01:00
*/
2023-01-18 22:27:52 +01:00
public function getKanbanView ( $option = '' , $arraydata = null )
2022-12-02 15:10:54 +01:00
{
global $langs ;
2023-03-07 22:04:40 +01:00
$selected = ( empty ( $arraydata [ 'selected' ]) ? 0 : $arraydata [ 'selected' ]);
2022-12-02 15:10:54 +01:00
$return = '<div class="box-flex-item box-flex-grow-zero">' ;
$return .= '<div class="info-box info-box-sm">' ;
$return .= '<span class="info-box-icon bg-infobox-action">' ;
$return .= img_picto ( '' , $this -> picto );
$return .= '</span>' ;
$return .= '<div class="info-box-content">' ;
2023-05-11 11:03:26 +02:00
$return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . ( method_exists ( $this , 'getNomUrl' ) ? $this -> getNomUrl () : $this -> ref ) . '</span>' ;
2023-10-17 15:43:56 +02:00
if ( $selected >= 0 ) {
$return .= '<input id="cb' . $this -> id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this -> id . '"' . ( $selected ? ' checked="checked"' : '' ) . '>' ;
}
2022-12-02 15:10:54 +01:00
if ( property_exists ( $this , 'members' )) {
2023-01-31 05:01:30 +01:00
$return .= '<br><span class="info-box-status opacitymedium">' . ( empty ( $this -> nb_users ) ? 0 : $this -> nb_users ) . ' ' . $langs -> trans ( 'Users' ) . '</span>' ;
2022-12-02 15:10:54 +01:00
}
if ( property_exists ( $this , 'nb_rights' )) {
2023-01-31 05:01:30 +01:00
$return .= '<br><div class="info-box-status margintoponly opacitymedium">' . $langs -> trans ( 'NbOfPermissions' ) . ' : ' . ( empty ( $this -> nb_rights ) ? 0 : $this -> nb_rights ) . '</div>' ;
2022-12-02 15:10:54 +01:00
}
$return .= '</div>' ;
$return .= '</div>' ;
$return .= '</div>' ;
return $return ;
}
2005-01-27 16:55:40 +01:00
}