Skip to content
Snippets Groups Projects
Commit 6c4fde76 authored by srosse's avatar srosse
Browse files

OO-4842: option to synchronize LDAP group coaches as participant too

Option to synchronize LDAP group coaches automatically as participant
too
parent faa8e66f
No related branches found
No related tags found
No related merge requests found
......@@ -99,6 +99,8 @@ public class LDAPSyncConfiguration {
private String learningResourceManagerRoleAttribute;
private String learningResourceManagerRoleValue;
private String groupCoachAsParticipant;
/**
* Static user properties that should be added to user when syncing
*/
......@@ -260,6 +262,18 @@ public class LDAPSyncConfiguration {
public void setAuthorRoleValue(String value) {
this.authorRoleValue = value;
}
public boolean isGroupCoachParticipant() {
return "true".equals(groupCoachAsParticipant);
}
public String getGroupCoachAsParticipant() {
return groupCoachAsParticipant;
}
public void setGroupCoachAsParticipant(String groupCoachAsParticipant) {
this.groupCoachAsParticipant = groupCoachAsParticipant;
}
public List<String> getUserManagersGroupBase() {
return userManagersGroupBase;
......
......@@ -39,6 +39,7 @@
<property name="coachedGroupAttributeSeparator" value="${ldap.user.coachedGroupAttributeSeparator}"/>
<property name="coachRoleAttribute" value="${ldap.coachRoleAttribute}"/>
<property name="coachRoleValue" value="${ldap.coachRoleValue}"/>
<property name="groupCoachAsParticipant" value="${ldap.groupCoachAsParticipant}"/>
<!-- sync authors -->
<property name="authorsGroupBase" >
......
......@@ -26,6 +26,7 @@ import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.SizeLimitExceededException;
......@@ -144,15 +145,15 @@ public class LDAPDAO {
counter++;
}
} catch (SizeLimitExceededException e) {
log.error("SizeLimitExceededException after "
+ counter
+ " records when getting all users from LDAP, reconfigure your LDAP server, hints: http://www.ldapbrowser.com/forum/viewtopic.php?t=14");
log.error("SizeLimitExceededException after {} records when getting all users from LDAP, reconfigure your LDAP server, hints: http://www.ldapbrowser.com/forum/viewtopic.php?t=14", counter);
} catch (NameNotFoundException e) {
log.warn("Name not found: {} in base: {}", filter, ldapBase);
} catch (NamingException e) {
log.error("NamingException when trying to search from LDAP using ldapBase::" + ldapBase + " on row::" + counter, e);
log.error("NamingException when trying to search from LDAP using ldapBase::{} on row::{}", ldapBase,counter, e);
} catch (Exception e) {
log.error("Exception when trying to search from LDAP using ldapBase::" + ldapBase + " on row::" + counter, e);
log.error("Exception when trying to search from LDAP using ldapBase::{} on row::{}", ldapBase, counter, e);
}
log.debug("finished search for ldapBase:: " + ldapBase);
log.debug("finished search for ldapBase:: {}", ldapBase);
}
......
......@@ -605,7 +605,11 @@ public class LDAPLoginManagerImpl implements LDAPLoginManager, AuthenticationPro
String olatProperty = mapLdapAttributeToOlatProperty(attr.getID());
if (!attr.getID().equalsIgnoreCase(syncConfiguration.getOlatPropertyToLdapAttribute(LDAPConstants.LDAP_USER_IDENTIFYER)) ) {
String ldapValue = getAttributeValue(attr);
if (olatProperty == null || ldapValue == null) continue;
if (olatProperty == null || ldapValue == null) {
continue;
} else if(ldapValue != null && ldapValue.length() > 250) {
ldapValue = ldapValue.substring(0, 250);
}
user.setProperty(olatProperty, ldapValue);
}
}
......@@ -791,7 +795,8 @@ public class LDAPLoginManagerImpl implements LDAPLoginManager, AuthenticationPro
boolean coach = groupList != null && groupList.contains(group.getCommonName());
if(coach) {
businessGroupRelationDao.addRole(identity, managedGroup, GroupRoles.coach.name());
} else {
}
if(!coach || syncConfiguration.isGroupCoachParticipant()) {
businessGroupRelationDao.addRole(identity, managedGroup, GroupRoles.participant.name());
}
}
......@@ -1440,25 +1445,24 @@ public class LDAPLoginManagerImpl implements LDAPLoginManager, AuthenticationPro
}
int count = 0;
for(LDAPUser participant:participants) {
IdentityRef memberIdentity = participant.getCachedIdentity();
if(memberIdentity != null && memberIdentity.getKey() != null) {
syncMembership(businessGroup, memberIdentity, false);
currentMemberKeys.remove(memberIdentity.getKey());
}
if(count % 20 == 0) {
dbInstance.commitAndCloseSession();
Set<LDAPUser> members = new HashSet<>(participants);
members.addAll(coaches);
for(LDAPUser member:members) {
boolean participant = participants.contains(member);
boolean coach = coaches.contains(member);
if(syncConfiguration.isGroupCoachParticipant()) {
if(coach && !participant) {
participant = true;
}
} else if(coach && participant) {
participant = false;
}
count++;
}
for(LDAPUser coach:coaches) {
IdentityRef memberIdentity = coach.getCachedIdentity();
IdentityRef memberIdentity = member.getCachedIdentity();
if(memberIdentity != null && memberIdentity.getKey() != null) {
syncMembership(businessGroup, memberIdentity, true);
syncMemberships(businessGroup, memberIdentity, coach, participant);
currentMemberKeys.remove(memberIdentity.getKey());
}
if(count % 20 == 0) {
dbInstance.commitAndCloseSession();
}
......@@ -1479,37 +1483,29 @@ public class LDAPLoginManagerImpl implements LDAPLoginManager, AuthenticationPro
}
dbInstance.commitAndCloseSession();
}
private void syncMembership(BusinessGroup businessGroup, IdentityRef identityRef, boolean coach) {
if(identityRef != null) {
List<String> roles = businessGroupRelationDao.getRoles(identityRef, businessGroup);
if(roles.isEmpty()) {
Identity identity = securityManager.loadIdentityByKey(identityRef.getKey());
if(coach) {
businessGroupRelationDao.addRole(identity, businessGroup, GroupRoles.coach.name());
} else {
businessGroupRelationDao.addRole(identity, businessGroup, GroupRoles.participant.name());
}
} else if(coach && roles.size() == 1 && roles.contains(GroupRoles.coach.name())) {
//coach and only coach, do nothing
} else if(!coach && roles.size() == 1 && roles.contains(GroupRoles.participant.name())) {
//participant and only participant, do nothing
} else {
boolean already = false;
Identity identity = securityManager.loadIdentityByKey(identityRef.getKey());
String mainRole = coach ? GroupRoles.coach.name() : GroupRoles.participant.name();
for(String role:roles) {
if(mainRole.equals(role)) {
already = true;
} else {
businessGroupRelationDao.removeRole(identity, businessGroup, role);
}
}
if(!already) {
businessGroupRelationDao.addRole(identity, businessGroup, mainRole);
}
}
private void syncMemberships(BusinessGroup businessGroup, IdentityRef identityRef, boolean coach, boolean participant) {
if(identityRef == null || businessGroup == null) return;
List<String> roles = businessGroupRelationDao.getRoles(identityRef, businessGroup);
if((coach && participant && roles.size() == 2 && roles.contains(GroupRoles.coach.name()) && roles.contains(GroupRoles.participant.name()))
|| (coach && !participant && roles.size() == 1 && roles.contains(GroupRoles.coach.name()))
|| (!coach && participant && roles.size() == 1 && roles.contains(GroupRoles.participant.name()))
|| (!coach && !participant && roles.isEmpty())) {
return;// fail fast
}
Identity identity = securityManager.loadIdentityByKey(identityRef.getKey());
if(coach && !roles.contains(GroupRoles.coach.name())) {
businessGroupRelationDao.addRole(identity, businessGroup, GroupRoles.coach.name());
} else if(!coach && roles.contains(GroupRoles.coach.name())) {
businessGroupRelationDao.removeRole(identity, businessGroup, GroupRoles.coach.name());
}
if(participant && !roles.contains(GroupRoles.participant.name())) {
businessGroupRelationDao.addRole(identity, businessGroup, GroupRoles.participant.name());
} else if(!participant && roles.contains(GroupRoles.participant.name())) {
businessGroupRelationDao.removeRole(identity, businessGroup, GroupRoles.participant.name());
}
}
......@@ -1519,7 +1515,7 @@ public class LDAPLoginManagerImpl implements LDAPLoginManager, AuthenticationPro
List<BusinessGroup> businessGroups = businessGroupService.findBusinessGroups(params, null, 0, -1);
BusinessGroup managedBusinessGroup;
if(businessGroups.size() == 0) {
if(businessGroups.isEmpty()) {
String managedFlags = BusinessGroupManagedFlag.membersmanagement.name() + "," + BusinessGroupManagedFlag.delete.name();
managedBusinessGroup = businessGroupService
.createBusinessGroup(null, externalId, externalId, externalId, managedFlags, null, null, false, false, null);
......@@ -1527,7 +1523,7 @@ public class LDAPLoginManagerImpl implements LDAPLoginManager, AuthenticationPro
} else if(businessGroups.size() == 1) {
managedBusinessGroup = businessGroups.get(0);
} else {
log.error(businessGroups.size() + " managed groups found with the following external id: " + externalId);
log.error("{} managed groups found with the following external id: {}", businessGroups.size(), externalId);
managedBusinessGroup = null;
}
return managedBusinessGroup;
......
......@@ -1304,6 +1304,8 @@ ldap.user.groupAttributeSeparator=,
ldap.user.coachedGroupAttribute=
ldap.user.coachedGroupAttribute.values=o
ldap.user.coachedGroupAttributeSeparator=,
# sync group coaches as participant too
ldap.groupCoachAsParticipant=false
# sync authors
ldap.authorsGroupBases=
......
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