Search in sources :

Example 1 with LDAPNode

use of org.structr.ldap.entity.LDAPNode in project structr by structr.

the class StructrLDAPWrapper method add.

public void add(final Entry entry) throws LdapException {
    try (final Tx tx = app().tx()) {
        // create while descending
        final Dn dn = entry.getDn();
        final LDAPNode parent = find(dn.getParent());
        if (parent != null) {
            final Attribute objectClasses = entry.get(schemaManager.getAttributeType(SchemaConstants.OBJECT_CLASS_AT_OID));
            final ObjectClassRegistry reg = schemaManager.getObjectClassRegistry();
            final Set<String> classes = new LinkedHashSet<>();
            final Rdn rdn = dn.getRdn();
            String mainClass = null;
            // make rdn schema aware
            if (!rdn.isSchemaAware()) {
                rdn.apply(schemaManager);
            }
            if (objectClasses != null) {
                for (final Value<?> value : objectClasses) {
                    final String cls = value.getString();
                    final String objectClassOid = reg.getOidByName(cls);
                    if (reg.get(objectClassOid).isStructural()) {
                        mainClass = cls;
                    } else {
                        classes.add(cls);
                    }
                }
                final LDAPNode newChild = parent.createChild(rdn.getNormName(), rdn.getName(), mainClass, classes);
                if (newChild != null) {
                    for (final Attribute attr : entry) {
                        AttributeType type = attr.getAttributeType();
                        String oid = null;
                        if (type != null) {
                            oid = type.getOid();
                        } else {
                            type = schemaManager.getAttributeType(attr.getUpId());
                            oid = type.getOid();
                        }
                        newChild.createAttribute(oid, attr.getUpId(), attr);
                    }
                } else {
                    logger.warn("Unable to add entry {}, could not create new instance", entry);
                }
            } else {
                logger.warn("Unable to add entry {}, could not determine object class(es)", entry);
            }
        } else {
            logger.warn("Unable to add entry {}, parent not found", entry);
        }
        tx.success();
    } catch (FrameworkException fex) {
        handleException(fex);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LDAPNode(org.structr.ldap.entity.LDAPNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) NodeAttribute(org.structr.core.graph.NodeAttribute) LDAPAttribute(org.structr.ldap.entity.LDAPAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) ObjectClassRegistry(org.apache.directory.api.ldap.model.schema.registries.ObjectClassRegistry) Dn(org.apache.directory.api.ldap.model.name.Dn) Rdn(org.apache.directory.api.ldap.model.name.Rdn)

Example 2 with LDAPNode

use of org.structr.ldap.entity.LDAPNode in project structr by structr.

the class StructrLDAPWrapper method find.

private LDAPNode find(final Dn dn) throws FrameworkException, LdapException, LdapInvalidDnException {
    final List<Rdn> rdns = new LinkedList<>(dn.getRdns());
    Collections.reverse(rdns);
    LDAPNode current = getRoot();
    for (final Rdn rdn : rdns) {
        if (!rdn.isSchemaAware()) {
            rdn.apply(schemaManager);
        }
        current = current.getChild(rdn.getNormName());
        // break early to avoid NPE
        if (current == null) {
            return null;
        }
    }
    return current;
}
Also used : LDAPNode(org.structr.ldap.entity.LDAPNode) Rdn(org.apache.directory.api.ldap.model.name.Rdn) LinkedList(java.util.LinkedList)

Example 3 with LDAPNode

use of org.structr.ldap.entity.LDAPNode in project structr by structr.

the class StructrLDAPWrapper method getRoot.

private LDAPNode getRoot() throws FrameworkException {
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("LDAPNode");
    final App app = app();
    LDAPNode root = (LDAPNode) app.nodeQuery(type).andName(partitionId).getFirst();
    if (root == null) {
        root = app.create(type, new NodeAttribute<>(StructrApp.key(LDAPNode.class, "name"), partitionId), new NodeAttribute<>(StructrApp.key(LDAPNode.class, "isRoot"), true));
    }
    return root;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) LDAPNode(org.structr.ldap.entity.LDAPNode) NodeAttribute(org.structr.core.graph.NodeAttribute)

Example 4 with LDAPNode

use of org.structr.ldap.entity.LDAPNode in project structr by structr.

the class StructrLDAPWrapper method filter.

private List<Entry> filter(final LDAPNode node, final ExprNode filter, final SearchScope scope, final int depth) throws FrameworkException, LdapException {
    final boolean base = SearchScope.OBJECT.equals(scope);
    final boolean oneLevel = SearchScope.ONELEVEL.equals(scope);
    final boolean subtree = SearchScope.SUBTREE.equals(scope);
    final List<Entry> list = new LinkedList<>();
    if (base || !(depth == 0 && oneLevel)) {
        if (matches(node, filter)) {
            list.add(getEntry(node));
        }
    }
    if (!base && (subtree || (depth == 0 && oneLevel))) {
        // recurse
        for (final LDAPNode child : node.getChildren()) {
            list.addAll(filter(child, filter, scope, depth + 1));
        }
    }
    return list;
}
Also used : LDAPNode(org.structr.ldap.entity.LDAPNode) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) ClonedServerEntry(org.apache.directory.server.core.api.entry.ClonedServerEntry) LinkedList(java.util.LinkedList)

Example 5 with LDAPNode

use of org.structr.ldap.entity.LDAPNode in project structr by structr.

the class StructrLDAPWrapper method delete.

public void delete(final Dn dn) throws LdapException {
    final App app = app();
    try (final Tx tx = app.tx()) {
        final LDAPNode entry = find(dn);
        if (entry != null) {
            entry.delete();
        }
        tx.success();
    } catch (FrameworkException fex) {
        handleException(fex);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) LDAPNode(org.structr.ldap.entity.LDAPNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException)

Aggregations

LDAPNode (org.structr.ldap.entity.LDAPNode)7 FrameworkException (org.structr.common.error.FrameworkException)4 Tx (org.structr.core.graph.Tx)4 LinkedList (java.util.LinkedList)2 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)2 Entry (org.apache.directory.api.ldap.model.entry.Entry)2 Rdn (org.apache.directory.api.ldap.model.name.Rdn)2 ClonedServerEntry (org.apache.directory.server.core.api.entry.ClonedServerEntry)2 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 NodeAttribute (org.structr.core.graph.NodeAttribute)2 LinkedHashSet (java.util.LinkedHashSet)1 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)1 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)1 Dn (org.apache.directory.api.ldap.model.name.Dn)1 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)1 ObjectClassRegistry (org.apache.directory.api.ldap.model.schema.registries.ObjectClassRegistry)1 LDAPAttribute (org.structr.ldap.entity.LDAPAttribute)1