From 1bb18f30c9a4cc19117c2f19d7b512f662be05b0 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Tue, 10 Mar 2020 22:06:08 +0530 Subject: [PATCH] Add get tenant API --- .../admin/DeviceManagementAdminService.java | 57 ++++++++++++ .../DeviceManagementAdminServiceImpl.java | 54 ++++++++++++ .../mgt/common/general/TenantDetail.java | 88 +++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/general/TenantDetail.java 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 29d8a2cb43..d5695b519a 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 @@ -49,6 +49,7 @@ 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; @@ -100,6 +101,12 @@ 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"} ) } ) @@ -353,4 +360,54 @@ public interface DeviceManagementAdminService { value = "List of device identifiers.", 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(); } 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 9533c43a42..dccd5f69d2 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,13 +45,19 @@ 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.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; @@ -62,6 +68,7 @@ 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") @@ -189,4 +196,51 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } + + @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(); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/general/TenantDetail.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/general/TenantDetail.java new file mode 100644 index 0000000000..870207fb5d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/general/TenantDetail.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2019, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.mgt.common.general; + +public class TenantDetail { + + private int id; + private String domain; + + private String adminName; + private String adminFullName; + private String adminFirstName; + private String adminLastName; + private String email; + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } + + public String getAdminName() { + return adminName; + } + + public void setAdminName(String adminName) { + this.adminName = adminName; + } + + public String getAdminFullName() { + return adminFullName; + } + + public void setAdminFullName(String adminFullName) { + this.adminFullName = adminFullName; + } + + public String getAdminFirstName() { + return adminFirstName; + } + + public void setAdminFirstName(String adminFirstName) { + this.adminFirstName = adminFirstName; + } + + public String getAdminLastName() { + return adminLastName; + } + + public void setAdminLastName(String adminLastName) { + this.adminLastName = adminLastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + +}