Search in sources :

Example 46 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project fess by codelibs.

the class LdapManager method insert.

public void insert(final User user) {
    if (!fessConfig.isLdapAdminEnabled(user.getName())) {
        return;
    }
    final Supplier<Hashtable<String, String>> adminEnv = this::createAdminEnv;
    final String userDN = fessConfig.getLdapAdminUserSecurityPrincipal(user.getName());
    // attributes
    search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(user.getName()), null, adminEnv, result -> {
        if (!result.isEmpty()) {
            modifyUserAttributes(user, adminEnv, userDN, result);
        } else {
            final BasicAttributes entry = new BasicAttributes();
            addUserAttributes(entry, user);
            final Attribute oc = fessConfig.getLdapAdminUserObjectClassAttribute();
            entry.put(oc);
            insert(userDN, entry, adminEnv);
        }
    });
    // groups and roles
    search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(user.getName()), new String[] { fessConfig.getLdapMemberofAttribute() }, adminEnv, result -> {
        if (!result.isEmpty()) {
            final List<String> oldGroupList = new ArrayList<>();
            final List<String> oldRoleList = new ArrayList<>();
            final String lowerGroupDn = fessConfig.getLdapAdminGroupBaseDn().toLowerCase(Locale.ROOT);
            final String lowerRoleDn = fessConfig.getLdapAdminRoleBaseDn().toLowerCase(Locale.ROOT);
            processSearchRoles(result, (entryDn, name) -> {
                final String lowerEntryDn = entryDn.toLowerCase(Locale.ROOT);
                if (lowerEntryDn.indexOf(lowerGroupDn) != -1) {
                    oldGroupList.add(name);
                } else if (lowerEntryDn.indexOf(lowerRoleDn) != -1) {
                    oldRoleList.add(name);
                }
            });
            final List<String> newGroupList = stream(user.getGroupNames()).get(stream -> stream.collect(Collectors.toList()));
            stream(user.getGroupNames()).of(stream -> stream.forEach(name -> {
                if (oldGroupList.contains(name)) {
                    oldGroupList.remove(name);
                    newGroupList.remove(name);
                }
            }));
            oldGroupList.stream().forEach(name -> {
                search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
                    if (!subResult.isEmpty()) {
                        final List<ModificationItem> modifyList = new ArrayList<>();
                        modifyDeleteEntry(modifyList, "member", userDN);
                        modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
                    }
                });
            });
            newGroupList.stream().forEach(name -> {
                search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
                    if (subResult.isEmpty()) {
                        final Group group = new Group();
                        group.setName(name);
                        insert(group);
                    }
                    final List<ModificationItem> modifyList = new ArrayList<>();
                    modifyAddEntry(modifyList, "member", userDN);
                    modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
                });
            });
            final List<String> newRoleList = stream(user.getRoleNames()).get(stream -> stream.collect(Collectors.toList()));
            stream(user.getRoleNames()).of(stream -> stream.forEach(name -> {
                if (oldRoleList.contains(name)) {
                    oldRoleList.remove(name);
                    newRoleList.remove(name);
                }
            }));
            oldRoleList.stream().forEach(name -> {
                search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
                    if (!subResult.isEmpty()) {
                        final List<ModificationItem> modifyList = new ArrayList<>();
                        modifyDeleteEntry(modifyList, "member", userDN);
                        modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
                    }
                });
            });
            newRoleList.stream().forEach(name -> {
                search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
                    if (subResult.isEmpty()) {
                        final Role role = new Role();
                        role.setName(name);
                        insert(role);
                    }
                    final List<ModificationItem> modifyList = new ArrayList<>();
                    modifyAddEntry(modifyList, "member", userDN);
                    modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
                });
            });
        } else {
            stream(user.getGroupNames()).of(stream -> stream.forEach(name -> {
                search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
                    if (subResult.isEmpty()) {
                        final Group group = new Group();
                        group.setName(name);
                        insert(group);
                    }
                    final List<ModificationItem> modifyList = new ArrayList<>();
                    modifyAddEntry(modifyList, "member", userDN);
                    modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
                });
            }));
            stream(user.getRoleNames()).of(stream -> stream.forEach(name -> {
                search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
                    if (subResult.isEmpty()) {
                        final Role role = new Role();
                        role.setName(name);
                        insert(role);
                    }
                    final List<ModificationItem> modifyList = new ArrayList<>();
                    modifyAddEntry(modifyList, "member", userDN);
                    modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
                });
            }));
        }
    });
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) Constants(org.codelibs.fess.Constants) NamingException(javax.naming.NamingException) User(org.codelibs.fess.es.user.exentity.User) Supplier(java.util.function.Supplier) SearchControls(javax.naming.directory.SearchControls) ArrayList(java.util.ArrayList) InitialDirContext(javax.naming.directory.InitialDirContext) HashSet(java.util.HashSet) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Role(org.codelibs.fess.es.user.exentity.Role) LdapConfigurationException(org.codelibs.fess.exception.LdapConfigurationException) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Locale(java.util.Locale) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) FessUser(org.codelibs.fess.entity.FessUser) Context(javax.naming.Context) Hashtable(java.util.Hashtable) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) OptionalUtil(org.codelibs.fess.util.OptionalUtil) OptionalEntity(org.dbflute.optional.OptionalEntity) LdapOperationException(org.codelibs.fess.exception.LdapOperationException) DirContext(javax.naming.directory.DirContext) StringUtil(org.codelibs.core.lang.StringUtil) Set(java.util.Set) BasicAttributes(javax.naming.directory.BasicAttributes) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DfTypeUtil(org.dbflute.util.DfTypeUtil) Attributes(javax.naming.directory.Attributes) SystemHelper(org.codelibs.fess.helper.SystemHelper) PostConstruct(javax.annotation.PostConstruct) TimeoutManager(org.codelibs.core.timer.TimeoutManager) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) SearchResult(javax.naming.directory.SearchResult) Group(org.codelibs.fess.es.user.exentity.Group) BasicAttributes(javax.naming.directory.BasicAttributes) Group(org.codelibs.fess.es.user.exentity.Group) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Role(org.codelibs.fess.es.user.exentity.Role) ModificationItem(javax.naming.directory.ModificationItem) ArrayList(java.util.ArrayList) List(java.util.List)

Example 47 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project fess by codelibs.

the class LdapManager method insert.

public void insert(final Group group) {
    if (!fessConfig.isLdapAdminEnabled()) {
        return;
    }
    final Supplier<Hashtable<String, String>> adminEnv = this::createAdminEnv;
    final String entryDN = fessConfig.getLdapAdminGroupSecurityPrincipal(group.getName());
    search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(group.getName()), null, adminEnv, result -> {
        if (!result.isEmpty()) {
            logger.info("{} exists in LDAP server.", group.getName());
            modifyGroupAttributes(group, adminEnv, entryDN, result);
        } else {
            final BasicAttributes entry = new BasicAttributes();
            addGroupAttributes(entry, group);
            final Attribute oc = fessConfig.getLdapAdminGroupObjectClassAttribute();
            entry.put(oc);
            insert(entryDN, entry, adminEnv);
        }
    });
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable)

Example 48 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project perun by CESNET.

the class ExtSourceEGISSOTest method getUsersSubjectsTest.

@Test
public void getUsersSubjectsTest() throws Exception {
    System.out.println("getUsersSubjectsTest");
    // define needed attributes
    String usersQuery = "firstName=josef";
    Map<String, String> mapOfAttributes = new HashMap<>();
    mapOfAttributes.put("usersQuery", usersQuery);
    doReturn(mapOfAttributes).when(extSourceEGISSO).getAttributes();
    // mock connection and define received attributes
    DirContext dirContext = mock(DirContext.class);
    doReturn(dirContext).when(extSourceEGISSO).getContext();
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute("cn", "josef"));
    NamingEnumeration<SearchResult> namingEnumeration = mock(NamingEnumeration.class);
    doReturn(namingEnumeration).when(dirContext).search(anyString(), anyString(), any());
    doReturn(true, false).when(namingEnumeration).hasMore();
    SearchResult searchResult = new SearchResult("name", namingEnumeration, attributes);
    doReturn(searchResult).when(namingEnumeration).next();
    extSourceEGISSO.mapping = new HashMap<>();
    extSourceEGISSO.mapping.put("cn", "cn");
    // create expected subject to get
    List<Map<String, String>> expectedSubjects = new ArrayList<>();
    Map<String, String> subject = new HashMap<>();
    subject.put("cn", "josef");
    expectedSubjects.add(subject);
    // test the method
    List<Map<String, String>> actualSubjects = extSourceEGISSO.getUsersSubjects();
    assertEquals("subjects should be same", expectedSubjects, actualSubjects);
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) HashMap(java.util.HashMap) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) SearchResult(javax.naming.directory.SearchResult) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DirContext(javax.naming.directory.DirContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 49 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project yyl_example by Relucent.

the class LdapDaoHelper method create.

/**
 * 创建新条目
 * @param entry 新条目
 * @param ctx LDAP上下文连接
 * @deprecated 该方法未经过严谨测试
 */
public static void create(LdapEntry entry, LdapContext ctx) throws NamingException {
    try {
        String dn = entry.getDn();
        Attributes attrs = new BasicAttributes(true);
        if (entry != null && !entry.isEmpty()) {
            Iterator<String> iterator = entry.keySet().iterator();
            while (iterator.hasNext()) {
                String id = iterator.next();
                Attribute attr = new BasicAttribute(id);
                List<?> values = entry.getAll(id);
                if (values != null) {
                    for (Object value : values) {
                        attr.add(value);
                    }
                }
                attrs.put(attr);
            }
        }
        ctx.createSubcontext(dn, attrs);
    } catch (NamingException e) {
        throw e;
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException)

Example 50 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project jmeter by apache.

the class LDAPSampler method getBasicAttributes.

/**
 * This will create the Basic Attributes for the In build TestCase for Add
 * Test.
 *
 * @return the BasicAttributes
 */
private BasicAttributes getBasicAttributes() {
    BasicAttributes basicattributes = new BasicAttributes();
    // $NON-NLS-1$
    BasicAttribute basicattribute = new BasicAttribute("objectclass");
    // $NON-NLS-1$
    basicattribute.add("top");
    // $NON-NLS-1$
    basicattribute.add("person");
    // $NON-NLS-1$
    basicattribute.add("organizationalPerson");
    // $NON-NLS-1$
    basicattribute.add("inetOrgPerson");
    basicattributes.put(basicattribute);
    // $NON-NLS-1$
    String s1 = "User";
    // $NON-NLS-1$
    String s3 = "Test";
    // $NON-NLS-1$
    String s5 = "user";
    // $NON-NLS-1$
    String s6 = "test";
    COUNTER.incrementAndGet();
    // $NON-NLS-1$
    basicattributes.put(new BasicAttribute("givenname", s1));
    // $NON-NLS-1$
    basicattributes.put(new BasicAttribute("sn", s3));
    // $NON-NLS-1$ //$NON-NLS-2$
    basicattributes.put(new BasicAttribute("cn", "TestUser" + COUNTER.get()));
    // $NON-NLS-1$
    basicattributes.put(new BasicAttribute("uid", s5));
    // $NON-NLS-1$
    basicattributes.put(new BasicAttribute("userpassword", s6));
    // $NON-NLS-1$
    setProperty(new StringProperty(ADD, "cn=TestUser" + COUNTER.get()));
    return basicattributes;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) StringProperty(org.apache.jmeter.testelement.property.StringProperty)

Aggregations

BasicAttributes (javax.naming.directory.BasicAttributes)100 Attributes (javax.naming.directory.Attributes)62 BasicAttribute (javax.naming.directory.BasicAttribute)57 Attribute (javax.naming.directory.Attribute)44 Test (org.junit.Test)22 SearchResult (javax.naming.directory.SearchResult)21 DirContext (javax.naming.directory.DirContext)18 NamingException (javax.naming.NamingException)15 InitialDirContext (javax.naming.directory.InitialDirContext)14 NamingEnumeration (javax.naming.NamingEnumeration)12 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 LdapContext (javax.naming.ldap.LdapContext)9 HashMap (java.util.HashMap)8 InitialLdapContext (javax.naming.ldap.InitialLdapContext)8 File (java.io.File)7 Map (java.util.Map)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7 IOException (java.io.IOException)6