From 0649040f1dac3024961f7ab0ed77e6fa618c8f51 Mon Sep 17 00:00:00 2001 From: William Enright <william.enright@savoirfairelinux.com> Date: Thu, 13 Aug 2020 17:01:49 -0400 Subject: [PATCH] Fixed immutable list sent to UserProfile object as group memberships Change-Id: I91e357cd00a007f0ecf712c055fbcf6545098c63 --- .../jams/common/objects/user/UserProfile.java | 4 +- jams-react-client/src/views/Groups/Groups.js | 2 +- .../UserProfile/EditCreateUserProfile.js | 5 +- .../core/workflows/AddUserToGroupFlow.java | 88 +++++++++---------- 4 files changed, 45 insertions(+), 54 deletions(-) diff --git a/jams-common/src/main/java/net/jami/jams/common/objects/user/UserProfile.java b/jams-common/src/main/java/net/jami/jams/common/objects/user/UserProfile.java index 5965dc36..99c11ac3 100644 --- a/jams-common/src/main/java/net/jami/jams/common/objects/user/UserProfile.java +++ b/jams-common/src/main/java/net/jami/jams/common/objects/user/UserProfile.java @@ -89,9 +89,7 @@ public class UserProfile implements DatabaseObject { this.phoneNumberExtension = rs.getString("phoneNumberExtension"); this.faxNumber = rs.getString("faxNumber"); this.mobileNumber = rs.getString("mobileNumber"); - String groupMemberships = rs.getString("groupMemberships"); - String[] groups = groupMemberships.split(","); - this.groupMemberships = Arrays.asList(groups); + this.groupMemberships = new ArrayList<>(Arrays.asList(rs.getString("groupMemberships").split(","))); } @JsonIgnore diff --git a/jams-react-client/src/views/Groups/Groups.js b/jams-react-client/src/views/Groups/Groups.js index c537583f..16d0fd8a 100644 --- a/jams-react-client/src/views/Groups/Groups.js +++ b/jams-react-client/src/views/Groups/Groups.js @@ -184,7 +184,7 @@ export default function Groups() { const handleCreateGroup = () => { axios(configApiCall(api_path_post_create_group+"?name="+groupName, 'POST', null, null)).then(() => { - console.log("Successfully create " + groupName) + console.log("Successfully created " + groupName) setOpenCreate(false); }).catch((error) => { console.log("Error creating group: " + error) diff --git a/jams-react-client/src/views/UserProfile/EditCreateUserProfile.js b/jams-react-client/src/views/UserProfile/EditCreateUserProfile.js index 97516ec8..372be47e 100644 --- a/jams-react-client/src/views/UserProfile/EditCreateUserProfile.js +++ b/jams-react-client/src/views/UserProfile/EditCreateUserProfile.js @@ -209,7 +209,8 @@ export default function EditCreateUserProfile(props) { 'faxNumber': faxNumber, 'phoneNumber': phoneNumber, 'phoneNumberExtension': extension, - 'mobileNumber': mobileNumber + 'mobileNumber': mobileNumber, + 'jamiId': "" } axios(configApiCall(api_path_post_create_user + "?username="+ data.username, 'POST', null, null)).then((response) => { @@ -337,7 +338,7 @@ export default function EditCreateUserProfile(props) { <DialogTitle id="alert-dialog-title">{"Temporary password"}</DialogTitle> <DialogContent> <DialogContentText id="alert-dialog-description"> - New user successfully created. Here is the one time password: <strong>{temporaryPassword}</strong> ? + New user successfully created. Here is the one time password: <strong>{temporaryPassword}</strong> </DialogContentText> </DialogContent> <DialogActions> diff --git a/jams-server/src/main/java/net/jami/jams/server/core/workflows/AddUserToGroupFlow.java b/jams-server/src/main/java/net/jami/jams/server/core/workflows/AddUserToGroupFlow.java index 438b27d8..412b2fe6 100644 --- a/jams-server/src/main/java/net/jami/jams/server/core/workflows/AddUserToGroupFlow.java +++ b/jams-server/src/main/java/net/jami/jams/server/core/workflows/AddUserToGroupFlow.java @@ -2,6 +2,8 @@ package net.jami.jams.server.core.workflows; import lombok.extern.slf4j.Slf4j; +import net.jami.jams.common.authentication.AuthenticationSource; +import net.jami.jams.common.authmodule.AuthModuleKey; import net.jami.jams.common.dao.StatementElement; import net.jami.jams.common.dao.StatementList; import net.jami.jams.common.objects.user.Group; @@ -12,6 +14,7 @@ import net.minidev.json.JSONArray; import java.util.ArrayList; import java.util.List; +import java.util.Map; import static net.jami.jams.server.Server.dataStore; import static net.jami.jams.server.Server.userAuthenticationModule; @@ -21,55 +24,44 @@ public class AddUserToGroupFlow { public static void addUserToGroup(String groupName, String username) { - try { - userAuthenticationModule.getAuthSources().forEach((k, v) -> { - List<UserProfile> profiles = v.searchUserProfiles(username, "LOGON_NAME"); - if (!profiles.isEmpty()) { - UserProfile profile = profiles.get(0); - StatementList statementList = new StatementList(); - statementList.addStatement(new StatementElement("name", "=", username, "")); - - if (profile != null) { - if (profile.getGroupMemberships() == null) - profile.setGroupMemberships(new ArrayList<>()); - profile.getGroupMemberships().add(groupName); - } - - statementList = new StatementList(); - statementList.addStatement(new StatementElement("username", "=", username, "")); - if (dataStore.getUserGroupMappingsDao().getObjects(statementList).isEmpty()) { - // if the mapping doesn't exist, create it and add the group directly. - UserGroupMapping newMapping = new UserGroupMapping(); - newMapping.setUsername(username); - newMapping.setGroups(""); - newMapping.addGroup(groupName); - dataStore.getUserGroupMappingsDao().storeObject(newMapping); - } else { - // otherwise, update the object. - - UserGroupMapping mapping = dataStore.getUserGroupMappingsDao().getObjects(statementList).get(0); - mapping.addGroup(groupName); - String newGroups = mapping.getGroups(); - - StatementList update = new StatementList(); - StatementElement st0 = new StatementElement("groups","=", newGroups,""); - update.addStatement(st0); - StatementList constraint = new StatementList(); - StatementElement st1 = new StatementElement("username","=",username,""); - constraint.addStatement(st1); - dataStore.getUserGroupMappingsDao().updateObject(update, constraint); - } - } + List<UserProfile> profiles = v.searchUserProfiles(username, "LOGON_NAME"); + if (!profiles.isEmpty()) { + UserProfile profile = profiles.get(0); + StatementList statementList = new StatementList(); + statementList.addStatement(new StatementElement("name", "=", username, "")); + + if (profile != null) { + if (profile.getGroupMemberships() == null) + profile.setGroupMemberships(new ArrayList<>()); + profile.getGroupMemberships().add(groupName); + } + + statementList = new StatementList(); + statementList.addStatement(new StatementElement("username", "=", username, "")); + if (dataStore.getUserGroupMappingsDao().getObjects(statementList).isEmpty()) { + // if the mapping doesn't exist, create it and add the group directly. + UserGroupMapping newMapping = new UserGroupMapping(); + newMapping.setUsername(username); + newMapping.setGroups(""); + newMapping.addGroup(groupName); + dataStore.getUserGroupMappingsDao().storeObject(newMapping); + } else { + // otherwise, update the object. + + UserGroupMapping mapping = dataStore.getUserGroupMappingsDao().getObjects(statementList).get(0); + mapping.addGroup(groupName); + String newGroups = mapping.getGroups(); + + StatementList update = new StatementList(); + StatementElement st0 = new StatementElement("groups", "=", newGroups, ""); + update.addStatement(st0); + StatementList constraint = new StatementList(); + StatementElement st1 = new StatementElement("username", "=", username, ""); + constraint.addStatement(st1); + dataStore.getUserGroupMappingsDao().updateObject(update, constraint); + } + } }); - - - - - - } catch (Exception e) { - log.error("An error occurred while attempting to add " + username + " to group with name " + groupName); - } - } } -- GitLab