]> source.dussan.org Git - nextcloud-server.git/commitdiff
UserManager can now count disabled users
authorArthur Schiwon <blizzz@arthur-schiwon.de>
Wed, 26 Apr 2017 10:37:48 +0000 (12:37 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Sat, 29 Apr 2017 03:59:09 +0000 (00:59 -0300)
Users page takes advantage of that

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
lib/private/User/Manager.php
lib/public/IUserManager.php
settings/users.php
tests/lib/User/ManagerTest.php

index 4a87dc7161c32cdb0e1cb3f80787c4103cc77f80..0477f23e552e0227b8ca746721e5f287f6460063 100644 (file)
@@ -408,6 +408,28 @@ class Manager extends PublicEmitter implements IUserManager {
                }
        }
 
+       /**
+        * returns how many users have logged in once
+        *
+        * @return int
+        * @since 12.0.0
+        */
+       public function countDisabledUsers() {
+               $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
+               $queryBuilder->select($queryBuilder->createFunction('COUNT(*)'))
+                       ->from('preferences')
+                       ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
+                       ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled')))
+                       ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false')));
+
+               $query = $queryBuilder->execute();
+
+               $result = (int)$query->fetchColumn();
+               $query->closeCursor();
+
+               return $result;
+       }
+
        /**
         * returns how many users have logged in once
         *
index 1ec392dfd826497204c3e7176d6c28bdb7491eaf..6c6724487de3ead5a28556820cfe7f19b837238f 100644 (file)
@@ -154,6 +154,14 @@ interface IUserManager {
         */
        public function callForAllUsers(\Closure $callback, $search = '');
 
+       /**
+        * returns how many users have logged in once
+        *
+        * @return int
+        * @since 11.0.0
+        */
+       public function countDisabledUsers();
+
        /**
         * returns how many users have logged in once
         *
index 9f3433aa33a8492edacb8eada909435274e653b1..4d214bf950226a5dc336f295aec1051e51b63069 100644 (file)
@@ -45,6 +45,7 @@ $groupManager = \OC::$server->getGroupManager();
 // Set the sort option: SORT_USERCOUNT or SORT_GROUPNAME
 $sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT;
 
+$isLDAPUsed = false;
 if (\OC_App::isEnabled('user_ldap')) {
        $isLDAPUsed =
                   $groupManager->isBackendUsed('\OCA\User_LDAP\Group_LDAP')
@@ -95,17 +96,13 @@ if($isAdmin) {
        }
        $subAdmins = false;
 }
-$disabledUsers = 0;
-foreach (OC_User::getUsers() as $uid) {
-       if(!$userManager->get($uid)->isEnabled()) {
-               $disabledUsers++;
-       }
-}
-$disabledUsersGroup = array(
+
+$disabledUsers = $isLDAPUsed ? 0 : $userManager->countDisabledUsers();
+$disabledUsersGroup = [
        'id' => '_disabledUsers',
        'name' => '_disabledUsers',
        'usercount' => $disabledUsers
-);
+];
 
 // load preset quotas
 $quotaPreset=$config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB');
index aaee64f18638223f091a4c9ad3381c5a7c5549e1..cf725aae6719be1af0ffdad0a56ff24b15706cdc 100644 (file)
@@ -510,6 +510,31 @@ class ManagerTest extends TestCase {
                $this->assertEquals(7 + 16, $users);
        }
 
+       public function testCountUsersOnlyDisabled() {
+               $manager = \OC::$server->getUserManager();
+               // count other users in the db before adding our own
+               $countBefore = $manager->countDisabledUsers();
+
+               //Add test users
+               $user1 = $manager->createUser('testdisabledcount1', 'testdisabledcount1');
+
+               $user2 = $manager->createUser('testdisabledcount2', 'testdisabledcount2');
+               $user2->setEnabled(false);
+
+               $user3 = $manager->createUser('testdisabledcount3', 'testdisabledcount3');
+
+               $user4 = $manager->createUser('testdisabledcount4', 'testdisabledcount4');
+               $user4->setEnabled(false);
+
+               $this->assertEquals($countBefore + 2, $manager->countDisabledUsers());
+
+               //cleanup
+               $user1->delete();
+               $user2->delete();
+               $user3->delete();
+               $user4->delete();
+       }
+
        public function testCountUsersOnlySeen() {
                $manager = \OC::$server->getUserManager();
                // count other users in the db before adding our own