diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 905d8cbc2c5..355a6dc7684 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2111,7 +2111,9 @@ abstract class CommonObject $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 (empty($user->admin) || !empty($user->entity) || $conf->entity != 1) { + $sql .= " INNER JOIN ".MAIN_DB_PREFIX."usergroup_user as ug ON ug.fk_user = te.rowid"; + } } if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) { $tmparray = explode('@', $this->ismultientitymanaged); @@ -2148,7 +2150,6 @@ abstract class CommonObject 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').")"; } } else { @@ -2181,7 +2182,9 @@ 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 (empty($user->admin) || !empty($user->entity) || $conf->entity != 1) { + $sql .= " INNER JOIN ".MAIN_DB_PREFIX."usergroup_user as ug ON ug.fk_user = te.rowid"; + } } if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) { $tmparray = explode('@', $this->ismultientitymanaged); @@ -2218,7 +2221,6 @@ abstract class CommonObject 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').")"; } } else {