Search in sources :

Example 21 with BasicAttributes

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

the class LdapManager method insert.

public void insert(final User user) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    if (!fessConfig.isLdapAdminEnabled(user.getName())) {
        return;
    }
    final Supplier<Hashtable<String, String>> adminEnv = () -> 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, fessConfig);
        } else {
            final BasicAttributes entry = new BasicAttributes();
            addUserAttributes(entry, user, fessConfig);
            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) LoggerFactory(org.slf4j.LoggerFactory) 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) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Role(org.codelibs.fess.es.user.exentity.Role) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Locale(java.util.Locale) 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) Logger(org.slf4j.Logger) 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) BasicAttributes(javax.naming.directory.BasicAttributes) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Base64(java.util.Base64) List(java.util.List) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DfTypeUtil(org.dbflute.util.DfTypeUtil) Attributes(javax.naming.directory.Attributes) SystemHelper(org.codelibs.fess.helper.SystemHelper) Collections(java.util.Collections) 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) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Role(org.codelibs.fess.es.user.exentity.Role) ModificationItem(javax.naming.directory.ModificationItem) ArrayList(java.util.ArrayList) List(java.util.List)

Example 22 with BasicAttributes

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

the class LdapManager method insert.

public void insert(final Role role) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    if (!fessConfig.isLdapAdminEnabled()) {
        return;
    }
    final Supplier<Hashtable<String, String>> adminEnv = () -> createAdminEnv();
    search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(role.getName()), null, adminEnv, result -> {
        if (!result.isEmpty()) {
            logger.info("{} exists in LDAP server.", role.getName());
        } else {
            final String entryDN = fessConfig.getLdapAdminRoleSecurityPrincipal(role.getName());
            final BasicAttributes entry = new BasicAttributes();
            addRoleAttributes(entry, role, fessConfig);
            final Attribute oc = fessConfig.getLdapAdminRoleObjectClassAttribute();
            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) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 23 with BasicAttributes

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

the class LdapTestUtils method mockAttributes.

private static Attributes mockAttributes(NameValues... namedValues) throws NamingException {
    Attributes attributes = new BasicAttributes();
    for (NameValues namedValue : namedValues) {
        Attribute attr = new BasicAttribute(namedValue.name);
        for (String value : namedValue.values) {
            attr.add(value);
        }
        attributes.put(attr);
    }
    return attributes;
}
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)

Example 24 with BasicAttributes

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

the class TestLdapGroupsMappingBase method setupMocksBase.

@Before
public void setupMocksBase() throws NamingException {
    MockitoAnnotations.initMocks(this);
    DirContext ctx = getContext();
    doReturn(ctx).when(groupsMapping).getDirContext();
    when(ctx.search(Mockito.anyString(), Mockito.anyString(), Mockito.any(Object[].class), Mockito.any(SearchControls.class))).thenReturn(userNames);
    // We only ever call hasMoreElements once for the user NamingEnum, so
    // we can just have one return value
    when(userNames.hasMoreElements()).thenReturn(true);
    SearchResult groupSearchResult = mock(SearchResult.class);
    // We're going to have to define the loop here. We want two iterations,
    // to get both the groups
    when(groupNames.hasMoreElements()).thenReturn(true, true, false);
    when(groupNames.nextElement()).thenReturn(groupSearchResult);
    // Define the attribute for the name of the first group
    Attribute group1Attr = new BasicAttribute("cn");
    group1Attr.add(testGroups[0]);
    Attributes group1Attrs = new BasicAttributes();
    group1Attrs.put(group1Attr);
    // Define the attribute for the name of the second group
    Attribute group2Attr = new BasicAttribute("cn");
    group2Attr.add(testGroups[1]);
    Attributes group2Attrs = new BasicAttributes();
    group2Attrs.put(group2Attr);
    // This search result gets reused, so return group1, then group2
    when(groupSearchResult.getAttributes()).thenReturn(group1Attrs, group2Attrs);
    when(getUserNames().nextElement()).thenReturn(getUserSearchResult());
    when(getUserSearchResult().getAttributes()).thenReturn(getAttributes());
    // Define results for groups 1 level up
    SearchResult parentGroupResult = mock(SearchResult.class);
    // only one parent group
    when(parentGroupNames.hasMoreElements()).thenReturn(true, false);
    when(parentGroupNames.nextElement()).thenReturn(parentGroupResult);
    // Define the attribute for the parent group
    Attribute parentGroup1Attr = new BasicAttribute("cn");
    parentGroup1Attr.add(testParentGroups[2]);
    Attributes parentGroup1Attrs = new BasicAttributes();
    parentGroup1Attrs.put(parentGroup1Attr);
    // attach the attributes to the result
    when(parentGroupResult.getAttributes()).thenReturn(parentGroup1Attrs);
    when(parentGroupResult.getNameInNamespace()).thenReturn("CN=some_group,DC=test,DC=com");
}
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) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext) Before(org.junit.Before)

Example 25 with BasicAttributes

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

the class SpringLdapProducerTest method testBind.

@Test
public void testBind() throws Exception {
    String dn = "some dn";
    BasicAttributes attributes = new BasicAttributes();
    Exchange exchange = new DefaultExchange(context);
    Message in = new DefaultMessage();
    Map<String, Object> body = new HashMap<String, Object>();
    body.put(SpringLdapProducer.DN, dn);
    body.put(SpringLdapProducer.ATTRIBUTES, attributes);
    when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.BIND);
    processBody(exchange, in, body);
    verify(ldapTemplate).bind(eq(dn), isNull(), eq(attributes));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultMessage(org.apache.camel.impl.DefaultMessage) BasicAttributes(javax.naming.directory.BasicAttributes) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

BasicAttributes (javax.naming.directory.BasicAttributes)39 BasicAttribute (javax.naming.directory.BasicAttribute)29 Attribute (javax.naming.directory.Attribute)23 Attributes (javax.naming.directory.Attributes)23 Test (org.junit.Test)9 File (java.io.File)7 HashSet (java.util.HashSet)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 DirContext (javax.naming.directory.DirContext)5 PrivkeySchema (org.nhindirect.ldap.PrivkeySchema)5 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)4 Hashtable (java.util.Hashtable)4 SearchControls (javax.naming.directory.SearchControls)4 SearchResult (javax.naming.directory.SearchResult)4 Lookup (org.nhindirect.stagent.cert.impl.util.Lookup)4 NamingException (javax.naming.NamingException)3 LdapContext (javax.naming.ldap.LdapContext)3