2021-07-22 05:41:29 -04:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-12 06:24:33 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2021-07-22 05:41:29 -04:00
|
|
|
/**
|
2024-06-03 04:23:34 -04:00
|
|
|
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-07-22 05:41:29 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Settings\Service;
|
|
|
|
|
|
|
|
|
|
use OC\Settings\AuthorizedGroup;
|
|
|
|
|
use OC\Settings\AuthorizedGroupMapper;
|
2023-11-23 04:22:34 -05:00
|
|
|
|
2021-07-22 05:41:29 -04:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
|
|
|
|
use OCP\DB\Exception;
|
|
|
|
|
use OCP\IGroup;
|
2026-02-12 06:24:33 -05:00
|
|
|
use Throwable;
|
2021-07-22 05:41:29 -04:00
|
|
|
|
2026-02-12 06:24:33 -05:00
|
|
|
readonly class AuthorizedGroupService {
|
2024-10-18 06:04:22 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private AuthorizedGroupMapper $mapper,
|
|
|
|
|
) {
|
2021-07-22 05:41:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return AuthorizedGroup[]
|
2026-02-12 06:24:33 -05:00
|
|
|
* @throws Exception
|
2021-07-22 05:41:29 -04:00
|
|
|
*/
|
|
|
|
|
public function findAll(): array {
|
|
|
|
|
return $this->mapper->findAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find AuthorizedGroup by id.
|
2026-02-12 06:24:33 -05:00
|
|
|
* @throws DoesNotExistException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @throws MultipleObjectsReturnedException
|
2021-07-22 05:41:29 -04:00
|
|
|
*/
|
|
|
|
|
public function find(int $id): ?AuthorizedGroup {
|
|
|
|
|
return $this->mapper->find($id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws NotFoundException
|
2026-02-12 06:24:33 -05:00
|
|
|
* @throws Throwable
|
2021-07-22 05:41:29 -04:00
|
|
|
*/
|
2026-02-12 06:24:33 -05:00
|
|
|
private function handleException(Throwable $e): void {
|
2021-07-22 05:41:29 -04:00
|
|
|
if ($e instanceof DoesNotExistException
|
|
|
|
|
|| $e instanceof MultipleObjectsReturnedException) {
|
|
|
|
|
throw new NotFoundException('AuthorizedGroup not found');
|
|
|
|
|
}
|
2026-02-12 06:24:33 -05:00
|
|
|
|
|
|
|
|
throw $e;
|
2021-07-22 05:41:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new AuthorizedGroup
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
2025-10-09 10:01:11 -04:00
|
|
|
* @throws ConflictException
|
2026-02-12 06:24:33 -05:00
|
|
|
* @throws MultipleObjectsReturnedException
|
2021-07-22 05:41:29 -04:00
|
|
|
*/
|
|
|
|
|
public function create(string $groupId, string $class): AuthorizedGroup {
|
2025-10-09 10:01:11 -04:00
|
|
|
// Check if the group is already assigned to this class
|
|
|
|
|
try {
|
2026-02-12 06:24:33 -05:00
|
|
|
$this->mapper->findByGroupIdAndClass($groupId, $class);
|
|
|
|
|
throw new ConflictException('Group is already assigned to this class');
|
|
|
|
|
} catch (DoesNotExistException) {
|
2025-10-09 10:01:11 -04:00
|
|
|
// This is expected when no duplicate exists, continue with creation
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 05:41:29 -04:00
|
|
|
$authorizedGroup = new AuthorizedGroup();
|
|
|
|
|
$authorizedGroup->setGroupId($groupId);
|
|
|
|
|
$authorizedGroup->setClass($class);
|
|
|
|
|
return $this->mapper->insert($authorizedGroup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws NotFoundException
|
2026-02-12 06:24:33 -05:00
|
|
|
* @throws Throwable
|
2021-07-22 05:41:29 -04:00
|
|
|
*/
|
|
|
|
|
public function delete(int $id): void {
|
|
|
|
|
try {
|
|
|
|
|
$authorizedGroup = $this->mapper->find($id);
|
|
|
|
|
$this->mapper->delete($authorizedGroup);
|
2026-02-12 06:24:33 -05:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
$this->handleException($exception);
|
2021-07-22 05:41:29 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 06:24:33 -05:00
|
|
|
/**
|
|
|
|
|
* @return list<AuthorizedGroup>
|
|
|
|
|
*/
|
2021-07-22 05:41:29 -04:00
|
|
|
public function findExistingGroupsForClass(string $class): array {
|
|
|
|
|
try {
|
2026-02-12 06:24:33 -05:00
|
|
|
return $this->mapper->findExistingGroupsForClass($class);
|
|
|
|
|
} catch (\Exception) {
|
2021-07-22 05:41:29 -04:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 06:24:33 -05:00
|
|
|
/**
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
* @throws NotFoundException
|
|
|
|
|
*/
|
2021-07-22 05:41:29 -04:00
|
|
|
public function removeAuthorizationAssociatedTo(IGroup $group): void {
|
|
|
|
|
try {
|
|
|
|
|
$this->mapper->removeGroup($group->getGID());
|
2026-02-12 06:24:33 -05:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
$this->handleException($exception);
|
2021-07-22 05:41:29 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|