From a20ecdb7e7c199885517b8cecb276aaf2fa911dd Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Thu, 2 Oct 2014 22:02:23 +0200 Subject: [PATCH] SimpleFocalObjectNotifier and FocusType filter (MID-2044). --- .../xml/ns/public/common/common-3.xsd | 20 +- .../model/api/context/ModelContext.java | 3 + .../notifications/api/events/ModelEvent.java | 37 +++- .../impl/formatters/TextFormatter.java | 1 + .../impl/handlers/AggregatedEventHandler.java | 10 + .../impl/helpers/FocusTypeFilterHelper.java | 68 +++++++ .../notifiers/SimpleFocalObjectNotifier.java | 188 ++++++++++++++++++ .../impl/notifiers/SimpleUserNotifier.java | 126 +----------- .../impl/notifiers/UserPasswordNotifier.java | 13 +- 9 files changed, 335 insertions(+), 131 deletions(-) create mode 100644 model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/helpers/FocusTypeFilterHelper.java create mode 100644 model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleFocalObjectNotifier.java diff --git a/infra/schema/src/main/resources/xml/ns/public/common/common-3.xsd b/infra/schema/src/main/resources/xml/ns/public/common/common-3.xsd index 65438f27272..fa265764157 100644 --- a/infra/schema/src/main/resources/xml/ns/public/common/common-3.xsd +++ b/infra/schema/src/main/resources/xml/ns/public/common/common-3.xsd @@ -8784,7 +8784,6 @@ Focus types supported by this handler. (Default: all) Types not listed are filtered out: not handled by this handler nor any of its successors. (Relevant only for model-generated events.) - NOT IMPLEMENTED YET. @@ -8816,6 +8815,13 @@ + + + + TODO + + + @@ -9075,6 +9081,18 @@ + + + TODO + + + + + + + + + TODO diff --git a/model/model-api/src/main/java/com/evolveum/midpoint/model/api/context/ModelContext.java b/model/model-api/src/main/java/com/evolveum/midpoint/model/api/context/ModelContext.java index 6a19a6be091..4281eaed521 100644 --- a/model/model-api/src/main/java/com/evolveum/midpoint/model/api/context/ModelContext.java +++ b/model/model-api/src/main/java/com/evolveum/midpoint/model/api/context/ModelContext.java @@ -20,6 +20,7 @@ import com.evolveum.midpoint.common.refinery.ResourceShadowDiscriminator; import com.evolveum.midpoint.model.api.ProgressInformation; +import com.evolveum.midpoint.prism.PrismContext; import com.evolveum.midpoint.util.DebugDumpable; import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType; @@ -40,4 +41,6 @@ public interface ModelContext extends Serializable, DebugD Class getFocusClass(); void reportProgress(ProgressInformation progress); + + PrismContext getPrismContext(); // use with care } diff --git a/model/notifications-api/src/main/java/com/evolveum/midpoint/notifications/api/events/ModelEvent.java b/model/notifications-api/src/main/java/com/evolveum/midpoint/notifications/api/events/ModelEvent.java index 7550bfabc93..6fa8d6f7e84 100644 --- a/model/notifications-api/src/main/java/com/evolveum/midpoint/notifications/api/events/ModelEvent.java +++ b/model/notifications-api/src/main/java/com/evolveum/midpoint/notifications/api/events/ModelEvent.java @@ -19,6 +19,8 @@ import com.evolveum.midpoint.model.api.context.ModelContext; import com.evolveum.midpoint.model.api.context.ModelElementContext; import com.evolveum.midpoint.model.api.context.ModelProjectionContext; +import com.evolveum.midpoint.prism.PrismContainerDefinition; +import com.evolveum.midpoint.prism.PrismContext; import com.evolveum.midpoint.prism.delta.ChangeType; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.schema.ObjectDeltaOperation; @@ -29,8 +31,9 @@ import com.evolveum.midpoint.xml.ns._public.common.common_3.EventCategoryType; import com.evolveum.midpoint.xml.ns._public.common.common_3.EventOperationType; import com.evolveum.midpoint.xml.ns._public.common.common_3.EventStatusType; -import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType; +import javax.xml.namespace.QName; import java.util.ArrayList; import java.util.List; @@ -140,10 +143,10 @@ public boolean isCategoryType(EventCategoryType eventCategoryType) { return eventCategoryType == EventCategoryType.MODEL_EVENT; } - public List> getUserDeltas() { - List> retval = new ArrayList>(); + public List> getFocusDeltas() { + List> retval = new ArrayList<>(); Class c = modelContext.getFocusClass(); - if (c != null && UserType.class.isAssignableFrom(c)) { + if (c != null && FocusType.class.isAssignableFrom(c)) { for (Object o : getFocusExecutedDeltas()) { ObjectDeltaOperation objectDeltaOperation = (ObjectDeltaOperation) o; retval.add(objectDeltaOperation.getObjectDelta()); @@ -152,7 +155,29 @@ public List> getUserDeltas() { return retval; } - public ObjectDelta getSummarizedUserDeltas() throws SchemaException { - return ObjectDelta.summarize(getUserDeltas()); + public ObjectDelta getSummarizedFocusDeltas() throws SchemaException { + return ObjectDelta.summarize(getFocusDeltas()); + } + + public boolean hasFocusOfType(Class clazz) { + return clazz.isAssignableFrom(getFocusContext().getObjectTypeClass()); + } + + public boolean hasFocusOfType(QName focusType) { + PrismContext prismContext = getModelContext().getPrismContext(); + if (prismContext == null) { + throw new IllegalStateException("No prismContext in model context"); + } + PrismContainerDefinition pcd = prismContext.getSchemaRegistry().findContainerDefinitionByType(focusType); + if (pcd == null) { + LOGGER.warn("Couldn't find definition for type " + focusType); + return false; + } + Class expectedClass = pcd.getCompileTimeClass(); + if (expectedClass == null) { + LOGGER.warn("Couldn't find class for type " + focusType); + return false; + } + return hasFocusOfType(expectedClass); } } diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/formatters/TextFormatter.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/formatters/TextFormatter.java index 3dba4cb25a5..bae7a6ea59d 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/formatters/TextFormatter.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/formatters/TextFormatter.java @@ -178,6 +178,7 @@ private void formatContainerValue(StringBuilder sb, String prefix, PrismContaine String prefixSubContainer = prefix + " "; formatContainerValue(sb, prefixSubContainer, subContainerValue, mightBeRemoved, hiddenPaths, showOperationalAttributes); } + sb.append("\n"); } else { sb.append("Unexpected Item type: "); sb.append(item); diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/handlers/AggregatedEventHandler.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/handlers/AggregatedEventHandler.java index 368d4dd74ba..a9f3fd7c37c 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/handlers/AggregatedEventHandler.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/handlers/AggregatedEventHandler.java @@ -22,6 +22,7 @@ import com.evolveum.midpoint.notifications.impl.helpers.CategoryFilterHelper; import com.evolveum.midpoint.notifications.impl.helpers.ChainHelper; import com.evolveum.midpoint.notifications.impl.helpers.ExpressionFilterHelper; +import com.evolveum.midpoint.notifications.impl.helpers.FocusTypeFilterHelper; import com.evolveum.midpoint.notifications.impl.helpers.ForkHelper; import com.evolveum.midpoint.notifications.impl.helpers.KindIntentFilterHelper; import com.evolveum.midpoint.notifications.impl.helpers.OperationFilterHelper; @@ -29,6 +30,7 @@ import com.evolveum.midpoint.notifications.impl.notifiers.AccountPasswordNotifier; import com.evolveum.midpoint.notifications.impl.notifiers.GeneralNotifier; import com.evolveum.midpoint.notifications.impl.notifiers.SimpleResourceObjectNotifier; +import com.evolveum.midpoint.notifications.impl.notifiers.SimpleFocalObjectNotifier; import com.evolveum.midpoint.notifications.impl.notifiers.SimpleUserNotifier; import com.evolveum.midpoint.notifications.impl.notifiers.SimpleWorkflowNotifier; import com.evolveum.midpoint.notifications.impl.notifiers.UserPasswordNotifier; @@ -71,6 +73,9 @@ public class AggregatedEventHandler extends BaseHandler { @Autowired private KindIntentFilterHelper kindIntentFilter; + @Autowired + private FocusTypeFilterHelper focusTypeFilterHelper; + @Autowired private ExpressionFilterHelper expressionFilter; @@ -80,6 +85,9 @@ public class AggregatedEventHandler extends BaseHandler { @Autowired private ForkHelper forkHelper; + @Autowired + protected SimpleFocalObjectNotifier simpleFocalObjectNotifier; + @Autowired protected SimpleUserNotifier simpleUserNotifier; @@ -114,11 +122,13 @@ public boolean processEvent(Event event, EventHandlerType eventHandlerType, Noti operationFilter.processEvent(event, eventHandlerType, notificationManager, task, result) && statusFilter.processEvent(event, eventHandlerType, notificationManager, task, result) && kindIntentFilter.processEvent(event, eventHandlerType, notificationManager, task, result) && + focusTypeFilterHelper.processEvent(event, eventHandlerType, notificationManager, task, result) && expressionFilter.processEvent(event, eventHandlerType, notificationManager, task, result) && chainHelper.processEvent(event, eventHandlerType, notificationManager, task, result) && forkHelper.processEvent(event, eventHandlerType, notificationManager, task, result); shouldContinue = shouldContinue && processNotifiers(event, eventHandlerType.getSimpleUserNotifier(), notificationManager, task, result); + shouldContinue = shouldContinue && processNotifiers(event, eventHandlerType.getSimpleFocalObjectNotifier(), notificationManager, task, result); shouldContinue = shouldContinue && processNotifiers(event, eventHandlerType.getSimpleResourceObjectNotifier(), notificationManager, task, result); shouldContinue = shouldContinue && processNotifiers(event, eventHandlerType.getSimpleWorkflowNotifier(), notificationManager, task, result); shouldContinue = shouldContinue && processNotifiers(event, eventHandlerType.getUserPasswordNotifier(), notificationManager, task, result); diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/helpers/FocusTypeFilterHelper.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/helpers/FocusTypeFilterHelper.java new file mode 100644 index 00000000000..343ece6b195 --- /dev/null +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/helpers/FocusTypeFilterHelper.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2010-2014 Evolveum + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.evolveum.midpoint.notifications.impl.helpers; + +import com.evolveum.midpoint.notifications.api.NotificationManager; +import com.evolveum.midpoint.notifications.api.events.Event; +import com.evolveum.midpoint.notifications.api.events.ModelEvent; +import com.evolveum.midpoint.schema.result.OperationResult; +import com.evolveum.midpoint.task.api.Task; +import com.evolveum.midpoint.util.logging.Trace; +import com.evolveum.midpoint.util.logging.TraceManager; +import com.evolveum.midpoint.xml.ns._public.common.common_3.EventHandlerType; +import org.springframework.stereotype.Component; + +import javax.xml.namespace.QName; + +/** + * @author mederly + */ +@Component +public class FocusTypeFilterHelper extends BaseHelper { + + private static final Trace LOGGER = TraceManager.getTrace(FocusTypeFilterHelper.class); + + @Override + public boolean processEvent(Event event, EventHandlerType eventHandlerType, NotificationManager notificationManager, + Task task, OperationResult result) { + + if (eventHandlerType.getFocusType().isEmpty()) { + return true; + } + + if (!(event instanceof ModelEvent)) { + return true; // or should we return false? + } + ModelEvent modelEvent = (ModelEvent) event; + + logStart(LOGGER, event, eventHandlerType, eventHandlerType.getStatus()); + + boolean retval = false; + + for (QName focusType : eventHandlerType.getFocusType()) { + if (focusType == null) { + LOGGER.warn("Filtering on null focusType; filter = " + eventHandlerType); + } else if (modelEvent.hasFocusOfType(focusType)) { + retval = true; + break; + } + } + + logEnd(LOGGER, event, eventHandlerType, retval); + return retval; + } +} diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleFocalObjectNotifier.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleFocalObjectNotifier.java new file mode 100644 index 00000000000..a5915d3ffc3 --- /dev/null +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleFocalObjectNotifier.java @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2010-2013 Evolveum + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.evolveum.midpoint.notifications.impl.notifiers; + +import com.evolveum.midpoint.model.api.context.ModelContext; +import com.evolveum.midpoint.model.api.context.ModelElementContext; +import com.evolveum.midpoint.notifications.api.events.Event; +import com.evolveum.midpoint.notifications.api.events.ModelEvent; +import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.delta.ObjectDelta; +import com.evolveum.midpoint.prism.path.ItemPath; +import com.evolveum.midpoint.prism.polystring.PolyString; +import com.evolveum.midpoint.schema.result.OperationResult; +import com.evolveum.midpoint.util.exception.SchemaException; +import com.evolveum.midpoint.util.logging.Trace; +import com.evolveum.midpoint.util.logging.TraceManager; +import com.evolveum.midpoint.xml.ns._public.common.common_3.*; + +import org.apache.commons.lang.StringUtils; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * @author mederly + */ +@Component +public class SimpleFocalObjectNotifier extends GeneralNotifier { + + private static final Trace LOGGER = TraceManager.getTrace(SimpleFocalObjectNotifier.class); + + @PostConstruct + public void init() { + register(SimpleFocalObjectNotifierType.class); + } + + @Override + protected boolean quickCheckApplicability(Event event, GeneralNotifierType generalNotifierType, OperationResult result) { + if (!(event instanceof ModelEvent)) { + LOGGER.trace("{} is not applicable for this kind of event, continuing in the handler chain; event class = {}", getClass().getSimpleName(), event.getClass()); + return false; + } + ModelEvent modelEvent = (ModelEvent) event; + if (modelEvent.getFocusContext() == null || !FocusType.class.isAssignableFrom(modelEvent.getFocusContext().getObjectTypeClass())) { + LOGGER.trace("{} is not applicable to non-focus related model operations, continuing in the handler chain", getClass().getSimpleName()); + return false; + } + return true; + } + + @Override + protected boolean checkApplicability(Event event, GeneralNotifierType generalNotifierType, OperationResult result) { + List> deltas = ((ModelEvent) event).getFocusDeltas(); + if (deltas.isEmpty()) { + return false; + } + + if (isWatchAuxiliaryAttributes(generalNotifierType)) { + return true; + } + + for (ObjectDelta delta : deltas) { + if (!delta.isModify() || deltaContainsOtherPathsThan(delta, auxiliaryPaths)) { + return true; + } + } + + return false; + } + + @Override + protected String getSubject(Event event, GeneralNotifierType generalNotifierType, String transport, OperationResult result) { + + String typeName = getFocusTypeName(event); + + if (event.isAdd()) { + return typeName + " creation notification"; + } else if (event.isModify()) { + return typeName + " modification notification"; + } else if (event.isDelete()) { + return typeName + " deletion notification"; + } else { + return "(unknown " + typeName.toLowerCase() + " operation)"; + } + } + + // assuming the quick availability check was passed + private String getFocusTypeName(Event event) { + String simpleName = ((ModelEvent) event).getFocusContext().getObjectTypeClass().getSimpleName(); + return StringUtils.substringBeforeLast(simpleName, "Type"); // should usually work ;) + } + + @Override + protected String getBody(Event event, GeneralNotifierType generalNotifierType, String transport, OperationResult result) throws SchemaException { + + String typeName = getFocusTypeName(event); + String typeNameLower = typeName.toLowerCase(); + + boolean techInfo = Boolean.TRUE.equals(generalNotifierType.isShowTechnicalInformation()); + + ModelContext modelContext = (ModelContext) ((ModelEvent) event).getModelContext(); + ModelElementContext focusContext = modelContext.getFocusContext(); + PrismObject focus = focusContext.getObjectNew() != null ? focusContext.getObjectNew() : focusContext.getObjectOld(); + FocusType userType = focus.asObjectable(); + String oid = focusContext.getOid(); + + String fullName; + if (userType instanceof UserType) { + fullName = PolyString.getOrig(((UserType) userType).getFullName()); + } else if (userType instanceof AbstractRoleType) { + fullName = PolyString.getOrig(((AbstractRoleType) userType).getDisplayName()); + } else { + fullName = ""; // TODO (currently it's not possible to get here) + } + + ObjectDelta delta = ObjectDelta.summarize(((ModelEvent) event).getFocusDeltas()); + + StringBuilder body = new StringBuilder(); + + String status; + if (event.isSuccess()) { + status = "SUCCESS"; + } else if (event.isOnlyFailure()) { + status = "FAILURE"; + } else if (event.isFailure()) { + status = "PARTIAL FAILURE"; + } else if (event.isInProgress()) { + status = "IN PROGRESS"; + } else { + status = "UNKNOWN"; + } + + String attemptedTo = event.isSuccess() ? "" : "(attempted to be) "; + + body.append("Notification about ").append(typeNameLower).append("-related operation (status: " + status + ")\n\n"); + body.append(typeName).append(": " + fullName + " (" + userType.getName() + ", oid " + oid + ")\n"); + body.append("Notification created on: " + new Date() + "\n\n"); + + List hiddenPaths = isWatchAuxiliaryAttributes(generalNotifierType) ? new ArrayList() : auxiliaryPaths; + if (delta.isAdd()) { + body.append("The ").append(typeNameLower).append(" record was " + attemptedTo + "created with the following data:\n"); + body.append(textFormatter.formatObject(delta.getObjectToAdd(), hiddenPaths, isWatchAuxiliaryAttributes(generalNotifierType))); + body.append("\n"); + } else if (delta.isModify()) { + body.append("The ").append(typeNameLower).append(" record was " + attemptedTo + "modified. Modified attributes are:\n"); + body.append(textFormatter.formatObjectModificationDelta(delta, hiddenPaths, isWatchAuxiliaryAttributes(generalNotifierType))); + body.append("\n"); + } else if (delta.isDelete()) { + body.append("The ").append(typeNameLower).append(" record was " + attemptedTo + "removed.\n\n"); + } + + if (!event.isSuccess()) { + body.append("More information about the status of the request was displayed and/or is present in log files.\n\n"); + } + + if (techInfo) { + body.append("----------------------------------------\n"); + body.append("Technical information:\n\n"); + body.append(modelContext.debugDump(2)); + } + + return body.toString(); + } + + @Override + protected Trace getLogger() { + return LOGGER; + } + +} diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleUserNotifier.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleUserNotifier.java index ff822cad675..b60c9167492 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleUserNotifier.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleUserNotifier.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2013 Evolveum + * Copyright (c) 2010-2014 Evolveum * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,32 +16,23 @@ package com.evolveum.midpoint.notifications.impl.notifiers; -import com.evolveum.midpoint.model.api.context.ModelContext; -import com.evolveum.midpoint.model.api.context.ModelElementContext; import com.evolveum.midpoint.notifications.api.events.Event; import com.evolveum.midpoint.notifications.api.events.ModelEvent; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.delta.ObjectDelta; -import com.evolveum.midpoint.prism.path.ItemPath; import com.evolveum.midpoint.schema.result.OperationResult; -import com.evolveum.midpoint.util.exception.SchemaException; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; -import com.evolveum.midpoint.xml.ns._public.common.common_3.*; - +import com.evolveum.midpoint.xml.ns._public.common.common_3.GeneralNotifierType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.SimpleUserNotifierType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - /** * @author mederly */ @Component -public class SimpleUserNotifier extends GeneralNotifier { +public class SimpleUserNotifier extends SimpleFocalObjectNotifier { private static final Trace LOGGER = TraceManager.getTrace(SimpleUserNotifier.class); @@ -52,115 +43,14 @@ public void init() { @Override protected boolean quickCheckApplicability(Event event, GeneralNotifierType generalNotifierType, OperationResult result) { - if (!(event instanceof ModelEvent)) { - LOGGER.trace("SimpleUserNotifier is not applicable for this kind of event, continuing in the handler chain; event class = " + event.getClass()); - return false; - } - ModelEvent modelEvent = (ModelEvent) event; - if (modelEvent.getFocusContext() == null || !UserType.class.isAssignableFrom(modelEvent.getFocusContext().getObjectTypeClass())) { - LOGGER.trace("SimpleUserNotifier is not applicable to non-user related model operations, continuing in the handler chain"); - return false; - } - return true; - } - - @Override - protected boolean checkApplicability(Event event, GeneralNotifierType generalNotifierType, OperationResult result) { - List> deltas = ((ModelEvent) event).getUserDeltas(); - if (deltas.isEmpty()) { + if (!super.quickCheckApplicability(event, generalNotifierType, result)) { return false; } - - if (isWatchAuxiliaryAttributes(generalNotifierType)) { - return true; - } - - for (ObjectDelta delta : deltas) { - if (!delta.isModify() || deltaContainsOtherPathsThan(delta, auxiliaryPaths)) { - return true; - } - } - - return false; - } - - @Override - protected String getSubject(Event event, GeneralNotifierType generalNotifierType, String transport, OperationResult result) { - - if (event.isAdd()) { - return "User creation notification"; - } else if (event.isModify()) { - return "User modification notification"; - } else if (event.isDelete()) { - return "User deletion notification"; - } else { - return "(unknown user operation)"; - } - } - - @Override - protected String getBody(Event event, GeneralNotifierType generalNotifierType, String transport, OperationResult result) throws SchemaException { - - boolean techInfo = Boolean.TRUE.equals(generalNotifierType.isShowTechnicalInformation()); - - ModelContext modelContext = (ModelContext) ((ModelEvent) event).getModelContext(); - ModelElementContext focusContext = modelContext.getFocusContext(); - PrismObject user = focusContext.getObjectNew() != null ? focusContext.getObjectNew() : focusContext.getObjectOld(); - UserType userType = user.asObjectable(); - String oid = focusContext.getOid(); - - ObjectDelta delta = ObjectDelta.summarize(((ModelEvent) event).getUserDeltas()); - - StringBuilder body = new StringBuilder(); - - String status; - if (event.isSuccess()) { - status = "SUCCESS"; - } else if (event.isOnlyFailure()) { - status = "FAILURE"; - } else if (event.isFailure()) { - status = "PARTIAL FAILURE"; - } else if (event.isInProgress()) { - status = "IN PROGRESS"; - } else { - status = "UNKNOWN"; - } - - String attemptedTo = event.isSuccess() ? "" : "(attempted to be) "; - - body.append("Notification about user-related operation (status: " + status + ")\n\n"); - body.append("User: " + userType.getFullName() + " (" + userType.getName() + ", oid " + oid + ")\n"); - body.append("Notification created on: " + new Date() + "\n\n"); - - List hiddenPaths = isWatchAuxiliaryAttributes(generalNotifierType) ? new ArrayList() : auxiliaryPaths; - if (delta.isAdd()) { - body.append("The user record was " + attemptedTo + "created with the following data:\n"); - body.append(textFormatter.formatObject(delta.getObjectToAdd(), hiddenPaths, isWatchAuxiliaryAttributes(generalNotifierType))); - body.append("\n"); - } else if (delta.isModify()) { - body.append("The user record was " + attemptedTo + "modified. Modified attributes are:\n"); - body.append(textFormatter.formatObjectModificationDelta(delta, hiddenPaths, isWatchAuxiliaryAttributes(generalNotifierType))); - body.append("\n"); - } else if (delta.isDelete()) { - body.append("The user record was " + attemptedTo + "removed.\n\n"); - } - - if (!event.isSuccess()) { - body.append("More information about the status of the request was displayed and/or is present in log files.\n\n"); - } - - if (techInfo) { - body.append("----------------------------------------\n"); - body.append("Technical information:\n\n"); - body.append(modelContext.debugDump(2)); - } - - return body.toString(); + return UserType.class.isAssignableFrom(((ModelEvent) event).getFocusContext().getObjectTypeClass()); } @Override protected Trace getLogger() { return LOGGER; } - } diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/UserPasswordNotifier.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/UserPasswordNotifier.java index 0799a010c68..c3282afcedd 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/UserPasswordNotifier.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/UserPasswordNotifier.java @@ -27,6 +27,7 @@ import com.evolveum.midpoint.util.logging.LoggingUtils; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; +import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType; import com.evolveum.midpoint.xml.ns._public.common.common_3.GeneralNotifierType; import com.evolveum.midpoint.xml.ns._public.common.common_3.UserPasswordNotifierType; import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType; @@ -59,7 +60,7 @@ public void init() { @Override protected boolean quickCheckApplicability(Event event, GeneralNotifierType generalNotifierType, OperationResult result) { - if (!(event instanceof ModelEvent)) { + if (!(event instanceof ModelEvent) || !((ModelEvent) event).hasFocusOfType(UserType.class)) { LOGGER.trace("UserPasswordNotifier is not applicable for this kind of event, continuing in the handler chain; event class = " + event.getClass()); return false; } else { @@ -76,11 +77,11 @@ protected boolean checkApplicability(Event event, GeneralNotifierType generalNot } ModelEvent modelEvent = (ModelEvent) event; - if (modelEvent.getUserDeltas().isEmpty()) { + if (modelEvent.getFocusDeltas().isEmpty()) { LOGGER.trace("No user deltas in event, exiting."); return false; } - if (getPasswordFromDeltas(modelEvent.getUserDeltas()) != null) { + if (getPasswordFromDeltas(modelEvent.getFocusDeltas()) != null) { LOGGER.trace("Found password in user delta(s), continuing."); return true; } else { @@ -89,9 +90,9 @@ protected boolean checkApplicability(Event event, GeneralNotifierType generalNot } } - private String getPasswordFromDeltas(List> deltas) { + private String getPasswordFromDeltas(List> deltas) { try { - return midpointFunctions.getPlaintextUserPasswordFromDeltas(deltas); + return midpointFunctions.getPlaintextUserPasswordFromDeltas((List) deltas); } catch (EncryptionException e) { LoggingUtils.logException(LOGGER, "Couldn't decrypt password from user deltas: {}", e, DebugUtil.debugDump(deltas)); return null; @@ -107,7 +108,7 @@ protected String getSubject(Event event, GeneralNotifierType generalNotifierType protected String getBody(Event event, GeneralNotifierType generalNotifierType, String transport, OperationResult result) { ModelEvent modelEvent = (ModelEvent) event; - List> deltas = modelEvent.getUserDeltas(); + List> deltas = modelEvent.getFocusDeltas(); return "Password for user " + notificationsUtil.getObjectType(event.getRequestee(), result).getName() + " is: " + getPasswordFromDeltas(deltas); }