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

Backport: Add user as workspace manager with workspace:create command, even if the workspace already exists #1165

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
11 changes: 7 additions & 4 deletions lib/Commands/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
namespace OCA\Workspace\Commands;

use OCA\Workspace\Group\Admin\AdminGroup;
use OCA\Workspace\Group\Admin\AdminGroupManager;
use OCA\Workspace\Group\User\UserGroup;
use OCA\Workspace\Group\User\UserGroupManager;
use OCA\Workspace\Helper\GroupfolderHelper;
Expand Down Expand Up @@ -131,7 +130,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$workspace = $this->spaceManager->create($spacename);
try {
$workspace = $this->spaceManager->create($spacename);
} catch(\OCA\Workspace\Exceptions\WorkspaceNameExistException $e) {
$output->writeln(sprintf('The space %s already exists', $spacename));
$workspace = $this->spaceManager->getByName($spacename);
}

if ($input->hasParameterOption('--user-workspace-manager')) {
$userManagerName = $input->getOption('user-workspace-manager');
Expand Down Expand Up @@ -220,8 +224,7 @@ private function convertToByte(string $value): int {

private function addUserToAdminGroupManager(string $username, array $workspace): bool {
$user = $this->userFinder->findUser($username);
$groupname = AdminGroupManager::findWorkspaceManager($workspace);
$this->adminGroup->addUser($user, $groupname);
$this->adminGroup->addUser($user, (int) $workspace['id']);

return true;
}
Expand Down
13 changes: 13 additions & 0 deletions lib/Db/SpaceMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public function find($id): Space {
return $this->findEntity($query);
}

/**
* retrieve a space with its name
*/
public function findByName($spacename): Space {
$qb = $this->db->getQueryBuilder();
$query = $qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('space_name', $qb->createNamedParameter($spacename, $qb::PARAM_STR))
);
return $this->findEntity($query);
}

/**
* work
* @return Space[]
Expand Down
13 changes: 13 additions & 0 deletions lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function create(string $spacename): array {

return [
'name' => $space->getSpaceName(),
'id' => $space->getId(),
'id_space' => $space->getId(),
'folder_id' => $space->getGroupfolderId(),
'color' => $space->getColorCode(),
Expand All @@ -153,8 +154,12 @@ public function get(int $spaceId): array {

$space = $this->spaceMapper->find($spaceId);
$groupfolder = $this->folderHelper->getFolder($space->getGroupfolderId(), $this->rootFolder->getRootFolderStorageId());
if ($groupfolder === false) {
return [];
}

$workspace = array_merge($space->jsonSerialize(), $groupfolder);
$workspace['id'] = $space->getSpaceId();

$folderInfo = $this->folderHelper->getFolder(
$workspace['groupfolder_id'],
Expand All @@ -178,6 +183,14 @@ public function get(int $spaceId): array {
return $workspace;
}

public function getByName(string $spacename): array {
$space = $this->spaceMapper->findByName($spacename);
if ($space === null) {
return [];
}
return $this->get($space->getSpaceId());
}

public function attachGroup(int $folderId, string $gid): void {
$this->folderHelper->addApplicableGroup($folderId, $gid);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Space/SpaceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public function testArrayAfterCreatedTheEspace01Workspace(): void {
$space,
[
'name' => 'Espace01',
'id' => null,
'id_space' => null,
'folder_id' => 1,
'color' => '#a50b1c',
Expand Down
Loading