Skip to main content
MediaBeacon University

How to Implement a Username Dictionary

Version: 8.5

Issue 

MediaBeacon has no native dictionary of all users in the system, below are some implementation options.

Resolution

Script 1

/*
UsernameDictionary v4 2017-10-19
Written by Dave Burris-Brown
This script generates a dictionary of users from a list of groups by name in the system.
It can display and enter the username, but may be modified to enter email or any other user object property. 
*/

// The workflow name
var kWorkflowName = "UsernameDictionary";

// The groups from which to get the lists of users
var kGroupNames = ["Global Admin", "O=NA,General Import NA", "General Download NA"];

// Returns true if theUsers array contains theUser, otherwise returns false.
function containsUser(theUsers, theUser) {
    for (var i = 0; i < theUsers.length; i++) {
        if (theUsers[i].username == theUser.username) {
            return true;
        }
    }
    return false;
}

function main() {
    var aResult = [];
    try {
        // Create array with all the usernames in all the groups, with no duplicates
        var aUsers = [];
        for (var i = 0; i < kGroupNames.length; i++) {
            var aGroupName = kGroupNames[i];
            var aGroupUsers = groupManager.getGroup(aGroupName).getUsers();
            for (var j = 0; j < aGroupUsers.length; j++) {
                var aUser = aGroupUsers[j];
                if (!containsUser(aUsers, aUser)) {
                    aUsers.push(aUser);
                }
            }
        }

        // Sort the usernames array
        aResult.sort(function(theEntry1, theEntry2) {
            return theEntry1.username.localeCompare(theEntry2.username);
        });

        // Build array of username dictionary elements (where 'display' and 'value' properties equal the respective user object property values)
        for (var i = 0; i < aUsers.length; i++) {
            aResult.push({ "display": aUsers[i].username, "value": aUsers[i].username });
            // aResult.push({ "display": aUsers[i].username, "value": aUsers[i].email });
        }
    } catch (theException) {
        logAtLevel("Failed to generate dictionary:  " + theException, kWorkflowName, logLevels.WARNING);
    }

    return JSON.stringify(aResult);
}

main();
  • Was this article helpful?