usergroup.class.php 23 KB

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