Skip to content

Commit

Permalink
added support for setting a preffered starting space each user indivi…
Browse files Browse the repository at this point in the history
…dually, closes #461
  • Loading branch information
albogdano committed Oct 24, 2024
1 parent d839031 commit 98e7163
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/erudika/scoold/ScooldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,14 @@ public String autoAssignSpaces() {
return getConfigParam("auto_assign_spaces", "");
}

@Documented(position = 1681,
identifier = "default_starting_space",
category = "Spaces",
description = "The starting space to be selected for all users upon sign in.")
public String defaultStartingSpace() {
return getConfigParam("default_starting_space", "");
}

@Documented(position = 1690,
identifier = "reset_spaces_on_new_assignment",
value = "true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public String post(@RequestParam(required = false) String tags, @RequestParam(re
}
setAnonymity(authUser, req.getParameter("anon"));
setDarkMode(authUser, req.getParameter("dark"));
authUser.setPreferredSpace(req.getParameter("preferredSpace"));
authUser.setReplyEmailsEnabled(Boolean.valueOf(replyEmailsOn) && utils.isReplyNotificationAllowed());
authUser.setCommentEmailsEnabled(Boolean.valueOf(commentEmailsOn) && utils.isCommentNotificationAllowed());
authUser.setFavtagsEmailsEnabled(Boolean.valueOf(favtagsEmailsOn) && utils.isFavTagsNotificationAllowed());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public String signup(@RequestParam String name, @RequestParam String email, @Req
if (!isEmailRegistered(email) && approvedDomain && isSubmittedByHuman(req) && goodPass) {
User u = pc.signIn("password", email + ":" + name + ":" + passw, false);
if (u != null && u.getActive()) {
setAuthCookie(u.getPassword(), req, res);
setAuthCookie(u, req, res);
triggerLoginEvent(u, req);
return "redirect:" + getBackToUrl(req);
} else {
Expand Down Expand Up @@ -359,7 +359,7 @@ private String loginWithIdToken(String jwt, HttpServletRequest req, HttpServletR
private String onAuthSuccess(User u, HttpServletRequest req, HttpServletResponse res) {
if (u != null && utils.isEmailDomainApproved(u.getEmail())) {
// the user password in this case is a Bearer token (JWT)
setAuthCookie(u.getPassword(), req, res);
setAuthCookie(u, req, res);
triggerLoginEvent(u, req);
return "redirect:" + getBackToUrl(req);
} else if (u != null && !utils.isEmailDomainApproved(u.getEmail())) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/erudika/scoold/core/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class Profile extends Sysprop {
@Stored private List<Map<String, String>> customBadges;
@Stored private String pendingEmail;
@Stored private Boolean editorRoleEnabled;
@Stored private String preferredSpace;

private transient String currentSpace;
private transient String newbadges;
Expand Down Expand Up @@ -368,6 +369,18 @@ public void setFavspaces(Set<String> favspaces) {
this.favspaces = favspaces;
}

public String getPreferredSpace() {
// returns a preferred staring space upon login
if (StringUtils.isBlank(preferredSpace)) {
preferredSpace = ScooldUtils.getConfig().defaultStartingSpace();
}
return preferredSpace;
}

public void setPreferredSpace(String preferredSpace) {
this.preferredSpace = preferredSpace;
}

public boolean isModInCurrentSpace() {
return isModInSpace(currentSpace);
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/erudika/scoold/utils/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.erudika.para.core.utils.Utils;
import com.erudika.scoold.ScooldConfig;
import static com.erudika.scoold.ScooldServer.HOMEPAGE;
import com.erudika.scoold.core.Profile;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -269,15 +270,22 @@ public static void getDefaultAvatarImage(HttpServletResponse res) {

/**
* Sets the session cookie.
* @param jwt a JWT from Para
* @param authUser auth user
* @param req req
* @param res res
*/
public static void setAuthCookie(String jwt, HttpServletRequest req, HttpServletResponse res) {
if (StringUtils.isBlank(jwt)) {
return;
public static void setAuthCookie(User authUser, HttpServletRequest req, HttpServletResponse res) {
if (!StringUtils.isBlank(authUser.getPassword())) {
setRawCookie(CONF.authCookie(), authUser.getPassword(), req, res, "Lax", CONF.sessionTimeoutSec());
}
try {
Profile authu = ScooldUtils.getInstance().getParaClient().read(Profile.id(authUser.getId()));
if (authu != null && !StringUtils.isBlank(authu.getPreferredSpace())) {
ScooldUtils.getInstance().storeSpaceIdInCookie(authu.getPreferredSpace(), req, res);
}
} catch (Exception ex) {
logger.error(null, ex);
}
setRawCookie(CONF.authCookie(), jwt, req, res, "Lax", CONF.sessionTimeoutSec());
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/erudika/scoold/utils/ScooldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import jakarta.validation.ConstraintViolation;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
Expand Down Expand Up @@ -233,7 +234,7 @@ public static ScooldConfig getConfig() {

public static void setParaEndpointAndApiPath(ParaClient pc) {
try {
URL endpoint = new URL(CONF.paraEndpoint());
URL endpoint = new URI(CONF.paraEndpoint()).toURL();
if (!StringUtils.isBlank(endpoint.getPath()) && !"/".equals(endpoint.getPath())) {
// support Para deployed under a specific context path
pc.setEndpoint(StringUtils.removeEnd(CONF.paraEndpoint(), endpoint.getPath()));
Expand Down Expand Up @@ -1455,7 +1456,7 @@ public void storeSpaceIdInCookie(String space, HttpServletRequest req, HttpServl
// used for setting the space from a direct URL to a particular space
req.setAttribute(CONF.spaceCookie(), space);
HttpUtils.setRawCookie(CONF.spaceCookie(), Utils.base64encURL(space.getBytes()),
req, res, "Strict", StringUtils.isBlank(space) ? 0 : 365 * 24 * 60 * 60);
req, res, "Lax", StringUtils.isBlank(space) ? 0 : 365 * 24 * 60 * 60);
}

public String verifyExistingSpace(Profile authUser, String space) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ settings.scancode = Scan the QR code with an app like Google Authenticator and e
settings.savecode = Save this recovery code - it's only shown once!
settings.twofaon = Two-factor authentication is enabled.
settings.twofaoff = To disable, enter the two-factor authentication code or the recovery code.
settings.preferredspace = Preferred starting space

reports.spam = Spam or commercial content
reports.offensive = Offensive content, violence or abuse
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ img.profile-pic {
}
.user-card img.profile-pic, .user-card-compact img.profile-pic {
width: 100%;
padding: 6px 0px 0px 6px;
padding: 6px 0px 0px 7px;
}
img.profile-pic:before {
content: " ";
Expand Down
19 changes: 17 additions & 2 deletions src/main/resources/templates/settings.vm
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,23 @@
</div>

<form method="post">
#set($userSpaces = $authUser.allSpaces)
<h3 class="ptl">$!lang.get('settings.preferredspace')</h3>
<div class="mbm">
<div class="input-field">
<i class="fa fa-th-large pvm prefix grey-text"></i>
<select name="preferredSpace" >
<option value="scooldspace:*" #if($authUser.preferredSpace.isBlank())selected="selected"#end>$!lang.get('allspaces')</option>
<option value="scooldspace:default" #if($authUser.preferredSpace == "scooldspace:default")selected="selected"#end>$!lang.get('defaultspace')</option>
#foreach($space in $userSpaces)
<option value="$space" #if($authUser.preferredSpace == $space)selected="selected"#end>$scooldUtils.getSpaceName($space)</option>
#end
</select>
</div>
</div>

#if($scooldUtils.isNearMeFeatureEnabled())
<h3>$!lang.get('posts.locationfilter')</h3>
<h3 class="ptl">$!lang.get('posts.locationfilter')</h3>
<div class="mbm">
<p>$!lang.get('settings.location')</p>
<input type="hidden" name="latlng" class="latlngbox" value="$!{authUser.latlng}">
Expand Down Expand Up @@ -179,7 +194,7 @@
<select multiple name="favspaces">
<option value="" disabled #if($authUser.favspaces.isEmpty())selected#end>$!lang.get('allspaces')</option>
<option value="$scooldUtils.getDefaultSpace()">$!lang.get('defaultspace')</option>
#foreach($space in $authUser.allSpaces)
#foreach($space in $userSpaces)
#set($spaceId = $!scooldUtils.getSpaceId($!space))
#if($newpostEmailsEnabled && $authUser.favspaces.contains($spaceId))
<option value="$spaceId" selected="selected">$!scooldUtils.getSpaceName($space)</option>
Expand Down

0 comments on commit 98e7163

Please sign in to comment.