Tuesday, July 2, 2013

Adding OIM User Defined Attribute through the OIM API

Version: Oracle Identity Manager 11g R1
Description: This post contains java code to add OIM User Profile attribute using the OIM API. The ConfigManager class contains the method to add User Defined (UDF) attribute. The code below is only for demonstration purposes for adding a string attribute. Dealing with other attribute types may require setting additional properties. Also, the value of an attribute property may be dependent on other property values. WARNING: Use at your own risk. This OIM API does some data validation. Creating an invalid attribute can cause a lot of problems (E.g. An error message dialog will prevent you from viewing any OIM user's profile).  If you happen to create a bad attribute, most of the time deleting the attribute will fix the problem.

package oimudfattributeutil;

import java.util.Hashtable;
import javax.security.auth.login.LoginException;
import oracle.iam.configservice.api.ConfigManager;
import oracle.iam.configservice.api.Constants;
import oracle.iam.configservice.api.Constants.Encryption;
import oracle.iam.configservice.exception.AttributeAlreadyExistsException;
import oracle.iam.configservice.exception.AttributeCannotBeRequiredException;
import oracle.iam.configservice.exception.AttributeCreateException;
import oracle.iam.configservice.exception.CategoryDoesNotExistException;
import oracle.iam.configservice.exception.ConfigManagerException;
import oracle.iam.configservice.exception.InvalidCharacterException;
import oracle.iam.configservice.exception.NoSuchEntityException;
import oracle.iam.configservice.vo.AttributeDefinition;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.kernel.ValidationFailedException;

/**
 * This demonstrates how to add user defined attributes in OIM User
 * profile using the OIM APIs. The ConfigManager contains the services
 * to add and remove attributes and categories.
 */
public class OIMUDFAttributeUtil
{
    public static void main(String[] args) 
    {
        //Info required for the OIMClient
        String ctxFactory = "weblogic.jndi.WLInitialContextFactory";
        String oimServerURL = "t3://localhost:14000";
        String authwlConfigPath = "/home/oracle/oimClient_lib/conf/authwl.conf";
        String username = "xelsysadm";
        String password = "Password1";
        OIMClient oimClient = null;

        System.setProperty("java.security.auth.login.config", authwlConfigPath); 
        Hashtable<String,String> env = new Hashtable<String,String>();
        env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, ctxFactory);
        env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, oimServerURL);

        try
        {    
            oimClient = new OIMClient(env);
            oimClient.login(username, password.toCharArray());
            ConfigManager configMgrOps = oimClient.getService(ConfigManager.class);

            //Minimum amount of info needed to create a string attribute type
            String attrName = "Custom Attribute3";
            String columnName = "USR_UDF_CUSTOM_ATTRIBUTE3";
            String categoryName = "Other User Attributes";
            String displayType = "TEXT";
            boolean isReadOnly = false;
            Encryption encryptionType = Constants.Encryption.CLEAR;
            boolean isVisible = true;
            Integer attrSize = 100;
            boolean isSearchable = true;
            boolean isBulkUpdatable = false;
            boolean isCustomAttr = true;
            String attrBackEndType = "string";
            boolean isUserSearchable = true;
            boolean isRequired = false;
            boolean isSystemControlled = false;

            //Stage the attribute object to be added
            AttributeDefinition attrObj = new AttributeDefinition(attrName);
            attrObj.setBackendName(columnName);
            attrObj.setCategory(categoryName);
            attrObj.setDisplayType(displayType);
            attrObj.setReadOnly(isReadOnly);
            attrObj.setEncryption(encryptionType);
            attrObj.setVisible(isVisible);
            attrObj.setMaxSize(attrSize);
            attrObj.setSearchable(isSearchable);
            attrObj.setBulkUpdatable(isBulkUpdatable);
            attrObj.setCustomAttribute(isCustomAttr);
            attrObj.setBackendType(attrBackEndType);
            attrObj.setUserSearchable(isUserSearchable);
            attrObj.setRequired(isRequired);
            attrObj.setSystemControlled(isSystemControlled);

            //Add User attribute to the OIM User Profile
            configMgrOps.addAttribute(Constants.Entity.USER, attrObj);
        } 

        catch (NoSuchEntityException e) {e.printStackTrace();} 
        catch (InvalidCharacterException e) {e.printStackTrace();}
        catch (AttributeAlreadyExistsException e) {e.printStackTrace();}
        catch (CategoryDoesNotExistException e) {e.printStackTrace();}
        catch (AttributeCannotBeRequiredException e) {e.printStackTrace();}
        catch (ValidationFailedException e) {e.printStackTrace();}
        catch (AttributeCreateException e) {e.printStackTrace();}
        catch (ConfigManagerException e) {e.printStackTrace();}  
        catch (LoginException e) {e.printStackTrace();}     
        finally{try{oimClient.logout();} catch(Exception e) {}} 
    }
}

No comments:

Post a Comment