diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/AccountActivationNotifier.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/AccountActivationNotifier.java index abd3eef8d66..18b4f773798 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/AccountActivationNotifier.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/AccountActivationNotifier.java @@ -8,8 +8,18 @@ package com.evolveum.midpoint.notifications.impl.notifiers; import java.util.Collection; +import java.util.Date; import java.util.List; +import com.evolveum.midpoint.common.Clock; +import com.evolveum.midpoint.model.api.ModelService; +import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.schema.processor.ResourceAttribute; +import com.evolveum.midpoint.util.exception.*; +import com.evolveum.midpoint.xml.ns._public.common.common_3.*; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.evolveum.midpoint.model.api.context.ModelElementContext; @@ -18,13 +28,13 @@ 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.AccountActivationNotifierType; -import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType; -import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType; @Component public class AccountActivationNotifier extends ConfirmationNotifier { + @Autowired Clock clock; + @Autowired private ModelService modelService; + private static final Trace LOGGER = TraceManager.getTrace(AccountActivationNotifier.class); @Override @@ -70,14 +80,74 @@ protected String getSubject(ModelEvent event, AccountActivationNotifierType conf protected String getBody(ModelEvent event, AccountActivationNotifierType configuration, String transport, Task task, OperationResult result) { + StringBuilder body = new StringBuilder(); String message = "Your accounts was successfully created. To activate your accounts, please click on the link bellow."; + body.append(message).append("\n\n").append(createConfirmationLink(getUser(event), configuration, result)).append("\n\n"); + + FocusType owner = (FocusType) event.getRequesteeObject(); + String userOrOwner = owner instanceof UserType ? "User" : "Owner"; + if (owner != null) { + body.append(userOrOwner).append(": ").append(event.getRequesteeDisplayName()); + body.append(" (").append(owner.getName()).append(", oid ").append(owner.getOid()).append(")\n"); + } else { + body.append(userOrOwner).append(": unknown\n"); + } + body.append("Notification created on: ").append(new Date(clock.currentTimeMillis())).append("\n\n"); - StringBuilder accountsToActivate = new StringBuilder("Shadow to be activated: \n"); + body.append("Account to be activated: \n"); for (ShadowType shadow : getShadowsToActivate(event)) { - accountsToActivate.append(shadow.asPrismObject().debugDump()).append("\n"); + String resourceOid = shadow.getResourceRef() != null ? shadow.getResourceRef().getOid() : null; + body.append(" Resource: "); + if (StringUtils.isNotBlank(resourceOid)) { + PrismObject resource; + try { + resource = modelService.getObject(ResourceType.class, resourceOid, null, task, result); + } catch (ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException + | ExpressionEvaluationException | SchemaException e) { + getLogger().error("Could't get Resource with oid " + resourceOid, e); + throw new SystemException("Couldn't get resource " + resourceOid, e); + } + body.append(StringUtils.isNotBlank(resource.getDisplayName()) ? resource.getDisplayName() : resource.getName()); + body.append(" (").append("oid ").append(resourceOid).append(")\n"); + + } else { + body.append("unknown\n"); + } + for (Object att : shadow.getAttributes().asPrismContainerValue().getItems()) { + if (att instanceof ResourceAttribute) { + ResourceAttribute attribute = (ResourceAttribute) att; + body.append(" - ").append(attribute.getDisplayName()).append(": "); + if (attribute.isSingleValue()) { + body.append(attribute.getRealValue()).append("\n"); + } else { + body.append("\n"); + for (Object value : attribute.getRealValues()) { + body.append(" - ").append(value).append("\n"); + } + } + } + } } - return message + "\n\n" + createConfirmationLink(getUser(event), configuration, result) + "\n\n" + accountsToActivate; + ObjectType requester = event.getRequester() != null ? event.getRequester().getObjectType() : null; + if (requester != null) { + body.append("\nRequester: ").append(getRequestorDisplayName(requester)); + body.append(" (").append(requester.getName()).append(", oid ").append(requester.getOid()).append(")\n"); + } + return body.toString(); + } + + private String getRequestorDisplayName(ObjectType requester) { + String name = requester.getName().getOrig(); + if (requester.asPrismObject().getDisplayName() != null) { + name = requester.asPrismObject().getDisplayName(); + } + if (requester instanceof UserType){ + if (((UserType) requester).getFullName() != null) { + name = ((UserType) requester).getFullName().getOrig(); + } + } + return name; } private List getShadowsToActivate(ModelEvent modelEvent) {