1) Checking conditions and getting the true error when creating users

Let’s say you want to create users in OpenIDM through REST, under certain conditions, and you want to catch the failure in case the policy validation service rejects the creation. One simple way I found to accomplish this was to:

  • define a custom endpoint
  • make your own tests in a script triggered when calling your endpoint
  • call the « validateObject » action
  • trigger the creation if everything went well, otherwise print the validateObject service error or any other error you want,

2) A few simple policies not provided by default

var policy1 = {
                "policyId" : "at-least-X-letters",
                "policyExec" : "atLeastXLetters",
                "clientValidation": true,
                "validateOnlyIfPresent": true,
                "policyRequirements": ["AT_LEAST_X_LETTER"]
              }

addPolicy(policy1);

var policy2 = {
                "policyId" : "at-least-X-special-chars",
                "policyExec" : "atLeastXSpecialChars",
                "clientValidation": true,
                "validateOnlyIfPresent": true,
                "policyRequirements": ["AT_LEAST_X_SPECIAL_CHARS"]
              }

addPolicy(policy2);

var policy3 = {   "policyId" : "maximum-length",
                "policyExec" : "maxLength", 
                "clientValidation": true,
                "validateOnlyIfPresent": true,
                "policyRequirements" : ["MAX_LENGTH"]
              }

addPolicy(policy3);

var policy4 = {   "policyId" : "allowed-characters",
                "policyExec" : "AllowedCharacters", 
                "clientValidation": true,
                "validateOnlyIfPresent": true,
                "policyRequirements" : ["ALLOWED_CHARACTERS"]
              }

addPolicy(policy4);

function AllowedCharacters(fullObject, value, params, property) {
    var i,
        join = function (arr, d) { // my own join needed since it appears params.AllowedChars is not a proper JS array with the normal join method available
            var j,list = "";
            for (j in arr) {
                list += arr[j] + d
            }
            return list.replace(new RegExp(d + "$"), '');
        };

    if (typeof(value) === "string" && value.length) {
        for (i=0; i<value.length; i++) {
            if (params.AllowedChars[0].indexOf(value[i]) == -1) {
                return [ { "policyRequirement" : "ALLOWED_CHARACTERS", "params" : {"AllowedChars" : join(params.AllowedChars, ", ")} } ];
            }
        }
    }
    return [];
}

function maxLength(fullObject, value, params, property) {
    var maxLength = params.maxLength;

    if (typeof(value) !== "string" || !value.length || value.length > maxLength) {
        return [ { "policyRequirement" : "MAX_LENGTH", "params" : {"maxLength":maxLength} } ];
    } else {
        return [];
    }
}

function atLeastXLetters(fullObject, value, params, property) {
    var reg = /[(A-Z)(a-z)]/g;

    if (typeof(value) !== "string" || !value.length || value.match(reg) === null || value.match(reg).length < params.numLetters) {
        return [ { "policyRequirement" : "AT_LEAST_X_LETTER", "params" : {"numLetters": params.numLetters} } ];
    } else {
        return [];
    }
}

function atLeastXSpecialChars(fullObject, value, params, property) {
var reg = new RegExp("[" + params.specialChars + "]","g");

    if (typeof(value) !== "string" || !value.length || value.match(reg) === null || value.match(reg).length < params.numChars) {
        return [ { "policyRequirement" : "AT_LEAST_X_SPECIAL_CHARS", "params" : {"numChars": params.numChars, "Special Chars": params.specialChars } } ];
    } else {
        return [];
    }
}
Les derniers articles par janua (tout voir)