diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java index 6097b4e43ad..ce403fa2813 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java @@ -288,12 +288,12 @@ public interface UserManagementService { httpMethod = "GET", value = "Get user list", notes = "If you wish to get the details of all the users registered with EMM, you can do so " - + "using the REST API", + + "using this REST API", tags = "User Management") @ApiResponses(value = { @ApiResponse( code = 200, - message = "OK. \n Successfully fetched the requested role.", + message = "OK. \n Successfully fetched the requested users.", response = BasicUserInfoList.class, responseHeaders = { @ResponseHeader( @@ -343,6 +343,36 @@ public interface UserManagementService { required = false) @QueryParam("limit") int limit); + @GET + @Path("/count") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get user count", + notes = "If you wish to get the user count registered with EMM, you can do so using this REST API", + tags = "User Management") + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the user count.", + response = BasicUserInfoList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body") + }), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the user list.", + response = ErrorResponse.class) + }) + @Permission(name = "View Users", permission = "/device-mgt/users/view") + Response getUserCount(); + @GET @Path("/search/usernames") @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 6df497f8c1a..df34771125c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -316,6 +316,26 @@ public class UserManagementServiceImpl implements UserManagementService { } } + @GET + @Path("/count") + @Override + public Response getUserCount() { + if (log.isDebugEnabled()) { + log.debug("Getting the user count"); + } + + try { + UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + int userCount = userStoreManager.listUsers("*", -1).length; + return Response.status(Response.Status.OK).entity(userCount).build(); + } catch (UserStoreException e) { + String msg = "Error occurred while retrieving the user count."; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + } + @GET @Path("/search/usernames") @Override diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index a3f9976fefb..1f62c3fccfd 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -3,7 +3,8 @@ CREATE TABLE DM_DEVICE_TYPE ( NAME VARCHAR(300) DEFAULT NULL, PROVIDER_TENANT_ID INTEGER NULL, SHARED_WITH_ALL_TENANTS BIT NOT NULL DEFAULT 0, - PRIMARY KEY (ID) + PRIMARY KEY (ID), + CONSTRAINT DEVICE_TYPE_NAME UNIQUE(NAME) ); CREATE INDEX IDX_DEVICE_TYPE ON DM_DEVICE_TYPE (NAME); @@ -122,12 +123,12 @@ CREATE TABLE DM_PROFILE ( ID INTEGER IDENTITY(1,1) NOT NULL, PROFILE_NAME VARCHAR(45) NOT NULL , TENANT_ID INTEGER NOT NULL , - DEVICE_TYPE_ID INTEGER NOT NULL , + DEVICE_TYPE VARCHAR(300) NOT NULL , CREATED_TIME DATETIME NOT NULL , UPDATED_TIME DATETIME NOT NULL , PRIMARY KEY (ID) , - CONSTRAINT DM_PROFILE_DEVICE_TYPE FOREIGN KEY (DEVICE_TYPE_ID) REFERENCES - DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + CONSTRAINT DM_PROFILE_DEVICE_TYPE FOREIGN KEY (DEVICE_TYPE) REFERENCES + DM_DEVICE_TYPE (NAME) ON DELETE NO ACTION ON UPDATE NO ACTION ); CREATE TABLE DM_POLICY (