Example usage for javax.naming.directory Attributes put

List of usage examples for javax.naming.directory Attributes put

Introduction

In this page you can find the example usage for javax.naming.directory Attributes put.

Prototype

Attribute put(Attribute attr);

Source Link

Document

Adds a new attribute to the attribute set.

Usage

From source file:org.apache.archiva.redback.rbac.ldap.LdapRbacManagerTest.java

private void createGroup(DirContext context, String groupName, String dn, List<String> users) throws Exception {

    Attributes attributes = new BasicAttributes(true);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("groupOfUniqueNames");
    attributes.put(objectClass);
    attributes.put("cn", groupName);
    if (!users.isEmpty()) {
        BasicAttribute basicAttribute = new BasicAttribute("uniquemember");
        for (String user : users) {
            basicAttribute.add("uid=" + user + "," + suffix);// dc=archiva,dc=apache,dc=org" );
        }/*from   ww  w.  jav a  2  s . c  o  m*/

        attributes.put(basicAttribute);
    }

    context.createSubcontext(dn, attributes);
}

From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java

private Attributes getAttributesToBind(Person person) {
    Attributes attrs = new BasicAttributes();
    BasicAttribute ocattr = new BasicAttribute("objectclass");
    ocattr.add("top");
    ocattr.add("person");
    attrs.put(ocattr);
    attrs.put("cn", person.getFullName());
    attrs.put("sn", person.getLastName());
    attrs.put("description", person.getDescription());
    attrs.put("telephoneNumber", person.getPhone());
    return attrs;
}

From source file:org.apache.archiva.redback.common.ldap.role.TestLdapRoleMapper.java

private void createGroup(DirContext context, String groupName, String dn, List<String> users) throws Exception {

    Attributes attributes = new BasicAttributes(true);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("groupOfUniqueNames");
    attributes.put(objectClass);
    attributes.put("cn", groupName);
    BasicAttribute basicAttribute = new BasicAttribute("uniquemember");
    for (String user : users) {
        basicAttribute.add("uid=" + user + "," + suffix);// dc=archiva,dc=apache,dc=org" );
    }//from www . j  ava2  s.  co m

    attributes.put(basicAttribute);
    context.createSubcontext(dn, attributes);
}

From source file:org.apache.directory.server.operations.bind.MiscBindIT.java

/**
 * Reproduces the problem with/*  www  . j a  va 2s.c o m*/
 * <a href="http://issues.apache.org/jira/browse/DIREVE-239">DIREVE-239</a>.
 *
 * @throws Exception if anything goes wrong
 */
@Test
public void testAdminAccessBug() throws Exception {
    getLdapServer().getDirectoryService().setAllowAnonymousAccess(true);

    // Use the SUN JNDI provider to hit server port and bind as anonymous

    final Hashtable<String, Object> env = new Hashtable<String, Object>();

    env.put(Context.PROVIDER_URL, Network.ldapLoopbackUrl(getLdapServer().getPort()));
    env.put("java.naming.ldap.version", "3");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    Attributes attributes = new BasicAttributes(true);
    Attribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("organizationalUnit");
    attributes.put(objectClass);
    attributes.put("ou", "blah");
    InitialDirContext ctx = new InitialDirContext(env);
    ctx.createSubcontext("ou=blah,ou=system", attributes);
    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.OBJECT_SCOPE);
    controls.setReturningAttributes(new String[] { "+" });
    NamingEnumeration<SearchResult> list = ctx.search("ou=blah,ou=system", "(objectClass=*)", controls);
    SearchResult result = list.next();
    list.close();
    Attribute creatorsName = result.getAttributes().get("creatorsName");
    assertEquals("", creatorsName.get());
    ctx.destroySubcontext("ou=blah,ou=system");
}

From source file:org.easy.ldap.LdapDao.java

/**
 * @param rootDn//  w  w w.  j  a  v a 2  s.com
 * @param type
 * @return
 */
public List<String> findRdnValue(LdapName rootDn, RdnType type) {
    NamingEnumeration<SearchResult> result = null;
    List<String> out = new ArrayList<String>(0);

    DirContext ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());
        Attributes attributes = new BasicAttributes();
        attributes.put(new BasicAttribute(type.toString()));

        result = ctx.search("", attributes);

        while (result.hasMore()) {
            attributes = result.next().getAttributes();
            out.add(attributes.get(type.toString()).get().toString());
        }

    } catch (NamingException e) {
        throw new RuntimeException(type.toString() + "," + rootDn.toString(), e);
    } finally {
        if (contextFactory != null)
            contextFactory.closeContext(ctx);
    }

    return out;
}

From source file:org.easy.ldap.AdminServiceImpl.java

@Override
public List<String> findGrantedRoles(LdapUser user) {
    List<String> out = null;

    String uniqueMemberDn = namingFactory.createUserDn(user.getTenantId(), user.getUserId()).toString();
    LdapName rolesDn = namingFactory.createRolesDn(user.getTenantId());
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute(RdnType.UNIQUE_MEMBER.toString(), uniqueMemberDn));

    out = ldapDao.findRdnValues(rolesDn, attributes, RdnType.CN);

    return out;/*w w  w.j a va 2 s.  c  o m*/
}

From source file:org.mule.providers.ldap.util.DSManager.java

private void setUpPartition(MutableServerStartupConfiguration configuration) throws NamingException {
    // Add partition 'sevenSeas'
    MutablePartitionConfiguration pcfg = new MutablePartitionConfiguration();
    pcfg.setName("sevenSeas");
    pcfg.setSuffix("o=sevenseas");

    // Create some indices
    java.util.Set indexedAttrs = new HashSet();
    indexedAttrs.add("objectClass");
    indexedAttrs.add("o");
    pcfg.setIndexedAttributes(indexedAttrs);

    // Create a first entry associated to the partition
    Attributes attrs = new BasicAttributes(true);

    // First, the objectClass attribute
    Attribute attr = new BasicAttribute("objectClass");
    attr.add("top");
    attr.add("organization");
    attrs.put(attr);

    // The the 'Organization' attribute
    attr = new BasicAttribute("o");
    attr.add("sevenseas");
    attrs.put(attr);/*from w w  w .  j  a va  2 s.  c  om*/

    // Associate this entry to the partition
    pcfg.setContextEntry(attrs);

    // As we can create more than one partition, we must store
    // each created partition in a Set before initialization
    Set pcfgs = new HashSet();
    pcfgs.add(pcfg);

    configuration.setContextPartitionConfigurations(pcfgs);

    // Create a working directory
    // File workingDirectory = new File( "server-work" );
    // configuration.setWorkingDirectory( workingDirectory );

}

From source file:org.easy.ldap.AdminServiceImpl.java

@Override
public void revokeAllRoles(LdapUser user) {
    LdapName userDn = namingFactory.createUserDn(user.getTenantId(), user.getUserId());
    LdapName rolesDn = namingFactory.createRolesDn(user.getTenantId());
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute(RdnType.UNIQUE_MEMBER.toString(), userDn));

    List<String> grantedRoles = ldapDao.findRdnValues(rolesDn, attributes, RdnType.CN);

    for (String role : grantedRoles) {
        LdapName roleDn = namingFactory.createRoleDn(user.getTenantId(), role);
        ldapDao.removeRdn(roleDn, RdnType.UNIQUE_MEMBER, userDn.toString());
    }/*from   ww w .  ja  v  a2  s  . c  om*/
}

From source file:org.easy.ldap.AdminServiceImpl.java

@Override
public void deleteRole(String tenantId, String role) {
    try {/*from   w  w  w.j  a v a  2s . c  o  m*/
        LdapName roleDn = namingFactory.createRoleDn(tenantId, role);
        LdapName rolesDn = namingFactory.createRolesDn(tenantId);
        Attributes attributes = new BasicAttributes();
        attributes.put(new BasicAttribute(RdnType.UNIQUE_MEMBER.toString(), roleDn));

        List<String> grantedRoles = ldapDao.findRdnValues(rolesDn, attributes, RdnType.CN);

        for (String grantedRole : grantedRoles) {
            LdapName grantedRoleDn = namingFactory.createRoleDn(tenantId, grantedRole);
            ldapDao.removeRdn(roleDn, RdnType.UNIQUE_MEMBER, grantedRoleDn.toString());
        }

        Rdn roleRdn = namingFactory.createRoleRdn(role);
        ldapDao.deleteSubContext(rolesDn, roleRdn);
    } catch (NamingException e) {
        log.error(e);
        throw new java.lang.RuntimeException(e);
    }

}

From source file:org.easy.ldap.AdminServiceImpl.java

@Override
public void addUser(LdapUser newUser) {
    Preconditions.checkNotNull(newUser);
    LoggingUtil.createDebugLog(log, "addUser", newUser);

    try {/*  ww  w.  ja  va2 s  .  com*/
        Attributes attributes = LdapDao.toAttributes(newUser);
        attributes.put(NamingFactory.getUserObjectClasses());

        String userRdnName = environment.getProperty(PropertyNames.USERS_RDN);
        userRdnName = userRdnName.replace(RdnType.OU.toString() + "=", "");
        Attribute ouAttr = new BasicAttribute(RdnType.OU.toString(), userRdnName);
        attributes.put(ouAttr);

        LdapName rootDn = namingFactory.createUsersDn(newUser.getTenantId());
        Rdn userRdn = NamingFactory.createRdn(RdnType.UID, newUser.getUserId());

        ldapDao.createSubContext(rootDn, userRdn, attributes);

    } catch (Exception e) {
        log.error(e);
        throw new java.lang.RuntimeException(e);
    }

}