Merge branch 'user-claim-attribute' into 'master'

Add user claim attribute programmatically

See merge request entgra/carbon-device-mgt!525
reporting
Dharmakeerthi Lasantha 5 years ago
commit aa76a10756

@ -417,5 +417,11 @@
<artifactId>powermock-api-mockito</artifactId> <artifactId>powermock-api-mockito</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.claim.metadata.mgt</artifactId>
<version>${carbon.identity.framework.version}</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -67,6 +67,11 @@ import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.identity.claim.metadata.mgt.ClaimMetadataManagementAdminService;
import org.wso2.carbon.identity.claim.metadata.mgt.dto.AttributeMappingDTO;
import org.wso2.carbon.identity.claim.metadata.mgt.dto.ClaimPropertyDTO;
import org.wso2.carbon.identity.claim.metadata.mgt.dto.LocalClaimDTO;
import org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException;
import org.wso2.carbon.identity.user.store.count.UserStoreCountRetriever; import org.wso2.carbon.identity.user.store.count.UserStoreCountRetriever;
import org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException; import org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException;
import org.wso2.carbon.user.api.Permission; import org.wso2.carbon.user.api.Permission;
@ -934,6 +939,12 @@ public class UserManagementServiceImpl implements UserManagementService {
@PathParam("username") String username, @PathParam("username") String username,
JsonArray deviceList) { JsonArray deviceList) {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
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() RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getUserRealm() .getUserRealm()
.getRealmConfiguration(); .getRealmConfiguration();
@ -942,14 +953,31 @@ public class UserManagementServiceImpl implements UserManagementService {
if (!StringUtils.isBlank(domain)) { if (!StringUtils.isBlank(domain)) {
username = domain + Constants.FORWARD_SLASH + username; username = domain + Constants.FORWARD_SLASH + username;
} }
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); ClaimMetadataManagementAdminService
if (!userStoreManager.isExistingUser(username)) { claimMetadataManagementAdminService = new ClaimMetadataManagementAdminService();
if (log.isDebugEnabled()) { //Get all available claim URIs
log.debug("User by username: " + username + " does not exist."); String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris();
} //Check they contains a claim attribute for external devices
return Response.status(Response.Status.NOT_FOUND).entity( if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) {
new ErrorResponse.ErrorResponseBuilder().setMessage( List<ClaimPropertyDTO> claimPropertyDTOList = new ArrayList<>();
"User doesn't exist.").build()).build(); claimPropertyDTOList
.add(DeviceMgtAPIUtils.buildClaimPropertyDTO
(Constants.ATTRIBUTE_DISPLAY_NAME, Constants.EXTERNAL_DEVICE_CLAIM_DISPLAY_NAME));
claimPropertyDTOList
.add(DeviceMgtAPIUtils.buildClaimPropertyDTO
(Constants.ATTRIBUTE_DESCRIPTION, Constants.EXTERNAL_DEVICE_CLAIM_DESCRIPTION));
LocalClaimDTO localClaimDTO = new LocalClaimDTO();
localClaimDTO.setLocalClaimURI(Constants.USER_CLAIM_DEVICES);
localClaimDTO.setClaimProperties(claimPropertyDTOList.toArray(
new ClaimPropertyDTO[claimPropertyDTOList.size()]));
AttributeMappingDTO attributeMappingDTO = new AttributeMappingDTO();
attributeMappingDTO.setAttributeName(Constants.DEVICES);
attributeMappingDTO.setUserStoreDomain(domain);
localClaimDTO.setAttributeMappings(new AttributeMappingDTO[]{attributeMappingDTO});
claimMetadataManagementAdminService.addLocalClaim(localClaimDTO);
} }
Map<String, String> userClaims = Map<String, String> userClaims =
this.buildExternalDevicesUserClaims(username, domain, deviceList, userStoreManager); this.buildExternalDevicesUserClaims(username, domain, deviceList, userStoreManager);
@ -958,8 +986,11 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occurred while updating external device claims of the user '" + username + "'"; String msg = "Error occurred while updating external device claims of the user '" + username + "'";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } catch (ClaimMetadataException e) {
String msg = "Error occurred while adding claim attribute";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }
} }
@ -969,6 +1000,13 @@ public class UserManagementServiceImpl implements UserManagementService {
public Response getUserClaimsForDevices( public Response getUserClaimsForDevices(
@PathParam("username") String username) { @PathParam("username") String username) {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
Map<String, String> claims = new HashMap<>();
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() RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getUserRealm() .getUserRealm()
.getRealmConfiguration(); .getRealmConfiguration();
@ -977,23 +1015,20 @@ public class UserManagementServiceImpl implements UserManagementService {
if (!StringUtils.isBlank(domain)) { if (!StringUtils.isBlank(domain)) {
username = domain + Constants.FORWARD_SLASH + username; username = domain + Constants.FORWARD_SLASH + username;
} }
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris();
if (!userStoreManager.isExistingUser(username)) { if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("User by username: " + username + " does not exist."); log.debug("Claim attribute for external device doesn't exist.");
} }
return Response.status(Response.Status.NOT_FOUND).entity( return Response.status(Response.Status.OK).entity(claims).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(
"User doesn't exist.").build()).build();
} }
String[] claimArray = {Constants.USER_CLAIM_DEVICES}; String[] claimArray = {Constants.USER_CLAIM_DEVICES};
Map<String, String> claims = userStoreManager.getUserClaimValues(username, claimArray, domain); claims = userStoreManager.getUserClaimValues(username, claimArray, domain);
return Response.status(Response.Status.OK).entity(claims).build(); return Response.status(Response.Status.OK).entity(claims).build();
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occurred while retrieving external device claims of the user '" + username + "'"; String msg = "Error occurred while retrieving external device claims of the user '" + username + "'";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }
@ -1003,6 +1038,13 @@ public class UserManagementServiceImpl implements UserManagementService {
public Response deleteUserClaimsForDevices( public Response deleteUserClaimsForDevices(
@PathParam("username") String username) { @PathParam("username") String username) {
try { try {
String[] claimArray = new String[1];
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
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() RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getUserRealm() .getUserRealm()
.getRealmConfiguration(); .getRealmConfiguration();
@ -1011,16 +1053,14 @@ public class UserManagementServiceImpl implements UserManagementService {
if (!StringUtils.isBlank(domain)) { if (!StringUtils.isBlank(domain)) {
username = domain + Constants.FORWARD_SLASH + username; username = domain + Constants.FORWARD_SLASH + username;
} }
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris();
if (!userStoreManager.isExistingUser(username)) { if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("User by username: " + username + " does not exist."); log.debug("Claim attribute for external device doesn't exist.");
} }
return Response.status(Response.Status.NOT_FOUND).entity( return Response.status(Response.Status.OK).entity(claimArray).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(
"User doesn't exist.").build()).build();
} }
String[] claimArray = {Constants.USER_CLAIM_DEVICES}; claimArray[0] = Constants.USER_CLAIM_DEVICES;
userStoreManager.deleteUserClaimValues( userStoreManager.deleteUserClaimValues(
username, username,
claimArray, claimArray,
@ -1029,8 +1069,7 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occurred while deleting external device claims of the user '" + username + "'"; String msg = "Error occurred while deleting external device claims of the user '" + username + "'";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }

@ -69,6 +69,11 @@ public class Constants {
public static final String NOTNOW = "notnow"; public static final String NOTNOW = "notnow";
public static final String REPEATED = "repeated"; public static final String REPEATED = "repeated";
} }
public static final String DEVICES = "devices";
public static final String ATTRIBUTE_DISPLAY_NAME = "DisplayName";
public static final String ATTRIBUTE_DESCRIPTION = "Description";
public static final String EXTERNAL_DEVICE_CLAIM_DISPLAY_NAME = "Devices";
public static final String EXTERNAL_DEVICE_CLAIM_DESCRIPTION = "Device list";
public final class ErrorMessages { public final class ErrorMessages {
private ErrorMessages () { throw new AssertionError(); } private ErrorMessages () { throw new AssertionError(); }

@ -67,6 +67,7 @@ import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub;
import org.wso2.carbon.event.publisher.stub.EventPublisherAdminServiceStub; import org.wso2.carbon.event.publisher.stub.EventPublisherAdminServiceStub;
import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub; import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub;
import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub; import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub;
import org.wso2.carbon.identity.claim.metadata.mgt.dto.ClaimPropertyDTO;
import org.wso2.carbon.identity.jwt.client.extension.JWTClient; import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
@ -863,4 +864,18 @@ public class DeviceMgtAPIUtils {
} }
return operation; return operation;
} }
/**
* This method is used to set property name and value to ClaimPropertyDTO
*
* @param propertyName Name of the property
* @param propertyValue Value of the property
* @return {@link ClaimPropertyDTO}
*/
public static ClaimPropertyDTO buildClaimPropertyDTO(String propertyName, String propertyValue) {
ClaimPropertyDTO claimPropertyDTO = new ClaimPropertyDTO();
claimPropertyDTO.setPropertyName(propertyName);
claimPropertyDTO.setPropertyValue(propertyValue);
return claimPropertyDTO;
}
} }

Loading…
Cancel
Save