From 8cc8d796ece1d8fddd0cea956078c563b724d24d Mon Sep 17 00:00:00 2001 From: Ethan Kromhout Date: Wed, 3 Apr 2019 10:40:06 -0400 Subject: [PATCH] Added support for activation status and handling UnknownUidException --- .../samples/connector-rest-wordpress.xml | 8 +++ .../samples/subscriber-role.xml | 62 +++++++++++++++++++ .../wordpress/WordpressRestConnector.java | 50 +++++++++++++-- 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 connector-rest-wordpress/samples/subscriber-role.xml diff --git a/connector-rest-wordpress/samples/connector-rest-wordpress.xml b/connector-rest-wordpress/samples/connector-rest-wordpress.xml index 5e83b15..d692fbc 100644 --- a/connector-rest-wordpress/samples/connector-rest-wordpress.xml +++ b/connector-rest-wordpress/samples/connector-rest-wordpress.xml @@ -308,6 +308,11 @@ + + + + + @@ -362,6 +367,9 @@ true false + + + diff --git a/connector-rest-wordpress/samples/subscriber-role.xml b/connector-rest-wordpress/samples/subscriber-role.xml new file mode 100644 index 0000000..5a65f75 --- /dev/null +++ b/connector-rest-wordpress/samples/subscriber-role.xml @@ -0,0 +1,62 @@ + + Subscriber + + 2019-03-29T17:20:56.643Z + + + + 2019-03-29T17:20:56.702Z + + + + http://midpoint.evolveum.com/xml/ns/public/gui/channels-3#user + + + 2019-03-29T17:20:56.764Z + + + add + c:RoleType + + + com.evolveum.midpoint.model.impl.lens.ChangeExecutor.executeDelta + success + 1000000000000003970 + + Subscriber + + success + + + + http://midpoint.evolveum.com/xml/ns/public/gui/channels-3#user + + + enabled + enabled + 2019-03-29T17:20:56.652Z + + 0 + + Wordpress Subscriber + + + + + + + ri:roles + + strong + + $containingObject/name + + + subscriber + + + + + + + diff --git a/connector-rest-wordpress/src/main/java/edu/unc/polygon/connector/wordpress/WordpressRestConnector.java b/connector-rest-wordpress/src/main/java/edu/unc/polygon/connector/wordpress/WordpressRestConnector.java index 0313341..b87e52f 100644 --- a/connector-rest-wordpress/src/main/java/edu/unc/polygon/connector/wordpress/WordpressRestConnector.java +++ b/connector-rest-wordpress/src/main/java/edu/unc/polygon/connector/wordpress/WordpressRestConnector.java @@ -205,6 +205,7 @@ public Schema schema() { password.setRequired(false); password.setMultiValued(false); attributes.add(password.build()); + attributes.add(OperationalAttributeInfos.ENABLE); schemaBuilder.defineObjectClass(ObjectClass.ACCOUNT.getDisplayNameKey(),attributes); LOG.info(">>> schema finished"); return schemaBuilder.build(); @@ -459,14 +460,32 @@ public Uid create(ObjectClass objectClass, Set attributes, OperationO if (last_name != null) { jsonObject.put(ATTR_LAST_NAME, last_name); } + Boolean enable = getAttr(attributes, OperationalAttributes.ENABLE_NAME, Boolean.class); + if (enable != null) { + if (! enable) { + JSONArray rolesJsonArray = new JSONArray(); + jsonObject.put(ATTR_ROLES, (Object) rolesJsonArray); + } + else { + String[] rolecheck = getMultiValAttr(attributes, ATTR_ROLES, null); + if (rolecheck == null || rolecheck[0] == null) { + JSONArray rolesJsonArray = new JSONArray(); + rolesJsonArray.put(0, (Object) "subscriber"); + jsonObject.put(ATTR_ROLES, (Object) rolesJsonArray); + } + } + } String[] roles = getMultiValAttr(attributes, ATTR_ROLES, null); if ( roles != null ) { JSONArray rolesJsonArray = new JSONArray(); for ( int i = 0; i < roles.length; i++) { String role = roles[i]; - JSONObject roleJsonObject = new JSONObject(role); + //JSONObject roleJsonObject = new JSONObject(); + //roleJsonObject.put(role); + rolesJsonArray.put(i, (Object) role); + //JSONObject roleJsonObject = new JSONObject(role); //roleJsonObject.put(role); - rolesJsonArray.put(i, (Object) roleJsonObject); + //rolesJsonArray.put(i, (Object) roleJsonObject); } jsonObject.put(ATTR_ROLES, (Object) rolesJsonArray); } @@ -555,14 +574,29 @@ public Uid update(ObjectClass objectClass, Uid uid, Set attributes, O if (last_name != null) { jsonObject.put(ATTR_LAST_NAME, last_name); } + Boolean enable = getAttr(attributes, OperationalAttributes.ENABLE_NAME, Boolean.class); + if (enable != null) { + if (! enable) { + JSONArray rolesJsonArray = new JSONArray(); + jsonObject.put(ATTR_ROLES, (Object) rolesJsonArray); + } + else { + String[] rolecheck = getMultiValAttr(attributes, ATTR_ROLES, null); + if (rolecheck == null || rolecheck[0] == null) { + JSONArray rolesJsonArray = new JSONArray(); + rolesJsonArray.put(0, (Object) "subscriber"); + jsonObject.put(ATTR_ROLES, (Object) rolesJsonArray); + } + } + } String[] roles = getMultiValAttr(attributes, ATTR_ROLES, null); if ( roles != null ) { JSONArray rolesJsonArray = new JSONArray(); for ( int i = 0; i < roles.length; i++) { String role = roles[i]; - JSONObject roleJsonObject = new JSONObject(role); + //JSONObject roleJsonObject = new JSONObject(); //roleJsonObject.put(role); - rolesJsonArray.put(i, (Object) roleJsonObject); + rolesJsonArray.put(i, (Object) role); } jsonObject.put(ATTR_ROLES, (Object) rolesJsonArray); } @@ -643,6 +677,14 @@ protected JSONObject callRequest(HttpEntityEnclosingRequestBase request, JSONObj throw new AlreadyExistsException(jo.getString(ATTR_MAIL)); } } + if (statusCode == 404) { + JSONObject errorResult = new JSONObject(responseText); + //JSONObject errorData = errorResult.getJSONObject("data"); + if (errorResult.getString("code").equals("rest_user_invalid_id")) { + closeResponse(response); + throw new UnknownUidException(errorResult.toString()); + } + } processResponseErrors(response); JSONObject result = new JSONObject (responseText); LOG.info("response body: ");