summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorblizzz <blizzz@arthur-schiwon.de>2018-05-25 15:54:47 +0200
committerGitHub <noreply@github.com>2018-05-25 15:54:47 +0200
commit760b28ba4c7d4a9b6126aa4063238c2959eb263e (patch)
tree3671b2c23d7a9316a3b66e0e23d7062a65a30464 /lib
parentd4a0af7e5ec0d0e97dda2d826574779dab82260c (diff)
parent4644841ba67e1e1f606011b51148303f59be3960 (diff)
downloadnextcloud-server-760b28ba4c7d4a9b6126aa4063238c2959eb263e.tar.gz
nextcloud-server-760b28ba4c7d4a9b6126aa4063238c2959eb263e.zip
Merge pull request #9488 from nextcloud/disabled-users-fix
Added disabled count per groups & fixed disabled users query
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/Group/Database.php35
-rw-r--r--lib/private/Group/Group.php22
-rw-r--r--lib/private/Group/MetaData.php14
-rw-r--r--lib/private/User/Manager.php67
-rw-r--r--lib/public/Group/Backend/ICountDisabledInGroup.php36
-rw-r--r--lib/public/IGroup.php8
8 files changed, 173 insertions, 11 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index c6475f8c889..d457d2e758b 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -188,6 +188,7 @@ return array(
'OCP\\GroupInterface' => $baseDir . '/lib/public/GroupInterface.php',
'OCP\\Group\\Backend\\ABackend' => $baseDir . '/lib/public/Group/Backend/ABackend.php',
'OCP\\Group\\Backend\\IAddToGroupBackend' => $baseDir . '/lib/public/Group/Backend/IAddToGroupBackend.php',
+ 'OCP\\Group\\Backend\\ICountDisabledInGroup' => $baseDir . '/lib/public/Group/Backend/ICountDisabledInGroup.php',
'OCP\\Group\\Backend\\ICountUsersBackend' => $baseDir . '/lib/public/Group/Backend/ICountUsersBackend.php',
'OCP\\Group\\Backend\\ICreateGroupBackend' => $baseDir . '/lib/public/Group/Backend/ICreateGroupBackend.php',
'OCP\\Group\\Backend\\IDeleteGroupBackend' => $baseDir . '/lib/public/Group/Backend/IDeleteGroupBackend.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index f334733a2c9..88f8248b7f2 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -218,6 +218,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\GroupInterface' => __DIR__ . '/../../..' . '/lib/public/GroupInterface.php',
'OCP\\Group\\Backend\\ABackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ABackend.php',
'OCP\\Group\\Backend\\IAddToGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IAddToGroupBackend.php',
+ 'OCP\\Group\\Backend\\ICountDisabledInGroup' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ICountDisabledInGroup.php',
'OCP\\Group\\Backend\\ICountUsersBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ICountUsersBackend.php',
'OCP\\Group\\Backend\\ICreateGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ICreateGroupBackend.php',
'OCP\\Group\\Backend\\IDeleteGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IDeleteGroupBackend.php',
diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php
index 639df3de0de..9bcb7eb3385 100644
--- a/lib/private/Group/Database.php
+++ b/lib/private/Group/Database.php
@@ -5,6 +5,7 @@
* @author Aaron Wood <aaronjwood@gmail.com>
* @author Loki3000 <github@labcms.ru>
* @author Roeland Jago Douma <roeland@famdouma.nl>
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0
*
@@ -40,8 +41,10 @@
namespace OC\Group;
+use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Group\Backend\ABackend;
use OCP\Group\Backend\IAddToGroupBackend;
+use OCP\Group\Backend\ICountDisabledInGroup;
use OCP\Group\Backend\ICountUsersBackend;
use OCP\Group\Backend\ICreateGroupBackend;
use OCP\Group\Backend\IDeleteGroupBackend;
@@ -53,6 +56,7 @@ use OCP\IDBConnection;
*/
class Database extends ABackend
implements IAddToGroupBackend,
+ ICountDisabledInGroup,
ICountUsersBackend,
ICreateGroupBackend,
IDeleteGroupBackend,
@@ -373,4 +377,35 @@ class Database extends ABackend
return $count;
}
+ /**
+ * get the number of disabled users in a group
+ *
+ * @param string $search
+ * @return int|bool
+ */
+ public function countDisabledInGroup(string $gid): int {
+ $this->fixDI();
+
+ $query = $this->dbConn->getQueryBuilder();
+ $query->select($query->createFunction('COUNT(Distinct uid)'))
+ ->from('preferences', 'p')
+ ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid')
+ ->where($query->expr()->eq('appid', $query->createNamedParameter('core')))
+ ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled')))
+ ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
+ ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR));
+
+ $result = $query->execute();
+ $count = $result->fetchColumn();
+ $result->closeCursor();
+
+ if ($count !== false) {
+ $count = (int)$count;
+ } else {
+ $count = 0;
+ }
+
+ return $count;
+ }
+
}
diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php
index 6795f0e2f53..cc6315263d4 100644
--- a/lib/private/Group/Group.php
+++ b/lib/private/Group/Group.php
@@ -10,6 +10,7 @@
* @author Robin McCorkell <robin@mccorkell.me.uk>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Vincent Petry <pvince81@owncloud.com>
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0
*
@@ -31,6 +32,7 @@ namespace OC\Group;
use OCP\IGroup;
use OCP\IUser;
+use OCP\Group\Backend\ICountDisabledInGroup;
class Group implements IGroup {
/** @var null|string */
@@ -237,6 +239,26 @@ class Group implements IGroup {
}
/**
+ * returns the number of disabled users
+ *
+ * @return int|bool
+ */
+ public function countDisabled() {
+ $users = false;
+ foreach ($this->backends as $backend) {
+ if($backend instanceOf ICountDisabledInGroup) {
+ if($users === false) {
+ //we could directly add to a bool variable, but this would
+ //be ugly
+ $users = 0;
+ }
+ $users += $backend->countDisabledInGroup($this->gid);
+ }
+ }
+ return $users;
+ }
+
+ /**
* search for users in the group by displayname
*
* @param string $search
diff --git a/lib/private/Group/MetaData.php b/lib/private/Group/MetaData.php
index 99594301990..497dcf72b59 100644
--- a/lib/private/Group/MetaData.php
+++ b/lib/private/Group/MetaData.php
@@ -8,6 +8,7 @@
* @author Morris Jobke <hey@morrisjobke.de>
* @author Stephan Peijnik <speijnik@anexia-it.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0
*
@@ -28,6 +29,7 @@
namespace OC\Group;
use OCP\IUserSession;
+use OCP\IGroupManager;
class MetaData {
const SORT_NONE = 0;
@@ -40,7 +42,7 @@ class MetaData {
protected $isAdmin;
/** @var array */
protected $metaData = array();
- /** @var \OCP\IGroupManager */
+ /** @var IGroupManager */
protected $groupManager;
/** @var bool */
protected $sorting = false;
@@ -50,13 +52,14 @@ class MetaData {
/**
* @param string $user the uid of the current user
* @param bool $isAdmin whether the current users is an admin
- * @param \OCP\IGroupManager $groupManager
+ * @param IGroupManager $groupManager
+ * @param IUserManager $userManager
* @param IUserSession $userSession
*/
public function __construct(
$user,
$isAdmin,
- \OCP\IGroupManager $groupManager,
+ IGroupManager $groupManager,
IUserSession $userSession
) {
$this->user = $user;
@@ -155,13 +158,14 @@ class MetaData {
* creates an array containing the group meta data
* @param \OCP\IGroup $group
* @param string $userSearch
- * @return array with the keys 'id', 'name' and 'usercount'
+ * @return array with the keys 'id', 'name', 'usercount' and 'disabled'
*/
private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) {
return array(
'id' => $group->getGID(),
'name' => $group->getDisplayName(),
'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
+ 'disabled' => $group->countDisabled()
);
}
@@ -184,7 +188,7 @@ class MetaData {
* @param string $search a search string
* @return \OCP\IGroup[]
*/
- protected function getGroups($search = '') {
+ public function getGroups($search = '') {
if($this->isAdmin) {
return $this->groupManager->search($search);
} else {
diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php
index b5054bd1851..6784359538d 100644
--- a/lib/private/User/Manager.php
+++ b/lib/private/User/Manager.php
@@ -34,6 +34,7 @@ namespace OC\User;
use OC\Hooks\PublicEmitter;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IUser;
+use OCP\IGroup;
use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\IConfig;
@@ -385,6 +386,24 @@ class Manager extends PublicEmitter implements IUserManager {
}
/**
+ * returns how many users per backend exist in the requested groups (if supported by backend)
+ *
+ * @param IGroup[] $groups an array of gid to search in
+ * @return array|int an array of backend class as key and count number as value
+ * if $hasLoggedIn is true only an int is returned
+ */
+ public function countUsersOfGroups(array $groups) {
+ $users = [];
+ foreach($groups as $group) {
+ $usersIds = array_map(function($user) {
+ return $user->getUID();
+ }, $group->getUsers());
+ $users = array_merge($users, $usersIds);
+ }
+ return count(array_unique($users));
+ }
+
+ /**
* The callback is executed for each user on each backend.
* If the callback returns false no further users will be retrieved.
*
@@ -420,12 +439,12 @@ class Manager extends PublicEmitter implements IUserManager {
}
/**
- * returns how many users have logged in once
+ * returns how many users are disabled
*
* @return int
* @since 12.0.0
*/
- public function countDisabledUsers() {
+ public function countDisabledUsers(): int {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder->select($queryBuilder->createFunction('COUNT(*)'))
->from('preferences')
@@ -433,12 +452,48 @@ class Manager extends PublicEmitter implements IUserManager {
->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled')))
->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR));
- $query = $queryBuilder->execute();
+
+ $result = $queryBuilder->execute();
+ $count = $result->fetchColumn();
+ $result->closeCursor();
+
+ if ($count !== false) {
+ $count = (int)$count;
+ } else {
+ $count = 0;
+ }
- $result = (int)$query->fetchColumn();
- $query->closeCursor();
+ return $count;
+ }
- return $result;
+ /**
+ * returns how many users are disabled in the requested groups
+ *
+ * @param array $groups groupids to search
+ * @return int
+ * @since 14.0.0
+ */
+ public function countDisabledUsersOfGroups(array $groups): int {
+ $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
+ $queryBuilder->select($queryBuilder->createFunction('COUNT(Distinct uid)'))
+ ->from('preferences', 'p')
+ ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid')
+ ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
+ ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled')))
+ ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
+ ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)));
+
+ $result = $queryBuilder->execute();
+ $count = $result->fetchColumn();
+ $result->closeCursor();
+
+ if ($count !== false) {
+ $count = (int)$count;
+ } else {
+ $count = 0;
+ }
+
+ return $count;
}
/**
diff --git a/lib/public/Group/Backend/ICountDisabledInGroup.php b/lib/public/Group/Backend/ICountDisabledInGroup.php
new file mode 100644
index 00000000000..fa07bcf7e5b
--- /dev/null
+++ b/lib/public/Group/Backend/ICountDisabledInGroup.php
@@ -0,0 +1,36 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Group\Backend;
+
+/**
+ * @since 14.0.0
+ */
+interface ICountDisabledInGroup {
+
+ /**
+ * @since 14.0.0
+ */
+ public function countDisabledInGroup(string $gid): int;
+}
diff --git a/lib/public/IGroup.php b/lib/public/IGroup.php
index da1a4f5dd1e..22fa28f94be 100644
--- a/lib/public/IGroup.php
+++ b/lib/public/IGroup.php
@@ -101,6 +101,14 @@ interface IGroup {
public function count($search = '');
/**
+ * returns the number of disabled users
+ *
+ * @return int|bool
+ * @since 14.0.0
+ */
+ public function countDisabled();
+
+ /**
* search for users in the group by displayname
*
* @param string $search