Search in sources :

Example 1 with AMOrganizationalUnit

use of com.iplanet.am.sdk.AMOrganizationalUnit 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);
        }
    }
}
Also used : Set(java.util.Set) OrderedSet(com.sun.identity.shared.datastruct.OrderedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) AttrSet(com.iplanet.services.ldap.AttrSet) ArrayList(java.util.ArrayList) AMException(com.iplanet.am.sdk.AMException) RDN(org.forgerock.opendj.ldap.RDN) DN(org.forgerock.opendj.ldap.DN) SSOException(com.iplanet.sso.SSOException) AMStoreConnection(com.iplanet.am.sdk.AMStoreConnection) AMOrganization(com.iplanet.am.sdk.AMOrganization) Iterator(java.util.Iterator) AMOrganizationalUnit(com.iplanet.am.sdk.AMOrganizationalUnit) AMObject(com.iplanet.am.sdk.AMObject) RDN(org.forgerock.opendj.ldap.RDN)

Example 2 with AMOrganizationalUnit

use of com.iplanet.am.sdk.AMOrganizationalUnit in project OpenAM by OpenRock.

the class AMClientCapData method addClient.

/**
     * Add a client. For every property in the Map, it looks up the schema to
     * check if the property is known, if not known adds it to the
     * additionalProperties schema element. <br>
     * 
     * <b>Note: To add a property in the external db to mask the corresponding
     * property value in internal db, add the property with a " "
     * ("&lt;space&gt;") not an empty "" string. This is required because, when
     * dsame fetches the value from directory and sees it has no value, it
     * returns an empty set. (And we discard empty sets internally - bcos dsame
     * stores values for every property defined in the schema).</b>
     * 
     * @param token
     *            SSOToken to validate the user
     * @param props
     *            Map of profiles known to ClientCap. The Map "must" have a
     *            property "clientType"
     * 
     * @return 0 on success
     * @exception AMClientCapException
     *                if Client could not be added - permission problems or if
     *                the clientType property is mising in the Map.
     */
public int addClient(SSOToken token, Map props) throws AMClientCapException {
    int status = 0;
    String ct = getClientType(props);
    Map m = getKnownProperties(props);
    Map entityMap = new HashMap(1);
    entityMap.put(ct, m);
    try {
        AMStoreConnection conn = new AMStoreConnection(token);
        AMOrganizationalUnit amOU = conn.getOrganizationalUnit(databaseDN);
        amOU.createEntities(UMS_ADD_TEMPLATE_NAME, entityMap);
    } catch (Exception e) {
        String[] errArgs = { ct };
        AMClientCapException ace = new AMClientCapException(BUNDLE_NAME, ADD_FAILED, errArgs);
        String msg = ace.getMessage();
        debug.error(dbStr + msg, e);
        throw ace;
    }
    return status;
}
Also used : AMStoreConnection(com.iplanet.am.sdk.AMStoreConnection) HashMap(java.util.HashMap) AMOrganizationalUnit(com.iplanet.am.sdk.AMOrganizationalUnit) HashMap(java.util.HashMap) Map(java.util.Map) SSOException(com.iplanet.sso.SSOException)

Aggregations

AMOrganizationalUnit (com.iplanet.am.sdk.AMOrganizationalUnit)2 AMStoreConnection (com.iplanet.am.sdk.AMStoreConnection)2 SSOException (com.iplanet.sso.SSOException)2 AMException (com.iplanet.am.sdk.AMException)1 AMObject (com.iplanet.am.sdk.AMObject)1 AMOrganization (com.iplanet.am.sdk.AMOrganization)1 AttrSet (com.iplanet.services.ldap.AttrSet)1 OrderedSet (com.sun.identity.shared.datastruct.OrderedSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 DN (org.forgerock.opendj.ldap.DN)1 RDN (org.forgerock.opendj.ldap.RDN)1