use of javax.naming.directory.Attribute in project perun by CESNET.
the class EventProcessorImpl method updateResourceAttribute.
/**
* Update ldap attribute with attributeName for the resource 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, replace or remove, can't be null
* @param resource cant be null
*
* @exception InternalErrorException if an error occurs
*/
private void updateResourceAttribute(String attributeName, String attributeValue, LdapOperation operation, Resource resource) 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 updateResourceAttribute :" + attributeName);
if (resource == null)
throw new InternalErrorException("Resource is null in method updateResourceAttribute");
Attribute attribute;
if (attributeValue != null)
attribute = new BasicAttribute(attributeName, attributeValue);
else
attribute = new BasicAttribute(attributeName);
ModificationItem attributeItem = new ModificationItem(operation.getCode(), attribute);
ldapConnector.updateResource(resource, new ModificationItem[] { attributeItem });
}
use of javax.naming.directory.Attribute in project perun by CESNET.
the class EventProcessorImpl method updateVoAttribute.
/**
* Update ldap attribute with attributeName for the vo 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, replace or remove, can't be null
* @param vo cant be null
*
* @exception InternalErrorException if an error occurs
*/
private void updateVoAttribute(String attributeName, String attributeValue, LdapOperation operation, Vo vo) 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 updateVoAttribute :" + attributeName);
if (vo == null)
throw new InternalErrorException("Vo is null in method updateVoAttribute");
Attribute attribute;
if (attributeValue != null)
attribute = new BasicAttribute(attributeName, attributeValue);
else
attribute = new BasicAttribute(attributeName);
ModificationItem attributeItem = new ModificationItem(operation.getCode(), attribute);
ldapConnector.updateVo(vo, new ModificationItem[] { attributeItem });
}
use of javax.naming.directory.Attribute in project perun by CESNET.
the class EventProcessorImpl method updateResourceAttributes.
/**
* Update resource'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 resource cant be null
* @throws InternalErrorException if an error occurs
*/
private void updateResourceAttributes(Map<LdapOperation, List<Pair<String, String>>> mapOfAttributes, Resource resource) throws InternalErrorException {
//Resource cant be null
if (resource == null)
throw new InternalErrorException("Resource is null in method updateGroupAttributes");
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.updateResource(resource, items);
}
}
use of javax.naming.directory.Attribute in project perun by CESNET.
the class LdapConnectorImpl method userPasswordExists.
public boolean userPasswordExists(User user) {
Object o = ldapTemplate.lookup(getUserDN(String.valueOf(user.getId())), new UserAttributesContextMapper());
Attributes attrs = null;
if (o != null)
attrs = (Attributes) o;
if (attrs != null) {
Attribute a = attrs.get("userPassword");
if (a != null)
return true;
}
return false;
}
use of javax.naming.directory.Attribute in project perun by CESNET.
the class LdapConnectorImpl method removeMemberFromGroup.
public void removeMemberFromGroup(Member member, Group group) throws InternalErrorException {
//Remove member from group
Attribute uniqueMember = new BasicAttribute("uniqueMember", "perunUserId=" + member.getUserId() + ",ou=People," + ldapProperties.getLdapBase());
ModificationItem uniqueMemberItem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, uniqueMember);
this.updateGroup(group, new ModificationItem[] { uniqueMemberItem });
//Remove member from vo if this group is membersGroup
if (group.getName().equals(VosManager.MEMBERS_GROUP) && group.getParentGroupId() == null) {
//Remove info from vo
this.updateVo(group.getVoId(), new ModificationItem[] { uniqueMemberItem });
//Remove also information from user
Attribute memberOfPerunVo = new BasicAttribute("memberOfPerunVo", String.valueOf(group.getVoId()));
ModificationItem memberOfPerunVoItem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, memberOfPerunVo);
this.updateUserWithUserId(String.valueOf(member.getUserId()), new ModificationItem[] { memberOfPerunVoItem });
}
//Remove group info from member
Attribute memberOf = new BasicAttribute("memberOf", "perunGroupId=" + group.getId() + ",perunVoId=" + group.getVoId() + "," + ldapProperties.getLdapBase());
ModificationItem memberOfItem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, memberOf);
this.updateUserWithUserId(String.valueOf(member.getUserId()), new ModificationItem[] { memberOfItem });
}
Aggregations