[Pkg-owncloud-commits] [owncloud] 16/69: Fix everyone count for subadmins

David Prévot taffit at moszumanska.debian.org
Wed Nov 11 02:04:01 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch stable8
in repository owncloud.

commit e5035494a7184e90cb264bc82dfadbd83f00a54d
Author: Vincent Petry <pvince81 at owncloud.com>
Date:   Thu Oct 29 16:40:39 2015 +0100

    Fix everyone count for subadmins
    
    Also moved the logic to the UsersController
    
    Backport of 781bca2437628d2f932abd60c5dcec0ece4504e3 from master,
    adjusted to use the old SubadminFactory way
---
 settings/ajax/geteveryonecount.php                | 50 ----------------
 settings/controller/userscontroller.php           | 41 ++++++++++++++
 settings/js/users/groups.js                       |  6 +-
 settings/routes.php                               |  3 +-
 tests/settings/controller/userscontrollertest.php | 69 +++++++++++++++++++++++
 5 files changed, 114 insertions(+), 55 deletions(-)

diff --git a/settings/ajax/geteveryonecount.php b/settings/ajax/geteveryonecount.php
deleted file mode 100644
index 659c846..0000000
--- a/settings/ajax/geteveryonecount.php
+++ /dev/null
@@ -1,50 +0,0 @@
-<?php
-/**
- * @author Clark Tomlinson <fallen013 at gmail.com>
- * @author Lukas Reschke <lukas at owncloud.com>
- * @author Morris Jobke <hey at morrisjobke.de>
- *
- * @copyright Copyright (c) 2015, ownCloud, Inc.
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program.  If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-OC_JSON::callCheck();
-OC_JSON::checkSubAdminUser();
-
-$userCount = 0;
-
-$currentUser = \OC::$server->getUserSession()->getUser()->getUID();
-
-if (!OC_User::isAdminUser($currentUser)) {
-	$groups = OC_SubAdmin::getSubAdminsGroups($currentUser);
-
-	foreach ($groups as $group) {
-		$userCount += count(OC_Group::usersInGroup($group));
-
-	}
-} else {
-
-	$userCountArray = \OC::$server->getUserManager()->countUsers();
-
-	if (!empty($userCountArray)) {
-		foreach ($userCountArray as $classname => $usercount) {
-			$userCount += $usercount;
-		}
-	}
-}
-
-
-OC_JSON::success(array('count' => $userCount));
diff --git a/settings/controller/userscontroller.php b/settings/controller/userscontroller.php
index d9b6ba7..4aaf23f 100644
--- a/settings/controller/userscontroller.php
+++ b/settings/controller/userscontroller.php
@@ -525,4 +525,45 @@ class UsersController extends Controller {
 		);
 	}
 
+	/**
+	 * Count all unique users visible for the current admin/subadmin.
+	 *
+	 * @NoAdminRequired
+	 *
+	 * @return DataResponse
+	 */
+	public function stats() {
+		$userCount = 0;
+		if ($this->isAdmin) {
+			$countByBackend = $this->userManager->countUsers();
+
+			if (!empty($countByBackend)) {
+				foreach ($countByBackend as $count) {
+					$userCount += $count;
+				}
+			}
+		} else {
+
+			$groupNames = $this->subAdminFactory->getSubAdminsOfGroups($this->userSession->getUser()->getUID());
+
+			$uniqueUsers = [];
+			foreach ($groupNames as $groupName) {
+				$group = $this->groupManager->get($groupName);
+				if (!is_null($group)) {
+					foreach($group->getUsers() as $uid => $displayName) {
+						$uniqueUsers[$uid] = true;
+					}
+				}
+			}
+
+			$userCount = count($uniqueUsers);
+		}
+
+		return new DataResponse(
+			[
+				'totalUsers' => $userCount
+			]
+		);
+	}
+
 }
diff --git a/settings/js/users/groups.js b/settings/js/users/groups.js
index c8d2ef7..2639191 100644
--- a/settings/js/users/groups.js
+++ b/settings/js/users/groups.js
@@ -304,10 +304,10 @@ GroupList = {
 		$.ajax({
 			type: "GET",
 			dataType: "json",
-			url: OC.generateUrl('/settings/ajax/geteveryonecount')
+			url: OC.generateUrl('/settings/users/stats')
 		}).success(function (data) {
-			$('#everyonegroup').data('usercount', data.count);
-			$('#everyonecount').text(data.count);
+			$('#everyonegroup').data('usercount', data.totalUsers);
+			$('#everyonecount').text(data.totalUsers);
 		});
 	}
 };
diff --git a/settings/routes.php b/settings/routes.php
index 10c3117..6ba3838 100644
--- a/settings/routes.php
+++ b/settings/routes.php
@@ -47,6 +47,7 @@ $application->registerRoutes($this, [
 		['name' => 'AppSettings#changeExperimentalConfigState', 'url' => '/settings/apps/experimental', 'verb' => 'POST'],
 		['name' => 'SecuritySettings#trustedDomains', 'url' => '/settings/admin/security/trustedDomains', 'verb' => 'POST'],
 		['name' => 'Users#setMailAddress', 'url' => '/settings/users/{id}/mailAddress', 'verb' => 'PUT'],
+		['name' => 'Users#stats', 'url' => '/settings/users/stats', 'verb' => 'GET'],
 		['name' => 'LogSettings#setLogLevel', 'url' => '/settings/admin/log/level', 'verb' => 'POST'],
 		['name' => 'LogSettings#getEntries', 'url' => '/settings/admin/log/entries', 'verb' => 'GET'],
 		['name' => 'LogSettings#download', 'url' => '/settings/admin/log/download', 'verb' => 'GET'],
@@ -69,8 +70,6 @@ $this->create('settings_admin', '/settings/admin')
 	->actionInclude('settings/admin.php');
 // Settings ajax actions
 // users
-$this->create('settings_ajax_everyonecount', '/settings/ajax/geteveryonecount')
-	->actionInclude('settings/ajax/geteveryonecount.php');
 $this->create('settings_ajax_setquota', '/settings/ajax/setquota.php')
 	->actionInclude('settings/ajax/setquota.php');
 $this->create('settings_ajax_togglegroups', '/settings/ajax/togglegroups.php')
diff --git a/tests/settings/controller/userscontrollertest.php b/tests/settings/controller/userscontrollertest.php
index 6fab43d..968718f 100644
--- a/tests/settings/controller/userscontrollertest.php
+++ b/tests/settings/controller/userscontrollertest.php
@@ -1462,4 +1462,73 @@ class UsersControllerTest extends \Test\TestCase {
 		$this->assertSame($responseCode, $response->getStatus());
 	}
 
+	public function testStatsAdmin() {
+		$this->container['IsAdmin'] = true;
+
+		$this->container['UserManager']
+			->expects($this->at(0))
+			->method('countUsers')
+			->will($this->returnValue([128, 44]));
+
+		$expectedResponse = new DataResponse(
+			[
+				'totalUsers' => 172
+			]
+		);
+		$response = $this->container['UsersController']->stats();
+		$this->assertEquals($expectedResponse, $response);
+	}
+
+	/**
+	 * Tests that the subadmin stats return unique users, even
+	 * when a user appears in several groups.
+	 */
+	public function testStatsSubAdmin() {
+		$this->container['IsAdmin'] = false;
+
+		$user = $this->getMockBuilder('\OC\User\User')
+			->disableOriginalConstructor()->getMock();
+		$user->expects($this->once())
+			->method('getUID')
+			->will($this->returnValue('username'));
+
+		$this->container['SubAdminFactory']
+			->expects($this->once())
+			->method('getSubAdminsOfGroups')
+			->with('username')
+			->will($this->returnValue(['SubGroup1', 'SubGroup2']));
+
+		$this->container['UserSession']
+			->expects($this->once())
+			->method('getUser')
+			->will($this->returnValue($user));
+
+		$subGroup1 = $this->getMockBuilder('\OCP\IGroup')
+			->disableOriginalConstructor()->getMock();
+		$subGroup1
+			->expects($this->once())
+			->method('getUsers')
+			->will($this->returnValue(['foo' => 'Foo', 'bar' => 'Bar']));
+		$subGroup2 = $this->getMockBuilder('\OCP\IGroup')
+			->disableOriginalConstructor()->getMock();
+		$subGroup2
+			->expects($this->once())
+			->method('getUsers')
+			->will($this->returnValue(['foo' => 'Foo', 'two' => 'Two']));
+
+		$this->container['GroupManager']
+			->expects($this->exactly(2))
+			->method('get')
+			->will($this->onConsecutiveCalls($subGroup1, $subGroup2));
+
+		$expectedResponse = new DataResponse(
+			[
+				'totalUsers' => 3
+			]
+		);
+
+		$response = $this->container['UsersController']->stats();
+		$this->assertEquals($expectedResponse, $response);
+	}
+
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list