Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: General notification - role assignment example (another example and link to page)

...

Code Block
languagehtml/xml
linenumberstrue
<notificationConfiguration>
. . .
    <handler>
    	<generalNotifier>
        	<name>Notify system administrator for organization change - general</name>
            <expressionFilter>      <!-- Filter only changes of OrgType objects -->
                <script>
                    <code>
                    import com.evolveum.midpoint.notifications.api.events.ModelEvent
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType

                   (event instanceof ModelEvent &amp;&amp; event.getFocusContext() != null &amp;&amp; OrgType.class.isAssignableFrom(event.getFocusContext().getObjectTypeClass()))
                    </code>
                </script>
            </expressionFilter>
            <recipientExpression>    <!-- Send notifications to iam@localhost -->
                <value>iam@localhost</value>
            </recipientExpression>
            <subjectExpression>      <!-- Subject expression is defined here -->
                <script>
                    <code>
                    tmpObject = 'Organization - GeneralNotifier - '
                    if (event.isSuccess())
                        tmpText = "[IDM] SUCCESS: " + tmpObject + event?.getChangeType() + " operation succeeded"
                    else if (event.isFailure())
                        tmpText = "[IDM] ERROR: " + tmpObject + event?.getChangeType() + " operation failed"
                    else  tmpText = "[IDM] IN PROGRESS: " + tmpObject + event?.getChangeType() + " operation in progress"
                    return tmpText
                </code>
               </script>
            </subjectExpression>
            <bodyExpression>         <!-- Body expression is defined here -->
                <script>
                   <code>            <!-- Some imports needed to have all necessary objects and variables -->
                   import com.evolveum.midpoint.notifications.impl.notifiers.GeneralNotifier;
                   import com.evolveum.midpoint.notifications.api.events.ModelEvent;
                   import com.evolveum.midpoint.prism.delta.ObjectDelta;
                   delta = ObjectDelta.summarize(((ModelEvent) event).getFocusDeltas());
                   hiddenPaths = GeneralNotifier.getAuxiliaryPaths()
                   body = ''
                   attemptedTo = event.isSuccess() ? "" : "(attempted to be) ";
                   if (delta.isAdd()) {
                       body = "The object was " + attemptedTo + "created with the following data:\n";
                       body += textFormatter.formatObject(delta.getObjectToAdd(), hiddenPaths, false);
                   } else if (delta.isModify()) {
                       body = "The object was " + attemptedTo + "modified. Modified attributes are:\n";
                       body += textFormatter.formatObjectModificationDelta(delta, hiddenPaths, false);
                   } else if (delta.isDelete()) {
                       body = "The object was " + attemptedTo + "removed.\n\n";
                   }
                   </code>
               </script>
           </bodyExpression>
           <transport>mail</transport>
       </generalNotifier>
   </handler>
   <!-- configurations suitable for testing - they redirect all notifications to log files; some more real configurations are show below -->
   <mail>
        <redirectToFile>mail-notifications.log</redirectToFile>
   </mail>
   <sms>
        <redirectToFile>sms-notifications.log</redirectToFile>
   </sms>
</notificationConfiguration>

Another General Notifier Example

The description of this example can be found on the next wiki page - General notification - role assignment example.

Code Block
languagexml
linenumberstrue
    <notificationConfiguration>
        <handler>
            <generalNotifier>
                <name>Notify user about new role added - general</name>
                <expressionFilter>
                    <script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ScriptExpressionEvaluatorType">
                        <code>
                            import com.evolveum.midpoint.notifications.api.events.ModelEvent
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.EventOperationType
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType
                            import com.evolveum.midpoint.prism.delta.ObjectDelta
                            import com.evolveum.midpoint.prism.path.ItemPath
                            import com.evolveum.midpoint.prism.PrismValue

                            if (
                            event instanceof ModelEvent
                                    && event.getFocusContext().getObjectTypeClass().getName().equals(UserType.getName())
                                    && !event.isOperationType(EventOperationType.DELETE)
                            ) {
                                for (ObjectDelta delta : event.getFocusDeltas()) {
                                    if (delta.isAdd()) {
                                        for (AssignmentType localObject in delta.getObjectToAdd().asObjectable().assignment) {
                                            if (localObject.getTargetRef() == null) continue;
                                            if (localObject.getTargetRef().getType().localPart.equals("RoleType")) {
                                                if (midpoint.getObject(RoleType, localObject.getTargetRef().getOid(),midpoint.schemaHelper.operationOptionsBuilder.raw().build()).getIdentifier().equals("notification")) return true;
                                            }
                                        }
                                    } else if (delta.isModify()) {
                                        for (ItemPath paths : delta.getModifiedItems()) {
                                            if (AssignmentHolderType.F_ASSIGNMENT.equivalent(paths)) {
                                                for (PrismValue values : delta.getNewValuesFor(paths)) {
                                                    if (values.getRealClass().getName().equals(AssignmentType.getName())) {
                                                        AssignmentType localObject = values.getRealValue();
                                                        if (localObject.getTargetRef() == null) continue;
                                                        if (localObject.getTargetRef().getType().localPart.equals("RoleType")) {
                                                            if (midpoint.getObject(RoleType, localObject.getTargetRef().getOid(),midpoint.schemaHelper.operationOptionsBuilder.raw().build()).getIdentifier().equals("notification")) return true;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        </code>
                    </script>
                </expressionFilter>
                <recipientExpression>
                    <script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ScriptExpressionEvaluatorType">
                        <code>
                            if (
                            event.requesteeObject.emailAddress != null
                                    && event.requesteeObject.emailAddress != ""
                            ) {
                                return event.requesteeObject.emailAddress
                            } else {
                                return "iam@localhost"
                            }
                        </code>
                    </script>
                </recipientExpression>
                <subjectExpression>
                    <script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ScriptExpressionEvaluatorType">
                        <code>
                            if (
                            event.requesteeObject.emailAddress != null
                                    && event.requesteeObject.emailAddress != ""
                            ) {
                                return "[IDM] New Role assignment notification"
                            } else {
                                return "[IDM] unknown address for notification"
                            }
                        </code>
                    </script>
                </subjectExpression>
                <bodyExpression>
                    <script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ScriptExpressionEvaluatorType">
                        <code>
                            import com.evolveum.midpoint.notifications.api.events.ModelEvent
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.EventOperationType
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType
                            import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType
                            import com.evolveum.midpoint.prism.delta.ObjectDelta
                            import com.evolveum.midpoint.prism.path.ItemPath
                            import com.evolveum.midpoint.prism.PrismValue

                            String body = "";

                            if (
                            event instanceof ModelEvent
                                    && event.getFocusContext().getObjectTypeClass().getName().equals(UserType.getName())
                                    && !event.isOperationType(EventOperationType.DELETE)
                            ) {
                                for (ObjectDelta delta : event.getFocusDeltas()) {
                                    if (delta.isAdd()) {
                                        for (AssignmentType localObject in delta.getObjectToAdd().asObjectable().assignment) {
                                            if (localObject.getTargetRef() == null) continue;
                                            if (localObject.getTargetRef().getType().localPart.equals("RoleType")) {
                                                if (midpoint.getObject(RoleType, localObject.getTargetRef().getOid(),midpoint.schemaHelper.operationOptionsBuilder.raw().build()).getIdentifier().equals("notification")) {
                                                    body += midpoint.getObject(RoleType, localObject.getTargetRef().getOid(),midpoint.schemaHelper.operationOptionsBuilder.raw().build()).getDescription() + "\n\n";
                                                }
                                            }
                                        }
                                    } else if (delta.isModify()) {
                                        for (ItemPath paths : delta.getModifiedItems()) {
                                            if (AssignmentHolderType.F_ASSIGNMENT.equivalent(paths)) {
                                                for (PrismValue values : delta.getNewValuesFor(paths)) {
                                                    if (values.getRealClass().getName().equals(AssignmentType.getName())) {
                                                        AssignmentType localObject = values.getRealValue();
                                                        if (localObject.getTargetRef() == null) continue;
                                                        if (localObject.getTargetRef().getType().localPart.equals("RoleType")) {
                                                            if (midpoint.getObject(RoleType, localObject.getTargetRef().getOid(),midpoint.schemaHelper.operationOptionsBuilder.raw().build()).getIdentifier().equals("notification")) {
                                                                body += midpoint.getObject(RoleType, localObject.getTargetRef().getOid(),midpoint.schemaHelper.operationOptionsBuilder.raw().build()).getDescription() + "\n\n";
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (!body.isEmpty()) {
                                if (event?.requesteeDisplayName == null) {
                                    boolean nameContructed = false
                                    String tmpName = ""
                                    if (event?.requesteeObject.getGivenName() != null) {
                                        tmpName += event?.requesteeObject.givenName.toString();
                                        nameContructed = true
                                    }
                                    if (event?.requesteeObject.getFamilyName() != null) {
                                        if (nameContructed) tmpName += " "
                                        tmpName += event?.requesteeObject.familyName.toString();
                                        nameContructed = true
                                    }
                                    if (nameContructed) {
                                        body = "Dear " + tmpName +
                                                " (" + event.requesteeObject.name.toString() + ")" +
                                                ",\n\n" + body + "\n\n Best regards,\n\n IDM admin team"
                                    } else {
                                        body = "Dear " +
                                                event.requesteeObject.name.toString() +
                                                ",\n\n" + body + "\n\n Best regards,\n\n IDM admin team"
                                    }
                                } else {
                                    body = "Dear " + event?.requesteeDisplayName.toString() +
                                            " (" + event.requesteeObject.name.toString() + ")" +
                                            ",\n\n" + body + "\nBest regards,\n\n IDM admin team";
                                }

                                if (
                                event.requesteeObject.emailAddress == null
                                        || event.requesteeObject.emailAddress == ""
                                ) {
                                    body = "Notification for the user : " + event.requesteeObject.name.toString() + " / " + event.requesteeObject.oid +
                                            "\nSubject: [IDM] New Role assignment notification\n" +
                                            "\nThe e-mail address is not know at this moment" +
                                            "\n - - - - - - -\n" + body
                                }

                                return body
                            }
                        </code>
                    </script>
                </bodyExpression>
                <transport>mail</transport>
            </generalNotifier>
        </handler>
        <mail>
            <redirectToFile>/opt/midpoint/var/log/mails.log</redirectToFile>
        </mail>
    </notificationConfiguration>

A bit of theory related to event handling

...