From cd56147da9909f043b2204cf367e0553201a10db Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Thu, 7 Mar 2024 06:25:27 +0530 Subject: [PATCH] Add user removable status to basic user info --- .../mgt/api/jaxrs/beans/BasicUserInfo.java | 9 ++++++ .../impl/UserManagementServiceImpl.java | 23 ++++++++++---- .../impl/UserManagementServiceImplTest.java | 30 +++++++++++++++---- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfo.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfo.java index fa1e5612ce..56499d4e98 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfo.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfo.java @@ -35,6 +35,8 @@ public class BasicUserInfo { private String createdDate; @ApiModelProperty(name = "modifiedDate", value = "User modifiedDate date." ) private String modifiedDate; + @ApiModelProperty(name = "isRemovable", value = "User's removable status." ) + private boolean isRemovable; public String getUsername() { return username; @@ -84,4 +86,11 @@ public class BasicUserInfo { this.modifiedDate = modifiedDate; } + public boolean isRemovable() { + return isRemovable; + } + + public void setRemovable(boolean removable) { + isRemovable = removable; + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java index 820503eb30..748989879f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java @@ -252,7 +252,7 @@ public class UserManagementServiceImpl implements UserManagementService { BasicUserInfo user = this.getBasicUserInfo(username); return Response.status(Response.Status.OK).entity(user).build(); - } catch (UserStoreException e) { + } catch (UserStoreException | DeviceManagementException e) { String msg = "Error occurred while retrieving information of the user '" + username + "'"; log.error(msg, e); return Response.serverError().entity( @@ -334,7 +334,7 @@ public class UserManagementServiceImpl implements UserManagementService { BasicUserInfo updatedUserInfo = this.getBasicUserInfo(username); return Response.ok().entity(updatedUserInfo).build(); - } catch (UserStoreException e) { + } catch (UserStoreException | DeviceManagementException e) { String msg = "Error occurred while trying to update user '" + username + "'"; log.error(msg, e); return Response.serverError().entity( @@ -495,7 +495,7 @@ public class UserManagementServiceImpl implements UserManagementService { result.setCount(userList.size()); return Response.status(Response.Status.OK).entity(result).build(); - } catch (UserStoreException e) { + } catch (UserStoreException | DeviceManagementException e) { String msg = "Error occurred while retrieving the list of users."; log.error(msg, e); return Response.serverError().entity( @@ -574,6 +574,7 @@ public class UserManagementServiceImpl implements UserManagementService { basicUserInfo.setEmailAddress(getClaimValue(user, Constants.USER_CLAIM_EMAIL_ADDRESS)); basicUserInfo.setFirstname(getClaimValue(user, Constants.USER_CLAIM_FIRST_NAME)); basicUserInfo.setLastname(getClaimValue(user, Constants.USER_CLAIM_LAST_NAME)); + basicUserInfo.setRemovable(isUserRemovable(username)); filteredUserList.add(basicUserInfo); } } @@ -598,7 +599,7 @@ public class UserManagementServiceImpl implements UserManagementService { result.setCount(commonUsers != null ? commonUsers.size() : 0); return Response.status(Response.Status.OK).entity(result).build(); - } catch (UserStoreException e) { + } catch (UserStoreException | DeviceManagementException e) { String msg = "Error occurred while retrieving the list of users."; log.error(msg, e); return Response.serverError().entity( @@ -1249,7 +1250,7 @@ public class UserManagementServiceImpl implements UserManagementService { return initialUserPassword.toString(); } - private BasicUserInfo getBasicUserInfo(String username) throws UserStoreException { + private BasicUserInfo getBasicUserInfo(String username) throws UserStoreException, DeviceManagementException { BasicUserInfo userInfo = new BasicUserInfo(); userInfo.setUsername(username); userInfo.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); @@ -1257,9 +1258,21 @@ public class UserManagementServiceImpl implements UserManagementService { userInfo.setLastname(getClaimValue(username, Constants.USER_CLAIM_LAST_NAME)); userInfo.setCreatedDate(getClaimValue(username, Constants.USER_CLAIM_CREATED)); userInfo.setModifiedDate(getClaimValue(username, Constants.USER_CLAIM_MODIFIED)); + userInfo.setRemovable(isUserRemovable(username)); return userInfo; } + /** + * Check if the user can be removed or not + * @param username Username of the user + * @return True when user can be removed, otherwise false + * @throws DeviceManagementException Throws when error occurred while getting device count + */ + private boolean isUserRemovable(String username) throws DeviceManagementException { + DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); + return deviceManagementProviderService.getDeviceCount(username.contains("/") ? username.split("/")[1] : username) == 0; + } + private String getClaimValue(String username, String claimUri) throws UserStoreException { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); return userStoreManager.getUserClaimValue(username, claimUri, null); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java index 3c5e5e3fa1..f25a37c9e7 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java @@ -119,8 +119,9 @@ public class UserManagementServiceImplTest { PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) .toReturn(carbonContext); Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); - Mockito.when(carbonContext.getUsername()).thenReturn("admin"); Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); + Mockito.when(carbonContext.getUsername()).thenReturn("admin"); + Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME); Mockito.doAnswer(new Answer() { private int count = 0; @@ -156,9 +157,17 @@ public class UserManagementServiceImplTest { @Test(description = "This method tests the getUser method of UserManagementService", dependsOnMethods = "testAddUser") - public void testGetUser() throws UserStoreException { + public void testGetUser() throws UserStoreException, DeviceManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); + Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME); Response response = userManagementService.getUser(TEST_USERNAME, null, null); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "User retrieval failed"); BasicUserInfo userInfo = (BasicUserInfo) response.getEntity(); @@ -173,15 +182,18 @@ public class UserManagementServiceImplTest { @Test(description = "This method tests the updateUser method of UserManagementService", dependsOnMethods = {"testGetUser"}) - public void testUpdateUser() throws UserStoreException { + public void testUpdateUser() throws UserStoreException, DeviceManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) .toReturn(carbonContext); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); - Mockito.when(carbonContext.getUsername()).thenReturn("admin"); Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); + Mockito.when(carbonContext.getUsername()).thenReturn("admin"); + Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME); Response response = userManagementService.updateUser(TEST2_USERNAME, null, null); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Non-existing user was successfully updated"); @@ -250,10 +262,18 @@ public class UserManagementServiceImplTest { @Test(description = "This method tests the getUsers method of UserManagementService", dependsOnMethods = {"testGetUserNames"}) - public void testGetUsers() { + public void testGetUsers() throws DeviceManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); Response response = userManagementService.getUsers(null, "00", 0, 10, null); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); + Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetUsers request failed"); } -- 2.36.3