public Role createRole(String name, String displayName) throws IdentityException, IllegalArgumentException
{
if (name == null)
{
throw new IdentityException("Role name cannot be null");
}
LdapContext ldapContext = getConnectionContext().createInitialContext();
LdapContext ctx = null;
try
{
//
ctx = (LdapContext)ldapContext.lookup(getContainerDN());
//We store new entry using set of attributes. This should give more flexibility then
//extending user object from ContextDir - configure what objectClass place there
Attributes attrs = new BasicAttributes(true);
//add attribute using provided configuration
Map attributesToAdd = getAttributesToAdd();
//attribute
for (Iterator it1 = attributesToAdd.keySet().iterator(); it1.hasNext();)
{
String attributeName = (String)it1.next();
log.debug("adding attribute: " + attributeName);
Attribute attr = new BasicAttribute(attributeName);
Set attributeValues = (Set)attributesToAdd.get(attributeName);
//values
for (Iterator it2 = attributeValues.iterator(); it2.hasNext();)
{
String attrValue = (String)it2.next();
log.debug("adding attribute value: " + attrValue);
attr.add(attrValue);
}
attrs.put(attr);
}
//role name
attrs.put(getRidAttributeID(), name);
//display name
if (!getDisplayNameAttributeID().equals(getRidAttributeID()))
{
attrs.put(getDisplayNameAttributeID(), displayName);
}
String dn = getRidAttributeID().concat("=").concat(name);
log.debug("creating ldap entry for: " + dn + "; " + attrs);
ctx.createSubcontext(dn, attrs);
}
catch (NamingException e)
{
throw new IdentityException("Failed to create role", e);
}
finally
{
try
{
ldapContext.close();
ctx.close();
}
catch (NamingException e)
{
throw new IdentityException("Failed to close LDAP connection", e);
}
}
Role resultRole = findRoleByName(name);