Search in sources :

Example 11 with LDAPGroup

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

the class LDAPGroupVisitor method visit.

@Override
public void visit(SearchResult searchResult) throws NamingException {
    Attributes resAttributes = searchResult.getAttributes();
    Attribute memberAttr = resAttributes.get("member");
    Attribute cnAttr = resAttributes.get("cn");
    if (memberAttr != null) {
        LDAPGroup group = new LDAPGroup();
        Object cn = cnAttr.get();
        if (cn instanceof String) {
            group.setCommonName((String) cn);
        }
        List<String> members = new ArrayList<String>();
        try {
            for (NamingEnumeration<?> memberEn = memberAttr.getAll(); memberEn.hasMoreElements(); ) {
                Object member = memberEn.next();
                if (member instanceof String) {
                    members.add((String) member);
                }
            }
        } catch (NamingException e) {
            log.error("", e);
        }
        group.setMembers(members);
        groups.add(group);
    }
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) NamingException(javax.naming.NamingException) LDAPGroup(org.olat.ldap.model.LDAPGroup)

Example 12 with LDAPGroup

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

the class LDAPLoginManagerImpl method doSyncGroupByAttribute.

private void doSyncGroupByAttribute(List<LDAPUser> ldapUsers, Map<String, LDAPGroup> cnToGroupMap) {
    for (LDAPUser ldapUser : ldapUsers) {
        List<String> groupIds = ldapUser.getGroupIds();
        List<String> coachedGroupIds = ldapUser.getCoachedGroupIds();
        if ((groupIds != null && groupIds.size() > 0) || (coachedGroupIds != null && coachedGroupIds.size() > 0)) {
            IdentityRef identity = ldapUser.getCachedIdentity();
            if (identity == null) {
                log.error("Identity with dn=" + ldapUser.getDn() + " not found");
            } else {
                if (groupIds != null && groupIds.size() > 0) {
                    for (String groupId : groupIds) {
                        if (!cnToGroupMap.containsKey(groupId)) {
                            cnToGroupMap.put(groupId, new LDAPGroup(groupId));
                        }
                        cnToGroupMap.get(groupId).getParticipants().add(ldapUser);
                    }
                }
                if (coachedGroupIds != null && coachedGroupIds.size() > 0) {
                    for (String coachedGroupId : coachedGroupIds) {
                        if (!cnToGroupMap.containsKey(coachedGroupId)) {
                            cnToGroupMap.put(coachedGroupId, new LDAPGroup(coachedGroupId));
                        }
                        cnToGroupMap.get(coachedGroupId).getCoaches().add(ldapUser);
                    }
                }
            }
        }
    }
}
Also used : IdentityRef(org.olat.basesecurity.IdentityRef) LDAPUser(org.olat.ldap.model.LDAPUser) LDAPGroup(org.olat.ldap.model.LDAPGroup)

Example 13 with LDAPGroup

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

the class LDAPLoginManagerImpl method syncRole.

private void syncRole(LdapContext ctx, List<LDAPGroup> groups, String role, Map<String, LDAPUser> dnToIdentityKeyMap, LDAPError errors) {
    if (groups == null || groups.isEmpty())
        return;
    for (LDAPGroup group : groups) {
        List<String> members = group.getMembers();
        if (members != null && members.size() > 0) {
            for (String member : members) {
                LDAPUser ldapUser = getLDAPUser(ctx, member, dnToIdentityKeyMap, errors);
                if (ldapUser != null && ldapUser.getCachedIdentity() != null) {
                    syncRole(ldapUser, role);
                }
            }
        }
        dbInstance.commitAndCloseSession();
    }
}
Also used : LDAPUser(org.olat.ldap.model.LDAPUser) LDAPGroup(org.olat.ldap.model.LDAPGroup)

Example 14 with LDAPGroup

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

the class LDAPLoginManagerImpl method doBatchSyncGroups.

private void doBatchSyncGroups(LdapContext ctx, List<LDAPUser> ldapUsers, Map<String, LDAPUser> dnToIdentityKeyMap, LDAPError errors) throws NamingException {
    ctx.close();
    log.info("LDAP batch sync LDAP user to OO groups");
    ctx = bindSystem();
    // sync groups by LDAP groups or attributes
    Map<String, LDAPGroup> cnToGroupMap = new HashMap<>();
    // retrieve all ldap group's with their list of members
    if (syncConfiguration.syncGroupWithLDAPGroup()) {
        List<String> groupDNs = syncConfiguration.getLdapGroupBases();
        List<LDAPGroup> ldapGroups = ldapDao.searchGroups(ctx, groupDNs);
        for (LDAPGroup ldapGroup : ldapGroups) {
            cnToGroupMap.put(ldapGroup.getCommonName(), ldapGroup);
        }
    }
    if (syncConfiguration.syncGroupWithAttribute()) {
        doSyncGroupByAttribute(ldapUsers, cnToGroupMap);
    }
    int syncGroupCount = 0;
    for (LDAPGroup group : cnToGroupMap.values()) {
        BusinessGroup managedGroup = getManagerBusinessGroup(group.getCommonName());
        if (managedGroup != null) {
            syncBusinessGroup(ctx, managedGroup, group, dnToIdentityKeyMap, errors);
        }
        dbInstance.commitAndCloseSession();
        if (syncGroupCount % 100 == 0) {
            log.info("Synched " + syncGroupCount + "/" + cnToGroupMap.size() + " LDAP groups");
        }
        syncGroupCount++;
    }
}
Also used : HashMap(java.util.HashMap) BusinessGroup(org.olat.group.BusinessGroup) LDAPGroup(org.olat.ldap.model.LDAPGroup)

Aggregations

LDAPGroup (org.olat.ldap.model.LDAPGroup)14 Attribute (javax.naming.directory.Attribute)6 Attributes (javax.naming.directory.Attributes)6 LDAPUser (org.olat.ldap.model.LDAPUser)6 ArrayList (java.util.ArrayList)4 NamingException (javax.naming.NamingException)4 SearchResult (javax.naming.directory.SearchResult)4 BusinessGroup (org.olat.group.BusinessGroup)4 HashMap (java.util.HashMap)2 BasicAttribute (javax.naming.directory.BasicAttribute)2 SearchControls (javax.naming.directory.SearchControls)2 InitialLdapContext (javax.naming.ldap.InitialLdapContext)2 LdapContext (javax.naming.ldap.LdapContext)2 IdentityRef (org.olat.basesecurity.IdentityRef)2