Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove group folder handling from the back-end #1081

Merged
merged 12 commits into from
Nov 7, 2024
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
],
[
'name' => 'workspace#destroy',
'url' => '/api/delete/space',
'url' => '/spaces/{spaceId}',
'verb' => 'DELETE'
],
[
Expand Down
30 changes: 19 additions & 11 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use OCA\Workspace\Exceptions\CreateWorkspaceException;
use OCA\Workspace\Exceptions\WorkspaceNameExistException;
use OCA\Workspace\Folder\RootFolder;
use OCA\Workspace\Group\Admin\AdminGroup;
use OCA\Workspace\Group\Admin\AdminUserGroup;
use OCA\Workspace\Helper\GroupfolderHelper;
use OCA\Workspace\Service\Group\GroupFormatter;
use OCA\Workspace\Service\Group\ManagersWorkspace;
Expand All @@ -41,6 +43,7 @@
use OCA\Workspace\Service\UserService;
use OCA\Workspace\Service\Workspace\WorkspaceCheckService;
use OCA\Workspace\Service\WorkspaceService;
use OCA\Workspace\Space\SpaceManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
Expand All @@ -52,6 +55,8 @@
class WorkspaceController extends Controller {
public function __construct(
IRequest $request,
private AdminGroup $adminGroup,
private AdminUserGroup $adminUserGroup,
private GroupfolderHelper $folderHelper,
private IGroupManager $groupManager,
private RootFolder $rootFolder,
Expand All @@ -64,7 +69,8 @@ public function __construct(
private WorkspaceService $workspaceService,
private UserGroup $userGroup,
private WorkspaceManagerGroup $workspaceManagerGroup,
public $AppName
private SpaceManager $spaceManager,
public $AppName,
) {
parent::__construct($AppName, $request);
}
Expand Down Expand Up @@ -140,36 +146,38 @@ public function createWorkspace(string $spaceName,
*
* @NoAdminRequired
* @GeneralManagerRequired
* @param array $workspace
* @param int $spaceId
*
*/
public function destroy(array $workspace): JSONResponse {
public function destroy(int $spaceId): JSONResponse {
$this->logger->debug('Removing GE users from the WorkspacesManagers group if needed.');
$GEGroup = $this->groupManager->get(WorkspaceManagerGroup::get($workspace['id']));
foreach ($GEGroup->getUsers() as $user) {
foreach ($this->adminGroup->getUsers($spaceId) as $user) {
if ($this->userService->canRemoveWorkspaceManagers($user)) {
$this->userService->removeGEFromWM($user);
$this->adminUserGroup->removeUser($user);
}
}

// Removes all workspaces groups
$space = $this->spaceManager->get($spaceId);
$groups = [];
$this->logger->debug('Removing workspaces groups.');
foreach (array_keys($workspace['groups']) as $group) {
foreach (array_keys($space['groups']) as $group) {
$groups[] = $group;
$this->groupManager->get($group)->delete();
}

$this->spaceManager->remove($spaceId);

return new JSONResponse([
'http' => [
'statuscode' => 200,
'message' => 'The space is deleted.'
],
'data' => [
'name' => $workspace['name'],
'name' => $space['name'],
'groups' => $groups,
'space_id' => $workspace['id'],
'groupfolder_id' => $workspace['groupfolderId'],
'space_id' => $space['id'],
'groupfolder_id' => $space['groupfolderId'],
'state' => 'delete'
]
]);
Expand Down Expand Up @@ -206,7 +214,7 @@ public function findAll(): JSONResponse {
$this->logger->warning(
"Be careful, the $gid group is not exist in the oc_groups table."
. " But, it's present in the oc_group_folders_groups table."
. 'It necessary to recreate it with the occ command.'
. 'It necessary to recreate it with the occ command.'
);
continue;
}
Expand Down
14 changes: 13 additions & 1 deletion lib/Group/Admin/AdminGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

namespace OCA\Workspace\Group\Admin;

use OCP\IGroupManager;
use OCP\IUser;
use Psr\Log\LoggerInterface;

/**
* This class represents a Workspace Manager (GE-) group.
Expand All @@ -33,7 +35,9 @@ class AdminGroup {
public const GID_PREFIX = 'SPACE-GE-';

public function __construct(private AdminUserGroup $adminUserGroup,
private AdminGroupManager $adminGroupManager) {
private AdminGroupManager $adminGroupManager,
private LoggerInterface $logger,
private IGroupManager $groupManager) {
}

public function addUser(IUser $user, string $gid): bool {
Expand All @@ -43,4 +47,12 @@ public function addUser(IUser $user, string $gid): bool {

return true;
}

/**
* @return IUser[]
*/
public function getUsers(int $spaceId): array {
$group = $this->groupManager->get(self::GID_PREFIX . $spaceId);
return $group->getUsers();
}
}
12 changes: 11 additions & 1 deletion lib/Group/Admin/AdminUserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use OCP\IGroupManager;
use OCP\IUser;
use Psr\Log\LoggerInterface;

/**
* This class gathers all users from the
Expand All @@ -35,7 +36,10 @@
class AdminUserGroup {
public const GID = 'WorkspacesManagers';

public function __construct(private IGroupManager $groupManager) {
public function __construct(
private IGroupManager $groupManager,
private LoggerInterface $logger,
) {
}

public function addUser(IUser $user): bool {
Expand All @@ -49,4 +53,10 @@ public function addUser(IUser $user): bool {

return true;
}

public function removeUser(IUser $user): void {
$this->logger->debug('The ' . $user->getUID() . 'User is not manager of any other workspace, removing it from the ' . self::GID . ' group.');
$workspaceUserGroup = $this->groupManager->get(self::GID);
$workspaceUserGroup->removeUser($user);
}
}
12 changes: 10 additions & 2 deletions lib/Helper/GroupfolderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public function createFolder(string $mountpoint): int {
public function getFolder(int $folderId, int $rootStorageId) {
try {
return $this->folderManager->getFolder($folderId, $rootStorageId);
} catch(\Exception $e) {
} catch (\Exception $e) {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the getFolder function from FolderManager.');
}
}

public function setFolderAcl(int $folderId, bool $acl): void {
try {
$this->folderManager->setFolderAcl($folderId, $acl);
} catch(\Exception $e) {
} catch (\Exception $e) {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the setFolderAcl from FolderManager.');
}
}
Expand Down Expand Up @@ -96,4 +96,12 @@ public function setFolderQuota(int $folderId, int $quota): void {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the setFolderQuota from FolderManager.');
}
}

public function removeFolder(int $folderId): void {
try {
$this->folderManager->removeFolder($folderId);
} catch (\Exception $e) {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the removeFolder from FolderManager.');
}
}
}
2 changes: 1 addition & 1 deletion lib/Middleware/IsGeneralManagerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class IsGeneralManagerMiddleware extends Middleware {
public function __construct(
private IControllerMethodReflector $reflector,
private IRequest $request,
private UserService $userService
private UserService $userService,
) {
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ public function canRemoveWorkspaceManagers(IUser $user): bool {
*
* @param IUser $user
* @return void
*
* @deprecated
*
* @uses OCA\Workspace\Group\Admin\AdminUserGroup::removeUser
*/
public function removeGEFromWM(IUser $user): void {
$this->logger->debug('User is not manager of any other workspace, removing it from the ' . ManagersWorkspace::WORKSPACES_MANAGERS . ' group.');
Expand Down
6 changes: 6 additions & 0 deletions lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,10 @@ public function attachGroup(int $folderId, string $gid): void {
private function deleteBlankSpaceName(string $spaceName): string {
return trim($spaceName);
}

public function remove(string $spaceId): void {
$space = $this->get($spaceId);
$folderId = $space['groupfolder_id'];
$this->folderHelper->removeFolder($folderId);
}
}
Loading
Loading