Merge branch 'master' into 'master'

Fix tenant details retrieving and permission assigning for reporting

See merge request entgra/carbon-device-mgt!532
reporting
Charitha Goonetilleke 5 years ago
commit 428deeb459

@ -34,16 +34,19 @@ import io.swagger.annotations.Tag;
import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scope;
import org.wso2.carbon.apimgt.annotations.api.Scopes; 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.configuration.mgt.DeviceConfiguration;
import org.wso2.carbon.device.mgt.common.general.TenantDetail;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam; import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List;
@SwaggerDefinition( @SwaggerDefinition(
info = @Info( info = @Info(
@ -53,15 +56,14 @@ import javax.ws.rs.core.Response;
@Extension(properties = { @Extension(properties = {
@ExtensionProperty(name = "name", value = "DeviceManagementConfiguration"), @ExtensionProperty(name = "name", value = "DeviceManagementConfiguration"),
@ExtensionProperty(name = "context", @ExtensionProperty(name = "context",
value = "/api/device-mgt-config/v1.0/configurations"), value = "/api/device-mgt-config/v1.0"),
}) })
} }
), ),
tags = { tags = {
@Tag(name = "device_management", description = "") @Tag(name = "device_management", description = "Device management configuration service")
} }
) )
@Path("/configurations")
@Api(value = "Device Management Configuration") @Api(value = "Device Management Configuration")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Scopes(scopes = { @Scopes(scopes = {
@ -76,12 +78,25 @@ import javax.ws.rs.core.Response;
description = "", description = "",
key = "perm:manage-configuration", key = "perm:manage-configuration",
permissions = {"/device-mgt/platform-configurations/manage"} 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 { public interface DeviceManagementConfigService {
@GET @GET
@Path("/configurations")
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "GET", httpMethod = "GET",
@ -140,7 +155,7 @@ public interface DeviceManagementConfigService {
String properties); String properties);
@PUT @PUT
@Path("/transfer") @Path("/device/transfer")
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT", httpMethod = "PUT",
@ -196,7 +211,7 @@ public interface DeviceManagementConfigService {
DeviceTransferRequest deviceTransferRequest); DeviceTransferRequest deviceTransferRequest);
@GET @GET
@Path("/ui-config") @Path("/configurations/ui-config")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ApiOperation( @ApiOperation(
consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,
@ -220,4 +235,96 @@ public interface DeviceManagementConfigService {
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response getUiConfig(); 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<String> permissions);
} }

@ -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 io.entgra.carbon.device.mgt.config.jaxrs.util.DeviceMgtAPIUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials; import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials;
import org.wso2.carbon.device.mgt.common.ApplicationRegistrationException; 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.configuration.mgt.DeviceConfiguration;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; 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.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.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; 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.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; 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.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam; import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; 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.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Path("/configurations")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public class DeviceManagementConfigServiceImpl implements DeviceManagementConfigService { public class DeviceManagementConfigServiceImpl implements DeviceManagementConfigService {
@ -66,6 +74,7 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig
@Override @Override
@GET @GET
@Path("/configurations")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response getConfiguration(@HeaderParam("token") String token, public Response getConfiguration(@HeaderParam("token") String token,
@QueryParam("properties") String properties) { @QueryParam("properties") String properties) {
@ -162,7 +171,7 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig
@GET @GET
@Override @Override
@Consumes("application/json") @Consumes("application/json")
@Path("/ui-config") @Path("/configurations/ui-config")
public Response getUiConfig() { public Response getUiConfig() {
UIConfigurationManager uiConfigurationManager = UIConfigurationManager.getInstance(); UIConfigurationManager uiConfigurationManager = UIConfigurationManager.getInstance();
if (uiConfigurationManager == null) { if (uiConfigurationManager == null) {
@ -212,4 +221,76 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig
} }
} }
@Override
@Path("/tenants")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTenants() {
List<TenantDetail> 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<String> 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();
}
} }

@ -21,23 +21,61 @@ package io.entgra.carbon.device.mgt.config.jaxrs.util;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.user.core.service.RealmService;
/** /**
* MDMAPIUtils class provides utility function used by CDM REST-API classes. * MDMAPIUtils class provides utility function used by CDM REST-API classes.
*/ */
public class DeviceMgtAPIUtils { 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() { public static DeviceManagementProviderService getDeviceManagementService() {
if (deviceManagementProviderService == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
DeviceManagementProviderService deviceManagementProviderService = deviceManagementProviderService =
(DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null); (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
if (deviceManagementProviderService == null) { if (deviceManagementProviderService == null) {
String msg = "DeviceImpl Management provider service has not initialized."; String msg = "Device Management provider service has not initialized.";
log.error(msg); log.error(msg);
throw new IllegalStateException(msg); throw new IllegalStateException(msg);
} }
}
return deviceManagementProviderService; 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;
}
} }

@ -34,29 +34,33 @@
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.api.admin; 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.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses; 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.ResponseHeader;
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.annotations.Tag;
import org.apache.axis2.transport.http.HTTPConstants; import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scope;
import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.apimgt.annotations.api.Scopes;
import org.wso2.carbon.device.mgt.common.Device; 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.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import javax.validation.constraints.Size; 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.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
@ -101,18 +105,6 @@ import java.util.List;
description = "Permanently Delete the device specified by device id", description = "Permanently Delete the device specified by device id",
key = "perm:devices:permanent-delete", key = "perm:devices:permanent-delete",
permissions = {"/device-mgt/admin/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) required = true)
List<String> deviceIdentifiers); List<String> 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<String> permissions);
} }

@ -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.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; 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.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.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; 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.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceManagementAdminService; 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.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; 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.validation.constraints.Size;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam; import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Path("/admin/devices") @Path("/admin/devices")
@ -200,77 +190,4 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
} }
} }
@Override
@Path("/tenants")
@GET
public Response getTenants() {
List<TenantDetail> 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<String> 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();
}
} }

@ -17,6 +17,7 @@
*/ */
package org.wso2.carbon.device.mgt.common; package org.wso2.carbon.device.mgt.common;
import com.google.gson.Gson;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.Application;
@ -187,17 +188,7 @@ public class Device implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "device [" + return new Gson().toJson(this);
"name=" + name + ";" +
"type=" + type + ";" +
"description=" + description + ";" +
"identifier=" + deviceIdentifier + ";" +
// "EnrolmentInfo[" +
// "owner=" + enrolmentInfo.getOwner() + ";" +
// "ownership=" + enrolmentInfo.getOwnership() + ";" +
// "status=" + enrolmentInfo.getStatus() + ";" +
// "]" +
"]";
} }
@Override @Override

@ -318,14 +318,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED. if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED.
equals(existingEnrolmentInfo.getStatus())) { equals(existingEnrolmentInfo.getStatus())) {
enrollment = enrollmentDAO. enrollment = enrollmentDAO
addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); .addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId);
if (enrollment == null ){ if (enrollment == null ){
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException( throw new DeviceManagementException(
"Enrollment data persistence is failed in a re-enrollment. Device id : " "Enrollment data persistence is failed in a re-enrollment. Existing device: "
+ existingDevice.getId() + " Device Identifier: " + device + existingDevice.toString() + ", New Device: " + device.toString());
.getDeviceIdentifier());
} }
device.setEnrolmentInfo(enrollment); device.setEnrolmentInfo(enrollment);
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
@ -339,16 +338,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
status = true; status = true;
} else { } else {
log.warn("Unable to update device enrollment for device : " + device.getDeviceIdentifier() + log.warn("Unable to update device enrollment for device : " + device.toString());
" belonging to user : " + device.getEnrolmentInfo().getOwner());
} }
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); 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); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} catch (Exception 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); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} finally { } finally {
@ -367,8 +366,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (enrollment == null ){ if (enrollment == null ){
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException( throw new DeviceManagementException(
"Enrollment data persistence is failed in a new enrollment. Device id: " + deviceId "Enrollment data persistence is failed in a new enrollment. Device: " + device.toString());
+ " Device Identifier: " + device.getDeviceIdentifier());
} }
device.setEnrolmentInfo(enrollment); device.setEnrolmentInfo(enrollment);
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
@ -380,8 +378,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding metadata of '" + device.getType() + String msg = "Error occurred while adding metadata of device: " + device.toString();
"' device carrying the identifier '" + device.getDeviceIdentifier() + "'";
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
@ -389,7 +386,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} catch (Exception 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); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} finally { } finally {

Loading…
Cancel
Save