Search in sources :

Example 6 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method createUser.

//-----------------------USER MODIFICATION METHODS----------------------------
public void createUser(User user) throws InternalErrorException {
    // Create a set of attributes
    Attributes attributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute objClasses = new BasicAttribute("objectClass");
    objClasses.add("top");
    objClasses.add("person");
    objClasses.add("organizationalPerson");
    objClasses.add("inetOrgPerson");
    objClasses.add("perunUser");
    objClasses.add("tenOperEntry");
    objClasses.add("inetUser");
    String firstName = user.getFirstName();
    String lastName = user.getLastName();
    if (firstName == null)
        firstName = "";
    if (lastName == null || lastName.isEmpty())
        lastName = "N/A";
    // Add attributes
    attributes.put(objClasses);
    attributes.put("entryStatus", "active");
    attributes.put("sn", lastName);
    attributes.put("cn", firstName + " " + lastName);
    if (!firstName.isEmpty())
        attributes.put("givenName", firstName);
    attributes.put("perunUserId", String.valueOf(user.getId()));
    if (user.isServiceUser())
        attributes.put("isServiceUser", "1");
    else
        attributes.put("isServiceUser", "0");
    if (user.isSponsoredUser())
        attributes.put("isSponsoredUser", "1");
    else
        attributes.put("isSponsoredUser", "0");
    // Create the entry
    try {
        ldapTemplate.bind(getUserDN(String.valueOf(user.getId())), null, attributes);
        log.debug("New entry created in LDAP: User {} in Group with Id=" + user.getId() + ".", user);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 7 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method resourceAttributeExist.

public boolean resourceAttributeExist(Resource resource, String ldapAttributeName) throws InternalErrorException {
    if (ldapAttributeName == null)
        throw new InternalErrorException("ldapAttributeName can't be null.");
    Object o = null;
    try {
        setLdapAttributeName(ldapAttributeName);
        o = ldapTemplate.lookup(getResourceDN(String.valueOf(resource.getVoId()), String.valueOf(resource.getId())), new ResourcePerunResourceAttributeContextMapper());
    } catch (NameNotFoundException ex) {
        return false;
    }
    if (o == null)
        return false;
    return true;
}
Also used : NameNotFoundException(org.springframework.ldap.NameNotFoundException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 8 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method deleteUser.

public void deleteUser(User user) throws InternalErrorException {
    try {
        ldapTemplate.unbind(getUserDN(String.valueOf(user.getId())));
        log.debug("Entry deleted from LDAP: User {}.", user);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : NameNotFoundException(org.springframework.ldap.NameNotFoundException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 9 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method removeGroup.

public void removeGroup(Group group) throws InternalErrorException {
    List<String> uniqueUsersIds = new ArrayList<String>();
    uniqueUsersIds = this.getAllUniqueMembersInGroup(group.getId(), group.getVoId());
    for (String s : uniqueUsersIds) {
        Attribute memberOf = new BasicAttribute("memberOf", "perunGroupId=" + group.getId() + ",perunVoId=" + group.getVoId() + "," + ldapProperties.getLdapBase());
        ModificationItem memberOfItem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, memberOf);
        this.updateUserWithUserId(s, new ModificationItem[] { memberOfItem });
    }
    try {
        ldapTemplate.unbind(getGroupDN(String.valueOf(group.getVoId()), String.valueOf(group.getId())));
        log.debug("Entry deleted from LDAP: Group {} from Vo with ID=" + group.getVoId() + ".", group);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 10 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method userAttributeExist.

public boolean userAttributeExist(User user, String ldapAttributeName) throws InternalErrorException {
    if (ldapAttributeName == null)
        throw new InternalErrorException("ldapAttributeName can't be null.");
    Object o = null;
    try {
        setLdapAttributeName(ldapAttributeName);
        o = ldapTemplate.lookup(getUserDN(String.valueOf(user.getId())), new UserPerunUserAttributeContextMapper());
    } catch (NameNotFoundException ex) {
        return false;
    }
    if (o == null)
        return false;
    return true;
}
Also used : NameNotFoundException(org.springframework.ldap.NameNotFoundException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

NameNotFoundException (org.springframework.ldap.NameNotFoundException)11 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)10 Attribute (javax.naming.directory.Attribute)5 BasicAttribute (javax.naming.directory.BasicAttribute)5 Attributes (javax.naming.directory.Attributes)4 BasicAttributes (javax.naming.directory.BasicAttributes)4 ArrayList (java.util.ArrayList)1 ModificationItem (javax.naming.directory.ModificationItem)1 DirContextOperations (org.springframework.ldap.core.DirContextOperations)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 SpringSecurityLdapTemplate (org.springframework.security.ldap.SpringSecurityLdapTemplate)1