From 0cd62f612b56c171290f63230ca31b13bf175b39 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Mon, 21 Feb 2022 09:43:04 -0800 Subject: [PATCH] Improve error handling in not public maps. --- .../exceptions/MapNonPublicException.java | 37 +++++++++++++++++ .../wisemapping/service/MindmapService.java | 2 + .../service/MindmapServiceImpl.java | 40 +++++++++++-------- .../wisemapping/webmvc/MindmapController.java | 18 ++++++--- 4 files changed, 75 insertions(+), 22 deletions(-) create mode 100755 wise-webapp/src/main/java/com/wisemapping/exceptions/MapNonPublicException.java diff --git a/wise-webapp/src/main/java/com/wisemapping/exceptions/MapNonPublicException.java b/wise-webapp/src/main/java/com/wisemapping/exceptions/MapNonPublicException.java new file mode 100755 index 00000000..9bbb9e72 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/exceptions/MapNonPublicException.java @@ -0,0 +1,37 @@ +/* + * Copyright [2015] [wisemapping] + * + * Licensed under WiseMapping Public License, Version 1.0 (the "License"). + * It is basically the Apache License, Version 2.0 (the "License") plus the + * "powered by wisemapping" text requirement on every single page; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the license at + * + * http://www.wisemapping.org/license + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.wisemapping.exceptions; + +import org.jetbrains.annotations.NotNull; + +public class MapNonPublicException + extends ClientException { + public static final String MSG_KEY = "ACCESS_HAS_BEEN_REVOKED"; + + public MapNonPublicException(@NotNull String msg) { + super(msg, Severity.FATAL); + } + + @NotNull + @Override + protected String getMsgBundleKey() { + return MSG_KEY; + } + +} diff --git a/wise-webapp/src/main/java/com/wisemapping/service/MindmapService.java b/wise-webapp/src/main/java/com/wisemapping/service/MindmapService.java index f51b139c..eebf329a 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/MindmapService.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/MindmapService.java @@ -57,6 +57,8 @@ public interface MindmapService { boolean hasPermissions(@Nullable User user, int mapId, CollaborationRole allowedRole); + boolean isMindmapPublic(int mapId); + void revertChange(@NotNull Mindmap map, int historyId) throws WiseMappingException, IOException; MindMapHistory findMindmapHistory(int id, int hid) throws WiseMappingException; diff --git a/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java index 09a0ac23..e9d7e865 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java @@ -1,20 +1,20 @@ /* -* Copyright [2015] [wisemapping] -* -* Licensed under WiseMapping Public License, Version 1.0 (the "License"). -* It is basically the Apache License, Version 2.0 (the "License") plus the -* "powered by wisemapping" text requirement on every single page; -* you may not use this file except in compliance with the License. -* You may obtain a copy of the license at -* -* http://www.wisemapping.org/license -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright [2015] [wisemapping] + * + * Licensed under WiseMapping Public License, Version 1.0 (the "License"). + * It is basically the Apache License, Version 2.0 (the "License") plus the + * "powered by wisemapping" text requirement on every single page; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the license at + * + * http://www.wisemapping.org/license + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.wisemapping.service; @@ -62,6 +62,12 @@ public class MindmapServiceImpl return hasPermissions(user, map, grantedRole); } + @Override + public boolean isMindmapPublic(int mapId) { + final Mindmap map = mindmapManager.getMindmapById(mapId); + return map != null && map.isPublic(); + } + @Override public boolean hasPermissions(@Nullable User user, @Nullable Mindmap map, @NotNull CollaborationRole role) { boolean result = false; @@ -70,7 +76,7 @@ public class MindmapServiceImpl result = true; } else if (user != null) { final Optional collaboration = map.findCollaboration(user); - if (collaboration .isPresent()) { + if (collaboration.isPresent()) { result = collaboration .get() .hasPermissions(role); diff --git a/wise-webapp/src/main/java/com/wisemapping/webmvc/MindmapController.java b/wise-webapp/src/main/java/com/wisemapping/webmvc/MindmapController.java index 9fb0e49a..011f5eb9 100644 --- a/wise-webapp/src/main/java/com/wisemapping/webmvc/MindmapController.java +++ b/wise-webapp/src/main/java/com/wisemapping/webmvc/MindmapController.java @@ -20,6 +20,7 @@ package com.wisemapping.webmvc; import com.wisemapping.exceptions.MapCouldNotFoundException; +import com.wisemapping.exceptions.MapNonPublicException; import com.wisemapping.exceptions.WiseMappingException; import com.wisemapping.model.CollaborationRole; import com.wisemapping.model.Mindmap; @@ -107,7 +108,8 @@ public class MindmapController { public String showMindmapViewerPage(@PathVariable int id, @NotNull Model model) throws WiseMappingException { final String result = showPrintPage(id, model); model.addAttribute("readOnlyMode", true); - return result; } + return result; + } @RequestMapping(value = "maps/{id}/try", method = RequestMethod.GET) public String showMindmapTryPage(@PathVariable int id, @NotNull Model model) throws WiseMappingException { @@ -127,10 +129,13 @@ public class MindmapController { } @RequestMapping(value = "maps/{id}/embed") - public ModelAndView showEmbeddedPage(@PathVariable int id, @RequestParam(required = false) Float zoom) throws MapCouldNotFoundException { - ModelAndView view; + public ModelAndView showEmbeddedPage(@PathVariable int id, @RequestParam(required = false) Float zoom) throws MapCouldNotFoundException, MapNonPublicException { + if (!mindmapService.isMindmapPublic(id)) { + throw new MapNonPublicException("Map " + id + " is not public."); + } + final MindMapBean mindmap = findMindmapBean(id); - view = new ModelAndView("mindmapEmbedded", "mindmap", mindmap); + final ModelAndView view = new ModelAndView("mindmapEmbedded", "mindmap", mindmap); view.addObject("zoom", zoom == null ? 1 : zoom); final Locale locale = LocaleContextHolder.getLocale(); view.addObject("locale", locale.toString().toLowerCase()); @@ -139,6 +144,9 @@ public class MindmapController { @RequestMapping(value = "maps/{id}/public", method = RequestMethod.GET) public String showPublicViewPage(@PathVariable int id, @NotNull Model model) throws WiseMappingException { + if (!mindmapService.isMindmapPublic(id)) { + throw new MapNonPublicException("Map " + id + " is not public."); + } return this.showPrintPage(id, model); } @@ -156,7 +164,7 @@ public class MindmapController { @NotNull private Mindmap findMindmap(int mapId) throws MapCouldNotFoundException { - final Mindmap result = mindmapService.findMindmapById((int) mapId); + final Mindmap result = mindmapService.findMindmapById(mapId); if (result == null) { throw new MapCouldNotFoundException("Map could not be found " + mapId); }