From a3a434055b1a373e787a5f632a411d381ff59b39 Mon Sep 17 00:00:00 2001 From: Charitha Goonetilleke Date: Sun, 3 May 2020 14:28:15 +0000 Subject: [PATCH] Fix tenant details retrieving and permission assigning for reporting --- .../DeviceManagementConfigService.java | 117 +++++++++++++++- .../DeviceManagementConfigServiceImpl.java | 85 +++++++++++- .../config/jaxrs/util/DeviceMgtAPIUtils.java | 52 +++++++- .../admin/DeviceManagementAdminService.java | 126 ++---------------- .../DeviceManagementAdminServiceImpl.java | 83 ------------ .../wso2/carbon/device/mgt/common/Device.java | 13 +- .../DeviceManagementProviderServiceImpl.java | 25 ++-- 7 files changed, 265 insertions(+), 236 deletions(-) diff --git a/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/DeviceManagementConfigService.java b/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/DeviceManagementConfigService.java index 2cf90d4659..9f9e466562 100644 --- a/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/DeviceManagementConfigService.java +++ b/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/DeviceManagementConfigService.java @@ -34,16 +34,19 @@ import io.swagger.annotations.Tag; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.mgt.common.configuration.mgt.DeviceConfiguration; +import org.wso2.carbon.device.mgt.common.general.TenantDetail; 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; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.List; @SwaggerDefinition( info = @Info( @@ -53,15 +56,14 @@ import javax.ws.rs.core.Response; @Extension(properties = { @ExtensionProperty(name = "name", value = "DeviceManagementConfiguration"), @ExtensionProperty(name = "context", - value = "/api/device-mgt-config/v1.0/configurations"), + value = "/api/device-mgt-config/v1.0"), }) } ), tags = { - @Tag(name = "device_management", description = "") + @Tag(name = "device_management", description = "Device management configuration service") } ) -@Path("/configurations") @Api(value = "Device Management Configuration") @Consumes(MediaType.APPLICATION_JSON) @Scopes(scopes = { @@ -76,12 +78,25 @@ import javax.ws.rs.core.Response; description = "", key = "perm:manage-configuration", permissions = {"/device-mgt/platform-configurations/manage"} + ), + @Scope( + name = "Getting Details of Device tenants", + description = "Getting Details of Device tenants", + key = "perm:admin:tenant:view", + permissions = {"/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 = {"/permissions/add"} ) } ) public interface DeviceManagementConfigService { @GET + @Path("/configurations") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -140,7 +155,7 @@ public interface DeviceManagementConfigService { String properties); @PUT - @Path("/transfer") + @Path("/device/transfer") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", @@ -196,7 +211,7 @@ public interface DeviceManagementConfigService { DeviceTransferRequest deviceTransferRequest); @GET - @Path("/ui-config") + @Path("/configurations/ui-config") @Produces(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.APPLICATION_JSON, @@ -220,4 +235,96 @@ public interface DeviceManagementConfigService { response = ErrorResponse.class) }) Response getUiConfig(); + + @GET + @Path("/tenants") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting Details of tenants", + notes = "Get the details of tenants.", + response = TenantDetail.class, + responseContainer = "List", + tags = "Device Management Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "scope", value ="perm:admin:tenant:view") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of tenants.", + response = TenantDetail.class, + responseContainer = "List", + 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 = 304, + message = "Not Modified. Empty body because the client already has the latest version of the " + + "requested resource.\n"), + @ApiResponse( + code = 401, + message = "Unauthorized.\n The unauthorized access to the requested resource.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the" + + " tenant list.", + 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 = "scope", value = + "perm:admin:permissions:add") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully added the permissions.", + 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) + }) + @Produces(MediaType.APPLICATION_JSON) + Response addPermission(List permissions); } diff --git a/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/impl/DeviceManagementConfigServiceImpl.java b/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/impl/DeviceManagementConfigServiceImpl.java index 11fc0a2677..daf9d1c419 100644 --- a/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/impl/DeviceManagementConfigServiceImpl.java +++ b/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/impl/DeviceManagementConfigServiceImpl.java @@ -26,6 +26,7 @@ import io.entgra.carbon.device.mgt.config.jaxrs.service.DeviceManagementConfigSe import io.entgra.carbon.device.mgt.config.jaxrs.util.DeviceMgtAPIUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials; import org.wso2.carbon.device.mgt.common.ApplicationRegistrationException; @@ -34,6 +35,9 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.AmbiguousConfiguratio import org.wso2.carbon.device.mgt.common.configuration.mgt.DeviceConfiguration; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; +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.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; @@ -44,10 +48,14 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; +import org.wso2.carbon.user.api.Tenant; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.core.service.RealmService; 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; @@ -55,10 +63,10 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Map; -@Path("/configurations") @Consumes(MediaType.APPLICATION_JSON) public class DeviceManagementConfigServiceImpl implements DeviceManagementConfigService { @@ -66,6 +74,7 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig @Override @GET + @Path("/configurations") @Produces(MediaType.APPLICATION_JSON) public Response getConfiguration(@HeaderParam("token") String token, @QueryParam("properties") String properties) { @@ -162,7 +171,7 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig @GET @Override @Consumes("application/json") - @Path("/ui-config") + @Path("/configurations/ui-config") public Response getUiConfig() { UIConfigurationManager uiConfigurationManager = UIConfigurationManager.getInstance(); if (uiConfigurationManager == null) { @@ -212,4 +221,76 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig } } + @Override + @Path("/tenants") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getTenants() { + List tenantDetails; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (tenantId == MultitenantConstants.SUPER_TENANT_ID) { + RealmService realmService = DeviceMgtAPIUtils.getRealmService(); + try { + Tenant[] tenants = realmService.getTenantManager().getAllTenants(); + tenantDetails = new ArrayList<>(); + Tenant superTenant = new Tenant(); + superTenant.setId(MultitenantConstants.SUPER_TENANT_ID); + superTenant.setDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + superTenant.setAdminName(realmService.getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID) + .getRealmConfiguration().getAdminUserName()); + superTenant.setActive(true); + tenantDetails.add(getTenantDetail(superTenant)); + if (tenants != null && tenants.length > 0) { + for (Tenant tenant : tenants) { + tenantDetails.add(getTenantDetail(tenant)); + } + } + return Response.status(Response.Status.OK).entity(tenantDetails).build(); + } catch (UserStoreException e) { + String msg = "Error occurred while fetching tenant list"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + } else { + return Response.status(Response.Status.BAD_REQUEST).entity("This API is available " + + "for super tenant admin only.").build(); + } + } + + private TenantDetail getTenantDetail(Tenant tenant) { + TenantDetail tenantDetail = new TenantDetail(); + tenantDetail.setId(tenant.getId()); + tenantDetail.setAdminFirstName(tenant.getAdminFirstName()); + tenantDetail.setAdminFullName(tenant.getAdminFullName()); + tenantDetail.setAdminLastName(tenant.getAdminLastName()); + tenantDetail.setAdminName(tenant.getAdminName()); + tenantDetail.setDomain(tenant.getDomain()); + tenantDetail.setEmail(tenant.getEmail()); + return tenantDetail; + } + + @POST + @Path("/permissions") + @Produces({MediaType.APPLICATION_JSON}) + public Response addPermission(List permissions) { + 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) { + 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/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/util/DeviceMgtAPIUtils.java index 874a644bba..96ef7c0cbc 100644 --- a/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/util/DeviceMgtAPIUtils.java @@ -21,23 +21,61 @@ package io.entgra.carbon.device.mgt.config.jaxrs.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagerService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.user.core.service.RealmService; /** * MDMAPIUtils class provides utility function used by CDM REST-API classes. */ public class DeviceMgtAPIUtils { - private static Log log = LogFactory.getLog(DeviceMgtAPIUtils.class); + + private static final Log log = LogFactory.getLog(DeviceMgtAPIUtils.class); + + private static DeviceManagementProviderService deviceManagementProviderService = null; + private static PermissionManagerService permissionManagerService = null; + private static RealmService realmService = null; public static DeviceManagementProviderService getDeviceManagementService() { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - DeviceManagementProviderService deviceManagementProviderService = - (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null); if (deviceManagementProviderService == null) { - String msg = "DeviceImpl Management provider service has not initialized."; - log.error(msg); - throw new IllegalStateException(msg); + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + deviceManagementProviderService = + (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null); + if (deviceManagementProviderService == null) { + String msg = "Device Management provider service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } } return deviceManagementProviderService; } + + public static PermissionManagerService getPermissionManagerService() { + if (permissionManagerService == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + permissionManagerService = + (PermissionManagerService) ctx.getOSGiService(PermissionManagerService.class, null); + if (permissionManagerService == null) { + String msg = "Permission Management provider service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + return permissionManagerService; + } + + public static RealmService getRealmService() { + if (realmService == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = + (RealmService) ctx.getOSGiService(RealmService.class, null); + if (realmService == null) { + String msg = "Realm service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + return realmService; + } + } 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 4720b357c7..ecfbb385d0 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 @@ -34,29 +34,33 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.api.admin; -import io.swagger.annotations.SwaggerDefinition; -import io.swagger.annotations.Info; -import io.swagger.annotations.ExtensionProperty; -import io.swagger.annotations.Extension; -import io.swagger.annotations.Tag; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.Info; import io.swagger.annotations.ResponseHeader; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; import org.apache.axis2.transport.http.HTTPConstants; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.general.TenantDetail; -import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.validation.constraints.Size; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; @@ -101,18 +105,6 @@ import java.util.List; description = "Permanently Delete the device specified by device id", key = "perm:devices:permanent-delete", permissions = {"/device-mgt/admin/devices/permanent-delete"} - ), - @Scope( - name = "Getting Details of Device tenants", - 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"} ) } ) @@ -367,98 +359,4 @@ public interface DeviceManagementAdminService { required = true) List deviceIdentifiers); - @GET - @Path("/tenants") - @ApiOperation( - produces = MediaType.APPLICATION_JSON, - httpMethod = "GET", - value = "Getting Details of tenants", - notes = "Get the details of tenants.", - response = TenantDetail.class, - responseContainer = "List", - tags = "Device Management Administrative Service", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = Constants.SCOPE, value = - "perm:admin:tenant:view") - }) - } - ) - @ApiResponses(value = { - @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of tenants.", - response = TenantDetail.class, - responseContainer = "List", - 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 = 304, - message = "Not Modified. Empty body because the client already has the latest version of the " + - "requested resource.\n"), - @ApiResponse( - code = 401, - message = "Unauthorized.\n The unauthorized access to the requested resource.", - response = ErrorResponse.class), - @ApiResponse( - code = 500, - message = "Internal Server Error. \n Server error occurred while fetching the" + - " tenant list.", - 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 f88bab29c7..e4030b6d50 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 @@ -45,33 +45,23 @@ import org.wso2.carbon.device.mgt.common.PaginationRequest; 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; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; -import org.wso2.carbon.user.api.Tenant; -import org.wso2.carbon.user.api.TenantManager; -import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.core.service.RealmService; -import javax.servlet.http.HttpServletResponse; 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; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.ArrayList; import java.util.List; @Path("/admin/devices") @@ -200,77 +190,4 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } } - @Override - @Path("/tenants") - @GET - public Response getTenants() { - List tenantDetails; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - if (tenantId == MultitenantConstants.SUPER_TENANT_ID) { - RealmService realmService = (RealmService) PrivilegedCarbonContext - .getThreadLocalCarbonContext().getOSGiService(RealmService.class, null); - if (realmService == null) { - String msg = "RealmService is not initialized"; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } - - try { - Tenant[] tenants = realmService.getTenantManager().getAllTenants(); - tenantDetails = new ArrayList<>(); - if (tenants != null && tenants.length > 0) { - for (Tenant tenant : tenants) { - TenantDetail tenantDetail = new TenantDetail(); - tenantDetail.setId(tenant.getId()); - tenantDetail.setAdminFirstName(tenant.getAdminFirstName()); - tenantDetail.setAdminFullName(tenant.getAdminFullName()); - tenantDetail.setAdminLastName(tenant.getAdminLastName()); - tenantDetail.setAdminName(tenant.getAdminName()); - tenantDetail.setDomain(tenant.getDomain()); - tenantDetail.setEmail(tenant.getEmail()); - tenantDetails.add(tenantDetail); - } - return Response.status(Response.Status.OK).entity(tenantDetails).build(); - } else { - return Response.status(Response.Status.NOT_FOUND).entity("No tenants found") - .build(); - } - } catch (UserStoreException e) { - String msg = "Error occurred while fetching tenant list"; - log.error(msg, e); - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); - } - } else { - return Response.status(Response.Status.BAD_REQUEST).entity("This API is available " + - "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.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java index d492ce22e1..9ab36b2dc3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.mgt.common; +import com.google.gson.Gson; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import org.wso2.carbon.device.mgt.common.app.mgt.Application; @@ -187,17 +188,7 @@ public class Device implements Serializable { @Override public String toString() { - return "device [" + - "name=" + name + ";" + - "type=" + type + ";" + - "description=" + description + ";" + - "identifier=" + deviceIdentifier + ";" + -// "EnrolmentInfo[" + -// "owner=" + enrolmentInfo.getOwner() + ";" + -// "ownership=" + enrolmentInfo.getOwnership() + ";" + -// "status=" + enrolmentInfo.getStatus() + ";" + -// "]" + - "]"; + return new Gson().toJson(this); } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 3726819bb0..1c5f3ec73a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -318,14 +318,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED. equals(existingEnrolmentInfo.getStatus())) { - enrollment = enrollmentDAO. - addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); + enrollment = enrollmentDAO + .addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); if (enrollment == null ){ DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException( - "Enrollment data persistence is failed in a re-enrollment. Device id : " - + existingDevice.getId() + " Device Identifier: " + device - .getDeviceIdentifier()); + "Enrollment data persistence is failed in a re-enrollment. Existing device: " + + existingDevice.toString() + ", New Device: " + device.toString()); } device.setEnrolmentInfo(enrollment); DeviceManagementDAOFactory.commitTransaction(); @@ -339,16 +338,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } status = true; } else { - log.warn("Unable to update device enrollment for device : " + device.getDeviceIdentifier() + - " belonging to user : " + device.getEnrolmentInfo().getOwner()); + log.warn("Unable to update device enrollment for device : " + device.toString()); } } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while adding enrolment related metadata for device: " + device.getId(); + String msg = "Error occurred while adding enrolment related metadata for device: " + + device.toString(); log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred while enrolling device: " + device.getId(); + String msg = "Error occurred while enrolling device: " + device.toString(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -367,8 +366,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (enrollment == null ){ DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException( - "Enrollment data persistence is failed in a new enrollment. Device id: " + deviceId - + " Device Identifier: " + device.getDeviceIdentifier()); + "Enrollment data persistence is failed in a new enrollment. Device: " + device.toString()); } device.setEnrolmentInfo(enrollment); DeviceManagementDAOFactory.commitTransaction(); @@ -380,8 +378,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while adding metadata of '" + device.getType() + - "' device carrying the identifier '" + device.getDeviceIdentifier() + "'"; + String msg = "Error occurred while adding metadata of device: " + device.toString(); log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { @@ -389,7 +386,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred while enrolling device: " + device.getId(); + String msg = "Error occurred while enrolling device: " + device.toString(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally {