User notification preferences listing

Last modified by Anca Luca on 2022/08/18 13:23

cogDisplays the notification preferences of all users of the current wiki
Type
CategoryOther
Developed by

Anca Luca

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

In XWiki versions using the new notifications module but lower than 13.2, issue XWIKI-16158 is not yet fixed so it's not possible to see (nor edit) the notification preferences for a different user than one's own user. For debugging and other operation purposes, administrators may need to see the configurations of other users.

The following script allows to list the preferences of all the users or of a specified user. However, contrary to XWIKI-16158, it will still now allow you to edit them. It was tested on XWiki version 10.11.9 but should be compatible with other versions as well.

Note: contrary to how XWIKI-16158 would work, this script is only displaying settings that are explicitly set by the user on their profile, for example it will not show the configuration that applies to the user because they're inherited from the global notification settings.

Copy the following code in a page with a user having programming rights. Protect the access to the page in question, if don't delete it immediately after use, because it will show some private information about the users to whomever has the right to view the page containing the script.

{{toc /}}

{{velocity output="false"}}
#userPicker_import
{{/velocity}}

{{groovy}}
def displayUserNotificationSettings(String userName, def notificationFilterPreferenceService) {
 def userDoc = xwiki.getDocument(userName);
 def userObj = userDoc.getObject('XWiki.XWikiUsers');
 println("=**" + userObj.getValue('first_name') + " " + userObj.getValue('last_name') + " (" + services.rendering.escape(userName, org.xwiki.rendering.syntax.Syntax.XWIKI_2_1) + ")" + "**=")
 println("[[See user page>>" + userName + "]]")
 def emailPrefsObj = userDoc.getObject("XWiki.Notifications.Code.NotificationEmailPreferenceClass");
 def autoWatchPrefsObj = userDoc.getObject("XWiki.Notifications.Code.AutomaticWatchModeClass");
 def notificationPrefsObjs = userDoc.getObjects("XWiki.Notifications.Code.NotificationPreferenceClass");
 def filterPreferencesObjs = userDoc.getObjects("XWiki.Notifications.Code.ToggleableFilterPreferenceClass");
 def filters = notificationFilterPreferenceService.getFilterPreferences(userDoc.getDocumentReference());

 if (emailPrefsObj != null) {
   println("=={{translation key='notifications.settings.email.title' /}} ==")
   println("" + doc.displayPrettyName('interval', emailPrefsObj));
   println("" + emailPrefsObj.getValue('interval'));
   println("" + doc.displayPrettyName('diffType', emailPrefsObj));
   println("" + emailPrefsObj.getValue('diffType'));
   println();
  }

 if (autoWatchPrefsObj != null) {
   println(" =={{translation key='notifications.settings.watch.title' /}} ==")
   println("" + doc.displayPrettyName('automaticWatchMode', autoWatchPrefsObj));
   println("" + autoWatchPrefsObj.getValue('automaticWatchMode'));
   println();
  }

 if (notificationPrefsObjs.size() > 0) {
   println("=={{translation key='notifications.settings.applications.title' /}} ==")
   def titleDisplayed = false;
   for(def notificationPrefsObj : notificationPrefsObjs) {
     if (notificationPrefsObj == null) {
       continue;
      }
     if (!titleDisplayed) {
       print("|=" + doc.displayPrettyName('eventType', notificationPrefsObj));
       print("|=" + doc.displayPrettyName('format', notificationPrefsObj));
       print("|=" + doc.displayPrettyName('notificationEnabled', notificationPrefsObj));
       println("|=" + doc.displayPrettyName('startDate', notificationPrefsObj));
       titleDisplayed = true;
      }
     print("|" + notificationPrefsObj.getValue('eventType'));
     print("|" + notificationPrefsObj.getValue('format'));
     print("|" + notificationPrefsObj.getValue('notificationEnabled'));
     println("|" + notificationPrefsObj.getValue('startDate'));
    }
   println();
  }

 if (filterPreferencesObjs.size() > 0 || filters.size() > 0) {
   println("=={{translation key='notifications.settings.filters.preferences.title' /}}  ==")
   println("===Toggles===")
   def titleDisplayed = false;
   for(def filterPreferencesObj : filterPreferencesObjs) {
     if (filterPreferencesObj == null) {
       continue;
      }
     if (!titleDisplayed) {
       print("|=" + doc.displayPrettyName('filterName', filterPreferencesObj));
       println("|=" + doc.displayPrettyName('isEnabled', filterPreferencesObj));
       titleDisplayed = true;
      }
     print("|" + filterPreferencesObj.getValue('filterName'));
     println("|" + filterPreferencesObj.getValue('isEnabled'));
    }
   println();

   println("===CustomFilters===")
   println("|=No|=Name|=Wiki|=Page|=PageOnly|=Filter type|=Event types|=notificationFormats|=isEnabled")
   // TODO: display filters from service and change the if above
   def noFilters = 0;
   for(def filter : filters) {
     println("|" + (++noFilters) + "|{{{" + filter.getFilterName() + "}}}|{{{" + (filter.getWiki() != null ? filter.getWiki() : "") + "}}}|{{{" + (filter.getPage() != null ? filter.getPage() : "") + "}}}|{{{" + (filter.getPageOnly() != null ? filter.getPageOnly() : "") + "}}}|{{{" + filter.getFilterType() + "}}}|{{{" + filter.getEventTypes() + "}}}|{{{" + filter.getNotificationFormats() + "}}}|{{{" + filter.isEnabled() + "}}}|");
    }
   println();
  }
}

def notificationFilterPreferenceService = services.component.componentManager.getInstance(org.xwiki.notifications.filters.NotificationFilterPreferenceManager.class)
def user = request.getParameter("user");
if (user != null && !user.isEmpty()) {
 displayUserNotificationSettings(user, notificationFilterPreferenceService);
} else if ("true".equals(request.getParameter("allusers"))) {
 def users = services.query.xwql("from doc.object(XWiki.XWikiUsers) as user order by doc.fullName").execute();
 users.each{userToDisplay ->
   println("(%style='border-top-width: 0.4em'%)")
   println("----")
   displayUserNotificationSettings(userToDisplay, notificationFilterPreferenceService)
  }
} else {
 println("=Display preferences for a single user=")
 println("""{{html wiki='false' clean='false'}}<form method='get' class='xform'>
 <div>
 <input type='text' name='user' placeholder='User page name (username prefixed by XWiki.)' class='suggest-users'  data-xwiki-selectize='{\"maxItems\":1}'></input>
 </div>
 <div class='buttonwrapper'>
   <input type='submit' class='button primary btn btn-primary' name='action' value='Show'>
 </div>
 </form>{{/html}}""")
 println("=Display preferences for all users")
 println("\n{{warning}}Use this carefully for wikis with lots of users, there is no pagination{{/warning}}\n");
 println("""{{html wiki='false' clean='false'}}<form method='get' class='xform'>
 <div>
 <input type='hidden' name='allusers' value='true'></input>
 </div>
 <div class='buttonwrapper'>
   <input type='submit' class='button primary btn btn-primary' name='action' value='Show'>
 </div>
 </form>{{/html}}""")
}
{{/groovy}}
Created by Anca Luca on 2021/05/05 19:14
     

Get Connected