Assign New Users to Groups Upon Creation by Running a Script

The functionality provides for creating and assigning new users to Groups upon the initial login via SSO. Specific System Variables must be configured for the system to run a Custom Script which will assign Group access based on a Dataset.

PREREQUISITES:

Example Dataset

The Dataset must include username and group_id columns with the actual data.

Example Custom Script

You can use the below Custom Script for assigning Users to Groups upon initial login.

var datasetId = <user_group_access_dataset_ID>;
var dataReq = JSON.stringify({
    "filters": [{"group": {"operator": "AND", "rules": [{"column_name": "username", "condition": "=", "data": req.user.username}]}}]
});
function syncGroupMember(userId, data){
    customScript.runApiRequest(customScript.homeSite + 'api/user_group_member?user='+userId,{async:false}).done(function(response){
        var user_group_members = [];
        if ('undefined' !== typeof (response) && response.user_group_members && response.user_group_members.length)
            user_group_members = response.user_group_members;
        for (var i = 0; i < data.length; i++) {
            var found = false;
            for (var j = 0; j < user_group_members.length; j++)
                if (user_group_members[j].group==data[i].group_id) {
                    found = true;
                    break;
                }
            if (!found) {
                var request = JSON.stringify({"user":userId, "group":data[i].group_id});
                customScript.runApiRequest(customScript.homeSite+'api/user_group_member',{"type":"POST", "async":false, "data":request});
                customScript.log('POST: '+customScript.homeSite+'api/user_group_member '+request);
            }
        }
    });
};
customScript.runApiRequest(customScript.homeSite + 'api/dataset_data?dataset='+datasetId,{async:false, "type":"POST", "data":dataReq}).done(function(response){
    var data = response.data;
    if(data && data.length)
        syncGroupMember(req.user_id, data);
    setTimeout(function () { customScript.close(); }, 2000);

where:

var datasetId = <user_group_access_dataset_ID>

Configure Variables

Configure the INITIAL_USER_LOGIN Variables in the following way:

  1. INITIAL_USER_LOGIN_EXTERNAL_SYNC – set to "custom_script" to run the Custom Script
  2. INITIAL_USER_LOGIN_EXTERNAL_SYNC_SCRIPT – specify the name of the Custom Script
  3. INITIAL_USER_LOGIN_MESSAGE – create a custom login message if needed