From 6a6254644039a3f36ad41de62a2ef661b71b3608 Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Thu, 5 May 2022 23:51:46 +0530 Subject: [PATCH] Add optional user domain query param to user APIs --- .../service/api/UserManagementService.java | 50 ++++--- .../api/admin/UserManagementAdminService.java | 8 +- .../impl/UserManagementServiceImpl.java | 129 +++++++++++------- .../admin/UserManagementAdminServiceImpl.java | 8 +- .../impl/UserManagementServiceImplTest.java | 4 +- 5 files changed, 127 insertions(+), 72 deletions(-) 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 99fbf8eee69..e557ff7b433 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 @@ -254,7 +254,6 @@ public interface UserManagementService { required = true) UserInfo user); @GET - @Path("/{username}") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -310,7 +309,7 @@ public interface UserManagementService { value = "Provide the username of the user.", required = true, defaultValue = "admin") - @PathParam("username") String username, + @QueryParam("username") String username, @ApiParam( name = "domain", value = "The domain name of the user store.", @@ -325,7 +324,6 @@ public interface UserManagementService { @HeaderParam("If-Modified-Since") String ifModifiedSince); @PUT - @Path("/{username}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -380,7 +378,7 @@ public interface UserManagementService { value = "The username of the user.", required = true, defaultValue = "admin") - @PathParam("username") String username, + @QueryParam("username") String username, @ApiParam( name = "domain", value = "The domain name of the user store.", @@ -393,7 +391,6 @@ public interface UserManagementService { required = true) UserInfo userData); @DELETE - @Path("/{username}") @Consumes(MediaType.WILDCARD) @ApiOperation( httpMethod = "DELETE", @@ -431,7 +428,7 @@ public interface UserManagementService { "that user. Do not remove the admin user.", required = true, defaultValue = "[Create a new user named Jim, and then try out this API.]") - @PathParam("username") String username, + @QueryParam("username") String username, @ApiParam( name = "domain", value = "The domain name of the user store.", @@ -439,7 +436,7 @@ public interface UserManagementService { @QueryParam("domain") String domain); @GET - @Path("/{username}/roles") + @Path("/roles") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -495,7 +492,7 @@ public interface UserManagementService { value = "The username of the user.", required = true, defaultValue = "admin") - @PathParam("username") String username, + @QueryParam("username") String username, @ApiParam( name = "domain", value = "The domain name of the user store.", @@ -503,6 +500,7 @@ public interface UserManagementService { @QueryParam("domain") String domain); @GET + @Path("/list") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -573,7 +571,12 @@ public interface UserManagementService { value = "Provide how many user details you require from the starting pagination index/offset.", required = false, defaultValue = "5") - @QueryParam("limit") int limit); + @QueryParam("limit") int limit, + @ApiParam( + name = "domain", + value = "The domain name of the user store.", + required = false) + @QueryParam("domain") String domain); @GET @Path(("/search")) @@ -1034,7 +1037,7 @@ public interface UserManagementService { @HeaderParam("If-Modified-Since") String ifModifiedSince); @PUT - @Path("/claims/{username}") + @Path("/claims") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", @@ -1082,14 +1085,19 @@ public interface UserManagementService { value = "Provide the username of the user.", required = true, defaultValue = "admin") - @PathParam("username") String username, + @QueryParam("username") String username, @ApiParam( name = "device list", value = "Array of objects with device details", - required = true) JsonArray deviceList); + required = true) JsonArray deviceList, + @ApiParam( + name = "domain", + value = "The domain name of the user store.", + required = false) + @QueryParam("domain") String domain); @GET - @Path("/claims/{username}") + @Path("/claims") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -1137,10 +1145,15 @@ public interface UserManagementService { value = "Provide the username of the user.", required = true, defaultValue = "admin") - @PathParam("username") String username); + @QueryParam("username") String username, + @ApiParam( + name = "domain", + value = "The domain name of the user store.", + required = false) + @QueryParam("domain") String domain); @DELETE - @Path("/claims/{username}") + @Path("/claims") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "DELETE", @@ -1188,7 +1201,12 @@ public interface UserManagementService { value = "Provide the username of the user.", required = true, defaultValue = "admin") - @PathParam("username") String username); + @QueryParam("username") String username, + @ApiParam( + name = "domain", + value = "The domain name of the user store.", + required = false) + @QueryParam("domain") String domain); @GET @Path("/current-user/permissions") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java index 29260f31e08..f5013182d7a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java @@ -74,7 +74,7 @@ import javax.ws.rs.core.Response; public interface UserManagementAdminService { @POST - @Path("/{username}/credentials") + @Path("/credentials") @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -115,7 +115,7 @@ public interface UserManagementAdminService { value = "The username of the user." + "INFO: Add a new user using the POST /users API that is under User Management.", required = true) - @PathParam("username") + @QueryParam("username") @Size(max = 45) String username, @ApiParam( @@ -131,7 +131,7 @@ public interface UserManagementAdminService { @DELETE - @Path("/{username}/devices") + @Path("/devices") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -183,7 +183,7 @@ public interface UserManagementAdminService { name = "username", value = "Username of the users devices to be deleted.", required = true) - @PathParam("username") String username); + @QueryParam("username") String username); 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 f39c4cd1f4e..6316e6a41d2 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 @@ -270,9 +270,8 @@ public class UserManagementServiceImpl implements UserManagementService { } @GET - @Path("/{username}") @Override - public Response getUser(@PathParam("username") String username, @QueryParam("domain") String domain, + public Response getUser(@QueryParam("username") String username, @QueryParam("domain") String domain, @HeaderParam("If-Modified-Since") String ifModifiedSince) { if (domain != null && !domain.isEmpty()) { username = domain + '/' + username; @@ -299,9 +298,8 @@ public class UserManagementServiceImpl implements UserManagementService { } @PUT - @Path("/{username}") @Override - public Response updateUser(@PathParam("username") String username, @QueryParam("domain") String domain, UserInfo userInfo) { + public Response updateUser(@QueryParam("username") String username, @QueryParam("domain") String domain, UserInfo userInfo) { if (domain != null && !domain.isEmpty()) { username = domain + '/' + username; } @@ -378,10 +376,9 @@ public class UserManagementServiceImpl implements UserManagementService { } @DELETE - @Path("/{username}") @Consumes(MediaType.WILDCARD) @Override - public Response removeUser(@PathParam("username") String username, @QueryParam("domain") String domain) { + public Response removeUser(@QueryParam("username") String username, @QueryParam("domain") String domain) { if (domain != null && !domain.isEmpty()) { username = domain + '/' + username; } @@ -413,9 +410,9 @@ public class UserManagementServiceImpl implements UserManagementService { } @GET - @Path("/{username}/roles") + @Path("/roles") @Override - public Response getRolesOfUser(@PathParam("username") String username, @QueryParam("domain") String domain) { + public Response getRolesOfUser(@QueryParam("username") String username, @QueryParam("domain") String domain) { if (domain != null && !domain.isEmpty()) { username = domain + '/' + username; } @@ -442,10 +439,11 @@ public class UserManagementServiceImpl implements UserManagementService { } @GET + @Path("/list") @Override public Response getUsers(@QueryParam("filter") String filter, @HeaderParam("If-Modified-Since") String timestamp, - @QueryParam("offset") int offset, - @QueryParam("limit") int limit) { + @QueryParam("offset") int offset, @QueryParam("limit") int limit, + @QueryParam("domain") String domain) { if (log.isDebugEnabled()) { log.debug("Getting the list of users with all user-related information"); } @@ -464,8 +462,11 @@ public class UserManagementServiceImpl implements UserManagementService { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); //As the listUsers function accepts limit only to accommodate offset we are passing offset + limit - String[] users = userStoreManager.listUsers(appliedFilter, appliedLimit); - userList = new ArrayList<>(users.length); + List users = Arrays.asList(userStoreManager.listUsers(appliedFilter, appliedLimit)); + if (domain != null && !domain.isEmpty()) { + users = getUsersFromDomain(domain, users); + } + userList = new ArrayList<>(users.size()); BasicUserInfo user; for (String username : users) { user = getBasicUserInfo(username); @@ -487,7 +488,7 @@ public class UserManagementServiceImpl implements UserManagementService { } BasicUserInfoList result = new BasicUserInfoList(); result.setList(offsetList); - result.setCount(users.length); + result.setCount(userList.size()); return Response.status(Response.Status.OK).entity(result).build(); } catch (UserStoreException e) { @@ -507,7 +508,7 @@ public class UserManagementServiceImpl implements UserManagementService { @QueryParam("limit") int limit) { if (RequestValidationUtil.isNonFilterRequest(username,firstName, lastName, emailAddress)) { - return getUsers(null, timestamp, offset, limit); + return getUsers(null, timestamp, offset, limit, null); } RequestValidationUtil.validatePaginationParameters(offset, limit); @@ -693,7 +694,12 @@ public class UserManagementServiceImpl implements UserManagementService { List userList; try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); - String[] users = userStoreManager.listUsers(userStoreDomain + "/" + filter + "*", limit); + String[] users; + if (userStoreDomain.equals("all")) { + users = userStoreManager.listUsers(filter + "*", limit); + } else { + users = userStoreManager.listUsers(userStoreDomain + "/" + filter + "*", limit); + } userList = new ArrayList<>(); UserInfo user; for (String username : users) { @@ -919,25 +925,29 @@ public class UserManagementServiceImpl implements UserManagementService { @PUT @Override - @Path("/claims/{username}") + @Path("/claims") public Response updateUserClaimsForDevices( - @PathParam("username") String username, - JsonArray deviceList) { + @QueryParam("username") String username, JsonArray deviceList, + @QueryParam("domain") String domain) { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + if (domain != null && !domain.isEmpty()) { + username = domain + Constants.FORWARD_SLASH + username; + } else { + RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getUserRealm() + .getRealmConfiguration(); + domain = realmConfiguration + .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME); + if (!StringUtils.isBlank(domain)) { + username = domain + Constants.FORWARD_SLASH + username; + } + } if (!userStoreManager.isExistingUser(username)) { String msg = "User by username: " + username + " does not exist."; log.error(msg); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } - RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() - .getUserRealm() - .getRealmConfiguration(); - String domain = realmConfiguration - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME); - if (!StringUtils.isBlank(domain)) { - username = domain + Constants.FORWARD_SLASH + username; - } ClaimMetadataManagementAdminService claimMetadataManagementAdminService = new ClaimMetadataManagementAdminService(); //Get all available claim URIs @@ -981,25 +991,29 @@ public class UserManagementServiceImpl implements UserManagementService { @GET @Override - @Path("/claims/{username}") + @Path("/claims") public Response getUserClaimsForDevices( - @PathParam("username") String username) { + @QueryParam("username") String username, @QueryParam("domain") String domain) { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); Map claims = new HashMap<>(); + if (domain != null && !domain.isEmpty()) { + username = domain + Constants.FORWARD_SLASH + username; + } else { + RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getUserRealm() + .getRealmConfiguration(); + domain = realmConfiguration + .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME); + if (!StringUtils.isBlank(domain)) { + username = domain + Constants.FORWARD_SLASH + username; + } + } if (!userStoreManager.isExistingUser(username)) { String msg = "User by username: " + username + " does not exist."; log.error(msg); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } - RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() - .getUserRealm() - .getRealmConfiguration(); - String domain = realmConfiguration - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME); - if (!StringUtils.isBlank(domain)) { - username = domain + Constants.FORWARD_SLASH + username; - } String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris(); if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) { if (log.isDebugEnabled()) { @@ -1019,25 +1033,29 @@ public class UserManagementServiceImpl implements UserManagementService { @DELETE @Override - @Path("/claims/{username}") + @Path("/claims") public Response deleteUserClaimsForDevices( - @PathParam("username") String username) { + @QueryParam("username") String username, @QueryParam("domain") String domain) { try { String[] claimArray = new String[1]; UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + if (domain != null && !domain.isEmpty()) { + username = domain + Constants.FORWARD_SLASH + username; + } else { + RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getUserRealm() + .getRealmConfiguration(); + domain = realmConfiguration + .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME); + if (!StringUtils.isBlank(domain)) { + username = domain + Constants.FORWARD_SLASH + username; + } + } if (!userStoreManager.isExistingUser(username)) { String msg = "User by username: " + username + " does not exist."; log.error(msg); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } - RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() - .getUserRealm() - .getRealmConfiguration(); - String domain = realmConfiguration - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME); - if (!StringUtils.isBlank(domain)) { - username = domain + Constants.FORWARD_SLASH + username; - } String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris(); if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) { if (log.isDebugEnabled()) { @@ -1306,4 +1324,23 @@ public class UserManagementServiceImpl implements UserManagementService { userStoreList.setCount(userStores.size()); return Response.status(Response.Status.OK).entity(userStoreList).build(); } + + /** + * Iterates through the list of all users and returns a list of users from the specified user store domain + * @param domain user store domain name + * @param users list of all users from UserStoreManager + * @return list of users from specified user store domain + */ + public List getUsersFromDomain(String domain, List users) { + List userList = new ArrayList<>(); + for(String username : users) { + String[] domainName = username.split("/"); + if(domain.equals(Constants.PRIMARY_USER_STORE) && domainName.length == 1) { + userList.add(username); + } else if (domainName[0].equals(domain) && domainName.length > 1) { + userList.add(username); + } + } + return userList; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index b4cf3a02bbc..4634d40c6d4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -40,9 +40,9 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic private static final Log log = LogFactory.getLog(UserManagementAdminServiceImpl.class); @POST - @Path("/{username}/credentials") + @Path("/credentials") @Override - public Response resetUserPassword(@PathParam("username") + public Response resetUserPassword(@QueryParam("username") @Size(max = 45) String user, @QueryParam("domain") String domain, PasswordResetWrapper credentials) { if (domain != null && !domain.isEmpty()) { @@ -52,9 +52,9 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic } @DELETE - @Path("/{username}/devices") + @Path("/devices") @Override - public Response deleteDeviceOfUser(@PathParam("username") String username) { + public Response deleteDeviceOfUser(@QueryParam("username") String username) { try { DeviceMgtAPIUtils.getPrivacyComplianceProvider().deleteDevicesOfUser(username); return Response.status(Response.Status.OK).build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImplTest.java index aa2f84733fa..4bbdc09db20 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImplTest.java @@ -258,7 +258,7 @@ public class UserManagementServiceImplTest { public void testGetUsers() { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(userStoreManager); - Response response = userManagementService.getUsers(null, "00", 0, 10); + Response response = userManagementService.getUsers(null, "00", 0, 10, null); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetUsers request failed"); } @@ -383,7 +383,7 @@ public class UserManagementServiceImplTest { .getUserClaimValue(Mockito.any(), Mockito.any(), Mockito.any()); Mockito.doThrow(new UserStoreException()).when(userStoreManager) .listUsers(Mockito.anyString(), Mockito.anyInt()); - Response response = userManagementService.getUsers(TEST_USERNAME, "00", 0, 10); + Response response = userManagementService.getUsers(TEST_USERNAME, "00", 0, 10, null); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for a users retrieval request."); response = userManagementService.getUserCount();