5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay/*
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay *
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * Copyright (c) 2014 ForgeRock AS. All Rights Reserved
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay *
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * The contents of this file are subject to the terms
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * of the Common Development and Distribution License
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * (the License). You may not use this file except in
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * compliance with the License.
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay *
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * You can obtain a copy of the License at
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * http://forgerock.org/license/CDDLv1.0.html
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * See the License for the specific language governing
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * permission and limitations under the License.
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay *
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * When distributing Covered Code, include this CDDL
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * Header Notice in each file and include the License file
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * at http://forgerock.org/license/CDDLv1.0.html
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * If applicable, add the following below the CDDL Header,
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * with the fields enclosed by brackets [] replaced by
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * your own identifying information:
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay * "Portions Copyrighted [year] [name of copyright owner]"
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay */
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport groovy.json.JsonBuilder
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport groovyx.net.http.RESTClient
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.apache.http.client.HttpClient
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.forgerock.openicf.connectors.scriptedrest.ScriptedRESTConfiguration
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.forgerock.openicf.misc.scriptedcommon.OperationType
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.identityconnectors.common.logging.Log
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.identityconnectors.framework.common.objects.Attribute
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.identityconnectors.framework.common.objects.AttributesAccessor
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.identityconnectors.framework.common.objects.ObjectClass
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.identityconnectors.framework.common.objects.OperationOptions
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport org.identityconnectors.framework.common.objects.Uid
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport static groovyx.net.http.ContentType.JSON
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayimport static groovyx.net.http.Method.PUT
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef operation = operation as OperationType
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef updateAttributes = new AttributesAccessor(attributes as Set<Attribute>)
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef configuration = configuration as ScriptedRESTConfiguration
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef httpClient = connection as HttpClient
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef connection = customizedConnection as RESTClient
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef name = id as String
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef log = log as Log
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef objectClass = objectClass as ObjectClass
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef options = options as OperationOptions
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaydef uid = uid as Uid
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemaylog.info("Entering " + operation + " Script");
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayswitch (objectClass) {
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay case ObjectClass.ACCOUNT:
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay def builder = new JsonBuilder()
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay builder {
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay contactInformation {
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay telephoneNumber(updateAttributes.hasAttribute("telephoneNumber") ? updateAttributes.findString("telephoneNumber") : "")
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay emailAddress(updateAttributes.hasAttribute("emailAddress") ? updateAttributes.findString("emailAddress") : "")
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay }
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay delegate.name({
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay familyName(updateAttributes.hasAttribute("familyName") ? updateAttributes.findString("familyName") : "")
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay givenName(updateAttributes.hasAttribute("givenName") ? updateAttributes.findString("givenName") : "")
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay })
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay displayName(updateAttributes.hasAttribute("displayName") ? updateAttributes.findString("displayName") : "")
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay }
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay return connection.request(PUT, JSON) { req ->
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay uri.path = "/users/${uid.uidValue}"
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay body = builder.toString()
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay response.success = { resp, json ->
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay new Uid(json._id, json._rev)
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay }
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay }
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay case ObjectClass.GROUP:
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay if (updateAttributes.hasAttribute("members")) {
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay def builder = new JsonBuilder()
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay builder {
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay members(updateAttributes.findList("members"))
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay }
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay return connection.request(PUT, JSON) { req ->
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay uri.path = "/groups/${uid.uidValue}"
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay body = builder.toString()
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay response.success = { resp, json ->
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay new Uid(json._id, json._rev)
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay }
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay }
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay }
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemay}
5f22ff8ce7baf0b39668468cc854eec3eb946003Jason Lemayreturn uid