Skip to content

Commit

Permalink
prevent group remove if it has association with static pages
Browse files Browse the repository at this point in the history
  • Loading branch information
wangf1122 committed May 7, 2024
1 parent 262fa60 commit 2d82af0
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jeeves.server.UserSession;
import jeeves.server.context.ServiceContext;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
Expand All @@ -45,8 +46,10 @@
import org.fao.geonet.api.tools.i18n.TranslationPackBuilder;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.domain.*;
import org.fao.geonet.domain.page.Page;
import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.repository.*;
import org.fao.geonet.repository.page.PageRepository;
import org.fao.geonet.repository.specification.GroupSpecs;
import org.fao.geonet.repository.specification.MetadataSpecs;
import org.fao.geonet.repository.specification.OperationAllowedSpecs;
Expand Down Expand Up @@ -149,6 +152,9 @@ public class GroupsApi {
@Autowired
private MetadataRepository metadataRepository;

@Autowired
private PageRepository pageRepository;

private static Resources.ResourceHolder getImage(Resources resources, ServiceContext serviceContext, Group group) throws IOException {
final Path logosDir = resources.locateLogosDir(serviceContext);
final Path harvesterLogosDir = resources.locateHarvesterLogosDir(serviceContext);
Expand Down Expand Up @@ -541,6 +547,21 @@ public void deleteGroup(
));
}

List<Page> staticPages = pageRepository.findAll();

for (Page page: staticPages) {
if (CollectionUtils.isNotEmpty(page.getGroups())) {
for (Group pageGroup : page.getGroups()) {
if (pageGroup.getId() == groupIdentifier) {
throw new NotAllowedException(String.format(
"Group %s is associated with '%s' static page(s). Please remove the static page(s) associated with that group first.",
group.get().getName(), page.getLabel()
));
}
}
}
}

groupRepository.deleteById(groupIdentifier);

translationPackBuilder.clearCache();
Expand Down

0 comments on commit 2d82af0

Please sign in to comment.