From 4170223690f6f0dc66b1217a0a6c8c61d383d4ee Mon Sep 17 00:00:00 2001 From: Alex Bogdanovski Date: Thu, 5 Oct 2023 19:54:36 +0300 Subject: [PATCH] added support for having individually assigned moderators for each space --- .../scoold/controllers/ProfileController.java | 11 ++++- .../java/com/erudika/scoold/core/Profile.java | 29 ++++++++++++ .../com/erudika/scoold/utils/ScooldUtils.java | 18 +++++++- src/main/resources/static/scripts/scoold.js | 10 ++++- src/main/resources/static/styles/style.css | 3 ++ src/main/resources/templates/profile.vm | 44 ++++++++++++------- 6 files changed, 95 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/erudika/scoold/controllers/ProfileController.java b/src/main/java/com/erudika/scoold/controllers/ProfileController.java index 8dafa044..2e6cadcb 100755 --- a/src/main/java/com/erudika/scoold/controllers/ProfileController.java +++ b/src/main/java/com/erudika/scoold/controllers/ProfileController.java @@ -155,7 +155,16 @@ public String makeMod(@PathVariable String id, HttpServletRequest req, HttpServl Profile showUser = pc.read(Profile.id(id)); if (showUser != null) { if (utils.isAdmin(authUser) && !utils.isAdmin(showUser)) { - showUser.setGroups(utils.isMod(showUser) ? USERS.toString() : MODS.toString()); + if (CONF.modsAccessAllSpaces()) { + showUser.setGroups(utils.isMod(showUser) ? USERS.toString() : MODS.toString()); + } else { + String space = req.getParameter("space"); + if (showUser.isModInSpace(space)) { + showUser.getModspaces().remove(space); + } else { + showUser.getModspaces().add(space); + } + } showUser.update(); } } diff --git a/src/main/java/com/erudika/scoold/core/Profile.java b/src/main/java/com/erudika/scoold/core/Profile.java index 0850d10b..1aee1a07 100755 --- a/src/main/java/com/erudika/scoold/core/Profile.java +++ b/src/main/java/com/erudika/scoold/core/Profile.java @@ -63,6 +63,7 @@ public class Profile extends Sysprop { @Stored @URL private String website; @Stored private List favtags; @Stored private Set favspaces; + @Stored private Set modspaces; @Stored private Set spaces; @Stored private Boolean replyEmailsEnabled; @Stored private Boolean commentEmailsEnabled; @@ -77,6 +78,7 @@ public class Profile extends Sysprop { @Stored private String pendingEmail; @Stored private Boolean editorRoleEnabled; + private transient String currentSpace; private transient String newbadges; private transient Integer newreports; private transient User user; @@ -293,6 +295,25 @@ public void setUser(User user) { this.user = user; } + public String getCurrentSpace() { + return currentSpace; + } + + public void setCurrentSpace(String currentSpace) { + this.currentSpace = currentSpace; + } + + public Set getModspaces() { + if (modspaces == null) { + modspaces = new LinkedHashSet<>(); + } + return modspaces; + } + + public void setModspaces(Set modspaces) { + this.modspaces = modspaces; + } + public String getLatlng() { return latlng; } @@ -347,6 +368,14 @@ public void setFavspaces(Set favspaces) { this.favspaces = favspaces; } + public boolean isModInCurrentSpace() { + return isModInSpace(currentSpace); + } + + public boolean isModInSpace(String space) { + return (getModspaces().contains(space) || getModspaces().contains(ScooldUtils.getInstance().getSpaceId(space))); + } + public Set getSpaces() { if (ScooldUtils.getInstance().isMod(this) && ScooldUtils.getConfig().modsAccessAllSpaces()) { ScooldUtils utils = ScooldUtils.getInstance(); diff --git a/src/main/java/com/erudika/scoold/utils/ScooldUtils.java b/src/main/java/com/erudika/scoold/utils/ScooldUtils.java index 0b988b63..76a95bf3 100755 --- a/src/main/java/com/erudika/scoold/utils/ScooldUtils.java +++ b/src/main/java/com/erudika/scoold/utils/ScooldUtils.java @@ -292,6 +292,7 @@ public ParaObject checkAuth(HttpServletRequest req, HttpServletResponse res) thr authUser = getOrCreateProfile(u, req); authUser.setUser(u); authUser.setOriginalPicture(u.getPicture()); + authUser.setCurrentSpace(getSpaceIdFromCookie(authUser, req)); boolean updatedRank = promoteOrDemoteUser(authUser, u); boolean updatedProfile = updateProfilePictureAndName(authUser, u); if (updatedRank || updatedProfile) { @@ -1254,8 +1255,21 @@ public boolean isAdmin(Profile authUser) { } public boolean isMod(Profile authUser) { - return authUser != null && (isAdmin(authUser) || - (User.Groups.MODS.toString().equals(authUser.getGroups()) && authUser.getEditorRoleEnabled())); + if (authUser == null || !authUser.getEditorRoleEnabled()) { + return false; + } + if (ScooldUtils.getConfig().modsAccessAllSpaces()) { + return isAdmin(authUser) || User.Groups.MODS.toString().equals(authUser.getGroups()); + } else { + return isAdmin(authUser) || authUser.isModInCurrentSpace(); + } + } + + public boolean isModAnywhere(Profile authUser) { + if (authUser == null) { + return false; + } + return ScooldUtils.getConfig().modsAccessAllSpaces() ? isMod(authUser) : !authUser.getModspaces().isEmpty(); } public boolean isRecognizedAsAdmin(User u) { diff --git a/src/main/resources/static/scripts/scoold.js b/src/main/resources/static/scripts/scoold.js index 0a7980df..b7506b5a 100755 --- a/src/main/resources/static/scripts/scoold.js +++ b/src/main/resources/static/scripts/scoold.js @@ -610,8 +610,14 @@ $(function () { var dis = $(this); $.post(dis.attr("href")); dis.find("span").toggleClass("hide"); - $(".moderator-icon").toggleClass("hide"); - $(".moderator-icon").parent("div").toggleClass("hide"); + var modCount = $(".smallspacebox.orange-border").length; + dis.closest(".smallspacebox").toggleClass("orange-border"); + if ((modCount === 0 && $(".smallspacebox.orange-border").length === 0) || + (modCount === 0 && $(".smallspacebox.orange-border").length === 1) || + (modCount === 1 && $(".smallspacebox.orange-border").length === 0)) { + $(".moderator-icon").toggleClass("hide"); + $(".moderator-icon").parent("div").toggleClass("hide"); + } return false; }); diff --git a/src/main/resources/static/styles/style.css b/src/main/resources/static/styles/style.css index 310eaaa4..f6e90d85 100755 --- a/src/main/resources/static/styles/style.css +++ b/src/main/resources/static/styles/style.css @@ -585,6 +585,9 @@ small.badgebox, .custom-badge.small { color: inherit; } +.smallspacebox.orange-border { + border: 2px solid orange; +} .smallspacebox { border: 1px solid #DDD; background-color: transparent; diff --git a/src/main/resources/templates/profile.vm b/src/main/resources/templates/profile.vm index 58261e1b..66840a30 100755 --- a/src/main/resources/templates/profile.vm +++ b/src/main/resources/templates/profile.vm @@ -73,7 +73,7 @@ #end - #if($scooldUtils.isMod($showUser) || $scooldUtils.isAdmin($showUser)) + #if($scooldUtils.isModAnywhere($showUser) || $scooldUtils.isAdmin($showUser)) #set($hideBadgePanel = "") #else #set($hideBadgePanel = "hide") @@ -84,7 +84,7 @@ $!lang.get('admin') #end - #if ($showUser.groups == "mods")#set($hideModIcon = "")#else #set($hideModIcon = "hide")#end + #if ($scooldUtils.isModAnywhere($showUser))#set($hideModIcon = "")#else #set($hideModIcon = "hide")#end $!lang.get('mod') @@ -252,6 +252,22 @@ #end #end + #macro(makemodlink $s) + #if ($isAdmin && !$scooldUtils.config.modsAccessAllSpaces()) + #if(!$scooldUtils.isAdmin($showUser)) + + + #if ($showUser.isModInSpace($s))#set($hidemakemod = "hide")#else#set($unhidemakemod = "hide")#end + Unmake mod + Make mod + + + #end + #end + #end + + #macro(spaceboxclass $s)smallspacebox#if(!$scooldUtils.config.modsAccessAllSpaces() && $showUser.isModInSpace($s)) orange-border#end#end + #if ($showUser.hasSpaces() && $canEdit) #set($userSpaces = $showUser.allSpaces)

@@ -259,10 +275,12 @@

#if(!$showUser.hasSpaces() || $showUser.getSpaces().contains($scooldUtils.getDefaultSpace())) - - - $!lang.get('defaultspace') - + #if($userSpaces.size() > 5)
#end + + + $!lang.get('defaultspace') #makemodlink($scooldUtils.getSpaceId('default')) + + #if($userSpaces.size() > 5)
#end #end #foreach($space in $userSpaces) #if(!$scooldUtils.isDefaultSpace($space)) @@ -271,16 +289,12 @@ #else #set($spaceString = $scooldUtils.getSpaceName($space)) #end - #if($userSpaces.size() > 5) -
- #end - + #if($userSpaces.size() > 5)
#end + - ${spaceString} + ${spaceString} #makemodlink($scooldUtils.getSpaceId($space)) - #if($userSpaces.size() > 5) -
- #end + #if($userSpaces.size() > 5)
#end #end #end
@@ -361,7 +375,7 @@
#getreportlink($showUser "$profilelink/$!showUser.creatorid" "chip") - #if ($isAdmin) + #if ($isAdmin && $scooldUtils.config.modsAccessAllSpaces()) #if(!$scooldUtils.isAdmin($showUser)) #if ($showUser.groups == "mods")#set($hidemakemod = "hide")#else#set($unhidemakemod = "hide")#end