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

Fixed being unable to add LDAP users to groups

Change-Id: I048f42efdfad6d8da8f7c17da4308a8070726934
parent fbb79903
Branches
No related tags found
No related merge requests found
......@@ -157,4 +157,27 @@ public class UserProfile implements DatabaseObject {
profile.getLastName().equals(this.lastName));
}
public void setDefaultValues() {
if (this.firstName == null)
this.firstName = "";
if (this.lastName == null)
this.lastName = "";
if (this.email == null)
this.email = "";
if (this.profilePicture == null)
this.profilePicture = "";
if (this.organization == null)
this.organization = "";
if (this.phoneNumber == null)
this.phoneNumber = "";
if (this.phoneNumberExtension == null)
this.phoneNumberExtension = "";
if (this.faxNumber == null)
this.faxNumber = "";
if (this.mobileNumber == null)
this.mobileNumber = "";
if (this.groupMemberships == null)
this.groupMemberships = new ArrayList<>();
}
}
......@@ -40,22 +40,27 @@ public class AddUserToGroupFlow {
// otherwise, update the object.
mapping = dataStore.getUserGroupMappingsDao().getObjects(statementList).get(0);
mapping.addGroup(groupName);
String newGroups = mapping.getGroups();
if (!mapping.getGroups().contains(groupName)) {
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);
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);
}
}
if (profile != null) {
if (profile.getGroupMemberships() == null)
profile.setGroupMemberships(new ArrayList<>());
profile.getGroupMemberships().add(groupName);
if (!profile.getGroupMemberships().contains(groupName))
profile.getGroupMemberships().add(groupName);
dataStore.updateUserProfile(profile);
}
}
......
......@@ -52,6 +52,7 @@ public class RegisterDeviceFlow {
StatementList statementList = new StatementList();
statementList.addStatement(new StatementElement("username", "=", username, ""));
User user = dataStore.getUserDao().getObjects(statementList).get(0);
// UserProfile userProfile = dataStore.getUserProfileDao().getObjects(statementList).get(0);
UserProfile userProfile = userAuthenticationModule.getAuthSources()
.get(new AuthModuleKey(user.getRealm(),user.getUserType()))
.searchUserProfiles(username,"LOGON_NAME").get(0);
......
......@@ -171,12 +171,6 @@ public class GroupServlet extends HttpServlet {
List<UserProfile> validProfiles = dataStore.getUserProfileDao().getObjects(null).stream().filter(profile ->
profile.getGroupMemberships().remove(req.getParameter("groupName"))).collect(Collectors.toList());
validProfiles.parallelStream().forEach(profile -> {
StatementList statementList = new StatementList();
......@@ -207,4 +201,4 @@ public class GroupServlet extends HttpServlet {
resp.sendError(500, "Could not delete the group successfully!");
}
}
}
}
\ No newline at end of file
......@@ -51,6 +51,7 @@ public class SearchDirectoryServlet extends HttpServlet {
//The search directory function does not automatically create users, this would be costly at this point
//right now, we will implement it when Jami supports lists of users. this is a work in progress as it
//requires changes on the name server as well.
List<UserProfile> userProfiles = new ArrayList<>();
/**
*
......@@ -58,26 +59,24 @@ public class SearchDirectoryServlet extends HttpServlet {
@Override
@JsonContent
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
List<UserProfile> userProfiles = new ArrayList<>();
userAuthenticationModule.getAuthSources().forEach((k, v) -> {
List<UserProfile> profiles;
userAuthenticationModule.getAuthSources().forEach((k, v) -> {
if (req.getParameter("queryString").equals("*"))
profiles = v.searchUserProfiles(req.getParameter("queryString"), "FULL_TEXT_NAME");
userProfiles = v.searchUserProfiles(req.getParameter("queryString"), "FULL_TEXT_NAME");
else {
profiles = v.searchUserProfiles(req.getParameter("queryString"), "FULL_TEXT_NAME");
userProfiles = v.searchUserProfiles(req.getParameter("queryString"), "FULL_TEXT_NAME");
List<UserProfile> profiles2 = v.searchUserProfiles(req.getParameter("queryString"), "LOGON_NAME");
for (Iterator<UserProfile> it = profiles.iterator(); it.hasNext(); ) {
for (Iterator<UserProfile> it = userProfiles.iterator(); it.hasNext(); ) {
UserProfile p = it.next();
for (UserProfile p2 : profiles2) {
if (p2.equals(p))
it.remove();
}
}
profiles.addAll(profiles2);
userProfiles.addAll(profiles2);
}
profiles.parallelStream().forEach(profile -> {
userProfiles.parallelStream().forEach(profile -> {
StatementList statementList = new StatementList();
StatementElement statementElement = new StatementElement("username", "=", profile.getUsername(), "");
statementList.addStatement(statementElement);
......@@ -95,7 +94,6 @@ public class SearchDirectoryServlet extends HttpServlet {
profile.setId(results.get(0).getJamiId());
}
}
userProfiles.add(profile);
});
});
......
......@@ -29,6 +29,12 @@
<version>${embedded.ldap.unit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.jami</groupId>
<artifactId>jams-server</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
......
......@@ -23,6 +23,8 @@
package net.jami.jams.ldap.connector.service;
import lombok.extern.slf4j.Slf4j;
import net.jami.jams.common.dao.StatementElement;
import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.objects.user.UserProfile;
import net.jami.jams.ldap.connector.LDAPConnector;
import org.ldaptive.Connection;
......@@ -37,6 +39,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import static net.jami.jams.server.Server.dataStore;
@Slf4j
public class UserProfileService {
......@@ -57,7 +61,18 @@ public class UserProfileService {
SearchOperation search = new SearchOperation(connectionFactory);
SearchResponse res = search.execute(buildRequest(queryString,field, exactMatch));
if (res.getEntries().size() == 0) return new ArrayList<>();
return res.getEntries().stream().map(UserProfileService::profileFromResponse).collect(Collectors.toList());
List<UserProfile> profilesFromResponse = res.getEntries().stream().map(UserProfileService::profileFromResponse).collect(Collectors.toList());
for (UserProfile p: profilesFromResponse) {
StatementList statementList = new StatementList();
StatementElement st = new StatementElement("username", "=", p.getUsername(), "");
statementList.addStatement(st);
if (dataStore.getUserProfileDao().getObjects(statementList).isEmpty())
dataStore.getUserProfileDao().storeObject(p);
}
return profilesFromResponse;
} catch (Exception e) {
log.error("Could not search LDAP directory with error " + e.toString());
return null;
......@@ -104,6 +119,7 @@ public class UserProfileService {
UserProfile.exposedMethods.get("set" + fieldMap.get(attribute)).invoke(userProfile, entry.getAttribute(attribute).getStringValue());
}
}
userProfile.setDefaultValues();
return userProfile;
}
catch (Exception e){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment