From cdf4292415f257feeaa0d25b49ca6f34602cb343 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Wed, 11 Oct 2023 11:38:56 +0200 Subject: [PATCH] FIX remove DISTINCT when MULTICOMPANY_TRANSVERSE_MODE is used (#26143) * FIX (WIP) remove DISTINCT when MULTICOMPANY_TRANSVERSE_MODE is used * FIX optimize code * FIX replace with $this->db->prefix() * FIX works on uniformize MULTICOMPANY_TRANSVERSE_MODE * FIX syntax error --- htdocs/comm/action/peruser.php | 22 ++++++++++-------- htdocs/core/class/commonobject.class.php | 16 ++++--------- htdocs/core/class/html.form.class.php | 26 ++++++++++------------ htdocs/core/class/html.formother.class.php | 4 ++-- htdocs/core/class/ldap.class.php | 4 ++-- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/htdocs/comm/action/peruser.php b/htdocs/comm/action/peruser.php index bbc78dc5ec1..a80f787e0ac 100644 --- a/htdocs/comm/action/peruser.php +++ b/htdocs/comm/action/peruser.php @@ -900,22 +900,26 @@ while ($currentdaytoshow < $lastdaytoshow) { } } else { /* Use this list to have for all users */ - $sql = "SELECT DISTINCT u.rowid, u.lastname as lastname, u.firstname, u.statut, u.login, u.admin, u.entity"; - $sql .= " FROM ".MAIN_DB_PREFIX."user as u"; - if (isModEnabled('multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { - $sql .= ", ".MAIN_DB_PREFIX."usergroup_user as ug"; + $sql = "SELECT u.rowid, u.lastname as lastname, u.firstname, u.statut, u.login, u.admin, u.entity"; + $sql .= " FROM ".$db->prefix()."user as u"; + if (isModEnabled('multicompany') && getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE')) { + $sql .= " WHERE u.rowid IN ("; + $sql .= " SELECT ug.fk_user FROM ".$db->prefix()."usergroup_user as ug"; $sql .= " WHERE ug.entity IN (".getEntity('usergroup').")"; - $sql .= " AND ug.fk_user = u.rowid "; + if ($usergroup > 0) { + $sql .= " AND ug.fk_usergroup = ".((int) $usergroup); + } + $sql .= ")"; } else { if ($usergroup > 0) { - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."usergroup_user as ug ON u.rowid = ug.fk_user"; + $sql .= " LEFT JOIN ".$db->prefix()."usergroup_user as ug ON u.rowid = ug.fk_user"; } $sql .= " WHERE u.entity IN (".getEntity('user').")"; + if ($usergroup > 0) { + $sql .= " AND ug.fk_usergroup = ".((int) $usergroup); + } } $sql .= " AND u.statut = 1"; - if ($usergroup > 0) { - $sql .= " AND ug.fk_usergroup = ".((int) $usergroup); - } if ($user->socid > 0) { // External users should see only contacts of their company $sql .= " AND u.fk_soc = ".((int) $user->socid); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index bafb19b13f8..8a6b5c9907a 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2136,9 +2136,6 @@ abstract class CommonObject $restrictiononfksoc = empty($this->restrictiononfksoc) ? 0 : $this->restrictiononfksoc; $sql = "SELECT MAX(te.".$fieldid.")"; $sql .= " FROM ".(empty($nodbprefix) ?$this->db->prefix():'').$this->table_element." as te"; - if ($this->element == 'user' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { - $sql .= ",".$this->db->prefix()."usergroup_user as ug"; - } if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) { $tmparray = explode('@', $this->ismultientitymanaged); $sql .= ", ".$this->db->prefix().$tmparray[1]." as ".($tmparray[1] == 'societe' ? 's' : 'parenttable'); // If we need to link to this table to limit select to entity @@ -2174,12 +2171,11 @@ abstract class CommonObject $sql .= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to socid } if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) { - if ($this->element == 'user' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { + if ($this->element == 'user' && getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE')) { if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1) { $sql .= " AND te.entity IS NOT NULL"; // Show all users } else { - $sql .= " AND ug.fk_user = te.rowid"; - $sql .= " AND ug.entity IN (".getEntity('usergroup').")"; + $sql .= " AND te.rowid IN (SELECT ug.fk_user FROM ".$this->db->prefix()."usergroup_user as ug WHERE ug.entity IN (".getEntity('usergroup')."))"; } } else { $sql .= ' AND te.entity IN ('.getEntity($this->element).')'; @@ -2210,9 +2206,6 @@ abstract class CommonObject $sql = "SELECT MIN(te.".$fieldid.")"; $sql .= " FROM ".(empty($nodbprefix) ?$this->db->prefix():'').$this->table_element." as te"; - if ($this->element == 'user' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { - $sql .= ",".$this->db->prefix()."usergroup_user as ug"; - } if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) { $tmparray = explode('@', $this->ismultientitymanaged); $sql .= ", ".$this->db->prefix().$tmparray[1]." as ".($tmparray[1] == 'societe' ? 's' : 'parenttable'); // If we need to link to this table to limit select to entity @@ -2248,12 +2241,11 @@ abstract class CommonObject $sql .= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to socid } if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) { - if ($this->element == 'user' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { + if ($this->element == 'user' && getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE')) { if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1) { $sql .= " AND te.entity IS NOT NULL"; // Show all users } else { - $sql .= " AND ug.fk_user = te.rowid"; - $sql .= " AND ug.entity IN (".getEntity('usergroup').")"; + $sql .= " AND te.rowid IN (SELECT ug.fk_user FROM ".$this->db->prefix()."usergroup_user as ug WHERE ug.entity IN (".getEntity('usergroup')."))"; } } else { $sql .= ' AND te.entity IN ('.getEntity($this->element).')'; diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index fa0236a608e..9677edc0df3 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -2009,7 +2009,7 @@ class Form * @return array|string HTML select string * @see select_dolgroups() */ - public function select_dolusers($selected = '', $htmlname = 'userid', $show_empty = 0, $exclude = null, $disabled = 0, $include = '', $enableonly = '', $force_entity = '0', $maxlength = 0, $showstatus = 0, $morefilter = '', $show_every = 0, $enableonlytext = '', $morecss = '', $notdisabled = 0, $outputmode = 0, $multiple = false, $forcecombo = 0) + public function select_dolusers($selected = '', $htmlname = 'userid', $show_empty = 0, $exclude = null, $disabled = 0, $include = '', $enableonly = '', $force_entity = 0, $maxlength = 0, $showstatus = 0, $morefilter = '', $show_every = 0, $enableonlytext = '', $morecss = '', $notdisabled = 0, $outputmode = 0, $multiple = false, $forcecombo = 0) { // phpcs:enable global $conf, $user, $langs, $hookmanager; @@ -2056,18 +2056,16 @@ class Form $sql .= " FROM " . $this->db->prefix() . "user as u"; if (isModEnabled('multicompany') && $conf->entity == 1 && $user->admin && !$user->entity) { $sql .= " LEFT JOIN " . $this->db->prefix() . "entity as e ON e.rowid = u.entity"; - if ($force_entity) { + if (!empty($force_entity)) { $sql .= " WHERE u.entity IN (0, " . $this->db->sanitize($force_entity) . ")"; } else { $sql .= " WHERE u.entity IS NOT NULL"; } } else { - if (isModEnabled('multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { - $sql .= " LEFT JOIN " . $this->db->prefix() . "usergroup_user as ug"; - $sql .= " ON ug.fk_user = u.rowid"; - $sql .= " WHERE ug.entity = " . (int) $conf->entity; + if (isModEnabled('multicompany') && getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE')) { + $sql .= " WHERE u.rowid IN (SELECT ug.fk_user FROM ".$this->db->prefix()."usergroup_user as ug WHERE ug.entity IN (".getEntity('usergroup')."))"; } else { - $sql .= " WHERE u.entity IN (0, " . ((int) $conf->entity) . ")"; + $sql .= " WHERE u.entity IN (" . getEntity('user') . ")"; } } if (!empty($user->socid)) { @@ -2173,8 +2171,8 @@ class Form $moreinfohtml .= ($moreinfohtml ? ' - ' : ' (') . $langs->trans('Disabled'); } } - if (isModEnabled('multicompany') && empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && $user->admin && !$user->entity) { - if (!$obj->entity) { + if (isModEnabled('multicompany') && !getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE') && $conf->entity == 1 && !empty($user->admin) && empty($user->entity)) { + if (empty($obj->entity)) { $moreinfo .= ($moreinfo ? ' - ' : ' (') . $langs->trans("AllEntities"); $moreinfohtml .= ($moreinfohtml ? ' - ' : ' (') . $langs->trans("AllEntities"); } else { @@ -2184,9 +2182,9 @@ class Form } } } - $moreinfo .= ($moreinfo ? ')' : ''); - $moreinfohtml .= ($moreinfohtml ? ')' : ''); - if ($disableline && $disableline != '1') { + $moreinfo .= (!empty($moreinfo) ? ')' : ''); + $moreinfohtml .= (!empty($moreinfohtml) ? ')' : ''); + if (!empty($disableline) && $disableline != '1') { // Add text from $enableonlytext parameter $moreinfo .= ' - ' . $disableline; $moreinfohtml .= ' - ' . $disableline; @@ -2195,7 +2193,7 @@ class Form $labeltoshowhtml .= $moreinfohtml; $out .= '