Skip to content

Commit

Permalink
added support for having individually assigned moderators for each space
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Oct 5, 2023
1 parent c73198f commit 4170223
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/erudika/scoold/core/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class Profile extends Sysprop {
@Stored @URL private String website;
@Stored private List<String> favtags;
@Stored private Set<String> favspaces;
@Stored private Set<String> modspaces;
@Stored private Set<String> spaces;
@Stored private Boolean replyEmailsEnabled;
@Stored private Boolean commentEmailsEnabled;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> getModspaces() {
if (modspaces == null) {
modspaces = new LinkedHashSet<>();
}
return modspaces;
}

public void setModspaces(Set<String> modspaces) {
this.modspaces = modspaces;
}

public String getLatlng() {
return latlng;
}
Expand Down Expand Up @@ -347,6 +368,14 @@ public void setFavspaces(Set<String> 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<String> getSpaces() {
if (ScooldUtils.getInstance().isMod(this) && ScooldUtils.getConfig().modsAccessAllSpaces()) {
ScooldUtils utils = ScooldUtils.getInstance();
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/erudika/scoold/utils/ScooldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/static/scripts/scoold.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/static/styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 29 additions & 15 deletions src/main/resources/templates/profile.vm
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
#end
</div>
</div>
#if($scooldUtils.isMod($showUser) || $scooldUtils.isAdmin($showUser))
#if($scooldUtils.isModAnywhere($showUser) || $scooldUtils.isAdmin($showUser))
#set($hideBadgePanel = "")
#else
#set($hideBadgePanel = "hide")
Expand All @@ -84,7 +84,7 @@
<i class="fa fa-asterisk orange-text"></i> $!lang.get('admin')
</span>
#end
#if ($showUser.groups == "mods")#set($hideModIcon = "")#else #set($hideModIcon = "hide")#end
#if ($scooldUtils.isModAnywhere($showUser))#set($hideModIcon = "")#else #set($hideModIcon = "hide")#end
<span class="chip badgebox black-text moderator-icon ${hideModIcon}">
<i class="fa fa-dot-circle-o orange-text"></i> $!lang.get('mod')
</span>
Expand Down Expand Up @@ -252,17 +252,35 @@
#end
#end

#macro(makemodlink $s)
#if ($isAdmin && !$scooldUtils.config.modsAccessAllSpaces())
#if(!$scooldUtils.isAdmin($showUser))
<small>
<a href="$profilelink/$showUser.creatorid/make-mod?space=$!s" class="make-mod-btn">
#if ($showUser.isModInSpace($s))#set($hidemakemod = "hide")#else#set($unhidemakemod = "hide")#end
<span class="#if($showUser.isModInSpace($s))#else hide#end">Unmake mod</span>
<span class="#if($showUser.isModInSpace($s))hide#else#end">Make mod</span>
</a>
</small>
#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)
<h4 class="grey-text text-darken-1 pvs">
$!lang.get('spaces.title') <strong>$!userSpaces.size()</strong>
</h4>
<div class="#if($userSpaces.size() > 5)row#end">
#if(!$showUser.hasSpaces() || $showUser.getSpaces().contains($scooldUtils.getDefaultSpace()))
<span class="chip smallspacebox">
<i class="fa fa-th-large grey-text"></i>
<span class="mhm">$!lang.get('defaultspace')</span>
</span>
#if($userSpaces.size() > 5)<div class="col m6 l4">#end
<span class="chip #spaceboxclass('default')">
<i class="fa fa-th-large grey-text"></i>
<span class="mhm">$!lang.get('defaultspace') #makemodlink($scooldUtils.getSpaceId('default'))</span>
</span>
#if($userSpaces.size() > 5)</div>#end
#end
#foreach($space in $userSpaces)
#if(!$scooldUtils.isDefaultSpace($space))
Expand All @@ -271,16 +289,12 @@
#else
#set($spaceString = $scooldUtils.getSpaceName($space))
#end
#if($userSpaces.size() > 5)
<div class="col m6 l4">
#end
<span class="chip smallspacebox">
#if($userSpaces.size() > 5)<div class="col m6 l4">#end
<span class="chip #spaceboxclass($space)">
<i class="fa fa-th-large grey-text"></i>
<span class="mhm">${spaceString}</span>
<span class="mhs">${spaceString} #makemodlink($scooldUtils.getSpaceId($space))</span>
</span>
#if($userSpaces.size() > 5)
</div>
#end
#if($userSpaces.size() > 5)</div>#end
#end
#end
</div>
Expand Down Expand Up @@ -361,7 +375,7 @@
<div class="row">
<div class="col m6">
#getreportlink($showUser "$profilelink/$!showUser.creatorid" "chip")
#if ($isAdmin)
#if ($isAdmin && $scooldUtils.config.modsAccessAllSpaces())
#if(!$scooldUtils.isAdmin($showUser))
<a href="$profilelink/$showUser.creatorid/make-mod" class="chip make-mod-btn">
#if ($showUser.groups == "mods")#set($hidemakemod = "hide")#else#set($unhidemakemod = "hide")#end
Expand Down

0 comments on commit 4170223

Please sign in to comment.