From bea3a1c8dd2ff42e7d7b3da6a506a22311952470 Mon Sep 17 00:00:00 2001 From: Inosh Perara Date: Thu, 30 Apr 2020 04:58:17 +0000 Subject: [PATCH] Reporting permission API --- .../admin/DeviceManagementAdminService.java | 51 +++++++++++++++++++ .../DeviceManagementAdminServiceImpl.java | 30 +++++++++++ .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 11 ++++ 3 files changed, 92 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java index d5695b519a..4720b357c7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java @@ -107,6 +107,12 @@ import java.util.List; description = "Getting Details of Device tenants", key = "perm:admin:tenant:view", permissions = {"/device-mgt/devices/tenants/view"} + ), + @Scope( + name = "Add a permission to the permission tree", + description = "Add a permission to the permission tree", + key = "perm:admin:permissions:add", + permissions = {"/device-mgt/devices/permissions/add"} ) } ) @@ -410,4 +416,49 @@ public interface DeviceManagementAdminService { response = ErrorResponse.class) }) Response getTenants(); + + + @POST + @Path("/permissions") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Add permission to the tree", + notes = "Add permission to the tree.", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = + "perm:admin:permissions:add") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully update the owner of devices.", + response = DeviceList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 400, + message = "The incoming request has more than one selection criteria defined via the query parameters.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching " + + "adding permission to the tree.", + response = ErrorResponse.class) + }) + Response addPermission(List permissions); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java index dccd5f69d2..f88bab29c7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java @@ -46,6 +46,8 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.exceptions.UserNotFoundException; import org.wso2.carbon.device.mgt.common.general.TenantDetail; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagerService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; @@ -62,6 +64,7 @@ import javax.validation.constraints.Size; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; @@ -243,4 +246,31 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe "for super tenant admin only.").build(); } } + + + @POST + @Path("/permissions") + @Produces({MediaType.APPLICATION_JSON}) + public Response addPermission(List permissions) { + String PERMISSION_PREFIX = "/permission/admin"; + PermissionManagerService permissionService = DeviceMgtAPIUtils.getPermissionManagerService(); + org.wso2.carbon.device.mgt.common.permission.mgt.Permission permission = new org + .wso2.carbon.device.mgt.common.permission.mgt.Permission(); + + for (String path : permissions) { + path = PERMISSION_PREFIX + path; + permission.setPath(path); + permission.setUrl(path); + try { + permissionService.addPermission(permission); + } catch (PermissionManagementException e) { + String msg = "Error occurred adding permission"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + } + return Response.status(Response.Status.OK).build(); + + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 5f2fc3cbf2..6aec717dd9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -46,6 +46,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementService; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagerService; import org.wso2.carbon.device.mgt.common.report.mgt.ReportManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; @@ -468,6 +469,16 @@ public class DeviceMgtAPIUtils { return searchManagerService; } + public static PermissionManagerService getPermissionManagerService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + PermissionManagerService PermissionManagerService = + (PermissionManagerService) ctx.getOSGiService(PermissionManagerService.class, null); + if (PermissionManagerService == null) { + throw new IllegalStateException("Permission manager service is not initialized."); + } + return PermissionManagerService; + } + public static GeoLocationProviderService getGeoService() { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); GeoLocationProviderService