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 efec736
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 10 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 @@ -414,6 +414,24 @@ public String toggleEditorRole(HttpServletRequest req, Model model) {
return "redirect:" + HttpUtils.getBackToUrl(req, true);
}

@PostMapping("/{id}/set-preferred-space")
public String setPreferredSpace(@PathVariable String id, @RequestParam String preferredSpace,
HttpServletRequest req, HttpServletResponse res, Model model) {
Profile authUser = utils.getAuthUser(req);
Profile showUser = getProfileForEditing(id, authUser);
if (authUser != null && !StringUtils.isBlank(preferredSpace) &&
StringUtils.equalsAny(authUser.getGroups(), User.Groups.ADMINS.toString(), User.Groups.MODS.toString())) {
showUser.setPreferredSpace(preferredSpace);
showUser.update();
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
return "base";
} else {
return "redirect:" + HttpUtils.getBackToUrl(req, true);
}
}

private String changeEmail(User u, Profile showUser, String email) {
boolean approvedDomain = utils.isEmailDomainApproved(email);
if (approvedDomain && canChangeEmail(u, email)) {
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
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
14 changes: 14 additions & 0 deletions src/main/resources/templates/profile.vm
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,20 @@
<input id="website_text" name="website" type="url" value="$!{showUser.website}">
</div>

#if($isMod && (!$scooldUtils.isMod($showUser) || $isMyProfile))
<b>Preferred starting space:</b>
<div class="input-field">
<i class="fa fa-th-large pvm prefix grey-text"></i>
<select name="preferredSpace" >
<option value="scooldspace:*" #if($showUser.preferredSpace.isBlank())selected="selected"#end>$!lang.get('allspaces')</option>
<option value="scooldspace:default" #if($showUser.preferredSpace == "scooldspace:default")selected="selected"#end>$!lang.get('defaultspace')</option>
#foreach($space in $userSpaces)
<option value="$space" #if($showUser.preferredSpace == $space)selected="selected"#end>$scooldUtils.getSpaceName($space)</option>
#end
</select>
</div>
#end

<div class="center mtl">
<button type="submit" class="btn waves-effect waves-light mhl">$!lang.get('save')</button>
<button type="button" class="btn waves-effect waves-light canceledit ${hideview1}">$!{lang.get('close')}</button>
Expand Down

0 comments on commit efec736

Please sign in to comment.