Search in sources :

Example 11 with ModificationItem

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

the class LDAPExtSampler method getUserModAttributes.

/***************************************************************************
     * Collect all the value from the table (Arguments), using this create the
     * basicAttributes This will create the Basic Attributes for the User
     * defined TestCase for Modify test
     *
     * @return The BasicAttributes
     **************************************************************************/
private ModificationItem[] getUserModAttributes() {
    ModificationItem[] mods = new ModificationItem[getLDAPArguments().getArguments().size()];
    BasicAttribute attr;
    PropertyIterator iter = getLDAPArguments().iterator();
    int count = 0;
    while (iter.hasNext()) {
        LDAPArgument item = (LDAPArgument) iter.next().getObjectValue();
        if ((item.getValue()).length() == 0) {
            attr = new BasicAttribute(item.getName());
        } else {
            attr = getBasicAttribute(item.getName(), item.getValue());
        }
        final String opcode = item.getOpcode();
        if ("add".equals(opcode)) {
            // $NON-NLS-1$
            mods[count++] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr);
        } else if (// $NON-NLS-1$
        "delete".equals(opcode) || "remove".equals(opcode)) {
            // $NON-NLS-1$
            mods[count++] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attr);
        } else if ("replace".equals(opcode)) {
            // $NON-NLS-1$
            mods[count++] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
        } else {
            log.warn("Invalid opCode: " + opcode);
        }
    }
    return mods;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) PropertyIterator(org.apache.jmeter.testelement.property.PropertyIterator) LDAPArgument(org.apache.jmeter.protocol.ldap.config.gui.LDAPArgument)

Example 12 with ModificationItem

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

the class LDAPSampler method getUserModAttributes.

/**
     * Collect all the value from the table (Arguments), using this create the
     * basicAttributes. This will create the Basic Attributes for the User
     * defined TestCase for Modify test.
     *
     * @return the BasicAttributes
     */
private ModificationItem[] getUserModAttributes() {
    ModificationItem[] mods = new ModificationItem[getArguments().getArguments().size()];
    BasicAttribute attr;
    PropertyIterator iter = getArguments().iterator();
    int count = 0;
    while (iter.hasNext()) {
        Argument item = (Argument) iter.next().getObjectValue();
        attr = getBasicAttribute(item.getName(), item.getValue());
        mods[count] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
        count = +1;
    }
    return mods;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) Argument(org.apache.jmeter.config.Argument) PropertyIterator(org.apache.jmeter.testelement.property.PropertyIterator)

Example 13 with ModificationItem

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

the class LDAPSampler method getModificationItem.

/**
     * This will create the Basic Attributes for the Inbuilt TestCase for Modify
     * test.
     *
     * @return the BasicAttributes
     */
private ModificationItem[] getModificationItem() {
    ModificationItem[] mods = new ModificationItem[2];
    // replace (update) attribute
    //$NON-NLS-1$ //$NON-NLS-2$
    Attribute mod0 = new BasicAttribute("userpassword", "secret");
    // add mobile phone number attribute
    //$NON-NLS-1$ //$NON-NLS-2$
    Attribute mod1 = new BasicAttribute("mobile", "123-456-1234");
    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
    mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);
    return mods;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute)

Example 14 with ModificationItem

use of javax.naming.directory.ModificationItem 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 15 with ModificationItem

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

the class LdapManager method delete.

public void delete(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());
    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<>();
            modifyDeleteEntry(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<>();
            modifyDeleteEntry(modifyList, "member", userDN);
            modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
        });
    }));
    search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(user.getName()), null, adminEnv, result -> {
        if (!result.isEmpty()) {
            delete(userDN, adminEnv);
        } else {
            logger.info("{} does not exist in LDAP server.", user.getName());
        }
    });
}
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) Role(org.codelibs.fess.es.user.exentity.Role) Group(org.codelibs.fess.es.user.exentity.Group) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) List(java.util.List) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Aggregations

ModificationItem (javax.naming.directory.ModificationItem)23 BasicAttribute (javax.naming.directory.BasicAttribute)19 Attribute (javax.naming.directory.Attribute)12 ArrayList (java.util.ArrayList)5 NamingException (javax.naming.NamingException)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)3 OrderedSet (com.sun.identity.shared.datastruct.OrderedSet)3 SMSException (com.sun.identity.sm.SMSException)3 Hashtable (java.util.Hashtable)3 LinkedHashSet (java.util.LinkedHashSet)3 Attributes (javax.naming.directory.Attributes)3 DirContext (javax.naming.directory.DirContext)3 LdapOperationException (org.codelibs.fess.exception.LdapOperationException)3 Base64 (java.util.Base64)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Locale (java.util.Locale)2