Search in sources :

Example 51 with Attribute

use of javax.naming.directory.Attribute 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 52 with Attribute

use of javax.naming.directory.Attribute 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 53 with Attribute

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

the class LdapManager method getAttributeValueList.

protected List<Object> getAttributeValueList(final List<SearchResult> result, final String name) {
    try {
        for (final SearchResult srcrslt : result) {
            final Attributes attrs = srcrslt.getAttributes();
            final Attribute attr = attrs.get(name);
            if (attr == null) {
                continue;
            }
            final List<Object> attrList = new ArrayList<>();
            for (int i = 0; i < attr.size(); i++) {
                final Object attrValue = attr.get(i);
                if (attrValue != null) {
                    attrList.add(attrValue);
                }
            }
            return attrList;
        }
        return Collections.emptyList();
    } catch (final NamingException e) {
        throw new LdapOperationException("Failed to parse attribute values for " + name, e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) LdapOperationException(org.codelibs.fess.exception.LdapOperationException) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Example 54 with Attribute

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

the class FessProp method getLdapAdminRoleObjectClassAttribute.

public default default Attribute getLdapAdminRoleObjectClassAttribute() {
    final Attribute oc = new BasicAttribute("objectClass");
    split(getLdapAdminRoleObjectClasses(), ",").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(s -> oc.add(s.trim())));
    return oc;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ClassNotFoundRuntimeException(org.codelibs.core.exception.ClassNotFoundRuntimeException) Constants(org.codelibs.fess.Constants) SortBuilders(org.elasticsearch.search.sort.SortBuilders) Enumeration(java.util.Enumeration) Date(java.util.Date) JobConcurrentExec(org.lastaflute.job.subsidiary.JobConcurrentExec) OptionalThing(org.dbflute.optional.OptionalThing) Pair(org.codelibs.core.misc.Pair) PermissionHelper(org.codelibs.fess.helper.PermissionHelper) IntegerTypeValidator(org.lastaflute.web.validation.theme.typed.IntegerTypeValidator) LaRequestUtil(org.lastaflute.web.util.LaRequestUtil) LongTypeValidator(org.lastaflute.web.validation.theme.typed.LongTypeValidator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BasicAttribute(javax.naming.directory.BasicAttribute) FessUserBean(org.codelibs.fess.mylasta.action.FessUserBean) Attribute(javax.naming.directory.Attribute) StreamUtil.split(org.codelibs.core.stream.StreamUtil.split) Locale(java.util.Locale) Map(java.util.Map) PrunedTag(org.codelibs.fess.util.PrunedTag) HtmlNext(org.lastaflute.web.response.next.HtmlNext) SortBuilder(org.elasticsearch.search.sort.SortBuilder) HttpSession(javax.servlet.http.HttpSession) LaJob(org.lastaflute.job.LaJob) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) FloatTypeValidator(org.lastaflute.web.validation.theme.typed.FloatTypeValidator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StringUtil(org.codelibs.core.lang.StringUtil) Set(java.util.Set) RequiredValidator(org.lastaflute.web.validation.RequiredValidator) Tuple3(org.codelibs.core.misc.Tuple3) Collectors(java.util.stream.Collectors) List(java.util.List) ComponentUtil(org.codelibs.fess.util.ComponentUtil) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SortOrder(org.elasticsearch.search.sort.SortOrder) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) DoubleTypeValidator(org.lastaflute.web.validation.theme.typed.DoubleTypeValidator) FessFunctions(org.codelibs.fess.taglib.FessFunctions) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) StringUtil(org.codelibs.core.lang.StringUtil)

Example 55 with Attribute

use of javax.naming.directory.Attribute in project tomcat by apache.

the class JNDIRealm method getAttributeValue.

/**
     * Return a String representing the value of the specified attribute.
     *
     * @param attrId Attribute name
     * @param attrs Attributes containing the required value
     * @return the attribute value
     * @exception NamingException if a directory server error occurs
     */
private String getAttributeValue(String attrId, Attributes attrs) throws NamingException {
    if (containerLog.isTraceEnabled())
        containerLog.trace("  retrieving attribute " + attrId);
    if (attrId == null || attrs == null)
        return null;
    Attribute attr = attrs.get(attrId);
    if (attr == null)
        return null;
    Object value = attr.get();
    if (value == null)
        return null;
    String valueString = null;
    if (value instanceof byte[])
        valueString = new String((byte[]) value);
    else
        valueString = value.toString();
    return valueString;
}
Also used : Attribute(javax.naming.directory.Attribute)

Aggregations

Attribute (javax.naming.directory.Attribute)110 Attributes (javax.naming.directory.Attributes)57 NamingException (javax.naming.NamingException)39 BasicAttribute (javax.naming.directory.BasicAttribute)39 BasicAttributes (javax.naming.directory.BasicAttributes)30 ArrayList (java.util.ArrayList)29 SearchResult (javax.naming.directory.SearchResult)25 NamingEnumeration (javax.naming.NamingEnumeration)22 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)18 HashSet (java.util.HashSet)17 DirContext (javax.naming.directory.DirContext)17 SearchControls (javax.naming.directory.SearchControls)17 IOException (java.io.IOException)11 InitialDirContext (javax.naming.directory.InitialDirContext)11 ModificationItem (javax.naming.directory.ModificationItem)11 Hashtable (java.util.Hashtable)9 File (java.io.File)7 List (java.util.List)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7