Search in sources :

Example 21 with LDAPUser

use of org.olat.ldap.model.LDAPUser in project openolat by klemens.

the class LDAPLoginManagerImpl method syncBusinessGroup.

private void syncBusinessGroup(LdapContext ctx, BusinessGroup businessGroup, LDAPGroup ldapGroup, Map<String, LDAPUser> dnToIdentityKeyMap, LDAPError errors) {
    List<Identity> currentMembers = businessGroupRelationDao.getMembers(businessGroup, GroupRoles.coach.name(), GroupRoles.participant.name());
    Set<Long> currentMemberKeys = new HashSet<>();
    for (Identity currentMember : currentMembers) {
        currentMemberKeys.add(currentMember.getKey());
    }
    List<LDAPUser> coaches = new ArrayList<>(ldapGroup.getCoaches());
    List<LDAPUser> participants = new ArrayList<>(ldapGroup.getParticipants());
    // transfer member cn's to the participants list
    for (String member : ldapGroup.getMembers()) {
        try {
            LDAPUser ldapUser = getLDAPUser(ctx, member, dnToIdentityKeyMap, errors);
            dnToIdentityKeyMap.get(member);
            if (ldapUser != null && !participants.contains(ldapUser)) {
                participants.add(ldapUser);
            }
        } catch (Exception e) {
            log.error("Cannot retrieve this LDAP group member: " + member, e);
        }
    }
    // transfer to ldap user flagged as coach to the coach list
    for (Iterator<LDAPUser> participantIt = participants.iterator(); participantIt.hasNext(); ) {
        LDAPUser participant = participantIt.next();
        if (participant.isCoach()) {
            if (!coaches.contains(participant)) {
                coaches.add(participant);
            }
            participantIt.remove();
        }
    }
    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();
        }
        count++;
    }
    for (LDAPUser coach : coaches) {
        IdentityRef memberIdentity = coach.getCachedIdentity();
        if (memberIdentity != null && memberIdentity.getKey() != null) {
            syncMembership(businessGroup, memberIdentity, true);
            currentMemberKeys.remove(memberIdentity.getKey());
        }
        if (count % 20 == 0) {
            dbInstance.commitAndCloseSession();
        }
        count++;
    }
    for (Long currentMemberKey : currentMemberKeys) {
        Identity currentMember = securityManager.loadIdentityByKey(currentMemberKey);
        List<String> roles = businessGroupRelationDao.getRoles(currentMember, businessGroup);
        for (String role : roles) {
            businessGroupRelationDao.removeRole(currentMember, businessGroup, role);
        }
        if (count % 20 == 0) {
            dbInstance.commitAndCloseSession();
        }
        count++;
    }
    dbInstance.commitAndCloseSession();
}
Also used : ArrayList(java.util.ArrayList) LDAPUser(org.olat.ldap.model.LDAPUser) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException) IdentityRef(org.olat.basesecurity.IdentityRef) Identity(org.olat.core.id.Identity) HashSet(java.util.HashSet)

Example 22 with LDAPUser

use of org.olat.ldap.model.LDAPUser in project openolat by klemens.

the class LDAPLoginManagerImpl method getLDAPUser.

private LDAPUser getLDAPUser(LdapContext ctx, String member, Map<String, LDAPUser> dnToIdentityKeyMap, LDAPError errors) {
    LDAPUser ldapUser = dnToIdentityKeyMap.get(member);
    IdentityRef identity = ldapUser == null ? null : ldapUser.getCachedIdentity();
    if (identity == null) {
        String userFilter = syncConfiguration.getLdapUserFilter();
        String userDN = member;
        LDAPUserVisitor visitor = new LDAPUserVisitor(syncConfiguration);
        ldapDao.search(visitor, userDN, userFilter, syncConfiguration.getUserAttributes(), ctx);
        List<LDAPUser> ldapUserList = visitor.getLdapUserList();
        if (ldapUserList.size() == 1) {
            ldapUser = ldapUserList.get(0);
            Attributes userAttrs = ldapUser.getAttributes();
            identity = findIdentityByLdapAuthentication(userAttrs, errors);
            if (identity != null) {
                dnToIdentityKeyMap.put(userDN, ldapUser);
            }
        }
    }
    return ldapUser;
}
Also used : IdentityRef(org.olat.basesecurity.IdentityRef) Attributes(javax.naming.directory.Attributes) LDAPUser(org.olat.ldap.model.LDAPUser)

Aggregations

LDAPUser (org.olat.ldap.model.LDAPUser)22 Attributes (javax.naming.directory.Attributes)10 IdentityRef (org.olat.basesecurity.IdentityRef)8 Identity (org.olat.core.id.Identity)8 HashMap (java.util.HashMap)6 AuthenticationException (javax.naming.AuthenticationException)6 NamingException (javax.naming.NamingException)6 LdapContext (javax.naming.ldap.LdapContext)6 LDAPGroup (org.olat.ldap.model.LDAPGroup)6 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Map (java.util.Map)4 InitialLdapContext (javax.naming.ldap.InitialLdapContext)4 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 SecurityGroup (org.olat.basesecurity.SecurityGroup)2 IdentityRefImpl (org.olat.basesecurity.model.IdentityRefImpl)2