31 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
33 require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
45 public $element =
'usergroup';
50 public $table_element =
'usergroup';
56 public $ismultientitymanaged = 1;
61 public $picto =
'group';
101 public $members = array();
106 private $_tab_loaded = array();
111 public $all_permissions_are_loaded;
115 public $fields = array(
116 'rowid'=>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'index'=>1,
'position'=>1,
'comment'=>
'Id'),
117 'entity' => array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'notnull'=> 1,
'default'=>1,
'index'=>1,
'position'=>5),
118 'nom'=>array(
'type'=>
'varchar(180)',
'label'=>
'Name',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'showoncombobox'=>1,
'index'=>1,
'position'=>10,
'searchall'=>1,
'comment'=>
'Group name'),
119 'note' => array(
'type'=>
'html',
'label'=>
'Description',
'enabled'=>1,
'visible'=>1,
'position'=>20,
'notnull'=>-1,
'searchall'=>1),
120 'datec' => array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>50,
'notnull'=>1,),
121 'tms' => array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'position'=>60,
'notnull'=>1,),
122 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'ModelPDF',
'enabled'=>1,
'visible'=>0,
'position'=>100),
128 public $fk_element =
'fk_usergroup';
133 protected $childtables = array();
138 protected $childtablesoncascade = array(
'usergroup_rights',
'usergroup_user');
149 $this->nb_rights = 0;
161 public function fetch($id =
'', $groupname =
'', $load_members =
false)
165 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
166 if (!empty($groupname)) {
167 $result = $this->
fetchCommon(0,
'',
' AND nom = \''.$this->db->escape($groupname).
'\'');
172 $this->
name = $this->nom;
181 $this->error = $this->db->lasterror();
200 $sql =
"SELECT g.rowid, ug.entity as usergroup_entity";
201 $sql .=
" FROM ".$this->db->prefix().
"usergroup as g,";
202 $sql .=
" ".$this->db->prefix().
"usergroup_user as ug";
203 $sql .=
" WHERE ug.fk_usergroup = g.rowid";
204 $sql .=
" AND ug.fk_user = ".((int) $userid);
205 if (
isModEnabled(
'multicompany') && $conf->entity == 1 && $user->admin && !$user->entity) {
206 $sql .=
" AND g.entity IS NOT NULL";
208 $sql .=
" AND g.entity IN (0,".$conf->entity.
")";
210 $sql .=
" ORDER BY g.nom";
212 dol_syslog(get_class($this).
"::listGroupsForUser", LOG_DEBUG);
213 $result = $this->db->query(
$sql);
215 while ($obj = $this->db->fetch_object($result)) {
216 if (!array_key_exists($obj->rowid, $ret)) {
218 $newgroup->fetch($obj->rowid,
'', $load_members);
219 $ret[$obj->rowid] = $newgroup;
222 $ret[$obj->rowid]->usergroup_entity[] = $obj->usergroup_entity;
225 $this->db->free($result);
229 $this->error = $this->db->lasterror();
247 $sql =
"SELECT u.rowid, u.login, u.lastname, u.firstname, u.photo, u.fk_soc, u.entity, u.employee, u.email";
248 if (!empty($this->
id)) {
249 $sql .=
", ug.entity as usergroup_entity";
251 $sql .=
" FROM ".$this->db->prefix().
"user as u";
252 if (!empty($this->
id)) {
253 $sql .=
", ".$this->db->prefix().
"usergroup_user as ug";
255 $sql .=
" WHERE 1 = 1";
256 if (!empty($this->
id)) {
257 $sql .=
" AND ug.fk_user = u.rowid";
259 if (!empty($this->
id)) {
260 $sql .=
" AND ug.fk_usergroup = ".((int) $this->
id);
262 if (
isModEnabled(
'multicompany') && $conf->entity == 1 && $user->admin && !$user->entity) {
263 $sql .=
" AND u.entity IS NOT NULL";
265 $sql .=
" AND u.entity IN (0,".$conf->entity.
")";
267 if (!empty($excludefilter)) {
268 $sql .=
' AND ('.$excludefilter.
')';
271 dol_syslog(get_class($this).
"::listUsersForGroup", LOG_DEBUG);
272 $resql = $this->db->query(
$sql);
275 while ($obj = $this->db->fetch_object($resql)) {
276 if (!array_key_exists($obj->rowid, $ret)) {
278 $newuser =
new User($this->db);
280 $newuser->id = $obj->rowid;
281 $newuser->login = $obj->login;
282 $newuser->photo = $obj->photo;
283 $newuser->lastname = $obj->lastname;
284 $newuser->firstname = $obj->firstname;
285 $newuser->email = $obj->email;
286 $newuser->socid = $obj->fk_soc;
287 $newuser->entity = $obj->entity;
288 $newuser->employee = $obj->employee;
290 $ret[$obj->rowid] = $newuser;
292 $ret[$obj->rowid] = $obj->rowid;
295 if ($mode != 1 && !empty($obj->usergroup_entity)) {
296 $ret[$obj->rowid]->usergroup_entity[] = $obj->usergroup_entity;
300 $this->db->free($resql);
304 $this->error = $this->db->lasterror();
318 public function addrights($rid, $allmodule =
'', $allperms =
'', $entity = 0)
320 global $conf, $user, $langs;
322 $entity = (!empty($entity) ? $entity : $conf->entity);
324 dol_syslog(get_class($this).
"::addrights $rid, $allmodule, $allperms, $entity");
331 $module = $perms = $subperms =
'';
335 $sql =
"SELECT module, perms, subperms";
336 $sql .=
" FROM ".$this->db->prefix().
"rights_def";
337 $sql .=
" WHERE id = ".((int) $rid);
338 $sql .=
" AND entity = ".((int) $entity);
340 $result = $this->db->query(
$sql);
342 $obj = $this->db->fetch_object($result);
344 $module = $obj->module;
345 $perms = $obj->perms;
346 $subperms = $obj->subperms;
354 $whereforadd =
"id=".((int) $rid);
357 $whereforadd .=
" OR (module='".$this->db->escape($module).
"' AND perms='".$this->db->escape($perms).
"' AND (subperms='lire' OR subperms='read'))";
359 $whereforadd .=
" OR (module='".$this->db->escape($module).
"' AND (perms='lire' OR perms='read') AND subperms IS NULL)";
363 if (!empty($allmodule)) {
364 if ($allmodule ==
'allmodules') {
365 $whereforadd =
'allmodules';
367 $whereforadd =
"module='".$this->db->escape($allmodule).
"'";
368 if (!empty($allperms)) {
369 $whereforadd .=
" AND perms='".$this->db->escape($allperms).
"'";
376 if (!empty($whereforadd)) {
379 $sql .=
" FROM ".$this->db->prefix().
"rights_def";
380 $sql .=
" WHERE entity = ".((int) $entity);
381 if (!empty($whereforadd) && $whereforadd !=
'allmodules') {
382 $sql .=
" AND ".$whereforadd;
385 $result = $this->db->query(
$sql);
387 $num = $this->db->num_rows($result);
390 $obj = $this->db->fetch_object($result);
393 $sql =
"DELETE FROM ".$this->db->prefix().
"usergroup_rights WHERE fk_usergroup = ".((int) $this->
id).
" AND fk_id=".((int) $nid).
" AND entity = ".((int) $entity);
394 if (!$this->db->query(
$sql)) {
397 $sql =
"INSERT INTO ".$this->db->prefix().
"usergroup_rights (entity, fk_usergroup, fk_id) VALUES (".((int) $entity).
", ".((int) $this->
id).
", ".((int) $nid).
")";
398 if (!$this->db->query(
$sql)) {
410 $langs->load(
"other");
411 $this->context = array(
'audit'=>$langs->trans(
"PermissionsAdd").($rid ?
' (id='.$rid.
')' :
''));
414 $result = $this->
call_trigger(
'USERGROUP_MODIFY', $user);
423 $this->db->rollback();
441 public function delrights($rid, $allmodule =
'', $allperms =
'', $entity = 0)
443 global $conf, $user, $langs;
448 $entity = (!empty($entity) ? $entity : $conf->entity);
453 $module = $perms = $subperms =
'';
457 $sql =
"SELECT module, perms, subperms";
458 $sql .=
" FROM ".$this->db->prefix().
"rights_def";
459 $sql .=
" WHERE id = ".((int) $rid);
460 $sql .=
" AND entity = ".((int) $entity);
462 $result = $this->db->query(
$sql);
464 $obj = $this->db->fetch_object($result);
466 $module = $obj->module;
467 $perms = $obj->perms;
468 $subperms = $obj->subperms;
476 $wherefordel =
"id = ".((int) $rid);
478 if ($subperms ==
'lire' || $subperms ==
'read') {
479 $wherefordel .=
" OR (module='".$this->db->escape($module).
"' AND perms='".$this->db->escape($perms).
"' AND subperms IS NOT NULL)";
481 if ($perms ==
'lire' || $perms ==
'read') {
482 $wherefordel .=
" OR (module='".$this->db->escape($module).
"')";
490 if (!empty($allmodule)) {
491 if ($allmodule ==
'allmodules') {
492 $wherefordel =
'allmodules';
494 $wherefordel =
"module='".$this->db->escape($allmodule).
"'";
495 if (!empty($allperms)) {
496 $wherefordel .=
" AND perms='".$this->db->escape($allperms).
"'";
503 if (!empty($wherefordel)) {
506 $sql .=
" FROM ".$this->db->prefix().
"rights_def";
507 $sql .=
" WHERE entity = ".((int) $entity);
508 if (!empty($wherefordel) && $wherefordel !=
'allmodules') {
509 $sql .=
" AND ".$wherefordel;
512 $result = $this->db->query(
$sql);
514 $num = $this->db->num_rows($result);
519 $obj = $this->db->fetch_object($result);
524 $sql =
"DELETE FROM ".$this->db->prefix().
"usergroup_rights";
525 $sql .=
" WHERE fk_usergroup = $this->id AND fk_id=".((int) $nid);
526 $sql .=
" AND entity = ".((int) $entity);
527 if (!$this->db->query(
$sql)) {
539 $langs->load(
"other");
540 $this->context = array(
'audit'=>$langs->trans(
"PermissionsDelete").($rid ?
' (id='.$rid.
')' :
''));
543 $result = $this->
call_trigger(
'USERGROUP_MODIFY', $user);
552 $this->db->rollback();
571 if ($moduletag && isset($this->_tab_loaded[$moduletag]) && $this->_tab_loaded[$moduletag]) {
576 if (!empty($this->all_permissions_are_loaded)) {
584 $sql =
"SELECT r.module, r.perms, r.subperms ";
585 $sql .=
" FROM ".$this->db->prefix().
"usergroup_rights as u, ".$this->db->prefix().
"rights_def as r";
586 $sql .=
" WHERE r.id = u.fk_id";
587 $sql .=
" AND r.entity = ".((int) $conf->entity);
588 $sql .=
" AND u.entity = ".((int) $conf->entity);
589 $sql .=
" AND u.fk_usergroup = ".((int) $this->
id);
590 $sql .=
" AND r.perms IS NOT NULL";
592 $sql .=
" AND r.module = '".$this->db->escape($moduletag).
"'";
595 dol_syslog(get_class($this).
'::getrights', LOG_DEBUG);
596 $resql = $this->db->query(
$sql);
598 $num = $this->db->num_rows($resql);
601 $obj = $this->db->fetch_object($resql);
604 $module = $obj->module;
605 $perms = $obj->perms;
606 $subperms = $obj->subperms;
609 if (!isset($this->rights)) {
610 $this->rights =
new stdClass();
612 if (!isset($this->rights->$module) || !is_object($this->rights->$module)) {
613 $this->rights->$module =
new stdClass();
616 if (!isset($this->rights->$module->$perms) || !is_object($this->rights->$module->$perms)) {
617 $this->rights->$module->$perms =
new stdClass();
619 if (empty($this->rights->$module->$perms->$subperms)) {
622 $this->rights->$module->$perms->$subperms = 1;
624 if (empty($this->rights->$module->$perms)) {
627 $this->rights->$module->$perms = 1;
634 $this->db->free($resql);
637 if ($moduletag ==
'') {
640 $this->all_permissions_are_loaded = 1;
643 $this->_tab_loaded[$moduletag] = 1;
655 public function delete(
User $user)
671 if (!empty($this->
name)) {
672 $this->nom = $this->name;
675 if (!isset($this->entity)) {
676 $this->entity = $conf->entity;
692 if (!empty($this->
name)) {
693 $this->nom = $this->name;
723 $langs->load(
'users');
736 global $conf, $langs, $menumanager;
738 $option = $params[
'option'] ??
'';
741 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
742 $langs->load(
"users");
743 return [
'optimize' => $langs->trans(
"ShowGroup")];
745 $datas[
'divopen'] =
'<div class="centpercent">';
746 $datas[
'picto'] =
img_picto(
'',
'group').
' <u>'.$langs->trans(
"Group").
'</u><br>';
747 $datas[
'name'] =
'<b>'.$langs->trans(
'Name').
':</b> '.$this->name;
748 $datas[
'description'] =
'<br><b>'.$langs->trans(
"Description").
':</b> '.$this->note;
749 $datas[
'divclose'] =
'</div>';
765 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
767 global $langs, $conf, $db, $hookmanager;
768 global $dolibarr_main_authentication, $dolibarr_main_demo;
771 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) && $withpicto) {
778 'objecttype' => $this->element,
781 $classfortooltip =
'classfortooltip';
784 $classfortooltip =
'classforajaxtooltip';
785 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
791 if ($option ==
'permissions') {
792 $url = DOL_URL_ROOT.
'/user/group/perms.php?id='.$this->id;
794 $url = DOL_URL_ROOT.
'/user/group/card.php?id='.$this->id;
797 if ($option !=
'nolink') {
799 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
800 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
801 $add_save_lastsearch_values = 1;
803 if ($add_save_lastsearch_values) {
804 $url .=
'&save_lastsearch_values=1';
809 if (empty($notooltip)) {
810 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
811 $langs->load(
"users");
812 $label = $langs->trans(
"ShowGroup");
813 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1, 1).
'"';
815 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
816 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
819 $linkstart =
'<a href="'.$url.
'"';
820 $linkstart .= $linkclose.
'>';
823 $result = $linkstart;
825 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
827 if ($withpicto != 2) {
828 $result .= $this->name;
833 $hookmanager->initHooks(array(
'groupdao'));
834 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
835 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
837 $result = $hookmanager->resPrint;
839 $result .= $hookmanager->resPrint;
862 $dn = $conf->global->LDAP_KEY_GROUPS.
"=".$info[$conf->global->LDAP_KEY_GROUPS].
",".$conf->global->LDAP_GROUP_DN;
865 $dn = $conf->global->LDAP_GROUP_DN;
868 $dn = $conf->global->LDAP_KEY_GROUPS.
"=".$info[$conf->global->LDAP_KEY_GROUPS];
889 $info[
"objectclass"] = explode(
',', $conf->global->LDAP_GROUP_OBJECT_CLASS);
892 if ($this->
name && !empty($conf->global->LDAP_GROUP_FIELD_FULLNAME)) {
893 $info[$conf->global->LDAP_GROUP_FIELD_FULLNAME] = $this->name;
896 if ($this->note && !empty($conf->global->LDAP_GROUP_FIELD_DESCRIPTION)) {
899 if (!empty($conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS)) {
900 $valueofldapfield = array();
901 foreach ($this->members as $key => $val) {
902 $muser =
new User($this->db);
903 $muser->fetch($val->id);
904 $info2 = $muser->_load_ldap_info();
905 $valueofldapfield[] = $muser->_load_ldap_dn($info2);
907 $info[$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS] = (!empty($valueofldapfield) ? $valueofldapfield :
'');
909 if (!empty($conf->global->LDAP_GROUP_FIELD_GROUPID)) {
910 $info[$conf->global->LDAP_GROUP_FIELD_GROUPID] = $this->id;
925 global $conf, $user, $langs;
929 $this->
ref =
'SPECIMEN';
932 $this->
name =
'DOLIBARR GROUP SPECIMEN';
933 $this->note =
'This is a note';
934 $this->datec = time();
938 $this->members = array(
954 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
956 global $conf, $user, $langs;
958 $langs->load(
"user");
962 if (!empty($conf->global->USERGROUP_ADDON_PDF)) {
963 $modele = $conf->global->USERGROUP_ADDON_PDF;
969 $modelpath =
"core/modules/usergroup/doc/";
971 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
985 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
987 $return =
'<div class="box-flex-item box-flex-grow-zero">';
988 $return .=
'<div class="info-box info-box-sm">';
989 $return .=
'<span class="info-box-icon bg-infobox-action">';
991 $return .=
'</span>';
992 $return .=
'<div class="info-box-content">';
993 $return .=
'<span class="info-box-ref">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
994 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
995 if (property_exists($this,
'members')) {
996 $return .=
'<br><span class="info-box-status opacitymedium">'.(empty($this->nb_users) ? 0 : $this->nb_users).
' '.$langs->trans(
'Users').
'</span>';
998 if (property_exists($this,
'nb_rights')) {
999 $return .=
'<br><div class="info-box-status margintoponly opacitymedium">'.$langs->trans(
'NbOfPermissions').
' : '.(empty($this->nb_rights) ? 0 : $this->nb_rights).
'</div>';
1001 $return .=
'</div>';
1002 $return .=
'</div>';
1003 $return .=
'</div>';