Skip to content

Commit

Permalink
simplify entrypoint for GroupManager
Browse files Browse the repository at this point in the history
- client does not need to have insights
- limit visibility of methods
- remove unnecessary AddUserToGroupException exception
  - only DB issues expected, nothing to catch for us
  - saves from awkward handling

Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz committed Jan 18, 2023
1 parent 22b6b02 commit 16a67a1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 63 deletions.
32 changes: 0 additions & 32 deletions lib/Exceptions/AddUserToGroupException.php

This file was deleted.

21 changes: 12 additions & 9 deletions lib/GroupBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\FetchMode;
use OCA\User_SAML\Exceptions\AddUserToGroupException;
use OCP\DB\Exception;
use OCP\Group\Backend\ABackend;
use OCP\Group\Backend\IAddToGroupBackend;
use OCP\Group\Backend\ICountUsersBackend;
Expand Down Expand Up @@ -213,17 +213,20 @@ public function createGroup(string $gid, string $samlGid = null): bool {
return $result === 1;
}

/**
* @throws Exception
*/
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))
->executeStatement();
if ($this->inGroup($uid, $gid)) {
return true;
} catch (\Exception $e) {
throw new AddUserToGroupException($e->getMessage());
}

$qb = $this->dbc->getQueryBuilder();
$qb->insert(self::TABLE_MEMBERS)
->setValue('uid', $qb->createNamedParameter($uid))
->setValue('gid', $qb->createNamedParameter($gid))
->executeStatement();
return true;
}

public function removeFromGroup(string $uid, string $gid): bool {
Expand Down
24 changes: 16 additions & 8 deletions lib/GroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ private function getGroupsToAdd(array $samlGroupNames, array $assignedGroupIds):
return $groupsToAdd;
}

public function replaceGroups(string $uid, array $samlGroupNames): void {
$user = $this->userManager->get($uid);
if ($user === null) {
return;
public function handleIncomingGroups(IUser $user, array $samlGroupNames): void {
$this->replaceGroups($user, $samlGroupNames);
// TODO: drop following line with dropping NC 23 support
$this->evaluateGroupMigrations($samlGroupNames);
if (isset($e)) {
throw $e;
}
}

public function replaceGroups(IUser $user, array $samlGroupNames): void {
$this->translateGroupToIds($samlGroupNames);
$assignedGroups = $this->groupManager->getUserGroups($user);
$assignedGroupIds = array_map(function (IGroup $group) {
Expand All @@ -144,13 +149,13 @@ protected function translateGroupToIds(array &$samlGroups): void {
});
}

public function removeGroups(IUser $user, array $groupIds): void {
protected function removeGroups(IUser $user, array $groupIds): void {
foreach ($groupIds as $gid) {
$this->removeGroup($user, $gid);
}
}

public function removeGroup(IUser $user, string $gid): void {
protected function removeGroup(IUser $user, string $gid): void {
$group = $this->groupManager->get($gid);
if ($group === null) {
return;
Expand All @@ -168,13 +173,13 @@ public function removeGroup(IUser $user, string $gid): void {
}
}

public function addGroups(IUser $user, $groupIds): void {
protected function addGroups(IUser $user, $groupIds): void {
foreach ($groupIds as $gid) {
$this->addGroup($user, $gid);
}
}

public function addGroup(IUser $user, string $gid): void {
protected function addGroup(IUser $user, string $gid): void {
try {
$group = $this->findGroup($gid);
} catch (GroupNotFoundException $e) {
Expand Down Expand Up @@ -205,6 +210,9 @@ protected function createGroupInBackend(string $gid, ?string $originalGid = null
return $group;
}

/**
* @throws GroupNotFoundException|NonMigratableGroupException
*/
protected function findGroup(string $gid): IGroup {
$migrationAllowList = $this->config->getAppValue(
'user_saml',
Expand Down
11 changes: 1 addition & 10 deletions lib/UserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

namespace OCA\User_SAML;

use OCA\User_SAML\Exceptions\AddUserToGroupException;
use OCP\Authentication\IApacheBackend;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\NotPermittedException;
Expand Down Expand Up @@ -665,15 +664,7 @@ public function updateAttributes($uid,
$user->setQuota($newQuota);
}

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 23 support
if (is_array($newGroups)) {
$this->groupManager->evaluateGroupMigrations($newGroups);
}
$this->groupManager->handleIncomingGroups($user, $newGroups ?? []);
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/GroupManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function testRemoveGroups() {
->expects($this->once())
->method('deleteGroup');

$this->ownGroupManager->removeGroups($user, ['groupA']);
$this->invokePrivate($this->ownGroupManager, 'removeGroups', [['groupA']]);
}

public function testAddToExistingGroup() {
Expand All @@ -202,7 +202,7 @@ public function testAddToExistingGroup() {
->expects($this->never())
->method('createGroupInBackend');

$this->ownGroupManager->addGroups($user, ['groupA']);
$this->invokePrivate($this->ownGroupManager, 'addGroups', [['groupA']]);
}

public function testAddToNonExistingGroups() {
Expand Down Expand Up @@ -232,7 +232,7 @@ public function testAddToNonExistingGroups() {
->method('addUser')
->with($user);

$this->ownGroupManager->addGroups($user, ['groupB']);
$this->invokePrivate($this->ownGroupManager, 'addGroups', [['groupB']]);
}

public function testAddGroupsWithCollision() {
Expand Down Expand Up @@ -275,6 +275,6 @@ public function testAddGroupsWithCollision() {
->method('addUser')
->with($user);

$this->ownGroupManager->addGroups($user, ['groupC']);
$this->invokePrivate($this->ownGroupManager, 'addGroups', [['groupC']]);
}
}

0 comments on commit 16a67a1

Please sign in to comment.