Share reports on a given dashboard with 'user' , 'group' , and 'role' that dashboard is shared with

pbusch
Tera Expert

I was inspired by a similar script , and created the below UI Action running on pa_dashboards ; it is a form link that is executed from the form which loads when 'Dashboard Properties' is navigated to from a given db additional actions hamburger menu.

Bear in mind that this will share all reports on every tab, but if you are in an environment with properly configured ACLs, that will be a non-issue.

 

This shares all reports on the given dashboard with every 'user' , 'group' , and 'role' that the dashboard is shared with. Other UI Field configurations can be derived from the snip I am attaching as well.

pbusch_0-1728573740005.png

 

 

var dashboardTabM2M = new GlideRecord('pa_m2m_dashboard_tabs');
dashboardTabM2M.addQuery("dashboard", current.sys_id);
dashboardTabM2M.query();
var canvas_pages = [];
while (dashboardTabM2M.next()) {
    canvas_pages.push(dashboardTabM2M.tab.canvas_page.sys_id + "");
}

var portal_widgets = [];

canvas_pages.forEach(function(canvas_pageID) {
    var grid_canvas_pane = new GlideRecord("sys_grid_canvas_pane");
    grid_canvas_pane.addQuery("grid_canvas", canvas_pageID);
    grid_canvas_pane.query();
    while (grid_canvas_pane.next()) {
        portal_widgets.push(grid_canvas_pane.portal_widget + '');
    }
});

var reportIds = [];
portal_widgets.forEach(function(portal_widgetIDs) {
    var portals = new GlideRecord("sys_portal");
    portals.addQuery("sys_id", portal_widgetIDs);
    portals.query();
    while (portals.next()) {
        var pageProp = new GlideRecord("sys_portal_preferences");
        pageProp.addQuery("portal_section", portals.getUniqueValue());
        pageProp.addQuery("name""sys_id");
        pageProp.query();
        if (pageProp.next()) {
            reportIds.push(pageProp.getValue("value"));
        }
    }
});

var shareUsers = [];
var shareRoles = [];
var shareGroups = [];
var dashboardPerms = new GlideRecord("pa_dashboards_permissions");
dashboardPerms.addQuery("dashboard", current.getUniqueValue());
dashboardPerms.query();
while (dashboardPerms.next()) {
    if (dashboardPerms.type == "2")
        shareGroups.push(dashboardPerms.getValue("group"));
    if (dashboardPerms.type == "1")
        shareRoles.push(dashboardPerms.getValue("role"));
    if (dashboardPerms.type == "3")
        shareUsers.push(dashboardPerms.getValue("user"));
}

var reportGR = new GlideRecord("sys_report");
reportGR.addQuery("sys_id""IN", reportIds.join(","));
reportGR.query();
while (reportGR.next()) {
    reportGR.setValue("user""group");
    reportGR.update();
}

reportIds.forEach(function(reportId) {
    var validCheck = new GlideRecord('sys_report');
    if (!validCheck.get(reportId))
        return// not a real report id

    shareGroups.forEach(function(groupId) {
        var reportPerm = new GlideRecord("sys_report_users_groups");
        reportPerm.addQuery("report_id", reportId);
        reportPerm.addQuery("group_id", groupId);
        reportPerm.query();
        if (reportPerm.next()) {
            gs.addInfoMessage(gs.getMessage("Report {0} is already shared with group {1}", [reportPerm.getDisplayValue("report_id"), reportPerm.getDisplayValue("group_id")]));
        } else {
            reportPerm.newRecord();
            reportPerm.setValue("report_id", reportId);
            reportPerm.setValue("group_id", groupId);
            reportPerm.insert();
            gs.addInfoMessage(gs.getMessage("Report {0} shared with group {1}", [reportPerm.getDisplayValue("report_id"), reportPerm.getDisplayValue("group_id")]));
        }
    });

    shareUsers.forEach(function(userId) {
        var reportPerm = new GlideRecord("sys_report_users_groups");
        reportPerm.addQuery("report_id", reportId);
        reportPerm.addQuery("user_id", userId);
        reportPerm.query();
        if (reportPerm.next()) {
            gs.addInfoMessage(gs.getMessage("Report {0} is already shared with user {1}", [reportPerm.getDisplayValue("report_id"), reportPerm.getDisplayValue("user_id")]));
        } else {
            reportPerm.newRecord();
            reportPerm.setValue("report_id", reportId);
            reportPerm.setValue("user_id", userId);
            reportPerm.insert();
            gs.addInfoMessage(gs.getMessage("Report {0} shared with user {1}", [reportPerm.getDisplayValue("report_id"), reportPerm.getDisplayValue("user_id")]));
        }
    });

    shareRoles.forEach(function(roleId) {
        var userIds = [];
        var userRoleGR = new GlideRecord("sys_user_has_role");
        userRoleGR.addQuery("user.active"true);
        userRoleGR.addQuery("role", roleId);
        userRoleGR.query();
        var roleName;
        while (userRoleGR.next()) {
            if (!roleName) {
                roleName = userRoleGR.getDisplayValue("role"); // for info msg
            }
            userIds.push(userRoleGR.getValue("user"));
        }
        var au = new global.ArrayUtil();
        userIds = au.unique(userIds);

        var reportName;
        userIds.forEach(function(userId) {
            var reportPerm = new GlideRecord("sys_report_users_groups");
            reportPerm.addQuery("report_id", reportId);
            reportPerm.addQuery("user_id", userId);
            reportPerm.query();
            if (reportPerm.next()) {
                // gs.addInfoMessage(gs.getMessage("Report {0} is already shared with user {1}", [reportPerm.getDisplayValue("report_id"), reportPerm.getDisplayValue("user_id")]));
            } else {
                reportPerm.newRecord();
                reportPerm.setValue("report_id", reportId);
                reportPerm.setValue("user_id", userId);
                reportPerm.insert();
                gs.addInfoMessage(gs.getMessage("Report {0} shared with user {1}", [reportPerm.getDisplayValue("report_id"), reportPerm.getDisplayValue("user_id")]));
                gs.addAlert(gs.getMessage("Report {0} shared with user {1}", [reportPerm.getDisplayValue("report_id"), reportPerm.getDisplayValue("user_id")]));
                gs.addLog(gs.getMessage("Report {0} shared with user {1}", [reportPerm.getDisplayValue("report_id"), reportPerm.getDisplayValue("user_id")]));
            }
            if (!reportName) {
                reportName = reportPerm.getDisplayValue("report_id");
            }
        });
        gs.addInfoMessage(gs.getMessage("Report {0} shared with {1} users who have the {2} role", [reportName, userIds.length + "", roleName]));
    });
});

action.setRedirectURL(current);
~ "Breynia Disticha"
0 REPLIES 0