@@ -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}
0 commit comments