From 7b2fbdd638d96f0c5e479914cd79247b484d102b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 5 Oct 2015 10:04:34 +0200 Subject: [PATCH] FIX Can't change the admin with default setup --- htdocs/langs/en_US/users.lang | 3 ++- htdocs/user/card.php | 14 ++++++++++---- htdocs/user/class/user.class.php | 16 +++++++++------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/htdocs/langs/en_US/users.lang b/htdocs/langs/en_US/users.lang index e6764df49a2..81e1d48d46c 100644 --- a/htdocs/langs/en_US/users.lang +++ b/htdocs/langs/en_US/users.lang @@ -44,7 +44,8 @@ ListOfUsers=List of users Administrator=Administrator SuperAdministrator=Super Administrator SuperAdministratorDesc=Global administrator -AdministratorDesc=Administrator's entity +AdministratorDesc=Administrator +AdministratorDescEntity=Administrator (for its company) DefaultRights=Default permissions DefaultRightsDesc=Define here default permissions that are automatically granted to a new created user (Go on user card to change permission of an existing user). DolibarrUsers=Dolibarr users diff --git a/htdocs/user/card.php b/htdocs/user/card.php index 4cfb2a96562..5d63c755417 100644 --- a/htdocs/user/card.php +++ b/htdocs/user/card.php @@ -1883,10 +1883,16 @@ else else { print ''; - $nbSuperAdmin = $user->getNbOfUsers('superadmin'); - if ($user->admin - && ($user->id != $object->id) // Don't downgrade ourself - && ($object->entity > 0 || $nbSuperAdmin > 1) // Don't downgrade a superadmin if alone + $nbAdmin = $user->getNbOfUsers('active','',1); + $nbSuperAdmin = $user->getNbOfUsers('active','superadmin',1); + //var_dump($nbAdmin); + //var_dump($nbSuperAdmin); + if ($user->admin // Need to be admin to allow downgrade of an admin + && ($user->id != $object->id) // Don't downgrade ourself + && ( + (empty($conf->multicompany->enabled) && $nbAdmin > 1) + || (! empty($conf->multicompany->enabled) && ($object->entity > 0 || $nbSuperAdmin > 1)) // Don't downgrade a superadmin if alone + ) ) { print $form->selectyesno('admin',$object->admin,1); diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index 5f5fd60bd9b..b1b7819985c 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -2195,25 +2195,27 @@ class User extends CommonObject /** * Return number of existing users * - * @param string $limitTo Limit to 'active' or 'superadmin' users - * @param bool $all Return for all entities + * @param string $limitTo Limit to '' or 'active' + * @param string $option 'superadmin' = return for entity 0 only + * @param int $admin Filter on admin tag * @return int Number of users */ - function getNbOfUsers($limitTo='active', $all=false) + function getNbOfUsers($limitTo, $option='', $admin=-1) { global $conf; $sql = "SELECT count(rowid) as nb"; $sql.= " FROM ".MAIN_DB_PREFIX."user"; - if ($limitTo == 'superadmin') + if ($option == 'superadmin') { $sql.= " WHERE entity = 0"; + if ($admin >= 0) $sql.= " AND admin = ".$admin; } else { - if ($all) $sql.= " WHERE entity > 0"; // all users except superadmins - else $sql.= " WHERE entity = ".$conf->entity; + $sql.=" WHERE entity IN (".getEntity('user',0).")"; if ($limitTo == 'active') $sql.= " AND statut = 1"; + if ($admin >= 0) $sql.= " AND admin = ".$admin; } $resql=$this->db->query($sql); @@ -2227,7 +2229,7 @@ class User extends CommonObject } else { - $this->error=$this->db->error(); + $this->error=$this->db->lasterror(); return -1; } }