use of com.iplanet.am.sdk.AMObject in project OpenAM by OpenRock.
the class DirectoryServicesImpl method validateAttributeUniqueness.
/**
* Validate attribute uniqueness
*
* @param newEntry
* true if create a new user
* @throws AMException
* if attribute uniqueness is violated
*/
void validateAttributeUniqueness(String entryDN, int profileType, boolean newEntry, Map modMap) throws AMException {
boolean attrExists = false;
if (modMap == null || modMap.isEmpty()) {
return;
}
try {
if (profileType == AMTemplate.DYNAMIC_TEMPLATE || profileType == AMTemplate.ORGANIZATION_TEMPLATE || profileType == AMTemplate.POLICY_TEMPLATE) {
// no namespace validation for these objects
return;
}
DN dn = DN.valueOf(entryDN);
int size = dn.size();
if (size < 2) {
return;
}
List<RDN> rdns = new ArrayList<>();
for (Iterator<RDN> iter = dn.iterator(); iter.hasNext(); ) {
rdns.add(iter.next());
}
String orgDN = rdns.get(rdns.size() - 1).toString();
AMStoreConnection amsc = new AMStoreConnection(CommonUtils.getInternalToken());
DN rootDN = DN.valueOf(AMStoreConnection.getAMSdkBaseDN());
DN thisDN = DN.valueOf(orgDN);
for (int i = size - 2; i >= 0; i--) {
if (debug.messageEnabled()) {
debug.message("AMObjectImpl.validateAttributeUniqueness: " + "try DN = " + orgDN);
}
int type = -1;
if (!rootDN.isInScopeOf(thisDN, SearchScope.SUBORDINATES)) {
try {
type = amsc.getAMObjectType(orgDN);
} catch (AMException ame) {
if (debug.warningEnabled()) {
debug.warning("AMObjectImpl." + "validateAttributeUniqueness: " + "Unable to determine object type of " + orgDN + " :Attribute uniqueness check aborted..", ame);
}
return;
}
}
Set list = null;
AMObject amobj = null;
if (type == AMObject.ORGANIZATION) {
AMOrganization amorg = amsc.getOrganization(orgDN);
list = amorg.getAttribute(UNIQUE_ATTRIBUTE_LIST_ATTRIBUTE);
amobj = amorg;
} else if (type == AMObject.ORGANIZATIONAL_UNIT) {
AMOrganizationalUnit amorgu = amsc.getOrganizationalUnit(orgDN);
list = amorgu.getAttribute(UNIQUE_ATTRIBUTE_LIST_ATTRIBUTE);
amobj = amorgu;
}
if ((list != null) && !list.isEmpty()) {
if (debug.messageEnabled()) {
debug.message("AMObjectImpl." + "validateAttributeUniqueness: list =" + list);
}
/*
* After adding the uniquness attributes 'ou,cn' to the
* list, creating a role with the same name as the existing
* user say 'amadmin' fails with 'Attribute uniqueness
* violation' The filter (|(cn='attrname')) is used for all
* objects. Fixed the code to look for 'Role' profile types
* and set the filter as
* (&(objectclass=ldapsubentry)
* (objectclass=nsroledefinition)
* (cn='attrname'))
*
* The same issue happens when a group is created with
* existing user name. Fixed the code to look for 'Group'
* profile types and set the filter as
* (&(objectClass=groupofuniquenames)
* (objectClass=iplanet-am-managed-group)(cn='attrname'))
* The logic in the while loop is iterate through the
* attribute unique list and check if the list contains the
* naming attribute of the object we are trying to create.
* If the naming attribute is in the list,then look if the
* profile type of the object we are trying to create is
* 'role' or 'group', add appropriate objectclasses and the
* entry rdn to the search filter. This filter is used to
* search the iDS and determine the attribute uniqueness
* violation. The boolean variable 'attrExists' is set to
* false initially. This variable is set to true when the
* profile type is 'role' or 'group'. The check for this
* boolean variable decides the number of matching closing
* parens of the three different types of filters.
*/
Iterator iter = list.iterator();
StringBuffer filterSB = new StringBuffer();
StringBuffer newEntrySB = new StringBuffer();
filterSB.append("(|");
while (iter.hasNext()) {
String[] attrList = getAttrList((String) iter.next());
Set attr = getAttrValues(attrList, modMap);
for (int j = 0; j < attrList.length; j++) {
String attrName = attrList[j];
if (attrName.equals(getNamingAttribute(profileType)) && newEntry) {
if ((profileType == AMObject.ROLE) || (profileType == AMObject.MANAGED_ROLE) || (profileType == AMObject.FILTERED_ROLE)) {
newEntrySB.append("(&");
newEntrySB.append("(objectclass=ldapsubentry)");
newEntrySB.append("(" + "objectclass=nsroledefinition)");
attrExists = true;
} else if ((profileType == AMObject.GROUP) || (profileType == AMObject.STATIC_GROUP) || (profileType == AMObject.ASSIGNABLE_DYNAMIC_GROUP) || (profileType == AMObject.DYNAMIC_GROUP)) {
newEntrySB.append("(&");
newEntrySB.append("(objectclass=iplanet-am-managed-group)");
newEntrySB.append("(objectclass=groupofuniquenames)");
attrExists = true;
} else if (profileType == AMObject.ORGANIZATION) {
newEntrySB.append("(&(!");
newEntrySB.append("(objectclass=");
newEntrySB.append(SMSEntry.OC_REALM_SERVICE);
newEntrySB.append("))");
attrExists = true;
}
filterSB.append("(").append(rdns.get(0)).append(")");
}
if (attr != null && !attr.isEmpty()) {
Iterator itr = attr.iterator();
while (itr.hasNext()) {
filterSB.append("(").append(attrName);
filterSB.append("=").append(itr.next());
filterSB.append(")");
}
}
// if
}
}
if (filterSB.length() > 2) {
if (attrExists) {
// pre-pend the creation filter part to the filter
// This is being done so that the filter is
// correctly created as
// (&(<creation-filter)(|(<attr filter>)))
newEntrySB.append(filterSB.toString()).append("))");
filterSB = newEntrySB;
} else {
filterSB.append(")");
}
if (debug.messageEnabled()) {
debug.message("AMObjectImpl." + "validateAttributeUniqueness: " + "filter = " + filterSB.toString());
}
Set users = amobj.search(AMConstants.SCOPE_SUB, filterSB.toString());
// In that case,ignore the violation
if (users != null && users.size() == 1) {
String userDN = (String) users.iterator().next();
DN dnObject = DN.valueOf(userDN);
if (dnObject.equals(DN.valueOf(entryDN))) {
return;
}
}
if ((users != null) && !users.isEmpty()) {
throw new AMException(AMSDKBundle.getString("162"), "162");
}
}
}
orgDN = rdns.get(i).toString() + "," + orgDN;
thisDN = DN.valueOf(orgDN);
}
} catch (SSOException ex) {
if (debug.warningEnabled()) {
debug.warning("Unable to validate attribute uniqneness", ex);
}
}
}
use of com.iplanet.am.sdk.AMObject in project OpenAM by OpenRock.
the class AdminInterfaceUtils method inOrganizationalUnit.
/**
* Returns true if an object is a descendent of an organizational unit.
*
* @param debug
* instance
* @param storeConn
* Store Connection object.
* @param obj
* <code>AMObject</code> to be inspected.
* @return true if <code>obj</code> is a descendent of an organizational
* unit.
*/
public static boolean inOrganizationalUnit(Debug debug, AMStoreConnection storeConn, AMObject obj) {
boolean inOrgUnit = false;
String parentDN = obj.getParentDN();
try {
while ((parentDN != null) && !inOrgUnit) {
if (storeConn.getAMObjectType(parentDN) == AMObject.ORGANIZATIONAL_UNIT) {
inOrgUnit = true;
} else {
AMObject parent = getAMObject(debug, parentDN, storeConn);
parentDN = (parent != null) ? parent.getParentDN() : null;
}
}
} catch (SSOException ssoe) {
debug.warning("AdminInterfaceUtils.inOrganizationalUnit", ssoe);
} catch (AMException ame) {
debug.warning("AdminInterfaceUtils.inOrganizationalUnit", ame);
}
return inOrgUnit;
}
use of com.iplanet.am.sdk.AMObject in project OpenAM by OpenRock.
the class AdminInterfaceUtils method getAMObject.
/**
* Returns the <code>AMObject</code> of a given DN and a store connection.
*
* @param debug
* instance
* @param dn
* of the object as a String
* @param storeConn
* store connection
* @return <code>AMObject</code>
* @throws AMException
* if <code>AMSDK</code> is unable to get
* <code>AMObject</code> for <code>dn</code>
* @throws SSOException
* if session expires.
*/
public static AMObject getAMObject(Debug debug, String dn, AMStoreConnection storeConn) throws AMException, SSOException {
AMObject obj = null;
int objectType = storeConn.getAMObjectType(dn);
switch(objectType) {
case AMObject.ORGANIZATION:
obj = storeConn.getOrganization(dn);
break;
case AMObject.ORGANIZATIONAL_UNIT:
obj = storeConn.getOrganizationalUnit(dn);
break;
case AMObject.GROUP:
case AMObject.STATIC_GROUP:
obj = storeConn.getStaticGroup(dn);
break;
case AMObject.DYNAMIC_GROUP:
obj = storeConn.getDynamicGroup(dn);
break;
case AMObject.ASSIGNABLE_DYNAMIC_GROUP:
obj = storeConn.getAssignableDynamicGroup(dn);
break;
case AMObject.ROLE:
obj = storeConn.getRole(dn);
break;
case AMObject.FILTERED_ROLE:
obj = storeConn.getFilteredRole(dn);
break;
case AMObject.USER:
obj = storeConn.getUser(dn);
break;
case AMObject.PEOPLE_CONTAINER:
obj = storeConn.getPeopleContainer(dn);
break;
case AMObject.GROUP_CONTAINER:
obj = storeConn.getGroupContainer(dn);
break;
default:
if (debug.warningEnabled()) {
debug.warning("AdminInterfaceUtils.getAMObject: " + "Cannot create AMObject for:" + dn);
}
}
return obj;
}
Aggregations