Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions lib/Controller/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
namespace OCA\Workspace\Controller;

use OCA\Workspace\Service\Group\GroupFolder\GroupFolderManage;
use OCA\Workspace\Service\Group\GroupFormatter;
use OCA\Workspace\Service\Group\GroupsWorkspaceService;
use OCA\Workspace\Groups\GroupFormatter;
use OCA\Workspace\Groups\GroupManager;
use OCA\Workspace\Service\Group\ManagersWorkspace;
use OCA\Workspace\Service\Group\UserGroup;
use OCA\Workspace\Service\Group\WorkspaceManagerGroup;
use OCA\Workspace\Groups\Workspace\UserGroup;
use OCA\Workspace\Groups\Workspace\WorkspaceManagerGroup;
use OCA\Workspace\Service\User\UserFormatter;
use OCA\Workspace\Service\User\UserWorkspace;
use OCA\Workspace\Service\UserService;
Expand All @@ -48,13 +48,15 @@ class GroupController extends Controller {
];

public function __construct(
private GroupsWorkspaceService $groupsWorkspace,
private GroupManager $groupWorkspaceManager,
private IGroupManager $groupManager,
private LoggerInterface $logger,
private IUserManager $userManager,
private LoggerInterface $logger,
private UserFormatter $userFormatter,
private UserService $userService,
private UserWorkspace $userWorkspace
private UserWorkspace $userWorkspace,
private UserGroup $userGroup,
private WorkspaceManagerGroup $workspaceManagerGroup
) {
}

Expand Down Expand Up @@ -207,7 +209,7 @@ public function addUser(string $spaceId, string $gid, string $user): JSONRespons
$NCGroup->addUser($NCUser);

// Adds the user to the application manager group when we are adding a workspace manager
if ($gid === WorkspaceManagerGroup::get($spaceId)) {
if ($gid === $this->workspaceManagerGroup->get($spaceId)) {
$workspaceUsersGroup = $this->groupManager->get(ManagersWorkspace::WORKSPACES_MANAGERS);
if (!is_null($workspaceUsersGroup)) {
$workspaceUsersGroup->addUser($NCUser);
Expand All @@ -220,7 +222,7 @@ public function addUser(string $spaceId, string $gid, string $user): JSONRespons

// Adds user to workspace user group
// This must be the last action done, when all other previous actions have succeeded
$UGroup = $this->groupManager->get(UserGroup::get($spaceId));
$UGroup = $this->groupManager->get($this->userGroup->get($spaceId));
$UGroup->addUser($NCUser);

return new JSONResponse(['message' => 'The user ' . $user . ' is added in the ' . $gid . ' group'], Http::STATUS_CREATED);
Expand Down Expand Up @@ -258,8 +260,8 @@ public function removeUser(array|string $space,
// Removes user from group(s)
$NCUser = $this->userManager->get($user);
$groups = [];
if ($gid === WorkspaceManagerGroup::get($space['id'])
|| $gid === UserGroup::get($space['id'])) {
if ($gid === $this->workspaceManagerGroup->get($space['id'])
|| $gid === $this->userGroup->get($space['id'])) {
// Removing user from a U- group
$this->logger->debug('Removing user from a workspace, removing it from all the workspace subgroups too.');
$users = (array)$space['users'];
Expand All @@ -268,7 +270,7 @@ public function removeUser(array|string $space,
$NCGroup->removeUser($NCUser);
$groups[] = $NCGroup->getGID();
$this->logger->debug('User removed from group: ' . $NCGroup->getDisplayName());
if ($groupId === WorkspaceManagerGroup::get($space['id'])) {
if ($groupId === $this->workspaceManagerGroup->get($space['id'])) {
$this->logger->debug('Removing user from a workspace manager group, removing it from the WorkspacesManagers group if needed.');
$this->userService->removeGEFromWM($NCUser, $space);
}
Expand All @@ -278,7 +280,7 @@ public function removeUser(array|string $space,
$groups[] = $gid;
$NCGroup->removeUser($NCUser);
$this->logger->debug('User removed from group: ' . $NCGroup->getDisplayName());
if ($gid === WorkspaceManagerGroup::get($space['id'])) {
if ($gid === $this->workspaceManagerGroup->get($space['id'])) {
// Removing user from a GE- group
$this->logger->debug('Removing user from a workspace manager group, removing it from the WorkspacesManagers group if needed.');
$this->userService->removeGEFromWM($NCUser, $space);
Expand Down Expand Up @@ -309,8 +311,8 @@ public function transferUsersToGroups(string $spaceId,
$groups = GroupFormatter::formatGroups(
array_merge(
[
$this->groupsWorkspace->getWorkspaceManagerGroup($spaceId),
$this->groupsWorkspace->getUserGroup($spaceId)
$this->groupWorkspaceManager->getWorkspaceManagerGroup($spaceId),
$this->groupWorkspaceManager->getUserGroup($spaceId)
],
array_map(function ($groupName) {
return $this->groupManager->get($groupName);
Expand All @@ -323,11 +325,11 @@ public function transferUsersToGroups(string $spaceId,
$allUsers = $this->userWorkspace->getUsersFromGroup($groupsName);
$usersFromAdvancedPermissions = $this->userWorkspace->getUsersFromGroup($groupsNameFromAdvancedPermissions);

$this->groupsWorkspace
->transferUsersToGroup($allUsers, $this->groupsWorkspace->getUserGroup($spaceId));
$this->groupsWorkspace
->transferUsersToGroup($usersFromAdvancedPermissions, $this->groupsWorkspace->getWorkspaceManagerGroup($spaceId));
$this->groupsWorkspace
$this->groupWorkspaceManager
->transferUsersToGroup($allUsers, $this->groupWorkspaceManager->getUserGroup($spaceId));
$this->groupWorkspaceManager
->transferUsersToGroup($usersFromAdvancedPermissions, $this->groupWorkspaceManager->getWorkspaceManagerGroup($spaceId));
$this->groupWorkspaceManager
->transferUsersToGroup($usersFromAdvancedPermissions, $this->groupManager->get(ManagersWorkspace::WORKSPACES_MANAGERS));

$users = $this->userFormatter->formatUsers($allUsers, $groupfolder, $spaceId);
Expand Down
48 changes: 25 additions & 23 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,27 @@

namespace OCA\Workspace\Controller;

use OCP\IRequest;
use OCP\IUserManager;
use OCP\IGroupManager;
use OCP\AppFramework\Http;
use OCA\Workspace\Db\Space;
use Psr\Log\LoggerInterface;
use OCP\AppFramework\Controller;
use OCA\Workspace\Db\SpaceMapper;
use OCA\Workspace\BadRequestException;
use OCA\Workspace\Service\UserService;
use OCA\Workspace\CreateGroupException;
use OCA\Workspace\Service\SpaceService;
use OCP\AppFramework\Http\JSONResponse;
use OCA\Workspace\Groups\Workspace\UserGroup;
use OCA\Workspace\CreateWorkspaceException;
use OCA\Workspace\Db\Space;
use OCA\Workspace\Db\SpaceMapper;
use OCA\Workspace\Service\Group\GroupFormatter;
use OCA\Workspace\Service\WorkspaceService;
use OCA\Workspace\Groups\GroupFormatter;
use OCA\Workspace\Service\Group\GroupsWorkspace;
use OCA\Workspace\Service\Group\ManagersWorkspace;
use OCA\Workspace\Service\Group\UserGroup;
use OCA\Workspace\Service\Group\WorkspaceManagerGroup;
use OCA\Workspace\Service\SpaceService;
use OCA\Workspace\Service\UserService;
use OCA\Workspace\Groups\Workspace\WorkspaceManagerGroup;
use OCA\Workspace\Service\Workspace\WorkspaceCheckService;
use OCA\Workspace\Service\WorkspaceService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class WorkspaceController extends Controller {
public function __construct(
Expand All @@ -54,12 +55,13 @@ public function __construct(
private LoggerInterface $logger,
private SpaceMapper $spaceMapper,
private SpaceService $spaceService,
private UserGroup $userGroup,
private UserService $userService,
private WorkspaceCheckService $workspaceCheck,
private WorkspaceService $workspaceService,
private UserGroup $userGroup,
private WorkspaceManagerGroup $workspaceManagerGroup,
public $AppName
private WorkspaceService $workspaceService,
public $AppName,
private GroupsWorkspace $groupsWorkspace
) {
parent::__construct($AppName, $request);
}
Expand Down Expand Up @@ -107,8 +109,8 @@ public function createWorkspace(string $spaceName,
}

// #2 create groups
$newSpaceManagerGroup = $this->workspaceManagerGroup->create($space);
$newSpaceUsersGroup = $this->userGroup->create($space);
$newSpaceManagerGroup = $this->groupsWorkspace->create($this->workspaceManagerGroup, $space);
$newSpaceUsersGroup = $this->groupsWorkspace->create($this->userGroup, $space);

// #3 Returns result
return new JSONResponse([
Expand All @@ -135,7 +137,7 @@ public function createWorkspace(string $spaceName,
*/
public function destroy(array $workspace): JSONResponse {
$this->logger->debug('Removing GE users from the WorkspacesManagers group if needed.');
$GEGroup = $this->groupManager->get(WorkspaceManagerGroup::get($workspace['id']));
$GEGroup = $this->groupManager->get($this->workspaceManagerGroup->get($workspace['id']));
foreach ($GEGroup->getUsers() as $user) {
$this->userService->removeGEFromWM($user, $workspace);
}
Expand Down Expand Up @@ -240,15 +242,15 @@ public function changeUserRole(array|string $space,
}

$user = $this->userManager->get($userId);
$GEgroup = $this->groupManager->get(WorkspaceManagerGroup::get($space['id']));
$GEgroup = $this->groupManager->get($this->workspaceManagerGroup->get($space['id']));
if ($GEgroup->inGroup($user)) {
// Changing a user's role from admin to user
$GEgroup->removeUser($user);
$this->logger->debug('Removing a user from a GE group. Removing it from the ' . ManagersWorkspace::WORKSPACES_MANAGERS . ' group if needed.');
$this->userService->removeGEFromWM($user, $space);
} else {
// Changing a user's role from user to admin
$this->groupManager->get(WorkspaceManagerGroup::get($space['id']))->addUser($user);
$this->groupManager->get($this->workspaceManagerGroup->get($space['id']))->addUser($user);
$this->groupManager->get(ManagersWorkspace::WORKSPACES_MANAGERS)->addUser($user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
*/

namespace OCA\Workspace\Service\Group;
namespace OCA\Workspace\Groups;

use OCP\IGroup;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@
*
*/

namespace OCA\Workspace\Service\Group;
namespace OCA\Workspace\Groups;

use OCA\Workspace\GroupException;
use OCA\Workspace\UserGroup;
use OCA\Workspace\WorkspaceManagerGroup;
use OCP\IUser;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCA\Workspace\Exceptions\GroupException;
use OCA\Workspace\Groups\Workspace\UserGroup;
use OCA\Workspace\Groups\Workspace\WorkspaceManagerGroup;

class GroupsWorkspaceService {
public function __construct(private IGroupManager $groupManager) {
class GroupManager {
public function __construct(private IGroupManager $groupManager,
private WorkspaceManagerGroup $workspaceManagerGroup,
private UserGroup $userGroup) {
}

/**
* @throws GroupException
*/
public function getWorkspaceManagerGroup(string $spaceId): IGroup {
$groupSpaceManager = $this->groupManager->get(
WorkspaceManagerGroup::get($spaceId)
$this->workspaceManagerGroup->get($spaceId)
);

if (is_null($groupSpaceManager)) {
Expand All @@ -55,7 +57,7 @@ public function getWorkspaceManagerGroup(string $spaceId): IGroup {
*/
public function getUserGroup(string $spaceId): IGroup {
$groupUser = $this->groupManager->get(
UserGroup::get($spaceId)
$this->userGroup->get($spaceId)
);

if (is_null($groupUser)) {
Expand Down
37 changes: 37 additions & 0 deletions lib/Groups/GroupWorkspaceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @copyright Copyright (c) 2017 Arawa
*
* @author 2023 Baptiste Fotia <baptiste.fotia@arawa.fr>
*
* @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\Workspace\Groups;

interface GroupWorkspaceInterface {

/**
* @return string - Just the GID with the spaceId.
*/
public function get(int $spaceId): string;

public function getDisplayPrefix(): string;

public function getGidPrefix(): string;

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,32 @@
*
*/

namespace OCA\Workspace\Service\Group;
namespace OCA\Workspace\Groups\Workspace;

use OCA\Workspace\CreateGroupException;
use OCA\Workspace\Db\Space;
use OCP\AppFramework\Http;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCA\Workspace\Groups\GroupWorkspaceInterface;

class UserGroup extends GroupsWorkspace {
private IGroupManager $groupManager;
class UserGroup implements GroupWorkspaceInterface {

public function __construct(IGroupManager $groupManager, IAppConfig $appConfig) {
parent::__construct($appConfig);
$this->groupManager = $groupManager;
private const GID_SPACE = 'SPACE-';
private const PREFIX_GID_USERS = self::GID_SPACE . 'U-';
private string $DISPLAY_PREFIX_USER_GROUP;


public function __construct(IAppConfig $appConfig) {
$this->DISPLAY_PREFIX_USER_GROUP = $appConfig->getAppValue('DISPLAY_PREFIX_USER_GROUP');
}

public static function get(int $spaceId): string {
public function get(int $spaceId): string {
return self::PREFIX_GID_USERS . $spaceId;
}

public static function getPrefix(): string {
public function getGidPrefix(): string {
return self::PREFIX_GID_USERS;
}

public function create(Space $space): IGroup {
$group = $this->groupManager->createGroup(self::PREFIX_GID_USERS . $space->getId());

if (is_null($group)) {
throw new CreateGroupException('Error to create a Space Manager group.', Http::STATUS_CONFLICT);
}

$group->setDisplayName(self::getDisplayPrefixUserGroup() . $space->getSpaceName());

return $group;
}
public function getDisplayPrefix(): string
{
return $this->DISPLAY_PREFIX_USER_GROUP;
}
}
Loading