Sync Dashboard sharing permissions with all reports on the dashboard
A massive frustration of mine, and of many others it seems (see This Idea) is that sharing Reports on a dashboard is a major hassle. I've built a UI action, from the Dashboard Properties page, that will force a sync between the sharing permissions for the dashboard with all reports on that dashboard.
It is additive only, so you will not break the sharing permissions of reports that are on multiple dashboards or shared separately.
Attached is the UI Action XML, so you can just import this into your instance and you're good to go, or you can continue reading the code below and explanation so you can edit it to suit your purpose.
How to use
Open up a dashboard, and go to Dashboard Properties
Then the UI Action is a link in the related links section
How it works
Dashboards can be shared with 3 different things, Users, Groups, and Roles.
Reports can be shared with either Users or Groups, or they can be shared with Everyone and restricted by role. They can't be both.
As a workaround for the Role limitation, the code instead looks up all individuals with a particular role, and gives them User access permission to the report. The net result is the same, all users who can see the dashboard, can also see the reports on the dashboard.
Recommended changes for your instance
If you have very large groups/role inheritance (e.g. hundreds/thousands of users) it's probably a good idea to wrap this UI Action script into a Script Action, and trigger an event with the UI Action for the system to process later. However you wont get the instant feedback on what you were sharing
If you don't care about getting told what was shared and with who, then you can comment out or change the gs.addInfoMessage lines.
var dashboardTabM2M = new GlideRecord('pa_m2m_dashboard_tabs');
dashboardTabM2M.addQuery("dashboard", current.sys_id);
dashboardTabM2M.query();
var pages = [];
while (dashboardTabM2M.next()) {
pages.push(dashboardTabM2M.tab.page.sys_id + "");
}
var reportIds = [];
pages.forEach(function(pageID) {
var portals = new GlideRecord("sys_portal");
portals.addQuery("page", pageID);
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 shareGroup = [];
var dashboardPerms = new GlideRecord("pa_dashboards_permissions");
dashboardPerms.addQuery("dashboard", current.getUniqueValue());
dashboardPerms.query();
while (dashboardPerms.next()) {
if (dashboardPerms.type == "2")
shareGroup.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
shareGroup.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")]));
}
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);
Labels:
https://www.servicenow.com/community/platform-analytics-articles/sync-dashboard-sharing-permissions-with-all-reports-on-the/ta-p/2302389
