usergroup.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. <?php
  2. /* Copyright (c) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (c) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (c) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2012 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2014 Alexis Algoud <alexis@atm-consulting.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/user/class/usergroup.class.php
  24. * \brief File of class to manage user groups
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  27. if (! empty($conf->ldap->enabled)) require_once (DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
  28. /**
  29. * Class to manage user groups
  30. */
  31. class UserGroup extends CommonObject
  32. {
  33. public $element='usergroup';
  34. public $table_element='usergroup';
  35. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  36. public $picto='group';
  37. public $entity; // Entity of group
  38. /**
  39. * @deprecated
  40. * @see name
  41. */
  42. public $nom; // Name of group
  43. public $globalgroup; // Global group
  44. public $datec; // Creation date of group
  45. public $datem; // Modification date of group
  46. public $members=array(); // Array of users
  47. private $_tab_loaded=array(); // Array of cache of already loaded permissions
  48. public $oldcopy; // To contains a clone of this when we need to save old properties of object
  49. /**
  50. * Constructor de la classe
  51. *
  52. * @param DoliDb $db Database handler
  53. */
  54. function __construct($db)
  55. {
  56. $this->db = $db;
  57. return 0;
  58. }
  59. /**
  60. * Charge un objet group avec toutes ces caracteristiques (except ->members array)
  61. *
  62. * @param int $id id du groupe a charger
  63. * @param string $groupname name du groupe a charger
  64. * @return int <0 if KO, >0 if OK
  65. */
  66. function fetch($id='', $groupname='')
  67. {
  68. global $conf;
  69. $sql = "SELECT g.rowid, g.entity, g.nom as name, g.note, g.datec, g.tms as datem";
  70. $sql.= " FROM ".MAIN_DB_PREFIX."usergroup as g";
  71. if ($groupname)
  72. {
  73. $sql.= " WHERE g.nom = '".$this->db->escape($groupname)."'";
  74. }
  75. else
  76. {
  77. $sql.= " WHERE g.rowid = ".$id;
  78. }
  79. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  80. $result = $this->db->query($sql);
  81. if ($result)
  82. {
  83. if ($this->db->num_rows($result))
  84. {
  85. $obj = $this->db->fetch_object($result);
  86. $this->id = $obj->rowid;
  87. $this->ref = $obj->rowid;
  88. $this->entity = $obj->entity;
  89. $this->name = $obj->name;
  90. $this->nom = $obj->name; // Deprecated
  91. $this->note = $obj->note;
  92. $this->datec = $obj->datec;
  93. $this->datem = $obj->datem;
  94. $this->members=$this->listUsersForGroup();
  95. // Retreive all extrafield for group
  96. // fetch optionals attributes and labels
  97. dol_include_once('/core/class/extrafields.class.php');
  98. $extrafields=new ExtraFields($this->db);
  99. $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
  100. $this->fetch_optionals($this->id,$extralabels);
  101. // Sav current LDAP Current DN
  102. //$this->ldap_dn = $this->_load_ldap_dn($this->_load_ldap_info(),0);
  103. }
  104. $this->db->free($result);
  105. return 1;
  106. }
  107. else
  108. {
  109. $this->error=$this->db->lasterror();
  110. return -1;
  111. }
  112. }
  113. /**
  114. * Return array of groups objects for a particular user
  115. *
  116. * @param int $userid User id to search
  117. * @return array Array of groups objects
  118. */
  119. function listGroupsForUser($userid)
  120. {
  121. global $conf, $user;
  122. $ret=array();
  123. $sql = "SELECT g.rowid, ug.entity as usergroup_entity";
  124. $sql.= " FROM ".MAIN_DB_PREFIX."usergroup as g,";
  125. $sql.= " ".MAIN_DB_PREFIX."usergroup_user as ug";
  126. $sql.= " WHERE ug.fk_usergroup = g.rowid";
  127. $sql.= " AND ug.fk_user = ".$userid;
  128. if(! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
  129. {
  130. $sql.= " AND g.entity IS NOT NULL";
  131. }
  132. else
  133. {
  134. $sql.= " AND g.entity IN (0,".$conf->entity.")";
  135. }
  136. $sql.= " ORDER BY g.nom";
  137. dol_syslog(get_class($this)."::listGroupsForUser", LOG_DEBUG);
  138. $result = $this->db->query($sql);
  139. if ($result)
  140. {
  141. while ($obj = $this->db->fetch_object($result))
  142. {
  143. if (! array_key_exists($obj->rowid, $ret))
  144. {
  145. $newgroup=new UserGroup($this->db);
  146. $newgroup->fetch($obj->rowid);
  147. $ret[$obj->rowid]=$newgroup;
  148. }
  149. $ret[$obj->rowid]->usergroup_entity[]=$obj->usergroup_entity;
  150. }
  151. $this->db->free($result);
  152. return $ret;
  153. }
  154. else
  155. {
  156. $this->error=$this->db->lasterror();
  157. return -1;
  158. }
  159. }
  160. /**
  161. * Return array of User objects for group this->id (or all if this->id not defined)
  162. *
  163. * @param string $excludefilter Filter to exclude
  164. * @param int $mode 0=Return array of user instance, 1=Return array of users id only
  165. * @return mixed Array of users or -1 on error
  166. */
  167. function listUsersForGroup($excludefilter='', $mode=0)
  168. {
  169. global $conf, $user;
  170. $ret=array();
  171. $sql = "SELECT u.rowid";
  172. if (! empty($this->id)) $sql.= ", ug.entity as usergroup_entity";
  173. $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
  174. if (! empty($this->id)) $sql.= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
  175. $sql.= " WHERE 1 = 1";
  176. if (! empty($this->id)) $sql.= " AND ug.fk_user = u.rowid";
  177. if (! empty($this->id)) $sql.= " AND ug.fk_usergroup = ".$this->id;
  178. if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
  179. {
  180. $sql.= " AND u.entity IS NOT NULL";
  181. }
  182. else
  183. {
  184. $sql.= " AND u.entity IN (0,".$conf->entity.")";
  185. }
  186. if (! empty($excludefilter)) $sql.=' AND ('.$excludefilter.')';
  187. dol_syslog(get_class($this)."::listUsersForGroup", LOG_DEBUG);
  188. $resql = $this->db->query($sql);
  189. if ($resql)
  190. {
  191. while ($obj = $this->db->fetch_object($resql))
  192. {
  193. if (! array_key_exists($obj->rowid, $ret))
  194. {
  195. if ($mode != 1)
  196. {
  197. $newuser=new User($this->db);
  198. $newuser->fetch($obj->rowid);
  199. $ret[$obj->rowid]=$newuser;
  200. }
  201. else $ret[$obj->rowid]=$obj->rowid;
  202. }
  203. if ($mode != 1 && ! empty($obj->usergroup_entity))
  204. {
  205. $ret[$obj->rowid]->usergroup_entity[]=$obj->usergroup_entity;
  206. }
  207. }
  208. $this->db->free($resql);
  209. return $ret;
  210. }
  211. else
  212. {
  213. $this->error=$this->db->lasterror();
  214. return -1;
  215. }
  216. }
  217. /**
  218. * Add a permission to a group
  219. *
  220. * @param int $rid id du droit a ajouter
  221. * @param string $allmodule Ajouter tous les droits du module allmodule
  222. * @param string $allperms Ajouter tous les droits du module allmodule, perms allperms
  223. * @return int > 0 if OK, < 0 if KO
  224. */
  225. function addrights($rid,$allmodule='',$allperms='')
  226. {
  227. global $conf, $user, $langs;
  228. dol_syslog(get_class($this)."::addrights $rid, $allmodule, $allperms");
  229. $error=0;
  230. $whereforadd='';
  231. $this->db->begin();
  232. if (! empty($rid))
  233. {
  234. // Si on a demande ajout d'un droit en particulier, on recupere
  235. // les caracteristiques (module, perms et subperms) de ce droit.
  236. $sql = "SELECT module, perms, subperms";
  237. $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
  238. $sql.= " WHERE id = '".$this->db->escape($rid)."'";
  239. $sql.= " AND entity = ".$conf->entity;
  240. $result=$this->db->query($sql);
  241. if ($result) {
  242. $obj = $this->db->fetch_object($result);
  243. $module=$obj->module;
  244. $perms=$obj->perms;
  245. $subperms=$obj->subperms;
  246. }
  247. else {
  248. $error++;
  249. dol_print_error($this->db);
  250. }
  251. // Where pour la liste des droits a ajouter
  252. $whereforadd="id=".$this->db->escape($rid);
  253. // Ajout des droits induits
  254. if ($subperms) $whereforadd.=" OR (module='$module' AND perms='$perms' AND (subperms='lire' OR subperms='read'))";
  255. else if ($perms) $whereforadd.=" OR (module='$module' AND (perms='lire' OR perms='read') AND subperms IS NULL)";
  256. // Pour compatibilite, si lowid = 0, on est en mode ajout de tout
  257. // TODO A virer quand sera gere par l'appelant
  258. //if (substr($rid,-1,1) == 0) $whereforadd="module='$module'";
  259. }
  260. else {
  261. // Where pour la liste des droits a ajouter
  262. if (! empty($allmodule)) $whereforadd="module='".$this->db->escape($allmodule)."'";
  263. if (! empty($allperms)) $whereforadd=" AND perms='".$this->db->escape($allperms)."'";
  264. }
  265. // Ajout des droits de la liste whereforadd
  266. if (! empty($whereforadd))
  267. {
  268. //print "$module-$perms-$subperms";
  269. $sql = "SELECT id";
  270. $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
  271. $sql.= " WHERE $whereforadd";
  272. $sql.= " AND entity = ".$conf->entity;
  273. $result=$this->db->query($sql);
  274. if ($result)
  275. {
  276. $num = $this->db->num_rows($result);
  277. $i = 0;
  278. while ($i < $num)
  279. {
  280. $obj = $this->db->fetch_object($result);
  281. $nid = $obj->id;
  282. $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights WHERE fk_usergroup = $this->id AND fk_id=".$nid;
  283. if (! $this->db->query($sql)) $error++;
  284. $sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_rights (fk_usergroup, fk_id) VALUES ($this->id, $nid)";
  285. if (! $this->db->query($sql)) $error++;
  286. $i++;
  287. }
  288. }
  289. else
  290. {
  291. $error++;
  292. dol_print_error($this->db);
  293. }
  294. if (! $error)
  295. {
  296. $this->context = array('audit'=>$langs->trans("PermissionsAdd"));
  297. // Call trigger
  298. $result=$this->call_trigger('GROUP_MODIFY',$user);
  299. if ($result < 0) { $error++; }
  300. // End call triggers
  301. }
  302. }
  303. if ($error) {
  304. $this->db->rollback();
  305. return -$error;
  306. }
  307. else {
  308. $this->db->commit();
  309. return 1;
  310. }
  311. }
  312. /**
  313. * Remove a permission from group
  314. *
  315. * @param int $rid id du droit a retirer
  316. * @param string $allmodule Retirer tous les droits du module allmodule
  317. * @param string $allperms Retirer tous les droits du module allmodule, perms allperms
  318. * @return int > 0 if OK, < 0 if OK
  319. */
  320. function delrights($rid,$allmodule='',$allperms='')
  321. {
  322. global $conf, $user, $langs;
  323. $error=0;
  324. $wherefordel='';
  325. $this->db->begin();
  326. if (! empty($rid))
  327. {
  328. // Si on a demande supression d'un droit en particulier, on recupere
  329. // les caracteristiques module, perms et subperms de ce droit.
  330. $sql = "SELECT module, perms, subperms";
  331. $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
  332. $sql.= " WHERE id = '".$this->db->escape($rid)."'";
  333. $sql.= " AND entity = ".$conf->entity;
  334. $result=$this->db->query($sql);
  335. if ($result) {
  336. $obj = $this->db->fetch_object($result);
  337. $module=$obj->module;
  338. $perms=$obj->perms;
  339. $subperms=$obj->subperms;
  340. }
  341. else {
  342. $error++;
  343. dol_print_error($this->db);
  344. }
  345. // Where pour la liste des droits a supprimer
  346. $wherefordel="id=".$this->db->escape($rid);
  347. // Suppression des droits induits
  348. if ($subperms=='lire' || $subperms=='read') $wherefordel.=" OR (module='$module' AND perms='$perms' AND subperms IS NOT NULL)";
  349. if ($perms=='lire' || $perms=='read') $wherefordel.=" OR (module='$module')";
  350. // Pour compatibilite, si lowid = 0, on est en mode suppression de tout
  351. // TODO A virer quand sera gere par l'appelant
  352. //if (substr($rid,-1,1) == 0) $wherefordel="module='$module'";
  353. }
  354. else {
  355. // Where pour la liste des droits a supprimer
  356. if (! empty($allmodule)) $wherefordel="module='".$this->db->escape($allmodule)."'";
  357. if (! empty($allperms)) $wherefordel=" AND perms='".$this->db->escape($allperms)."'";
  358. }
  359. // Suppression des droits de la liste wherefordel
  360. if (! empty($wherefordel))
  361. {
  362. //print "$module-$perms-$subperms";
  363. $sql = "SELECT id";
  364. $sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
  365. $sql.= " WHERE $wherefordel";
  366. $sql.= " AND entity = ".$conf->entity;
  367. $result=$this->db->query($sql);
  368. if ($result)
  369. {
  370. $num = $this->db->num_rows($result);
  371. $i = 0;
  372. while ($i < $num)
  373. {
  374. $obj = $this->db->fetch_object($result);
  375. $nid = $obj->id;
  376. $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights";
  377. $sql.= " WHERE fk_usergroup = $this->id AND fk_id=".$nid;
  378. if (! $this->db->query($sql)) $error++;
  379. $i++;
  380. }
  381. }
  382. else
  383. {
  384. $error++;
  385. dol_print_error($this->db);
  386. }
  387. if (! $error)
  388. {
  389. $this->context = array('audit'=>$langs->trans("PermissionsDelete"));
  390. // Call trigger
  391. $result=$this->call_trigger('GROUP_MODIFY',$user);
  392. if ($result < 0) { $error++; }
  393. // End call triggers
  394. }
  395. }
  396. if ($error) {
  397. $this->db->rollback();
  398. return -$error;
  399. }
  400. else {
  401. $this->db->commit();
  402. return 1;
  403. }
  404. }
  405. /**
  406. * Charge dans l'objet group, la liste des permissions auquels le groupe a droit
  407. *
  408. * @param string $moduletag Name of module we want permissions ('' means all)
  409. * @return int <0 if KO, >0 if OK
  410. */
  411. function getrights($moduletag='')
  412. {
  413. global $conf;
  414. if ($moduletag && isset($this->_tab_loaded[$moduletag]) && $this->_tab_loaded[$moduletag])
  415. {
  416. // Le fichier de ce module est deja charge
  417. return;
  418. }
  419. if (! empty($this->all_permissions_are_loaded))
  420. {
  421. // Si les permissions ont deja ete chargees, on quitte
  422. return;
  423. }
  424. /*
  425. * Recuperation des droits
  426. */
  427. $sql = "SELECT r.module, r.perms, r.subperms ";
  428. $sql.= " FROM ".MAIN_DB_PREFIX."usergroup_rights as u, ".MAIN_DB_PREFIX."rights_def as r";
  429. $sql.= " WHERE r.id = u.fk_id";
  430. $sql.= " AND r.entity = ".$conf->entity;
  431. $sql.= " AND u.fk_usergroup = ".$this->id;
  432. $sql.= " AND r.perms IS NOT NULL";
  433. if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
  434. dol_syslog(get_class($this).'::getrights', LOG_DEBUG);
  435. $resql=$this->db->query($sql);
  436. if ($resql)
  437. {
  438. $num = $this->db->num_rows($resql);
  439. $i = 0;
  440. while ($i < $num)
  441. {
  442. $obj = $this->db->fetch_object($resql);
  443. $module=$obj->module;
  444. $perms=$obj->perms;
  445. $subperms=$obj->subperms;
  446. if ($perms)
  447. {
  448. if (! isset($this->rights)) $this->rights = new stdClass(); // For avoid error
  449. if (! isset($this->rights->$module) || ! is_object($this->rights->$module)) $this->rights->$module = new stdClass();
  450. if ($subperms)
  451. {
  452. if (! isset($this->rights->$module->$perms) || ! is_object($this->rights->$module->$perms)) $this->rights->$module->$perms = new stdClass();
  453. $this->rights->$module->$perms->$subperms = 1;
  454. }
  455. else
  456. {
  457. $this->rights->$module->$perms = 1;
  458. }
  459. }
  460. $i++;
  461. }
  462. $this->db->free($resql);
  463. }
  464. if ($moduletag == '')
  465. {
  466. // Si module etait non defini, alors on a tout charge, on peut donc considerer
  467. // que les droits sont en cache (car tous charges) pour cet instance de group
  468. $this->all_permissions_are_loaded=1;
  469. }
  470. else
  471. {
  472. // Si module defini, on le marque comme charge en cache
  473. $this->_tab_loaded[$moduletag]=1;
  474. }
  475. return 1;
  476. }
  477. /**
  478. * Efface un groupe de la base
  479. *
  480. * @return <0 if KO, > 0 if OK
  481. */
  482. function delete()
  483. {
  484. global $user,$conf,$langs;
  485. $error=0;
  486. $this->db->begin();
  487. $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights";
  488. $sql .= " WHERE fk_usergroup = ".$this->id;
  489. $this->db->query($sql);
  490. $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user";
  491. $sql .= " WHERE fk_usergroup = ".$this->id;
  492. $this->db->query($sql);
  493. // Remove extrafields
  494. if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
  495. {
  496. $result=$this->deleteExtraFields();
  497. if ($result < 0)
  498. {
  499. $error++;
  500. dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
  501. }
  502. }
  503. $sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup";
  504. $sql .= " WHERE rowid = ".$this->id;
  505. $result=$this->db->query($sql);
  506. if ($result)
  507. {
  508. // Call trigger
  509. $result=$this->call_trigger('GROUP_DELETE',$user);
  510. if ($result < 0) { $error++; $this->db->rollback(); return -1; }
  511. // End call triggers
  512. $this->db->commit();
  513. return 1;
  514. }
  515. else
  516. {
  517. $this->db->rollback();
  518. dol_print_error($this->db);
  519. return -1;
  520. }
  521. }
  522. /**
  523. * Create group into database
  524. *
  525. * @param int $notrigger 0=triggers enabled, 1=triggers disabled
  526. * @return int <0 if KO, >=0 if OK
  527. */
  528. function create($notrigger=0)
  529. {
  530. global $user, $conf, $langs, $hookmanager;
  531. $error=0;
  532. $now=dol_now();
  533. if (! isset($this->entity)) $this->entity=$conf->entity; // If not defined, we use default value
  534. $entity=$this->entity;
  535. if (! empty($conf->multicompany->enabled) && $conf->entity == 1) $entity=$this->entity;
  536. $this->db->begin();
  537. $sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup (";
  538. $sql.= "datec";
  539. $sql.= ", nom";
  540. $sql.= ", entity";
  541. $sql.= ") VALUES (";
  542. $sql.= "'".$this->db->idate($now)."'";
  543. $sql.= ",'".$this->db->escape($this->nom)."'";
  544. $sql.= ",".$this->db->escape($entity);
  545. $sql.= ")";
  546. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  547. $result=$this->db->query($sql);
  548. if ($result)
  549. {
  550. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."usergroup");
  551. if ($this->update(1) < 0) return -2;
  552. $action='create';
  553. // Actions on extra fields (by external module or standard code)
  554. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  555. {
  556. $result=$this->insertExtraFields();
  557. if ($result < 0)
  558. {
  559. $error++;
  560. }
  561. }
  562. if (! $error && ! $notrigger)
  563. {
  564. // Call trigger
  565. $result=$this->call_trigger('GROUP_CREATE',$user);
  566. if ($result < 0) { $error++; $this->db->rollback(); return -1; }
  567. // End call triggers
  568. }
  569. if ($error > 0) { $error++; $this->db->rollback(); return -1; }
  570. else $this->db->commit();
  571. return $this->id;
  572. }
  573. else
  574. {
  575. $this->db->rollback();
  576. $this->error=$this->db->lasterror();
  577. return -1;
  578. }
  579. }
  580. /**
  581. * Update group into database
  582. *
  583. * @param int $notrigger 0=triggers enabled, 1=triggers disabled
  584. * @return int <0 if KO, >=0 if OK
  585. */
  586. function update($notrigger=0)
  587. {
  588. global $user, $conf, $langs, $hookmanager;
  589. $error=0;
  590. $entity=$conf->entity;
  591. if(! empty($conf->multicompany->enabled) && $conf->entity == 1)
  592. {
  593. $entity=$this->entity;
  594. }
  595. $this->db->begin();
  596. $sql = "UPDATE ".MAIN_DB_PREFIX."usergroup SET ";
  597. $sql.= " nom = '" . $this->db->escape($this->name) . "'";
  598. $sql.= ", entity = " . $this->db->escape($entity);
  599. $sql.= ", note = '" . $this->db->escape($this->note) . "'";
  600. $sql.= " WHERE rowid = " . $this->id;
  601. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  602. $resql = $this->db->query($sql);
  603. if ($resql)
  604. {
  605. $action='update';
  606. // Actions on extra fields (by external module or standard code)
  607. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  608. {
  609. $result=$this->insertExtraFields();
  610. if ($result < 0)
  611. {
  612. $error++;
  613. }
  614. }
  615. if (! $error && ! $notrigger)
  616. {
  617. // Call trigger
  618. $result=$this->call_trigger('GROUP_MODIFY',$user);
  619. if ($result < 0) { $error++; }
  620. // End call triggers
  621. }
  622. if (! $error)
  623. {
  624. $this->db->commit();
  625. return 1;
  626. }
  627. else
  628. {
  629. $this->db->rollback();
  630. return -$error;
  631. }
  632. }
  633. else
  634. {
  635. $this->db->rollback();
  636. dol_print_error($this->db);
  637. return -1;
  638. }
  639. }
  640. /**
  641. * Return label of status of user (active, inactive)
  642. *
  643. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  644. * @return string Label of status
  645. */
  646. function getLibStatut($mode=0)
  647. {
  648. return $this->LibStatut(0,$mode);
  649. }
  650. /**
  651. * Renvoi le libelle d'un statut donne
  652. *
  653. * @param int $statut Id statut
  654. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  655. * @return string Label of status
  656. */
  657. function LibStatut($statut,$mode=0)
  658. {
  659. global $langs;
  660. $langs->load('users');
  661. return '';
  662. }
  663. /**
  664. * Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
  665. *
  666. * @param array $info Info array loaded by _load_ldap_info
  667. * @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
  668. * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
  669. * 2=Return key only (uid=qqq)
  670. * @return string DN
  671. */
  672. function _load_ldap_dn($info,$mode=0)
  673. {
  674. global $conf;
  675. $dn='';
  676. if ($mode==0) $dn=$conf->global->LDAP_KEY_GROUPS."=".$info[$conf->global->LDAP_KEY_GROUPS].",".$conf->global->LDAP_GROUP_DN;
  677. if ($mode==1) $dn=$conf->global->LDAP_GROUP_DN;
  678. if ($mode==2) $dn=$conf->global->LDAP_KEY_GROUPS."=".$info[$conf->global->LDAP_KEY_GROUPS];
  679. return $dn;
  680. }
  681. /**
  682. * Initialize the info array (array of LDAP values) that will be used to call LDAP functions
  683. *
  684. * @return array Tableau info des attributs
  685. */
  686. function _load_ldap_info()
  687. {
  688. global $conf,$langs;
  689. $info=array();
  690. // Object classes
  691. $info["objectclass"]=explode(',',$conf->global->LDAP_GROUP_OBJECT_CLASS);
  692. // Champs
  693. if ($this->name && ! empty($conf->global->LDAP_GROUP_FIELD_FULLNAME)) $info[$conf->global->LDAP_GROUP_FIELD_FULLNAME] = $this->name;
  694. //if ($this->name && ! empty($conf->global->LDAP_GROUP_FIELD_NAME)) $info[$conf->global->LDAP_GROUP_FIELD_NAME] = $this->name;
  695. if ($this->note && ! empty($conf->global->LDAP_GROUP_FIELD_DESCRIPTION)) $info[$conf->global->LDAP_GROUP_FIELD_DESCRIPTION] = $this->note;
  696. if (! empty($conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS))
  697. {
  698. $valueofldapfield=array();
  699. foreach($this->members as $key=>$val) // This is array of users for group into dolibarr database.
  700. {
  701. $muser=new User($this->db);
  702. $muser->fetch($val->id);
  703. $info2 = $muser->_load_ldap_info();
  704. $valueofldapfield[] = $muser->_load_ldap_dn($info2);
  705. }
  706. $info[$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS] = (!empty($valueofldapfield)?$valueofldapfield:'');
  707. }
  708. return $info;
  709. }
  710. /**
  711. * Initialise an instance with random values.
  712. * Used to build previews or test instances.
  713. * id must be 0 if object instance is a specimen.
  714. *
  715. * @return void
  716. */
  717. function initAsSpecimen()
  718. {
  719. global $conf, $user, $langs;
  720. // Initialise parametres
  721. $this->id=0;
  722. $this->ref = 'SPECIMEN';
  723. $this->specimen=1;
  724. $this->name='DOLIBARR GROUP SPECIMEN';
  725. $this->note='This is a note';
  726. $this->datec=time();
  727. $this->datem=time();
  728. // Members of this group is just me
  729. $this->members=array(
  730. $user->id => $user
  731. );
  732. }
  733. /**
  734. * Create a document onto disk according to template module.
  735. *
  736. * @param string $modele Force model to use ('' to not force)
  737. * @param Translate $outputlangs Object langs to use for output
  738. * @param int $hidedetails Hide details of lines
  739. * @param int $hidedesc Hide description
  740. * @param int $hideref Hide ref
  741. * @return int 0 if KO, 1 if OK
  742. */
  743. public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
  744. {
  745. global $conf,$user,$langs;
  746. $langs->load("user");
  747. // Positionne le modele sur le nom du modele a utiliser
  748. if (! dol_strlen($modele))
  749. {
  750. if (! empty($conf->global->USERGROUP_ADDON_PDF))
  751. {
  752. $modele = $conf->global->USERGROUP_ADDON_PDF;
  753. }
  754. else
  755. {
  756. $modele = 'grass';
  757. }
  758. }
  759. $modelpath = "core/modules/usergroup/doc/";
  760. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
  761. }
  762. }