From cf0501d632ade26aae8600e53a26455822bd7eec Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sun, 25 Mar 2018 21:04:03 +0200 Subject: [PATCH 1/3] Make the group backends also possible using interfaces Just like for the user backends the group backends should also be properly done using interfaces. This adds the interfaces and abstract class to get started Signed-off-by: Roeland Jago Douma --- lib/public/Group/Backend/ABackend.php | 67 +++++++++++++++++++ .../Group/Backend/IAddToGroupBackend.php | 36 ++++++++++ .../Group/Backend/ICountUsersBackend.php | 36 ++++++++++ .../Group/Backend/ICreateGroupBackend.php | 36 ++++++++++ .../Group/Backend/IDeleteGroupBackend.php | 36 ++++++++++ .../Group/Backend/IGroupDetailsBackend.php | 36 ++++++++++ lib/public/Group/Backend/IIsAdminBackend.php | 36 ++++++++++ .../Group/Backend/IRemoveFromGroupBackend.php | 36 ++++++++++ 8 files changed, 319 insertions(+) create mode 100644 lib/public/Group/Backend/ABackend.php create mode 100644 lib/public/Group/Backend/IAddToGroupBackend.php create mode 100644 lib/public/Group/Backend/ICountUsersBackend.php create mode 100644 lib/public/Group/Backend/ICreateGroupBackend.php create mode 100644 lib/public/Group/Backend/IDeleteGroupBackend.php create mode 100644 lib/public/Group/Backend/IGroupDetailsBackend.php create mode 100644 lib/public/Group/Backend/IIsAdminBackend.php create mode 100644 lib/public/Group/Backend/IRemoveFromGroupBackend.php diff --git a/lib/public/Group/Backend/ABackend.php b/lib/public/Group/Backend/ABackend.php new file mode 100644 index 00000000000..507b9225501 --- /dev/null +++ b/lib/public/Group/Backend/ABackend.php @@ -0,0 +1,67 @@ + + * + * @author Roeland Jago Douma + * + * @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; + +use OCP\GroupInterface; + +/** + * @since 14.0.0 + */ +abstract class ABackend implements GroupInterface { + + /** + * @deprecated 14.0.0 + * + * @param int $actions The action to check for + * @return bool + */ + public function implementsActions($actions): bool { + $implements = 0; + + if ($this instanceof IAddToGroupBackend) { + $implements |= GroupInterface::ADD_TO_GROUP; + } + if ($this instanceof ICountUsersBackend) { + $implements |= GroupInterface::COUNT_USERS; + } + if ($this instanceof ICreateGroupBackend) { + $implements |= GroupInterface::CREATE_GROUP; + } + if ($this instanceof IDeleteGroupBackend) { + $implements |= GroupInterface::DELETE_GROUP; + } + if ($this instanceof IGroupDetailsBackend) { + $implements |= GroupInterface::GROUP_DETAILS; + } + if ($this instanceof IIsAdminBackend) { + $implements |= GroupInterface::IS_ADMIN; + } + if ($this instanceof IRemoveFromGroupBackend) { + $implements |= GroupInterface::REMOVE_FROM_GOUP; + } + + return (bool)($actions & $implements); + } +} diff --git a/lib/public/Group/Backend/IAddToGroupBackend.php b/lib/public/Group/Backend/IAddToGroupBackend.php new file mode 100644 index 00000000000..aba0444e9b6 --- /dev/null +++ b/lib/public/Group/Backend/IAddToGroupBackend.php @@ -0,0 +1,36 @@ + + * + * @author Roeland Jago Douma + * + * @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 IAddToGroupBackend { + + /** + * @since 14.0.0 + */ + public function addToGroup(string $uid, string $gid): bool; +} diff --git a/lib/public/Group/Backend/ICountUsersBackend.php b/lib/public/Group/Backend/ICountUsersBackend.php new file mode 100644 index 00000000000..1bdf54b4fd8 --- /dev/null +++ b/lib/public/Group/Backend/ICountUsersBackend.php @@ -0,0 +1,36 @@ + + * + * @author Roeland Jago Douma + * + * @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 ICountUsersBackend { + + /** + * @since 14.0.0 + */ + public function countUsersInGroup(string $gid, string $search = ''): int; +} diff --git a/lib/public/Group/Backend/ICreateGroupBackend.php b/lib/public/Group/Backend/ICreateGroupBackend.php new file mode 100644 index 00000000000..1a49bc221c0 --- /dev/null +++ b/lib/public/Group/Backend/ICreateGroupBackend.php @@ -0,0 +1,36 @@ + + * + * @author Roeland Jago Douma + * + * @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 ICreateGroupBackend { + + /** + * @since 14.0.0 + */ + public function createGroup(string $gid): bool; +} diff --git a/lib/public/Group/Backend/IDeleteGroupBackend.php b/lib/public/Group/Backend/IDeleteGroupBackend.php new file mode 100644 index 00000000000..09ca789d381 --- /dev/null +++ b/lib/public/Group/Backend/IDeleteGroupBackend.php @@ -0,0 +1,36 @@ + + * + * @author Roeland Jago Douma + * + * @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 IDeleteGroupBackend { + + /** + * @since 14.0.0 + */ + public function deleteGroup(string $gid): bool; +} diff --git a/lib/public/Group/Backend/IGroupDetailsBackend.php b/lib/public/Group/Backend/IGroupDetailsBackend.php new file mode 100644 index 00000000000..fd43510c6bf --- /dev/null +++ b/lib/public/Group/Backend/IGroupDetailsBackend.php @@ -0,0 +1,36 @@ + + * + * @author Roeland Jago Douma + * + * @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 IGroupDetailsBackend { + + /** + * @since 14.0.0 + */ + public function getGroupDetails(string $gid): array; +} diff --git a/lib/public/Group/Backend/IIsAdminBackend.php b/lib/public/Group/Backend/IIsAdminBackend.php new file mode 100644 index 00000000000..e253a293dc9 --- /dev/null +++ b/lib/public/Group/Backend/IIsAdminBackend.php @@ -0,0 +1,36 @@ + + * + * @author Roeland Jago Douma + * + * @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 IIsAdminBackend { + + /** + * @since 14.0.0 + */ + public function isAdmin(string $uid): bool; +} diff --git a/lib/public/Group/Backend/IRemoveFromGroupBackend.php b/lib/public/Group/Backend/IRemoveFromGroupBackend.php new file mode 100644 index 00000000000..885a5bbcdca --- /dev/null +++ b/lib/public/Group/Backend/IRemoveFromGroupBackend.php @@ -0,0 +1,36 @@ + + * + * @author Roeland Jago Douma + * + * @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 IRemoveFromGroupBackend { + + /** + * @since 14.0.0 + */ + public function removeFromGroup(string $uid, string $gid); +} From 5fcd97dcb6121f6be6c8c44c99f556c8c6ded804 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 23 Apr 2018 13:27:14 +0200 Subject: [PATCH 2/3] Bump autoloader Signed-off-by: Roeland Jago Douma --- lib/composer/composer/autoload_classmap.php | 8 ++++++++ lib/composer/composer/autoload_static.php | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index c24b1832ec6..19cb583cc93 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -185,6 +185,14 @@ return array( 'OCP\\Files\\UnseekableException' => $baseDir . '/lib/public/Files/UnseekableException.php', 'OCP\\GlobalScale\\IConfig' => $baseDir . '/lib/public/GlobalScale/IConfig.php', '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\\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', + 'OCP\\Group\\Backend\\IGroupDetailsBackend' => $baseDir . '/lib/public/Group/Backend/IGroupDetailsBackend.php', + 'OCP\\Group\\Backend\\IIsAdminBackend' => $baseDir . '/lib/public/Group/Backend/IIsAdminBackend.php', + 'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => $baseDir . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php', 'OCP\\Http\\Client\\IClient' => $baseDir . '/lib/public/Http/Client/IClient.php', 'OCP\\Http\\Client\\IClientService' => $baseDir . '/lib/public/Http/Client/IClientService.php', 'OCP\\Http\\Client\\IResponse' => $baseDir . '/lib/public/Http/Client/IResponse.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index b4af81e36ce..a913b0498b9 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -215,6 +215,14 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Files\\UnseekableException' => __DIR__ . '/../../..' . '/lib/public/Files/UnseekableException.php', 'OCP\\GlobalScale\\IConfig' => __DIR__ . '/../../..' . '/lib/public/GlobalScale/IConfig.php', '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\\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', + 'OCP\\Group\\Backend\\IGroupDetailsBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IGroupDetailsBackend.php', + 'OCP\\Group\\Backend\\IIsAdminBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IIsAdminBackend.php', + 'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php', 'OCP\\Http\\Client\\IClient' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IClient.php', 'OCP\\Http\\Client\\IClientService' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IClientService.php', 'OCP\\Http\\Client\\IResponse' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IResponse.php', From 961b27eeb0ee1ec1bd472c73bd587e8636bb074d Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 23 Apr 2018 14:39:30 +0200 Subject: [PATCH 3/3] Convert group Database backend This now uses the new interfaces Signed-off-by: Roeland Jago Douma --- lib/private/Group/Database.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php index abd7f6ee6ee..639df3de0de 100644 --- a/lib/private/Group/Database.php +++ b/lib/private/Group/Database.php @@ -40,12 +40,23 @@ namespace OC\Group; +use OCP\Group\Backend\ABackend; +use OCP\Group\Backend\IAddToGroupBackend; +use OCP\Group\Backend\ICountUsersBackend; +use OCP\Group\Backend\ICreateGroupBackend; +use OCP\Group\Backend\IDeleteGroupBackend; +use OCP\Group\Backend\IRemoveFromGroupBackend; use OCP\IDBConnection; /** * Class for group management in a SQL Database (e.g. MySQL, SQLite) */ -class Database extends Backend { +class Database extends ABackend + implements IAddToGroupBackend, + ICountUsersBackend, + ICreateGroupBackend, + IDeleteGroupBackend, + IRemoveFromGroupBackend { /** @var string[] */ private $groupCache = []; @@ -79,7 +90,7 @@ class Database extends Backend { * Tries to create a new group. If the group name already exists, false will * be returned. */ - public function createGroup( $gid ) { + public function createGroup(string $gid): bool { $this->fixDI(); // Add group @@ -100,7 +111,7 @@ class Database extends Backend { * * Deletes a group and removes it from the group_user-table */ - public function deleteGroup( $gid ) { + public function deleteGroup(string $gid): bool { $this->fixDI(); // Delete the group @@ -160,7 +171,7 @@ class Database extends Backend { * * Adds a user to a group. */ - public function addToGroup( $uid, $gid ) { + public function addToGroup(string $uid, string $gid): bool { $this->fixDI(); // No duplicate entries! @@ -184,7 +195,7 @@ class Database extends Backend { * * removes the user from a group. */ - public function removeFromGroup( $uid, $gid ) { + public function removeFromGroup(string $uid, string $gid): bool { $this->fixDI(); $qb = $this->dbConn->getQueryBuilder(); @@ -333,9 +344,9 @@ class Database extends Backend { * get the number of all users matching the search string in a group * @param string $gid * @param string $search - * @return int|false + * @return int */ - public function countUsersInGroup($gid, $search = '') { + public function countUsersInGroup(string $gid, string $search = ''): int { $this->fixDI(); $query = $this->dbConn->getQueryBuilder(); @@ -355,7 +366,10 @@ class Database extends Backend { if ($count !== false) { $count = (int)$count; + } else { + $count = 0; } + return $count; }