diff --git a/apps/provisioning_api/lib/Controller/GroupsController.php b/apps/provisioning_api/lib/Controller/GroupsController.php index c2958bc477..9d64bb2251 100644 --- a/apps/provisioning_api/lib/Controller/GroupsController.php +++ b/apps/provisioning_api/lib/Controller/GroupsController.php @@ -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]); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index c6475f8c88..d457d2e758 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 f334733a2c..88f8248b7f 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 639df3de0d..90aacdf002 100644 --- a/lib/private/Group/Database.php +++ b/lib/private/Group/Database.php @@ -5,6 +5,7 @@ * @author Aaron Wood * @author Loki3000 * @author Roeland Jago Douma + * @author John Molakvoæ * * @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 6795f0e2f5..cc6315263d 100644 --- a/lib/private/Group/Group.php +++ b/lib/private/Group/Group.php @@ -10,6 +10,7 @@ * @author Robin McCorkell * @author Roeland Jago Douma * @author Vincent Petry + * @author John Molakvoæ * * @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 * diff --git a/lib/private/Group/MetaData.php b/lib/private/Group/MetaData.php index 9959430199..1e89472bc3 100644 --- a/lib/private/Group/MetaData.php +++ b/lib/private/Group/MetaData.php @@ -8,6 +8,7 @@ * @author Morris Jobke * @author Stephan Peijnik * @author Thomas Müller + * @author John Molakvoæ * * @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() ); } diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index b5054bd185..1771232193 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -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; } /** diff --git a/lib/public/Group/Backend/ICountDisabledInGroup.php b/lib/public/Group/Backend/ICountDisabledInGroup.php new file mode 100644 index 0000000000..fa07bcf7e5 --- /dev/null +++ b/lib/public/Group/Backend/ICountDisabledInGroup.php @@ -0,0 +1,36 @@ + + * + * @author John Molakvoæ + * + * @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 . + * + */ + +namespace OCP\Group\Backend; + +/** + * @since 14.0.0 + */ +interface ICountDisabledInGroup { + + /** + * @since 14.0.0 + */ + public function countDisabledInGroup(string $gid): int; +}