Skip to content
Snippets Groups Projects
Commit 5bd8fb07 authored by William Enright's avatar William Enright Committed by Larbi Gharib
Browse files

Refactoring and general fixes regarding group memberships

Change-Id: Icc32f7d7eb597db31df4037e13430e203cc8101b
parent 2c532a1c
No related branches found
No related tags found
No related merge requests found
......@@ -40,6 +40,7 @@ import net.jami.jams.common.dao.StatementElement;
import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.dao.connectivity.ConnectionPool;
import net.jami.jams.common.objects.user.User;
import net.jami.jams.common.objects.user.UserGroupMapping;
import net.jami.jams.common.objects.user.UserProfile;
import org.flywaydb.core.Flyway;
......@@ -143,6 +144,27 @@ public class DataStore implements AuthenticationSource {
return userProfileDao.storeObject(userProfile);
}
public boolean updateUserGroupMappings (UserProfile userProfile) {
StatementList statementList = new StatementList();
statementList.addStatement(new StatementElement("username", "=", userProfile.getUsername(), ""));
if (getUserGroupMappingsDao().getObjects(statementList) != null && !getUserGroupMappingsDao().getObjects(statementList).isEmpty()) {
UserGroupMapping mapping = getUserGroupMappingsDao().getObjects(statementList).get(0);
List<String> list = new ArrayList<>();
if (mapping.getUsername().equals(userProfile.getUsername())) {
String[] splits = mapping.getGroups().split(",");
for (int i = 0; i < splits.length; i++)
list.add(splits[i]);
}
if (!list.isEmpty())
userProfile.setGroupMemberships(list);
return (updateUserProfile(userProfile));
}
return true;
}
public boolean updateUserProfile(UserProfile userProfile){
StatementList update = new StatementList();
......
......@@ -32,8 +32,10 @@ public class UserGroupMapping implements DatabaseObject {
public void removeGroup(String s) {
if (this.groups.contains(s) ) {
if (this.groups.contains(","))
if (this.groups.contains(",")) {
this.groups = this.groups.replace(s + ",", "");
this.groups = this.groups.replace("," + s, "");
}
else
this.groups = "";
}
......
......@@ -39,12 +39,14 @@ import net.jami.jams.common.dao.StatementElement;
import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.objects.user.AccessLevel;
import net.jami.jams.common.objects.user.User;
import net.jami.jams.common.objects.user.UserGroupMapping;
import net.jami.jams.common.objects.user.UserProfile;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import static net.jami.jams.server.Server.dataStore;
......@@ -108,7 +110,9 @@ public class DirectoryEntryServlet extends HttpServlet {
}
if (callingUser.getAccessLevel() == AccessLevel.ADMIN || (callingUser.getAccessLevel() == AccessLevel.USER && callingUser.getUsername().equals(targetUser.getUsername()))) {
if (dataStore.updateUserProfile(userProfile))
select = new StatementList();
select.addStatement(new StatementElement("username", "=", userProfile.getUsername(), ""));
if (dataStore.updateUserGroupMappings(userProfile))
resp.setStatus(200);
else
resp.sendError(404, "Could not update the users's profile information");
......
......@@ -28,14 +28,16 @@ import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import net.jami.jams.common.annotations.JsonContent;
import net.jami.jams.common.objects.user.UserProfile;
import java.io.IOException;
import static net.jami.jams.server.Server.dataStore;
import static net.jami.jams.server.Server.userAuthenticationModule;
@WebServlet("/api/auth/userprofile/*")
@JsonContent
public class UserProfileServlet extends HttpServlet {
//Get the user profile
......@@ -49,6 +51,7 @@ public class UserProfileServlet extends HttpServlet {
if (v.getUserProfile(username) != null)
profile[0] = v.getUserProfile(username);
});
dataStore.updateUserGroupMappings(profile[0]);
if (profile[0] != null) {
resp.getOutputStream().write(JsonStream.serialize(profile[0]).getBytes());
......@@ -58,4 +61,4 @@ public class UserProfileServlet extends HttpServlet {
return;
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment