Skip to content

Commit

Permalink
Merge pull request #1176 from lasanthaS/export-permissions
Browse files Browse the repository at this point in the history
Support import/export dashboard permissions
  • Loading branch information
TanyaM authored Sep 29, 2019
2 parents 5239fea + 62b6c59 commit af117cb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ public Response updateDashboardRoles(@PathParam("url") String url, @Context Requ
@GET
@Path("/{url}/export")
@Produces(MediaType.APPLICATION_JSON)
public Response exportDashboard(@PathParam("url") String url, @QueryParam("download") boolean download) {

public Response exportDashboard(@PathParam("url") String url, @QueryParam("download") boolean download,
@QueryParam("permissions") boolean permissions) {
try {
DashboardArtifact artifact = dashboardDataProvider.exportDashboard(url);
DashboardArtifact artifact = dashboardDataProvider.exportDashboard(url, permissions);
Response.ResponseBuilder responseBuilder = Response.ok(artifact);
if (download) {
responseBuilder.header("Content-Disposition", "attachment; filename=\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ List<org.wso2.carbon.analytics.idp.client.core.models.Role> getRolesByUsername(S
void updateDashboardRoles(String user, String dashboardUrl, Map<String, List<String>> roles) throws
DashboardException;

void updateDashboardRoles(String dashboardUrl, Map<String, List<String>> roles) throws DashboardException;

/**
* Return exportable dashboard definition.
*
Expand All @@ -125,7 +127,7 @@ void updateDashboardRoles(String user, String dashboardUrl, Map<String, List<Str
* @return Exportable dashboard definition
* @throws DashboardException If an error occurred while reading or processing dashboards
*/
DashboardArtifact exportDashboard(String dashboardUrl) throws DashboardException;
DashboardArtifact exportDashboard(String dashboardUrl, boolean permissions) throws DashboardException;

DashboardConfigurations getReportGenerationConfigurations();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

import org.wso2.carbon.dashboards.core.bean.DashboardMetadata;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Defines a dashboard data format to import/export a dashboard.
*
Expand All @@ -28,6 +32,7 @@
public class DashboardArtifact {
private DashboardMetadata dashboard;
private WidgetCollection widgets = new WidgetCollection();
private Map<String, List<String>> permissions = new HashMap<>();

/**
* Returns dashboard metadata object.
Expand Down Expand Up @@ -64,4 +69,16 @@ public WidgetCollection getWidgets() {
public void setWidgets(WidgetCollection widgets) {
this.widgets = widgets;
}

public Map<String, List<String>> getPermissions() {
return permissions;
}

public void setPermissions(Map<String, List<String>> permissions) {
this.permissions = permissions;
}

public void addPermissions(String permission, List<String> roles) {
permissions.put(permission, roles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -44,6 +45,12 @@
public class DashboardImporter {

private static final Logger LOGGER = LoggerFactory.getLogger(DashboardImporter.class);
public static final String PERMISSION_VIEWERS = "viewers";
public static final String PERMISSION_EDITORS = "editors";
public static final String PERMISSION_OWNERS = "owners";
public static final String PERMISSION_VIEWER = "viewer";
public static final String PERMISSION_EDITOR = "editor";
public static final String PERMISSION_OWNER = "owner";

private final DashboardMetadataProvider dashboardMetadataProvider;
private final WidgetMetadataProvider widgetMetadataProvider;
Expand Down Expand Up @@ -77,6 +84,20 @@ public void importDashboards() {
} else {
dashboardMetadataProvider.add(dashboard);
}

// Fix keys before saving
Map<String, List<String>> permissionMap = dashboardArtifact.getPermissions();
if (permissionMap.containsKey(PERMISSION_VIEWERS)) {
permissionMap.put(PERMISSION_VIEWER, permissionMap.remove(PERMISSION_VIEWERS));
}
if (permissionMap.containsKey(PERMISSION_EDITORS)) {
permissionMap.put(PERMISSION_EDITOR, permissionMap.remove(PERMISSION_EDITORS));
}
if (permissionMap.containsKey(PERMISSION_OWNERS)) {
permissionMap.put(PERMISSION_OWNER, permissionMap.remove(PERMISSION_OWNERS));
}
dashboardMetadataProvider.updateDashboardRoles(dashboard.getUrl(), dashboardArtifact.getPermissions());

} catch (DashboardException e) {
LOGGER.warn("Cannot save dashboard importing from '{}' to the database.", dashboardArtifactPath, e);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,34 +273,39 @@ public void updateDashboardRoles(String user, String dashboardUrl, Map<String, L
throws DashboardException {
if (permissionProvider.hasPermission(user, new Permission(PERMISSION_APP_NAME,
dashboardUrl + PERMISSION_SUFFIX_OWNER))) {
List<org.wso2.carbon.analytics.idp.client.core.models.Role> allRoles = null;
try {
allRoles = identityClient.getAllRoles();
} catch (IdPClientException e) {
throw new DashboardException("Unable to get all user roles.", e);
}
Map<String, Role> allRoleMap = new HashMap<>();
for (org.wso2.carbon.analytics.idp.client.core.models.Role role : allRoles) {
allRoleMap.put(role.getDisplayName(), new Role(role.getId(), role.getDisplayName()));
}

Iterator iterator = roleIdMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Permission permission = new Permission(PERMISSION_APP_NAME, dashboardUrl + "." + entry.getKey()
.toString());
permissionProvider.revokePermission(permission);
for (String roleId : (List<String>) entry.getValue()) {
permissionProvider.grantPermission(permission, allRoleMap.get(roleId));
}
iterator.remove();
}
updateDashboardRoles(dashboardUrl, roleIdMap);
} else {
throw new UnauthorizedException("Insufficient permissions to update roles of the dashboard with ID" +
dashboardUrl);
}
}

public void updateDashboardRoles(String dashboardUrl, Map<String, List<String>> roleIdMap)
throws DashboardException {
List<org.wso2.carbon.analytics.idp.client.core.models.Role> allRoles;
try {
allRoles = identityClient.getAllRoles();
} catch (IdPClientException e) {
throw new DashboardException("Unable to get all user roles.", e);
}
Map<String, Role> allRoleMap = new HashMap<>();
for (org.wso2.carbon.analytics.idp.client.core.models.Role role : allRoles) {
allRoleMap.put(role.getDisplayName(), new Role(role.getId(), role.getDisplayName()));
}

Iterator iterator = roleIdMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Permission permission = new Permission(PERMISSION_APP_NAME, dashboardUrl + "." + entry.getKey()
.toString());
permissionProvider.revokePermission(permission);
for (String roleId : (List<String>) entry.getValue()) {
permissionProvider.grantPermission(permission, allRoleMap.get(roleId));
}
iterator.remove();
}
}

/**
* Return exportable dashboard definition.
*
Expand All @@ -309,8 +314,9 @@ public void updateDashboardRoles(String user, String dashboardUrl, Map<String, L
* @throws DashboardException If an error occurred while reading or processing dashboards
*/
@Override
public DashboardArtifact exportDashboard(String dashboardUrl) throws DashboardException {
public DashboardArtifact exportDashboard(String dashboardUrl, boolean permissions) throws DashboardException {
Optional<DashboardMetadata> dashboardMetadataOptional = dao.get(dashboardUrl);
Map<String, List<Role>> dashboardRoles = getDashboardRoles(dashboardUrl);
if (!dashboardMetadataOptional.isPresent()) {
throw new DashboardException("Cannot find the dashboard '" + dashboardUrl + "'");
}
Expand All @@ -332,6 +338,13 @@ public DashboardArtifact exportDashboard(String dashboardUrl) throws DashboardEx
// Set list of custom widgets
widgetCollection.setCustom(widgets.get(WidgetType.CUSTOM));
artifact.setWidgets(widgetCollection);

if (permissions) {
dashboardRoles.forEach((permission, roles) -> {
List<String> roleNames = roles.stream().map(Role::getName).collect(Collectors.toList());
artifact.addPermissions(permission, roleNames);
});
}
return artifact;
}

Expand Down

0 comments on commit af117cb

Please sign in to comment.