Implement backends add user to groups method

Signed-off-by: Jonathan Treffler <mail@jonathan-treffler.de>
Signed-off-by: Giuliano Mele <giuliano.mele@verdigado.com>
This commit is contained in:
Giuliano Mele 2021-07-20 14:13:24 +02:00 committed by Jonathan Treffler
parent 993529ed30
commit c5ff4555a3
3 changed files with 53 additions and 3 deletions

View file

@ -0,0 +1,30 @@
<?php
/**
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
*
* @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 OCA\User_SAML\Exceptions;
/**
* Class AddUserToGroupException is thrown when user could not be added to group
*
* @package OCA\User_SAML\Exceptions
*/
class AddUserToGroupException extends \Exception {
}

View file

@ -3,11 +3,13 @@
namespace OCA\User_SAML;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCA\User_SAML\Exceptions\AddUserToGroupException;
use OCP\Group\Backend\ABackend;
use OCP\Group\Backend\IAddToGroupBackend;
use OCP\Group\Backend\ICreateGroupBackend;
use OCP\IDBConnection;
class GroupBackend extends ABackend {
class GroupBackend extends ABackend implements IAddToGroupBackend {
/** @var IDBConnection */
private $dbc;
@ -175,4 +177,17 @@ class GroupBackend extends ABackend {
return $result === 1;
}
public function addToGroup(string $uid, string $gid): bool {
try {
$qb = $this->dbc->getQueryBuilder();
$qb->insert(self::TABLE_MEMBERS)
->setValue('uid', $qb->createNamedParameter($uid))
->setValue('gid', $qb->createNamedParameter($gid))
->execute();
return true;
} catch (\Exception $e) {
throw new AddUserToGroupException($e->getMessage());
}
}
}

View file

@ -21,6 +21,7 @@
namespace OCA\User_SAML;
use OCA\User_SAML\Exceptions\AddUserToGroupException;
use OCP\Authentication\IApacheBackend;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\NotPermittedException;
@ -672,9 +673,13 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
if ($newGroups === null) {
$newGroups = [];
}
$this->groupManager->replaceGroups($user->getUID(), $newGroups);
try {
$this->groupManager->replaceGroups($user->getUID(), $newGroups);
} catch (AddUserToGroupException $e) {
$this->logger->error('Failed to add user to group: {exception}', ['app' => 'user_saml', 'exception' => $e->getMessage()]);
}
// TODO: drop following line with dropping NC 18 support
// $this->groupManager->evaluateGroupMigrations($newGroups);
$this->groupManager->evaluateGroupMigrations($newGroups);
}
}