591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
c9e3af68445c04348ae8875179a6514b6f6539d2Jon Branch * Copyright (c) 2014-2015 ForgeRock AS. All rights reserved.
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * The contents of this file are subject to the terms
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * of the Common Development and Distribution License
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * (the License). You may not use this file except in
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * compliance with the License.
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * You can obtain a copy of the License at
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * See the License for the specific language governing
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * permission and limitations under the License.
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * When distributing Covered Code, include this CDDL
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * Header Notice in each file and include the License file
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * If applicable, add the following below the CDDL Header,
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * with the fields enclosed by brackets [] replaced by
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * your own identifying information:
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * "Portions Copyrighted [year] [name of copyright owner]"
591dd7e8dc5d7535cf44126ac61ded6e04b8b0e2Chad Kienle * Default mapping script for processing effectiveAssignments
4be5f91e069e2dfd8edb3d377202df39a656f604Chad Kienle * Assignment and unassignment operations are configured as scripts.
4be5f91e069e2dfd8edb3d377202df39a656f604Chad Kienle * An example operation configuration is:
4be5f91e069e2dfd8edb3d377202df39a656f604Chad Kienle// The result map
4554ee857b0583504120a76802beece3ce2a2e9aChad Kienle effectiveAssignments = source.effectiveAssignments;
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle// Default operations
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienlevar defaultAssignmentOperation = "replaceTarget";
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienlevar defaultUnassignmentOperation = "removeFromTarget";
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle// A map of operation aliases to their corresponding script configurations
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle "replaceTarget" : {
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle "mergeWithTarget" : {
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle "removeFromTarget" : {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle * Returns the script configuration with the necessary scope fields added.
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle * @param script a script configuration or a script alias
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle // Check if the script is an alias or a full configuration
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle if (script instanceof Object) {
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle // Full configuration
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle } else if (script instanceof String || typeof(script) === 'string') {
394a65c94dd86b23555c4a35105c6a0af4d39df5Chad Kienle // Add additional scope fields
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle * Executes a script from the given configurations.
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle * @param scriptConfig the script configuration
0b90cf39da4c7ba2b843ffd3512d84d009b5dff0Brendan Mmiller var result = openidm.action("script", "eval", getConfig(scriptConfig), {});
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle * Searches the given list for an assignment relationship matching the given assignment relationship.
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle * Returns the fetched assignment or null if no matching assignment is found.
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle * @param assignment the assignment to search for
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle * @param listOfAssignments a list of assignments
906e25715c624ee7307d9a50eaacc0c7ee494ad2Chad Kienlefunction findAssignment(assignment, listOfAssignments) {
906e25715c624ee7307d9a50eaacc0c7ee494ad2Chad Kienle for (var i = 0; i < listOfAssignments.length; i++) {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle if (assignment._id === listOfAssignments[i]._id) {
906e25715c624ee7307d9a50eaacc0c7ee494ad2Chad Kienle return null;
4be5f91e069e2dfd8edb3d377202df39a656f604Chad Kienle// Check for any assignments that have been removed or modified
9ba7a41b4fca67bf56ca5eaaf78de00d25f3055bAndi Egloffif (typeof oldSource !== 'undefined' && oldSource !== null) {
4be5f91e069e2dfd8edb3d377202df39a656f604Chad Kienle var oldAssignments = oldSource.effectiveAssignments; // Assignments from the old source value
4be5f91e069e2dfd8edb3d377202df39a656f604Chad Kienle var currentAssignments = source.effectiveAssignments; // Assignments from the current source value
4be5f91e069e2dfd8edb3d377202df39a656f604Chad Kienle // Loop through old assignments
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle if (typeof oldAssignments !== 'undefined' && oldAssignments !== null) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle for (var x = 0; x < oldAssignments.length; x++) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Check that this assignment is relevant to this mapping
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle if ((oldAssignment !== null) && (mappingName === oldAssignment.mapping)) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Get the Current assignment, may be null if it has been removed/unassigned
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle var currentAssignment = findAssignment(oldAssignment, currentAssignments);
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle if (currentAssignment === null) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // This assignment has been unassigned
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle var onUnassignment = oldAssignment.onUnassignment;
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Check if an onUnassignment script is configured
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle if (typeof onUnassignment !== 'undefined' && onUnassignment !== null) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle onUnassignment.attributes = oldAssignment.attributes;
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Get the Old assignment's attributes
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Loop through old attributes and execute the unassignmentOperation on any that were removed or updated
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle for (var i = 0; i < oldAttributes.length; i++) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // If the assignment has not been removed, then we need to check if the attribute has been removed or updated.
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle if (!assignmentRemoved && currentAssignment !== null) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle var currentAttributes = currentAssignment.attributes;
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Loop through attributes to check if they have been removed/updated
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle for (var j = 0; j < currentAttributes.length; j++) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle if (oldAttribute.name == currentAttribute.name) {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle if (JSON.stringify(oldAttribute) === JSON.stringify(currentAttribute)) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // attribute was found and not updated
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Check if the old attribute has been removed
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle var unassignmentOperation = oldAttribute.unassignmentOperation;
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Default to replace and use the entire value
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle unassignmentOperation = defaultUnassignmentOperation;
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle if (typeof unassignmentOperation !== 'undefined' && unassignmentOperation !== null) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle var unassignmentResult = openidm.action("script", "eval", config, {});
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Update the target (working copy)
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle // Update the existingTarget, in order to carry changes over to additional operations that may use the existingTarget
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle if (typeof existingTarget !== 'undefined' && existingTarget !== null) {
ae150fbaef02fe04e486cbf8a1582903da7ab022Chad Kienle existingTarget[oldAttribute.name] = unassignmentResult;
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle// Process effective assignments, if any
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Used to carry information across different assignmentOperations
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle for (var x = 0; x < effectiveAssignments.length; x++) {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Check that this assignment is relevant to this mapping
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle if ((assignment !== null) && (mappingName === assignment.mapping)) {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Only map if no linkQualifiers were specified or the current linkQualifier is in the list of linkQualifiers specified in the assignment
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle if (typeof linkQualifiers === 'undefined' || linkQualifiers === null
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle || linkQualifiers.indexOf(linkQualifier) > -1) {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Check if an onAssignment script is configured
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle if (typeof onAssignment !== 'undefined' && onAssignment !== null) {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Loop through attributes, performing the assignmentOperations
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle var assignmentOperation = attribute.assignmentOperation;
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle if (typeof attributesInfo === "undefined" || attributesInfo === null) {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Initialize if it has not already been defined for this attribute
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Default to replace and use the entire value
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle assignmentOperation = defaultAssignmentOperation;
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Process the assignmentOperation
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // The result of this call should be an object with a field "value" containing the updated target field's value
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle var assignmentResult = openidm.action("script", "eval", config, {});
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Set the new target field's value
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle // Update any passed back attributesInfo
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle if (assignmentResult.hasOwnProperty("attributesInfo")) {
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle attributesInfoMap[name] = assignmentResult.attributesInfo;
b3c91ed9b21741200b26a3da31b64a887e629426Chad Kienle// Return the resulting map