Example usage for javax.naming.ldap LdapName add

List of usage examples for javax.naming.ldap LdapName add

Introduction

In this page you can find the example usage for javax.naming.ldap LdapName add.

Prototype

public Name add(Rdn comp) 

Source Link

Document

Adds a single RDN to the end of this LDAP name.

Usage

From source file:edu.kit.scc.ldap.LdapPosixUserDao.java

/**
 * Deletes a POSIX user from the LDAP directory.
 * /*from  w w w .java2s  . co m*/
 * @param posixUser the {@link PosixUser} to delete
 * @return true if success
 */
public boolean deleteUser(PosixUser posixUser) {
    LdapName userDn = LdapUtils.emptyLdapName();
    try {
        userDn = new LdapName(userBase);
        userDn.add("uid=" + posixUser.getUid());
        log.debug("Delete {}", userDn.toString());
        ldapTemplate.unbind(userDn);

        return true;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    }
    return false;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Deletes the {@link PosixGroup} from the LDAP directory.
 * //from  ww w. java2s  . c o m
 * @param group the {@link PosixGroup} to delete
 * @return true if success
 */
public boolean deleteGroup(PosixGroup group) {
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Delete {}", groupDn.toString());
        ldapTemplate.unbind(groupDn);

        return true;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    }
    return false;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Adds a POSIX user to the specified POSIX group.
 * /*from   www  .  j  a  v a  2 s  .c o  m*/
 * @param group the POSIX group
 * @param memberUid the POSIX user's uid
 * @return true on success
 */
public boolean addMember(PosixGroup group, String memberUid) {
    ModificationItem[] modificationItems = new ModificationItem[] {
            new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("memberUid", memberUid)) };
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Add member {} to {}", memberUid, groupDn.toString());
        ldapTemplate.modifyAttributes(groupDn, modificationItems);
        return true;
    } catch (AttributeInUseException ex) {
        log.error("ERROR {}", ex.toString());
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return false;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Removes a POSIX user from the specified POSIX group.
 * //from   w w w .j a  v a2  s . c om
 * @param group the POSIX group
 * @param memberUid the POSIX user's uid
 * @return true on success
 */
public boolean removeMember(PosixGroup group, String memberUid) {
    ModificationItem[] modificationItems = new ModificationItem[] {
            new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("memberUid", memberUid)) };
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Remove member {} from {}", memberUid, groupDn.toString());
        ldapTemplate.modifyAttributes(groupDn, modificationItems);
        return true;
    } catch (AttributeInUseException ex) {
        log.error("ERROR {}", ex.toString());
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return false;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Updates a POSIX group in the LDAP directory.
 * //from w w w  . ja v  a2 s .  c  o  m
 * @param group the {@link PosixGroup} to update
 * @return the {@link PosixGroup} updated
 */
public PosixGroup updateGroup(PosixGroup group) {
    BasicAttribute posixGroupBasicAttribute = new BasicAttribute("objectclass");
    posixGroupBasicAttribute.add("posixGroup");

    Attributes posixGroupAttributes = new BasicAttributes();
    posixGroupAttributes.put(posixGroupBasicAttribute);

    if (group.getCommonName() != null) {
        posixGroupAttributes.put("cn", group.getCommonName());
    }
    if (group.getGidNumber() != null) {
        posixGroupAttributes.put("gidNumber", String.valueOf(group.getGidNumber()));
    }
    if (group.getUserPassword() != null) {
        posixGroupAttributes.put("userPassword", group.getUserPassword());
    }
    if (group.getDescription() != null) {
        posixGroupAttributes.put("description", group.getDescription());
    }
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Update {}", groupDn.toString());
        ldapTemplate.bind(groupDn, null, posixGroupAttributes);

        return group;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return null;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Inserts a new POSIX group into the LDAP directory.
 * //from   w  w w.j a  va2  s. co  m
 * @param group the {@link PosixGroup} to insert
 * @return the {@link PosixGroup} inserted
 */
public PosixGroup insertGroup(PosixGroup group) {
    if (group.commonName == null || group.gidNumber == null) {
        log.warn("PosixGroup has missing mandatory attributes");
        return null;
    }

    BasicAttribute posixGroupBasicAttribute = new BasicAttribute("objectclass");
    posixGroupBasicAttribute.add("posixGroup");

    Attributes posixGroupAttributes = new BasicAttributes();
    posixGroupAttributes.put(posixGroupBasicAttribute);
    posixGroupAttributes.put("cn", group.getCommonName());
    posixGroupAttributes.put("gidNumber", String.valueOf(group.getGidNumber()));

    if (group.getUserPassword() != null) {
        posixGroupAttributes.put("userPassword", group.getUserPassword());
    }
    if (group.getDescription() != null) {
        posixGroupAttributes.put("description", group.getDescription());
    }
    LdapName newGroupDn = LdapUtils.emptyLdapName();
    try {
        newGroupDn = new LdapName(groupBase);
        newGroupDn.add("cn=" + group.getCommonName());
        log.debug("Insert {}", newGroupDn.toString());
        ldapTemplate.bind(newGroupDn, null, posixGroupAttributes);

        return group;
    } catch (NameAlreadyBoundException ex) {
        log.error("ERROR {}", ex.getMessage());
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.getMessage());
    }
    return null;
}

From source file:edu.kit.scc.ldap.LdapPosixUserDao.java

/**
 * Updates a POSIX user in the LDAP directory.
 * //w  ww.j a va  2s  .  c  o m
 * @param posixUser the {@link PosixUser} to update
 * @return the {@link PosixUser} updated
 */
public PosixUser updateUser(PosixUser posixUser) {
    BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
    personBasicAttribute.add("extensibleObject");
    personBasicAttribute.add("inetOrgPerson");
    personBasicAttribute.add("organizationalPerson");
    personBasicAttribute.add("person");
    personBasicAttribute.add("posixAccount");

    Attributes personAttributes = new BasicAttributes();
    personAttributes.put(personBasicAttribute);

    if (posixUser.getCommonName() != null) {
        personAttributes.put("cn", posixUser.getCommonName());
    }
    if (posixUser.getSurName() != null) {
        personAttributes.put("sn", posixUser.getSurName());
    }
    if (posixUser.getUid() != null) {
        personAttributes.put("uid", posixUser.getUid());
    }
    if (posixUser.getUidNumber() != null) {
        personAttributes.put("uidNumber", String.valueOf(posixUser.getUidNumber()));
    }
    if (posixUser.getGidNumber() != null) {
        personAttributes.put("gidNumber", String.valueOf(posixUser.getGidNumber()));
    }
    if (posixUser.getHomeDirectory() != null) {
        personAttributes.put("homeDirectory", posixUser.getHomeDirectory());
    }
    if (posixUser.getUniqueIdentifier() != null) {
        personAttributes.put("uniqueIdentifier", posixUser.getUniqueIdentifier());
    }
    if (posixUser.getDescription() != null) {
        personAttributes.put("description", posixUser.getDescription());
    }
    if (posixUser.getGecos() != null) {
        personAttributes.put("gecos", posixUser.getGecos());
    }
    if (posixUser.getLoginShell() != null) {
        personAttributes.put("loginShell", posixUser.getLoginShell());
    }
    if (posixUser.getUserPassword() != null) {
        personAttributes.put("userPassword", posixUser.getUserPassword());
    }
    if (posixUser.getGivenName() != null) {
        personAttributes.put("givenName", posixUser.getGivenName());
    }
    if (posixUser.getMail() != null) {
        personAttributes.put("mail", posixUser.getMail());
    }

    LdapName userDn = LdapUtils.emptyLdapName();
    try {
        userDn = new LdapName(userBase);
        userDn.add("uid=" + posixUser.getUid());
        log.debug("Update {}", userDn.toString());
        ldapTemplate.rebind(userDn, null, personAttributes);

        return posixUser;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    }
    return null;
}

From source file:edu.kit.scc.ldap.LdapPosixUserDao.java

/**
 * Inserts a new POSIX user into the LDAP directory.
 * //from   w ww.  j  a  va  2s .  c o m
 * @param posixUser the {@link PosixUser} to insert
 * @return the {@link PosixUser} inserted
 */
public PosixUser insertUser(PosixUser posixUser) {
    if (posixUser.commonName == null || posixUser.gidNumber == null || posixUser.homeDirectory == null
            || posixUser.surName == null || posixUser.uid == null || posixUser.uidNumber == null) {
        log.warn("PosixUser has missing mandatory attributes");
        return null;
    }

    BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
    personBasicAttribute.add("extensibleObject");
    personBasicAttribute.add("inetOrgPerson");
    personBasicAttribute.add("organizationalPerson");
    personBasicAttribute.add("person");
    personBasicAttribute.add("posixAccount");

    Attributes personAttributes = new BasicAttributes();
    personAttributes.put(personBasicAttribute);
    personAttributes.put("cn", posixUser.getCommonName());
    personAttributes.put("sn", posixUser.getSurName());
    personAttributes.put("uid", posixUser.getUid());
    personAttributes.put("uidNumber", String.valueOf(posixUser.getUidNumber()));
    personAttributes.put("gidNumber", String.valueOf(posixUser.getGidNumber()));
    personAttributes.put("homeDirectory", posixUser.getHomeDirectory());

    if (posixUser.getUniqueIdentifier() != null) {
        personAttributes.put("uniqueIdentifier", posixUser.getUniqueIdentifier());
    }
    if (posixUser.getDescription() != null) {
        personAttributes.put("description", posixUser.getDescription());
    }
    if (posixUser.getGecos() != null) {
        personAttributes.put("gecos", posixUser.getGecos());
    }
    if (posixUser.getLoginShell() != null) {
        personAttributes.put("loginShell", posixUser.getLoginShell());
    }
    if (posixUser.getUserPassword() != null) {
        personAttributes.put("userPassword", posixUser.getUserPassword());
    }
    if (posixUser.getGivenName() != null) {
        personAttributes.put("givenName", posixUser.getGivenName());
    }
    if (posixUser.getMail() != null) {
        personAttributes.put("mail", posixUser.getMail());
    }

    LdapName newUserDn = LdapUtils.emptyLdapName();
    try {
        newUserDn = new LdapName(userBase);
        newUserDn.add("uid=" + posixUser.getUid());
        log.debug("Insert {}", newUserDn.toString());
        ldapTemplate.bind(newUserDn, null, personAttributes);

        return posixUser;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    } catch (NameAlreadyBoundException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return null;
}

From source file:au.org.theark.core.service.ArkCommonServiceImpl.java

public ArkUserVO getUser(String username) throws ArkSystemException, EntityNotFoundException {
    ArkUserVO userVO = new ArkUserVO();
    try {//from   ww  w .  ja  va  2s  . com

        LdapName ldapName = new LdapName(ldapDataContextSource.getBasePeopleDn());
        ldapName.add(new Rdn("cn", username));
        Name nameObj = (Name) ldapName;

        userVO = (ArkUserVO) ldapDataContextSource.getLdapTemplate().lookup(nameObj, new PersonContextMapper());

    } catch (InvalidNameException ne) {
        throw new ArkSystemException("A System error has occured");
    } catch (NameNotFoundException ex) {
        log.error(username + " not found in LDAP");
        throw new EntityNotFoundException();
    }

    return userVO;
}