Merge pull request #9156 from DSpace/backport-9152-to-dspace-7_x

[Port dspace-7_x] Add a null check when assigning ldap groups
This commit is contained in:
Alan Orth
2023-10-29 21:11:35 +03:00
committed by GitHub

View File

@@ -713,8 +713,8 @@ public class LDAPAuthentication
private void assignGroups(String dn, ArrayList<String> group, Context context) { private void assignGroups(String dn, ArrayList<String> group, Context context) {
if (StringUtils.isNotBlank(dn)) { if (StringUtils.isNotBlank(dn)) {
System.out.println("dn:" + dn); System.out.println("dn:" + dn);
int i = 1; int groupmapIndex = 1;
String groupMap = configurationService.getProperty("authentication-ldap.login.groupmap." + i); String groupMap = configurationService.getProperty("authentication-ldap.login.groupmap." + groupmapIndex);
boolean cmp; boolean cmp;
@@ -725,52 +725,75 @@ public class LDAPAuthentication
String ldapSearchString = t[0]; String ldapSearchString = t[0];
String dspaceGroupName = t[1]; String dspaceGroupName = t[1];
// list of strings with dn from LDAP groups if (group == null) {
// inner loop cmp = StringUtils.containsIgnoreCase(dn, ldapSearchString + ",");
Iterator<String> groupIterator = group.iterator();
while (groupIterator.hasNext()) {
// save the current entry from iterator for further use
String currentGroup = groupIterator.next();
// very much the old code from DSpace <= 7.5
if (currentGroup == null) {
cmp = StringUtils.containsIgnoreCase(dn, ldapSearchString + ",");
} else {
cmp = StringUtils.equalsIgnoreCase(currentGroup, ldapSearchString);
}
if (cmp) { if (cmp) {
// assign user to this group assignGroup(context, groupmapIndex, dspaceGroupName);
try { }
Group ldapGroup = groupService.findByName(context, dspaceGroupName); } else {
if (ldapGroup != null) { // list of strings with dn from LDAP groups
groupService.addMember(context, ldapGroup, context.getCurrentUser()); // inner loop
groupService.update(context, ldapGroup); Iterator<String> groupIterator = group.iterator();
} else { while (groupIterator.hasNext()) {
// The group does not exist
log.warn(LogHelper.getHeader(context, // save the current entry from iterator for further use
"ldap_assignGroupsBasedOnLdapDn", String currentGroup = groupIterator.next();
"Group defined in authentication-ldap.login.groupmap." + i
+ " does not exist :: " + dspaceGroupName)); // very much the old code from DSpace <= 7.5
} if (currentGroup == null) {
} catch (AuthorizeException ae) { cmp = StringUtils.containsIgnoreCase(dn, ldapSearchString + ",");
log.debug(LogHelper.getHeader(context, } else {
"assignGroupsBasedOnLdapDn could not authorize addition to " + cmp = StringUtils.equalsIgnoreCase(currentGroup, ldapSearchString);
"group", }
dspaceGroupName));
} catch (SQLException e) { if (cmp) {
log.debug(LogHelper.getHeader(context, "assignGroupsBasedOnLdapDn could not find group", assignGroup(context, groupmapIndex, dspaceGroupName);
dspaceGroupName));
} }
} }
} }
groupMap = configurationService.getProperty("authentication-ldap.login.groupmap." + ++i); groupMap = configurationService.getProperty("authentication-ldap.login.groupmap." + ++groupmapIndex);
} }
} }
} }
/**
* Add the current authenticated user to the specified group
*
* @param context
* DSpace context
*
* @param groupmapIndex
* authentication-ldap.login.groupmap.* key index defined in dspace.cfg
*
* @param dspaceGroupName
* The DSpace group to add the user to
*/
private void assignGroup(Context context, int groupmapIndex, String dspaceGroupName) {
try {
Group ldapGroup = groupService.findByName(context, dspaceGroupName);
if (ldapGroup != null) {
groupService.addMember(context, ldapGroup, context.getCurrentUser());
groupService.update(context, ldapGroup);
} else {
// The group does not exist
log.warn(LogHelper.getHeader(context,
"ldap_assignGroupsBasedOnLdapDn",
"Group defined in authentication-ldap.login.groupmap." + groupmapIndex
+ " does not exist :: " + dspaceGroupName));
}
} catch (AuthorizeException ae) {
log.debug(LogHelper.getHeader(context,
"assignGroupsBasedOnLdapDn could not authorize addition to " +
"group",
dspaceGroupName));
} catch (SQLException e) {
log.debug(LogHelper.getHeader(context, "assignGroupsBasedOnLdapDn could not find group",
dspaceGroupName));
}
}
@Override @Override
public boolean isUsed(final Context context, final HttpServletRequest request) { public boolean isUsed(final Context context, final HttpServletRequest request) {
if (request != null && if (request != null &&