use of javax.naming.directory.Attribute 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);
}
}
use of javax.naming.directory.Attribute in project perun by CESNET.
the class EventProcessorImpl method updateGroupAttribute.
/**
* Update ldap attribute with attributeName for the group 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 group cant be null
*
* @exception InternalErrorException if an error occurs
*
*/
private void updateGroupAttribute(String attributeName, String attributeValue, LdapOperation operation, Group group) 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 updateGroupAttribute :" + attributeName);
if (group == null)
throw new InternalErrorException("Group is null in method updateGroupAttribute");
Attribute attribute;
if (attributeValue != null)
attribute = new BasicAttribute(attributeName, attributeValue);
else
attribute = new BasicAttribute(attributeName);
ModificationItem attributeItem = new ModificationItem(operation.getCode(), attribute);
ldapConnector.updateGroup(group, new ModificationItem[] { attributeItem });
}
use of javax.naming.directory.Attribute in project perun by CESNET.
the class EventProcessorImpl method emptyAndFillPerunBeans.
/**
* Fill objects from list of beans.
* If list of beans is empty or null, fill nothing.
*
* @param listOfBeans list of beans already parsed from message
*
* @throws InternalErrorException If there is some inconsistence in number of one type's objects.
*/
private void emptyAndFillPerunBeans(List<PerunBean> listOfBeans) throws InternalErrorException {
//First null all usefull objects
resource = null;
member = null;
group = null;
parentGroup = null;
vo = null;
user = null;
specificUser = null;
attribute = null;
attributeDef = null;
userExtSource = null;
facility = null;
//If there is no usefull object, exit method
if (listOfBeans == null)
return;
for (PerunBean perunBean : listOfBeans) {
if (perunBean instanceof Group) {
if (this.group == null)
this.group = (Group) perunBean;
else
this.parentGroup = (Group) perunBean;
} else if (perunBean instanceof Member) {
if (this.member == null)
this.member = (Member) perunBean;
else
throw new InternalErrorException("More than one member come to method parseMessages!");
} else if (perunBean instanceof Vo) {
if (this.vo == null)
this.vo = (Vo) perunBean;
else
throw new InternalErrorException("More than one vo come to method parserMessages!");
} else if (perunBean instanceof User) {
User u = (User) perunBean;
if (u.isServiceUser() || u.isSponsoredUser()) {
if (this.specificUser == null)
this.specificUser = u;
else
throw new InternalErrorException("More than one specificUser come to method parseMessages!");
} else {
if (this.user == null)
this.user = u;
else
throw new InternalErrorException("More than one user come to method parseMessages!");
}
} else if (perunBean instanceof AttributeDefinition && perunBean instanceof cz.metacentrum.perun.core.api.Attribute) {
if (this.attribute == null)
this.attribute = (cz.metacentrum.perun.core.api.Attribute) perunBean;
else
throw new InternalErrorException("More than one attribute come to method parseMessages!");
} else if (perunBean instanceof AttributeDefinition) {
if (this.attributeDef == null)
this.attributeDef = (AttributeDefinition) perunBean;
else
throw new InternalErrorException("More than one attribute come to method parseMessages!");
} else if (perunBean instanceof UserExtSource) {
if (this.userExtSource == null)
this.userExtSource = (UserExtSource) perunBean;
else
throw new InternalErrorException("More than one userExtSource come to method parseMessages!");
} else if (perunBean instanceof Resource) {
if (this.resource == null)
this.resource = (Resource) perunBean;
else
throw new InternalErrorException("More than one Resource come to method parseMessages!");
} else if (perunBean instanceof Facility) {
if (this.facility == null)
this.facility = (Facility) perunBean;
else
throw new InternalErrorException("More than one Facility come to method parseMessages!");
}
}
}
use of javax.naming.directory.Attribute in project perun by CESNET.
the class EventProcessorImpl method updateUserAttributes.
/**
* Update user'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 user cant be null
* @throws InternalErrorException if an error occurs
*/
private void updateUserAttributes(Map<LdapOperation, List<Pair<String, String>>> mapOfAttributes, User user) throws InternalErrorException {
//User cant be null
if (user == null)
throw new InternalErrorException("User is null in method updateUserAttributes");
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.updateUser(user, items);
}
}
use of javax.naming.directory.Attribute in project perun by CESNET.
the class EventProcessorImpl method updateGroupAttributes.
/**
* Update group'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 group cant be null
* @throws InternalErrorException if an error occurs
*/
private void updateGroupAttributes(Map<LdapOperation, List<Pair<String, String>>> mapOfAttributes, Group group) throws InternalErrorException {
//Group cant be null
if (group == null)
throw new InternalErrorException("group 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.updateGroup(group, items);
}
}
Aggregations