Allow group displaynames in the database backend (#17221)

Allow group displaynames in the database backend
This commit is contained in:
John Molakvoæ 2019-10-05 18:34:14 +02:00 committed by GitHub
commit 62399c76e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 237 additions and 6 deletions

View file

@ -40,6 +40,7 @@ return [
['root' => '/cloud', 'name' => 'Groups#getSubAdminsOfGroup', 'url' => '/groups/{groupId}/subadmins', 'verb' => 'GET', 'requirements' => ['groupId' => '.+']],
['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET', 'requirements' => ['groupId' => '.+']],
['root' => '/cloud', 'name' => 'Groups#updateGroup', 'url' => '/groups/{groupId}', 'verb' => 'PUT', 'requirements' => ['groupId' => '.+']],
['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE', 'requirements' => ['groupId' => '.+']],
// Users

View file

@ -129,7 +129,7 @@ class GroupsController extends AUserData {
*
* @param string $groupId
* @return DataResponse
* @throws OCSException
* @throws OCSException
*
* @deprecated 14 Use getGroupUsers
*/
@ -248,6 +248,28 @@ class GroupsController extends AUserData {
return new DataResponse();
}
/**
* @PasswordConfirmationRequired
*
* @param string $groupId
* @param string $key
* @param string $value
* @return DataResponse
* @throws OCSException
*/
public function updateGroup(string $groupId, string $key, string $value): DataResponse {
if ($key === 'displayname') {
$group = $this->groupManager->get($groupId);
if ($group->setDisplayName($value)) {
return new DataResponse();
}
throw new OCSException('Not supported by backend', 101);
} else {
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
}
}
/**
* @PasswordConfirmationRequired
*

View file

@ -28,6 +28,7 @@ use OC\Core\Command\Base;
use OCP\IGroupManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Add extends Base {
@ -49,7 +50,13 @@ class Add extends Base {
->addArgument(
'groupid',
InputArgument::REQUIRED,
'Group name'
'Group id'
)
->addOption(
'display-name',
null,
InputOption::VALUE_REQUIRED,
'Group name used in the web UI (can contain any characters)'
);
}
@ -62,6 +69,11 @@ class Add extends Base {
} else {
$group = $this->groupManager->createGroup($gid);
$output->writeln('Created group "' . $group->getGID() . '"');
$displayName = trim((string) $input->getOption('display-name'));
if ($displayName !== '') {
$group->setDisplayName($displayName);
}
}
}
}

View file

@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.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 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,
'default' => '',
]);
}
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();
}
}

View file

@ -290,6 +290,7 @@ return array(
'OCP\\Group\\Backend\\IHideFromCollaborationBackend' => $baseDir . '/lib/public/Group/Backend/IHideFromCollaborationBackend.php',
'OCP\\Group\\Backend\\IIsAdminBackend' => $baseDir . '/lib/public/Group/Backend/IIsAdminBackend.php',
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => $baseDir . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
'OCP\\Group\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/Group/Backend/ISetDisplayNameBackend.php',
'OCP\\Group\\ISubAdmin' => $baseDir . '/lib/public/Group/ISubAdmin.php',
'OCP\\Http\\Client\\IClient' => $baseDir . '/lib/public/Http/Client/IClient.php',
'OCP\\Http\\Client\\IClientService' => $baseDir . '/lib/public/Http/Client/IClientService.php',
@ -777,6 +778,7 @@ return array(
'OC\\Core\\Migrations\\Version16000Date20190427105638' => $baseDir . '/core/Migrations/Version16000Date20190427105638.php',
'OC\\Core\\Migrations\\Version16000Date20190428150708' => $baseDir . '/core/Migrations/Version16000Date20190428150708.php',
'OC\\Core\\Migrations\\Version17000Date20190514105811' => $baseDir . '/core/Migrations/Version17000Date20190514105811.php',
'OC\\Core\\Migrations\\Version18000Date20190920085628' => $baseDir . '/core/Migrations/Version18000Date20190920085628.php',
'OC\\Core\\Notification\\RemoveLinkSharesNotifier' => $baseDir . '/core/Notification/RemoveLinkSharesNotifier.php',
'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php',
'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php',

View file

@ -319,6 +319,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Group\\Backend\\IHideFromCollaborationBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IHideFromCollaborationBackend.php',
'OCP\\Group\\Backend\\IIsAdminBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IIsAdminBackend.php',
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
'OCP\\Group\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ISetDisplayNameBackend.php',
'OCP\\Group\\ISubAdmin' => __DIR__ . '/../../..' . '/lib/public/Group/ISubAdmin.php',
'OCP\\Http\\Client\\IClient' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IClient.php',
'OCP\\Http\\Client\\IClientService' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IClientService.php',
@ -806,6 +807,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Migrations\\Version16000Date20190427105638' => __DIR__ . '/../../..' . '/core/Migrations/Version16000Date20190427105638.php',
'OC\\Core\\Migrations\\Version16000Date20190428150708' => __DIR__ . '/../../..' . '/core/Migrations/Version16000Date20190428150708.php',
'OC\\Core\\Migrations\\Version17000Date20190514105811' => __DIR__ . '/../../..' . '/core/Migrations/Version17000Date20190514105811.php',
'OC\\Core\\Migrations\\Version18000Date20190920085628' => __DIR__ . '/../../..' . '/core/Migrations/Version18000Date20190920085628.php',
'OC\\Core\\Notification\\RemoveLinkSharesNotifier' => __DIR__ . '/../../..' . '/core/Notification/RemoveLinkSharesNotifier.php',
'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php',
'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php',

View file

@ -49,7 +49,10 @@ 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\Group\Backend\ISetDisplayNameBackend;
use OCP\IDBConnection;
/**
@ -61,7 +64,10 @@ class Database extends ABackend
ICountUsersBackend,
ICreateGroupBackend,
IDeleteGroupBackend,
IRemoveFromGroupBackend {
IGetDisplayNameBackend,
IGroupDetailsBackend,
IRemoveFromGroupBackend,
ISetDisplayNameBackend {
/** @var string[] */
private $groupCache = [];
@ -103,6 +109,7 @@ class Database extends ABackend
$builder = $this->dbConn->getQueryBuilder();
$result = $builder->insert('groups')
->setValue('gid', $builder->createNamedParameter($gid))
->setValue('displayname', $builder->createNamedParameter($gid))
->execute();
} catch(UniqueConstraintViolationException $e) {
$result = 0;
@ -396,7 +403,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')
@ -405,11 +412,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 {
@ -419,4 +426,49 @@ class Database extends ABackend
return $count;
}
public function getDisplayName(string $gid): string {
$this->fixDI();
$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;
}
public function getGroupDetails(string $gid): array {
$displayName = $this->getDisplayName($gid);
if ($displayName !== '') {
return ['displayName' => $displayName];
}
return [];
}
public function setDisplayName(string $gid, string $displayName): bool {
if (!$this->groupExists($gid)) {
return false;
}
$this->fixDI();
$displayName = trim($displayName);
if ($displayName === '') {
$displayName = $gid;
}
$query = $this->dbConn->getQueryBuilder();
$query->update('groups')
->set('displayname', $query->createNamedParameter($displayName))
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
$query->execute();
return true;
}
}

View file

@ -33,6 +33,7 @@ namespace OC\Group;
use OCP\Group\Backend\IGetDisplayNameBackend;
use OCP\Group\Backend\IHideFromCollaborationBackend;
use OC\Hooks\PublicEmitter;
use OCP\Group\Backend\ISetDisplayNameBackend;
use OCP\GroupInterface;
use OCP\IGroup;
use OCP\IUser;
@ -101,6 +102,20 @@ class Group implements IGroup {
return $this->displayName;
}
public function setDisplayName(string $displayName): bool {
$displayName = trim($displayName);
if ($displayName !== '') {
foreach ($this->backends as $backend) {
if (($backend instanceof ISetDisplayNameBackend)
&& $backend->setDisplayName($this->gid, $displayName)) {
$this->displayName = $displayName;
return true;
}
}
}
return false;
}
/**
* get all users in the group
*

View file

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

View file

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.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 18.0.0
*/
interface ISetDisplayNameBackend {
/**
* @param string $gid
* @param string $displayName
* @return bool
* @since 18.0.0
*/
public function setDisplayName(string $gid, string $displayName): bool;
}

View file

@ -47,6 +47,15 @@ interface IGroup {
*/
public function getDisplayName();
/**
* Set the group display name
*
* @param string $displayName
* @return bool
* @since 18.0.0
*/
public function setDisplayName(string $displayName): bool;
/**
* get all users in the group
*