From 45506adc5c2a34a8c812a2a3c9273a8447b450af Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 20 Sep 2019 11:04:36 +0200 Subject: [PATCH] Add a displayname to the database group backend Signed-off-by: Joas Schilling --- .../Version18000Date20190920085628.php | 74 +++++++++++++++++++ lib/private/Group/Database.php | 42 ++++++++++- .../Group/Backend/IGetDisplayNameBackend.php | 3 + 3 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 core/Migrations/Version18000Date20190920085628.php diff --git a/core/Migrations/Version18000Date20190920085628.php b/core/Migrations/Version18000Date20190920085628.php new file mode 100644 index 0000000000..8464057e4d --- /dev/null +++ b/core/Migrations/Version18000Date20190920085628.php @@ -0,0 +1,74 @@ + + * + * @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 OC\Core\Migrations; + +use Closure; +use Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\SimpleMigrationStep; +use OCP\Migration\IOutput; + +class Version18000Date20190920085628 extends SimpleMigrationStep { + + /** @var IDBConnection */ + protected $connection; + + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if ($schema->hasTable('groups')) { + $table = $schema->getTable('groups'); + + $table->addColumn('displayname', Type::STRING, [ + 'notnull' => true, + 'length' => 255, + ]); + } + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + $query = $this->connection->getQueryBuilder(); + $query->update('groups') + ->set('displayname', 'gid'); + $query->execute(); + } +} diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php index 7a5728b957..5e39398c60 100644 --- a/lib/private/Group/Database.php +++ b/lib/private/Group/Database.php @@ -49,6 +49,8 @@ use OCP\Group\Backend\ICountDisabledInGroup; use OCP\Group\Backend\ICountUsersBackend; use OCP\Group\Backend\ICreateGroupBackend; use OCP\Group\Backend\IDeleteGroupBackend; +use OCP\Group\Backend\IGetDisplayNameBackend; +use OCP\Group\Backend\IGroupDetailsBackend; use OCP\Group\Backend\IRemoveFromGroupBackend; use OCP\IDBConnection; @@ -61,6 +63,8 @@ class Database extends ABackend ICountUsersBackend, ICreateGroupBackend, IDeleteGroupBackend, + IGetDisplayNameBackend, + IGroupDetailsBackend, IRemoveFromGroupBackend { /** @var string[] */ @@ -391,7 +395,7 @@ class Database extends ABackend */ public function countDisabledInGroup(string $gid): int { $this->fixDI(); - + $query = $this->dbConn->getQueryBuilder(); $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) ->from('preferences', 'p') @@ -400,11 +404,11 @@ class Database extends ABackend ->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 { @@ -414,4 +418,36 @@ class Database extends ABackend return $count; } + /** + * @param string $gid + * @return string + * @since 17.0.0 + */ + public function getDisplayName(string $gid): string { + $query = $this->dbConn->getQueryBuilder(); + $query->select('displayname') + ->from('groups') + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); + + $result = $query->execute(); + $displayName = $result->fetchColumn(); + $result->closeCursor(); + + return (string) $displayName; + } + + /** + * @param string $gid + * @return array + * @since 14.0.0 + */ + public function getGroupDetails(string $gid): array { + $displayName = $this->getDisplayName($gid); + if ($displayName !== '') { + return ['displayName' => $displayName]; + } + + return []; + } + } diff --git a/lib/public/Group/Backend/IGetDisplayNameBackend.php b/lib/public/Group/Backend/IGetDisplayNameBackend.php index 69d1742a1e..2d750b8385 100644 --- a/lib/public/Group/Backend/IGetDisplayNameBackend.php +++ b/lib/public/Group/Backend/IGetDisplayNameBackend.php @@ -28,7 +28,10 @@ namespace OCP\Group\Backend; * @since 17.0.0 */ interface IGetDisplayNameBackend { + /** + * @param string $gid + * @return string * @since 17.0.0 */ public function getDisplayName(string $gid): string;