diff --git a/src/main/java/org/olat/ldap/LDAPSyncConfiguration.java b/src/main/java/org/olat/ldap/LDAPSyncConfiguration.java index 6de8f651c817cfbecc8c5edd5333f59bd7952704..d3901fff406a6be8fb8c9199fcaa396683b01fe9 100644 --- a/src/main/java/org/olat/ldap/LDAPSyncConfiguration.java +++ b/src/main/java/org/olat/ldap/LDAPSyncConfiguration.java @@ -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; diff --git a/src/main/java/org/olat/ldap/_spring/ldapContext.xml b/src/main/java/org/olat/ldap/_spring/ldapContext.xml index fd143fa0c4416ee1085dfa0f030f2d6b7d243e97..5b34afbfe83bbd972052b3b061869e637fdbfeab 100644 --- a/src/main/java/org/olat/ldap/_spring/ldapContext.xml +++ b/src/main/java/org/olat/ldap/_spring/ldapContext.xml @@ -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" > diff --git a/src/main/java/org/olat/ldap/manager/LDAPDAO.java b/src/main/java/org/olat/ldap/manager/LDAPDAO.java index e35eef3dbbf6bf9d5b66ceaa81c79cc50167b620..7d816283a5cae44d3e7a4ad3960cf28841b3856b 100644 --- a/src/main/java/org/olat/ldap/manager/LDAPDAO.java +++ b/src/main/java/org/olat/ldap/manager/LDAPDAO.java @@ -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); } diff --git a/src/main/java/org/olat/ldap/manager/LDAPLoginManagerImpl.java b/src/main/java/org/olat/ldap/manager/LDAPLoginManagerImpl.java index 2856073e7882a677c0fcf1407f116384d0107a63..5c042e16c8d6277d0024b830d05efbd4396c5a72 100644 --- a/src/main/java/org/olat/ldap/manager/LDAPLoginManagerImpl.java +++ b/src/main/java/org/olat/ldap/manager/LDAPLoginManagerImpl.java @@ -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; diff --git a/src/main/resources/serviceconfig/olat.properties b/src/main/resources/serviceconfig/olat.properties index d568f030358041bfe6150dc089c14ef52e795f64..384ac9de6d2504b9e15882bc1b2de510ddd04013 100644 --- a/src/main/resources/serviceconfig/olat.properties +++ b/src/main/resources/serviceconfig/olat.properties @@ -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=