Added disabled count per groups

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2018-05-16 12:32:56 +02:00
parent 6689a3e37b
commit 10c135ca34
No known key found for this signature in database
GPG key ID: 60C25B8C072916CF
8 changed files with 151 additions and 11 deletions

View file

@ -111,7 +111,12 @@ class GroupsController extends AUserData {
$groups = $this->groupManager->search($search, $limit, $offset);
$groups = array_map(function($group) {
/** @var IGroup $group */
return ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()];
return [
'id' => $group->getGID(),
'displayname' => $group->getDisplayName(),
'usercount' => $group->count(),
'disabled' => $group->countDisabled()
];
}, $groups);
return new DataResponse(['groups' => $groups]);

View file

@ -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',

View file

@ -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',

View file

@ -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;
}
}

View file

@ -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 */
@ -236,6 +238,26 @@ class Group implements IGroup {
return $users;
}
/**
* 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
*

View file

@ -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()
);
}

View file

@ -420,12 +420,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 +433,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;
}
/**

View file

@ -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;
}