Skip to content

Commit

Permalink
refactor: Use SpaceManager to remove a space from the Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
zak39 committed Nov 6, 2024
1 parent 4fa6937 commit 3910e36
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
15 changes: 8 additions & 7 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 @@ -53,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 Down Expand Up @@ -147,25 +151,22 @@ public function createWorkspace(string $spaceName,
*/
public function destroy(int $spaceId): JSONResponse {
$this->logger->debug('Removing GE users from the WorkspacesManagers group if needed.');
$space = $this->spaceManager->get($spaceId);
$folderId = $space['groupfolder_id'];

$GEGroup = $this->groupManager->get(WorkspaceManagerGroup::get($spaceId));
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($space['groups']) as $group) {
$groups[] = $group;
$this->groupManager->get($group)->delete();
}

$this->folderHelper->removeFolder($folderId);
$this->spaceManager->remove($spaceId);

return new JSONResponse([
'http' => [
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);
}
}
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);
}
}

0 comments on commit 3910e36

Please sign in to comment.