Add user removable status to basic user info

master
Rajitha Kumara 8 months ago
parent 3092737268
commit cd56147da9

@ -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;
}
}

@ -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);

@ -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");
}

Loading…
Cancel
Save