From d72d6f17d1ad307bedf8bb81ae0d82afdb47fb67 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Mon, 11 Nov 2024 14:20:53 +0100 Subject: [PATCH] feat: Add Spaces Administrators by Template - MEED-7751 - Meeds-io/MIPs#160 (#305) This change will allow to designate a space template super administrator in replacement to the previous Spaces Administrators concept, which wasn't related to the used Space Template to create the space. --- .../notification/utils/NotificationUtils.java | 4 +-- .../java/io/meeds/news/rest/NewsRest.java | 2 +- .../news/service/impl/NewsServiceImpl.java | 27 +++++++++---------- .../java/io/meeds/news/utils/NewsUtils.java | 2 +- .../java/io/meeds/news/rest/NewsRestTest.java | 2 +- .../io/meeds/news/utils/NewsUtilsTest.java | 2 +- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/content-service/src/main/java/io/meeds/news/notification/utils/NotificationUtils.java b/content-service/src/main/java/io/meeds/news/notification/utils/NotificationUtils.java index 4ea7959d9..5ac830093 100644 --- a/content-service/src/main/java/io/meeds/news/notification/utils/NotificationUtils.java +++ b/content-service/src/main/java/io/meeds/news/notification/utils/NotificationUtils.java @@ -40,9 +40,9 @@ public static String getUserFullName(String userName) throws Exception { return user.getFullName(); } - public static String getNotificationActivityLink(Space space, String activityId, boolean isMember) { + public static String getNotificationActivityLink(Space space, String activityId, boolean canView) { String activityLink = ""; - if (isMember) { + if (canView) { activityLink = getActivityPermalink(activityId); } else { activityLink = getNotificationActivityLinkForNotSpaceMembers(space); diff --git a/content-service/src/main/java/io/meeds/news/rest/NewsRest.java b/content-service/src/main/java/io/meeds/news/rest/NewsRest.java index 853467015..be0bd1fe6 100644 --- a/content-service/src/main/java/io/meeds/news/rest/NewsRest.java +++ b/content-service/src/main/java/io/meeds/news/rest/NewsRest.java @@ -473,7 +473,7 @@ public ResponseEntity getNews(@Parameter(description = "News author" for (String spaceId : spaces.split(",")) { Space space = spaceService.getSpaceById(spaceId); if (space == null - || (!spaceService.isSuperManager(authenticatedUser) && !spaceService.isMember(space, authenticatedUser))) { + || (!spaceService.isSuperManager(space, authenticatedUser) && !spaceService.isMember(space, authenticatedUser))) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); } spacesList.add(spaceId); diff --git a/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java b/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java index 5cff10032..86b8d36bc 100644 --- a/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java +++ b/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java @@ -806,15 +806,16 @@ public boolean canViewNews(News news, String authenticatedUser) { if (space == null) { LOG.warn("Can't find space with id {} when checking access on news with id {}", spaceId, news.getId()); return false; + } else if (spaceService.isSuperManager(space, authenticatedUser)) { + return true; } if (!news.isPublished() && StringUtils.equals(news.getPublicationState(), POSTED) - && !(spaceService.isSuperManager(authenticatedUser) || spaceService.isMember(space, authenticatedUser) - || isMemberOfsharedInSpaces(news, authenticatedUser))) { + && !(spaceService.canViewSpace(space, authenticatedUser) || canViewSharedInSpaces(news, authenticatedUser))) { return false; } if (news.isPublished() && StringUtils.equals(news.getPublicationState(), POSTED) && NewsUtils.SPACE_NEWS_AUDIENCE.equals(news.getAudience()) - && !(spaceService.isMember(space, authenticatedUser) || isMemberOfsharedInSpaces(news, authenticatedUser))) { + && !(spaceService.canViewSpace(space, authenticatedUser) || canViewSharedInSpaces(news, authenticatedUser))) { return false; } if (StringUtils.equals(news.getPublicationState(), STAGED) @@ -1228,10 +1229,9 @@ private News buildDraftArticle(String draftArticleId, String currentUserId) thro draftArticle.setSpaceAvatarUrl(draftArticleSpace.getAvatarUrl()); draftArticle.setSpaceDisplayName(draftArticleSpace.getDisplayName()); boolean hiddenSpace = draftArticleSpace.getVisibility().equals(Space.HIDDEN) - && !spaceService.isMember(draftArticleSpace, currentUserId) && !spaceService.isSuperManager(currentUserId); + && !spaceService.canViewSpace(draftArticleSpace, currentUserId); draftArticle.setHiddenSpace(hiddenSpace); - boolean isSpaceMember = - spaceService.isSuperManager(currentUserId) || spaceService.isMember(draftArticleSpace, currentUserId); + boolean isSpaceMember = spaceService.canViewSpace(draftArticleSpace, currentUserId); draftArticle.setSpaceMember(isSpaceMember); if (StringUtils.isNotEmpty(draftArticleSpace.getGroupId())) { draftArticle.setSpaceUrl(NewsUtils.buildSpaceUrl(draftArticleSpace.getId())); @@ -1293,7 +1293,7 @@ private void buildArticleProperties(News article, String currentUsername, Metada sharedInSpacesList.add(sharedInSpaceId); Space sharedInSpace = spaceService.getSpaceById(sharedInSpaceId); String activityId = activities[i].split(":")[1]; - if (sharedInSpace != null && currentUsername != null && spaceService.isMember(sharedInSpace, currentUsername) + if (sharedInSpace != null && currentUsername != null && spaceService.canViewSpace(sharedInSpace, currentUsername) && activityManager.isActivityExists(activityId)) { memberSpaceActivities.append(activities[i]).append(";"); } @@ -1525,10 +1525,10 @@ private boolean canDeleteNews(Identity currentIdentity, String posterId, String return spaceService.canRedactOnSpace(space, currentIdentity); } - private boolean isMemberOfsharedInSpaces(News news, String username) { + private boolean canViewSharedInSpaces(News news, String username) { for (String sharedInSpaceId : news.getSharedInSpacesList()) { Space sharedInSpace = spaceService.getSpaceById(sharedInSpaceId); - if (sharedInSpace != null && spaceService.isMember(sharedInSpace, username)) { + if (sharedInSpace != null && spaceService.canViewSpace(sharedInSpace, username)) { return true; } } @@ -1582,13 +1582,13 @@ private void sendNotification(String currentUserId, String contentSpaceId = lastSpaceIdActivityId.split(":")[0]; String contentActivityId = lastSpaceIdActivityId.split(":")[1]; Space contentSpace = spaceService.getSpaceById(contentSpaceId); - boolean isMember = spaceService.isMember(contentSpace, contentAuthor); + boolean canView = spaceService.canViewSpace(contentSpace, contentAuthor); if (contentSpace == null) { throw new NullPointerException("Cannot find a space with id " + contentSpaceId + ", it may not exist"); } org.exoplatform.social.core.identity.model.Identity identity = identityManager.getOrCreateUserIdentity(contentAuthor); String authorAvatarUrl = LinkProviderUtils.getUserAvatarUrl(identity.getProfile()); - String activityLink = NotificationUtils.getNotificationActivityLink(contentSpace, contentActivityId, isMember); + String activityLink = NotificationUtils.getNotificationActivityLink(contentSpace, contentActivityId, canView); String contentSpaceName = contentSpace.getDisplayName(); // Send Notification @@ -1875,10 +1875,9 @@ private News buildArticle(String newsId, String lang, boolean fetchOriginal) thr news.setSpaceId(space.getId()); news.setSpaceAvatarUrl(space.getAvatarUrl()); news.setSpaceDisplayName(space.getDisplayName()); - boolean hiddenSpace = space.getVisibility().equals(Space.HIDDEN) && !spaceService.isMember(space, currentUsername) - && !spaceService.isSuperManager(currentUsername); + boolean hiddenSpace = space.getVisibility().equals(Space.HIDDEN) && !spaceService.canViewSpace(space, currentUsername); news.setHiddenSpace(hiddenSpace); - boolean isSpaceMember = spaceService.isSuperManager(currentUsername) || spaceService.isMember(space, currentUsername); + boolean isSpaceMember = spaceService.canViewSpace(space, currentUsername); news.setSpaceMember(isSpaceMember); if (StringUtils.isNotEmpty(space.getGroupId())) { news.setSpaceUrl(NewsUtils.buildSpaceUrl(space.getId())); diff --git a/content-service/src/main/java/io/meeds/news/utils/NewsUtils.java b/content-service/src/main/java/io/meeds/news/utils/NewsUtils.java index 8331399f1..7961b8999 100644 --- a/content-service/src/main/java/io/meeds/news/utils/NewsUtils.java +++ b/content-service/src/main/java/io/meeds/news/utils/NewsUtils.java @@ -193,7 +193,7 @@ public static boolean canPublishNews(String spaceId, org.exoplatform.services.se && (currentIdentity.isMemberOf(PLATFORM_WEB_CONTRIBUTORS_GROUP, PUBLISHER_MEMBERSHIP_NAME) || spaceService.isPublisher(space, currentIdentity.getUserId()) || spaceService.isManager(space, currentIdentity.getUserId()) - || spaceService.isSuperManager(currentIdentity.getUserId())); + || spaceService.isSuperManager(space, currentIdentity.getUserId())); } return currentIdentity != null && currentIdentity.isMemberOf(PLATFORM_WEB_CONTRIBUTORS_GROUP, PUBLISHER_MEMBERSHIP_NAME); } diff --git a/content-service/src/test/java/io/meeds/news/rest/NewsRestTest.java b/content-service/src/test/java/io/meeds/news/rest/NewsRestTest.java index f6b7a2863..eacdc562c 100644 --- a/content-service/src/test/java/io/meeds/news/rest/NewsRestTest.java +++ b/content-service/src/test/java/io/meeds/news/rest/NewsRestTest.java @@ -701,7 +701,7 @@ public void shouldDeleteNewsWhenNewsExists() throws Exception { space1.setPrettyName("space1"); lenient().when(spaceService.getSpaceById(anyString())).thenReturn(space1); lenient().when(spaceService.isMember(any(Space.class), eq(JOHN))).thenReturn(true); - lenient().when(spaceService.isSuperManager(eq(JOHN))).thenReturn(true); + lenient().when(spaceService.isSuperManager(JOHN)).thenReturn(true); // When Response response = newsRestController.deleteNews("1", ARTICLE.name().toLowerCase(), 0L); diff --git a/content-service/src/test/java/io/meeds/news/utils/NewsUtilsTest.java b/content-service/src/test/java/io/meeds/news/utils/NewsUtilsTest.java index ab958e3f0..f969fa4d5 100644 --- a/content-service/src/test/java/io/meeds/news/utils/NewsUtilsTest.java +++ b/content-service/src/test/java/io/meeds/news/utils/NewsUtilsTest.java @@ -89,7 +89,7 @@ public void testCanPublishNews() { assertTrue(NewsUtils.canPublishNews(space.getId(), userAclIdentity)); when(spaceService.isManager(space, userAclIdentity.getUserId())).thenReturn(false); - when(spaceService.isSuperManager(userAclIdentity.getUserId())).thenReturn(true); + when(spaceService.isSuperManager(space, userAclIdentity.getUserId())).thenReturn(true); assertTrue(NewsUtils.canPublishNews(space.getId(), userAclIdentity)); }