Search in sources :

Example 11 with Attribute

use of javax.naming.directory.Attribute in project perun by CESNET.

the class EventProcessorImpl method updateVoAttributes.

/**
	 * Update vo's ldap attributes from Map by operation in key.
	 *
	 * Map<LdapOperation, List<Pair<String, String>>> => Map<LdapOperation, List<Pair<attributeName, attributeValue>>>
	 *
	 *
	 * attributeName cant be null and empty String
	 * attributeValue can be null
	 *
	 * Execute all operations on all attributes with (or without value) in 1 task.
	 *
	 * @param mapOfAttributes map of Operation to list of pairs where left is attributeName and right is attributeValue
	 * @param vo cant be null
	 * @throws InternalErrorException if an error occurs
	 */
private void updateVoAttributes(Map<LdapOperation, List<Pair<String, String>>> mapOfAttributes, Vo vo) throws InternalErrorException {
    //User cant be null
    if (vo == null)
        throw new InternalErrorException("Vo is null in method updateVoAttributes");
    //Only 3 types of key are allowed (1,2 or 3) Modification classes
    Set<LdapOperation> keys = mapOfAttributes.keySet();
    //Every Pair in List need to have "attributeName" and may have "attributeValue"
    for (LdapOperation operation : keys) {
        List<Pair<String, String>> listOfAttrs = mapOfAttributes.get(operation);
        for (Pair<String, String> pair : listOfAttrs) {
            if (pair.getLeft() == null || pair.getLeft().equals(""))
                throw new InternalErrorException("Some attributes in map has no name.");
        }
    }
    //If all is correct, can execute operations on attributes
    List<ModificationItem> listOfItemsToModify = new ArrayList<ModificationItem>();
    //For all attributes with operation ADD (1)
    if (mapOfAttributes.containsKey(LdapOperation.ADD_ATTRIBUTE)) {
        List<Pair<String, String>> listOfAddingAttributes = mapOfAttributes.get(LdapOperation.ADD_ATTRIBUTE);
        for (Pair<String, String> pair : listOfAddingAttributes) {
            Attribute attribute;
            if (pair.getRight() != null)
                attribute = new BasicAttribute(pair.getLeft(), pair.getRight());
            else
                attribute = new BasicAttribute(pair.getRight());
            ModificationItem attributeItem = new ModificationItem(LdapOperation.ADD_ATTRIBUTE.getCode(), attribute);
            listOfItemsToModify.add(attributeItem);
        }
    }
    //For all attributes with operation REPLACE (2)
    if (mapOfAttributes.containsKey(LdapOperation.REPLACE_ATTRIBUTE)) {
        List<Pair<String, String>> listOfAddingAttributes = mapOfAttributes.get(LdapOperation.REPLACE_ATTRIBUTE);
        for (Pair<String, String> pair : listOfAddingAttributes) {
            Attribute attribute;
            if (pair.getRight() != null)
                attribute = new BasicAttribute(pair.getLeft(), pair.getRight());
            else
                attribute = new BasicAttribute(pair.getRight());
            ModificationItem attributeItem = new ModificationItem(LdapOperation.REPLACE_ATTRIBUTE.getCode(), attribute);
            listOfItemsToModify.add(attributeItem);
        }
    }
    //For all attributes with operation REMOVE (3)
    if (mapOfAttributes.containsKey(LdapOperation.REMOVE_ATTRIBUTE)) {
        List<Pair<String, String>> listOfAddingAttributes = mapOfAttributes.get(LdapOperation.REMOVE_ATTRIBUTE);
        for (Pair<String, String> pair : listOfAddingAttributes) {
            Attribute attribute;
            if (pair.getRight() != null)
                attribute = new BasicAttribute(pair.getLeft(), pair.getRight());
            else
                attribute = new BasicAttribute(pair.getRight());
            ModificationItem attributeItem = new ModificationItem(LdapOperation.REMOVE_ATTRIBUTE.getCode(), attribute);
            listOfItemsToModify.add(attributeItem);
        }
    }
    //Execute all changes on the notEmpty list of items
    if (!listOfItemsToModify.isEmpty()) {
        ModificationItem[] items = Arrays.copyOf(listOfItemsToModify.toArray(), listOfItemsToModify.toArray().length, ModificationItem[].class);
        ldapConnector.updateVo(vo, items);
    }
}
Also used : LdapOperation(cz.metacentrum.perun.ldapc.beans.LdapOperation) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 12 with Attribute

use of javax.naming.directory.Attribute in project perun by CESNET.

the class EventProcessorImpl method updateUserAttribute.

/**
	 * Update ldap attribute with attributeName for the user by value with operation.
	 *
	 *
	 * @param attributeName name of attribute, is mandatory, cant be null
	 * @param attributeValue value of attribute, is not mandatory, can be null
	 * @param operation add, remove or replace (can't be null)
	 * @param user cant be null
	 *
	 * @exception InternalErrorException if an error occurs
	 *
	 */
private void updateUserAttribute(String attributeName, String attributeValue, LdapOperation operation, User user) throws InternalErrorException {
    if (operation == null)
        throw new InternalErrorException("Operation can't be null");
    if (attributeName == null || attributeName.equals(""))
        throw new InternalErrorException("Bad attribute Name in method updateUserAttribute :" + attributeName);
    if (user == null)
        throw new InternalErrorException("User is null in method updateUserAttribute");
    Attribute attribute;
    if (attributeValue != null)
        attribute = new BasicAttribute(attributeName, attributeValue);
    else
        attribute = new BasicAttribute(attributeName);
    ModificationItem attributeItem = new ModificationItem(operation.getCode(), attribute);
    ldapConnector.updateUser(user, new ModificationItem[] { attributeItem });
}
Also used : Attribute(javax.naming.directory.Attribute) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 13 with Attribute

use of javax.naming.directory.Attribute in project perun by CESNET.

the class LdapConnectorImpl method createResource.

//------------------RESOURCE MODIFICATION METHODS----------------------------
public void createResource(Resource resource, String entityID) 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("perunResource");
    // Add attributes
    attributes.put(objClasses);
    attributes.put("cn", resource.getName());
    attributes.put("perunResourceId", String.valueOf(resource.getId()));
    attributes.put("perunFacilityId", String.valueOf(resource.getFacilityId()));
    attributes.put("perunVoId", String.valueOf(resource.getVoId()));
    if (resource.getDescription() != null && !resource.getDescription().isEmpty())
        attributes.put("description", resource.getDescription());
    // get info about entityID attribute if exists
    if (entityID != null)
        attributes.put("entityID", entityID);
    // Create the entry
    try {
        ldapTemplate.bind(getResourceDN(String.valueOf(resource.getVoId()), String.valueOf(resource.getId())), null, attributes);
        log.debug("New entry created in LDAP: Resource {} in Vo with Id=" + resource.getVoId() + " and Facility with ID=" + resource.getFacilityId() + ".", resource);
    } 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 14 with Attribute

use of javax.naming.directory.Attribute in project perun by CESNET.

the class LdapConnectorImpl method addGroup.

//------------------GROUP MODIFICATION METHODS-------------------------------
public void addGroup(Group group) 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("perunGroup");
    // Add attributes
    attributes.put(objClasses);
    attributes.put("cn", group.getName());
    attributes.put("perunGroupId", String.valueOf(group.getId()));
    attributes.put("perunUniqueGroupName", new String(this.getVoShortName(group.getVoId()) + ":" + group.getName()));
    attributes.put("perunVoId", String.valueOf(group.getVoId()));
    if (group.getDescription() != null && !group.getDescription().isEmpty())
        attributes.put("description", group.getDescription());
    if (group.getParentGroupId() != null) {
        attributes.put("perunParentGroup", "perunGroupId=" + group.getParentGroupId().toString() + ",perunVoId=" + group.getVoId() + "," + ldapProperties.getLdapBase());
        attributes.put("perunParentGroupId", group.getParentGroupId().toString());
    }
    // Create the entry
    try {
        ldapTemplate.bind(getGroupDN(String.valueOf(group.getVoId()), String.valueOf(group.getId())), null, attributes);
        log.debug("New entry created in LDAP: Group {} in Vo with Id=" + group.getVoId() + ".", group);
    } 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 15 with Attribute

use of javax.naming.directory.Attribute in project perun by CESNET.

the class LdapConnectorImpl method createVo.

//--------------------------VO MODIFICATION METHODS---------------------------
public void createVo(Vo vo) throws InternalErrorException {
    // Create a set of attributes for vo
    Attributes voAttributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute voObjClasses = new BasicAttribute("objectClass");
    voObjClasses.add("top");
    voObjClasses.add("organization");
    voObjClasses.add("perunVO");
    // Add attributes
    voAttributes.put(voObjClasses);
    voAttributes.put("o", vo.getShortName());
    voAttributes.put("description", vo.getName());
    voAttributes.put("perunVoId", String.valueOf(vo.getId()));
    // Create the entires
    try {
        ldapTemplate.bind(getVoDNByVoId(String.valueOf(vo.getId())), null, voAttributes);
        log.debug("New entry created in LDAP: Vo {}.", vo);
    } 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)

Aggregations

Attribute (javax.naming.directory.Attribute)288 Attributes (javax.naming.directory.Attributes)162 NamingException (javax.naming.NamingException)133 BasicAttribute (javax.naming.directory.BasicAttribute)97 SearchResult (javax.naming.directory.SearchResult)92 ArrayList (java.util.ArrayList)74 BasicAttributes (javax.naming.directory.BasicAttributes)64 NamingEnumeration (javax.naming.NamingEnumeration)56 SearchControls (javax.naming.directory.SearchControls)55 DirContext (javax.naming.directory.DirContext)46 InitialDirContext (javax.naming.directory.InitialDirContext)40 HashSet (java.util.HashSet)38 HashMap (java.util.HashMap)29 IOException (java.io.IOException)24 LdapName (javax.naming.ldap.LdapName)20 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)18 Hashtable (java.util.Hashtable)17 Map (java.util.Map)17 ModificationItem (javax.naming.directory.ModificationItem)17 List (java.util.List)15