2626import org .jetbrains .annotations .NotNull ;
2727import org .jetbrains .annotations .Nullable ;
2828import org .json .JSONObject ;
29+ import org .labkey .api .audit .AuditLogService ;
30+ import org .labkey .api .audit .provider .SiteSettingsAuditProvider .SiteSettingsAuditEvent ;
2931import org .labkey .api .data .CompareType ;
3032import org .labkey .api .data .Container ;
3133import org .labkey .api .data .ContainerManager ;
@@ -168,11 +170,19 @@ public boolean isServiceEnabled()
168170 return Boolean .parseBoolean (prop );
169171 }
170172
171- public void setServiceEnabled (Boolean status )
173+ public void setServiceEnabled (User u , Boolean status )
172174 {
175+ boolean oldStatus = isServiceEnabled ();
173176 WritablePropertyMap pm = PropertyManager .getWritableProperties (ContainerManager .getRoot (), NotificationServiceImpl .CONFIG_PROPERTY_DOMAIN , true );
174177 pm .put (ENABLED_PROP , status .toString ());
175178 pm .save ();
179+
180+ if (status != oldStatus )
181+ {
182+ SiteSettingsAuditEvent event = new SiteSettingsAuditEvent (ContainerManager .getRoot (),
183+ "Notification service has been " + (status ? "enabled" : "disabled" ) + "." );
184+ AuditLogService .get ().addEvent (u , event );
185+ }
176186 }
177187
178188 @ Override
@@ -219,17 +229,28 @@ public Address getReturnEmail(Container c)
219229 return email == null ? null : getAddress (email );
220230 }
221231
222- public void setReturnEmail (Container c , String returnEmail )
232+ public void setReturnEmail (Container c , User u , String returnEmail )
223233 {
224234 try
225235 {
226236 if (returnEmail != null )
227237 {
228238 ValidEmail email = new ValidEmail (returnEmail );
239+ String previous = getConfigProperty (c , RETURN_EMAIL );
229240
230241 WritablePropertyMap pm = PropertyManager .getWritableProperties (c , NotificationServiceImpl .CONFIG_PROPERTY_DOMAIN , true );
231242 pm .put (RETURN_EMAIL , email .getEmailAddress ());
232243 pm .save ();
244+
245+ if (!returnEmail .equals (previous ))
246+ {
247+ SiteSettingsAuditEvent event = new SiteSettingsAuditEvent (c , "Notification reply-to email updated." );
248+ String before = previous == null ? "" : previous ;
249+ String after = email .getEmailAddress ();
250+ String html = "<table><tr><td class='labkey-form-label'>Reply-to email</td><td>" + PageFlowUtil .filter (before ) + " » " + PageFlowUtil .filter (after ) + "</td></tr></table>" ;
251+ event .setChanges (html );
252+ AuditLogService .get ().addEvent (u , event );
253+ }
233254 }
234255 }
235256 catch (ValidEmail .InvalidEmailException e )
@@ -247,13 +268,25 @@ public User getUser(Container c)
247268 return UserManager .getUser (Integer .parseInt (user ));
248269 }
249270
250- public void setUser (Container c , Integer userId )
271+ public void setUser (Container c , User u , Integer userId )
251272 {
252273 if (userId != null )
253274 {
275+ User previousUser = getUser (c );
254276 WritablePropertyMap pm = PropertyManager .getWritableProperties (c , NotificationServiceImpl .CONFIG_PROPERTY_DOMAIN , true );
255277 pm .put (USER_PROP , String .valueOf (userId ));
256278 pm .save ();
279+
280+ if (previousUser == null || (previousUser .getUserId () != userId ))
281+ {
282+ User newUser = UserManager .getUser (userId );
283+ String before = previousUser == null ? "" : previousUser .getEmail ();
284+ String after = newUser == null ? "" : newUser .getEmail ();
285+ SiteSettingsAuditEvent event = new SiteSettingsAuditEvent (c , "Notification service user updated." );
286+ String html = "<table><tr><td class='labkey-form-label'>Service user</td><td>" + PageFlowUtil .filter (before ) + " » " + PageFlowUtil .filter (after ) + "</td></tr></table>" ;
287+ event .setChanges (html );
288+ AuditLogService .get ().addEvent (u , event );
289+ }
257290 }
258291 }
259292
@@ -362,11 +395,20 @@ public boolean isActive(Notification n, Container c)
362395 return false ;
363396 }
364397
365- public void setActive (Notification n , Container c , boolean active )
398+ public void setActive (Notification n , Container c , User u , boolean active )
366399 {
400+ boolean old = isActive (n , c );
367401 WritablePropertyMap pm = PropertyManager .getWritableProperties (c , NotificationServiceImpl .STATUS_PROPERTY_DOMAIN , true );
368402 pm .put (getKey (n ), active ? String .valueOf (active ) : null );
369403 pm .save ();
404+
405+ if (old != active )
406+ {
407+ String action = active ? "enabled" : "disabled" ;
408+ String comment = "Notification '" + n .getName () + "' has been " + action + "." ;
409+ SiteSettingsAuditEvent event = new SiteSettingsAuditEvent (c , comment );
410+ AuditLogService .get ().addEvent (u , event );
411+ }
370412 }
371413
372414 @ Override
@@ -530,6 +572,9 @@ public void updateSubscriptions(Container c, User u, Notification n, @Nullable L
530572 {
531573 TableInfo ti = LDKSchema .getTable (LDKSchema .TABLE_NOTIFICATION_RECIPIENTS );
532574
575+ List <UserPrincipal > actuallyAdded = new ArrayList <>();
576+ List <UserPrincipal > actuallyRemoved = new ArrayList <>();
577+
533578 if (toAdd != null )
534579 {
535580 for (UserPrincipal up : toAdd )
@@ -548,20 +593,49 @@ public void updateSubscriptions(Container c, User u, Notification n, @Nullable L
548593 row .put ("createdby" , u .getUserId ());
549594 row .put ("created" , new Date ());
550595 Table .insert (u , ti , row );
596+ actuallyAdded .add (up );
551597 }
552598 }
599+ }
553600
554- if (toRemove != null )
601+ if (toRemove != null )
602+ {
603+ for (UserPrincipal up : toRemove )
555604 {
556- for (UserPrincipal up : toRemove )
557- {
558- SimpleFilter filter = new SimpleFilter (FieldKey .fromString ("container" ), c .getId (), CompareType .EQUAL );
559- filter .addCondition (FieldKey .fromString ("recipient" ), up .getUserId ());
560- filter .addCondition (FieldKey .fromString ("notificationtype" ), n .getName ());
605+ SimpleFilter filter = new SimpleFilter (FieldKey .fromString ("container" ), c .getId (), CompareType .EQUAL );
606+ filter .addCondition (FieldKey .fromString ("recipient" ), up .getUserId ());
607+ filter .addCondition (FieldKey .fromString ("notificationtype" ), n .getName ());
608+
609+ Table .delete (ti , filter );
610+ actuallyRemoved .add (up );
611+ }
612+ }
561613
562- Table .delete (ti , filter );
614+ // Audit event
615+ if ((!actuallyAdded .isEmpty () || !actuallyRemoved .isEmpty ()) && u != null )
616+ {
617+ StringBuilder html = new StringBuilder ();
618+ if (!actuallyAdded .isEmpty ())
619+ {
620+ html .append ("<div class='labkey-form-label'>Added recipients</div><ul>" );
621+ for (UserPrincipal up : actuallyAdded )
622+ {
623+ html .append ("<li>" ).append (PageFlowUtil .filter (up .getName ())).append (" (ID: " ).append (up .getUserId ()).append (")</li>" );
624+ }
625+ html .append ("</ul>" );
626+ }
627+ if (!actuallyRemoved .isEmpty ())
628+ {
629+ html .append ("<div class='labkey-form-label'>Removed recipients</div><ul>" );
630+ for (UserPrincipal up : actuallyRemoved )
631+ {
632+ html .append ("<li>" ).append (PageFlowUtil .filter (up .getName ())).append (" (ID: " ).append (up .getUserId ()).append (")</li>" );
563633 }
634+ html .append ("</ul>" );
564635 }
636+ SiteSettingsAuditEvent event = new SiteSettingsAuditEvent (c , "Updated notification subscriptions for '" + n .getName () + "'." );
637+ event .setChanges (html .toString ());
638+ AuditLogService .get ().addEvent (u , event );
565639 }
566640 }
567641
0 commit comments