Skip to content

Commit 62f4bf1

Browse files
committed
feat: Manage relations between spaces as platform admin - MEED-9850 - Meeds-io/MIPs#227 (#5395)
Fix feedbacks
1 parent 96af226 commit 62f4bf1

File tree

4 files changed

+54
-53
lines changed

4 files changed

+54
-53
lines changed

component/core/src/main/java/io/meeds/social/space/service/SpaceServiceImpl.java

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public Space createSpace(Space space,
406406
space.getTemplateId()));
407407
}
408408

409-
checkSubspaceCreationAllowed(space, parentSpaceId);
409+
checkSubspaceCreationAllowed(space, parentSpaceId, username);
410410

411411
// Copy only settable properties from provided DTO
412412
Space spaceToCreate = new Space();
@@ -1408,86 +1408,80 @@ private String buildPrettyName(String... names) {
14081408
.orElseThrow();
14091409
}
14101410

1411-
private void checkSubspaceCreationAllowed(Space space, long parentSpaceId) throws SpaceException {
1411+
@SneakyThrows
1412+
private void checkSubspaceCreationAllowed(Space space, long parentSpaceId, String username) {
14121413
if (parentSpaceId <= 0) {
14131414
return;
14141415
}
14151416
Space parentSpace = getSpaceById(parentSpaceId);
1417+
if (isMember(parentSpace, username)) {
1418+
throw new SpaceException(Code.SPACE_PERMISSION,
1419+
String.format("User %s isn't allowed to create subspace under parent space with id %s",
1420+
username,
1421+
parentSpaceId));
1422+
}
14161423
SpaceTemplate parentTemplate = spaceTemplateService.getSpaceTemplate(parentSpace.getTemplateId());
14171424
if (parentTemplate == null) {
14181425
throw new SpaceException(Code.UNKNOWN_SPACE_TEMPLATE,
14191426
String.format("Unknown parent space template for space %s", parentSpace.getDisplayName()));
14201427
}
14211428

14221429
Integer subspacesMaxLimit = parentTemplate.getSubspacesMaxLimit();
1423-
List<String> allowedSubspaceTemplates = parentTemplate.getAllowedSubspaceTemplates();
14241430

14251431
SpaceFilter spaceFilter = new SpaceFilter();
14261432
spaceFilter.setParentSpaceId(parentSpaceId);
14271433
ListAccess<Space> existingSubspacesAccess = getAllSpacesByFilter(spaceFilter);
1428-
Space[] existingSubspaces;
1434+
int subspacesCount;
14291435
try {
1430-
existingSubspaces = existingSubspacesAccess.load(0, -1);
1436+
subspacesCount = existingSubspacesAccess.getSize();
14311437
} catch (Exception e) {
1432-
throw new SpaceException(Code.ERROR_DATASTORE, "Failed to load subspaces for parent " + parentSpaceId, e);
1438+
throw new SpaceException(Code.ERROR_DATASTORE, "Failed to load subspaces count for parent %s".formatted(parentSpaceId), e);
14331439
}
14341440

1435-
int totalSubspaces = existingSubspaces != null ? existingSubspaces.length : 0;
1436-
1437-
if (subspacesMaxLimit != null && subspacesMaxLimit != 0 && totalSubspaces >= subspacesMaxLimit) {
1441+
if (subspacesMaxLimit != null && subspacesMaxLimit != 0 && subspacesCount >= subspacesMaxLimit) {
14381442
throw new SpaceException(Code.SUBSPACES_LIMIT_REACHED,
14391443
String.format("Cannot create more subspaces under '%s' (max %d reached)",
14401444
parentSpace.getDisplayName(),
14411445
subspacesMaxLimit));
14421446
}
14431447

1448+
List<String> allowedSubspaceTemplates = parentTemplate.getAllowedSubspaceTemplates();
1449+
14441450
if (allowedSubspaceTemplates == null || allowedSubspaceTemplates.isEmpty()) {
14451451
return;
14461452
}
14471453

14481454
long templateId = space.getTemplateId();
1449-
String matchedRule = null;
1450-
for (String rule : allowedSubspaceTemplates) {
1451-
if (rule != null && rule.startsWith(templateId + ":")) {
1452-
matchedRule = rule;
1453-
break;
1454-
}
1455-
}
1455+
String matchedRule =
1456+
allowedSubspaceTemplates.stream()
1457+
.filter(rule -> rule != null && rule.startsWith(templateId + ":")
1458+
&& rule.split(":").length == 2)
1459+
.findFirst()
1460+
.orElseThrow(() -> new SpaceException(Code.SPACE_PERMISSION,
1461+
"Subspace template '%s' is not allowed under parent template '%s'".formatted(templateId,
1462+
parentTemplate.getId())));
14561463

1457-
if (matchedRule == null) {
1458-
throw new SpaceException(Code.SPACE_PERMISSION,
1459-
String.format("Subspace template '%s' is not allowed under parent template '%s'",
1460-
templateId,
1461-
parentTemplate.getId()));
1464+
String[] parts = matchedRule.split(":");
1465+
int templateMaxLimit;
1466+
try {
1467+
templateMaxLimit = Integer.parseInt(parts[1]);
1468+
} catch (NumberFormatException e) {
1469+
throw new SpaceException(Code.ERROR_DATASTORE,
1470+
String.format("Invalid allowedSubspaceTemplates format: %s", matchedRule),
1471+
e);
14621472
}
14631473

1464-
String[] parts = matchedRule.split(":");
1465-
if (parts.length == 2) {
1466-
int templateMaxLimit;
1467-
try {
1468-
templateMaxLimit = Integer.parseInt(parts[1]);
1469-
} catch (NumberFormatException e) {
1470-
throw new SpaceException(Code.ERROR_DATASTORE,
1471-
String.format("Invalid allowedSubspaceTemplates format: %s", matchedRule),
1472-
e);
1473-
}
1474+
spaceFilter.setTemplateIds(List.of(templateId));
1475+
ListAccess<Space> existingSubspacesAccessByTemplateId = getAllSpacesByFilter(spaceFilter);
14741476

1475-
int countForTemplate = 0;
1476-
if (existingSubspaces != null) {
1477-
for (Space existing : existingSubspaces) {
1478-
if (existing.getTemplateId() > 0 && existing.getTemplateId() == templateId) {
1479-
countForTemplate++;
1480-
}
1481-
}
1482-
}
1477+
int countForTemplate = existingSubspacesAccessByTemplateId.getSize();
14831478

1484-
if (countForTemplate >= templateMaxLimit) {
1485-
throw new SpaceException(Code.SUBSPACES_LIMIT_REACHED,
1486-
String.format("Cannot create more subspaces of template '%s' (max %d reached under '%s')",
1487-
templateId,
1488-
templateMaxLimit,
1489-
parentSpace.getDisplayName()));
1490-
}
1479+
if (countForTemplate >= templateMaxLimit) {
1480+
throw new SpaceException(Code.SUBSPACES_LIMIT_REACHED,
1481+
String.format("Cannot create more subspaces of template '%s' (max %d reached under '%s')",
1482+
templateId,
1483+
templateMaxLimit,
1484+
parentSpace.getDisplayName()));
14911485
}
14921486
}
14931487
}

webapp/src/main/webapp/vue-apps/space-administration/components/drawer/SpacesAdministrationManageRelationshipsDrawer.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
:labels="spaceSuggesterLabels"
4444
:include-users="false"
4545
:ignore-items="ignoreItems"
46+
:search-options="{filterType: 'all'}"
4647
:width="220"
4748
name="activitySpaceAutocomplete"
4849
class="space-suggester activitySpaceAutocomplete"
@@ -156,10 +157,13 @@ export default {
156157
this.loading = true;
157158
try {
158159
const data = await this.$spaceService.getSpacesByFilter({
159-
sortBy: 'lastVisited',
160-
offset: this.offset,
161-
limit: 20,
160+
filter: 'all',
161+
expand: 'managers,groupBinding',
162162
parentSpaceId: this.spaceId,
163+
offset: 0,
164+
limit: 25,
165+
sortBy: 'title',
166+
sortDirection: 'desc',
163167
});
164168
this.subspaces = data?.spaces || [];
165169
} finally {

webapp/src/main/webapp/vue-apps/space-settings/components/drawer/SpaceSettingSubspacesDrawer.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,13 @@ export default {
9494
this.loading = true;
9595
try {
9696
const data = await this.$spaceService.getSpacesByFilter({
97-
sortBy: 'lastVisited',
98-
offset: this.offset,
99-
limit: 20,
97+
filter: 'all',
98+
expand: 'managers,groupBinding',
10099
parentSpaceId: this.spaceId,
100+
offset: 0,
101+
limit: 25,
102+
sortBy: 'title',
103+
sortDirection: 'desc',
101104
});
102105
this.subspaces = data?.spaces || [];
103106
} finally {

webapp/src/main/webapp/vue-apps/space-templates-management/components/drawer/SpaceTemplateCharacteristicsDrawer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ export default {
594594
spaceTemplate?.allowedSubspaceTemplates.length > 0 &&
595595
spaceTemplate?.allowedSubspaceTemplates.some(item => item && item.trim().length > 0);
596596
597-
if (spaceTemplate?.allowedSubspaceTemplates.length > 0) {
597+
if (Array.isArray(spaceTemplate?.allowedSubspaceTemplates) && spaceTemplate?.allowedSubspaceTemplates.length > 0) {
598598
const allTemplates = await this.$spaceTemplateService.getSpaceTemplates();
599599
this.selectedSubspaceTemplates = this.canHaveSubspaces
600600
? (spaceTemplate?.allowedSubspaceTemplates || []).map(item => {

0 commit comments

Comments
 (0)