Search in sources :

Example 56 with Role

use of org.wso2.charon3.core.objects.Role in project charon by wso2.

the class RoleResourceManager method doPatchRole.

private Role doPatchRole(Role oldRole, SCIMResourceTypeSchema roleSchema, String patchRequest) throws CharonException, BadRequestException, NotImplementedException, InternalErrorException {
    // Make a copy of the original group.
    Role originalRole = (Role) CopyUtil.deepCopy(oldRole);
    Role copyOfOldRole = (Role) CopyUtil.deepCopy(oldRole);
    Role patchedRole = null;
    List<PatchOperation> opList = getDecoder().decodeRequest(patchRequest);
    for (PatchOperation operation : opList) {
        switch(operation.getOperation()) {
            case SCIMConstants.OperationalConstants.ADD:
                if (patchedRole == null) {
                    patchedRole = (Role) PatchOperationUtil.doPatchAdd(operation, getDecoder(), oldRole, copyOfOldRole, roleSchema);
                } else {
                    patchedRole = (Role) PatchOperationUtil.doPatchAdd(operation, getDecoder(), patchedRole, copyOfOldRole, roleSchema);
                }
                copyOfOldRole = (Role) CopyUtil.deepCopy(patchedRole);
                break;
            case SCIMConstants.OperationalConstants.REMOVE:
                if (patchedRole == null) {
                    patchedRole = (Role) PatchOperationUtil.doPatchRemove(operation, oldRole, copyOfOldRole, roleSchema);
                } else {
                    patchedRole = (Role) PatchOperationUtil.doPatchRemove(operation, patchedRole, copyOfOldRole, roleSchema);
                }
                copyOfOldRole = (Role) CopyUtil.deepCopy(patchedRole);
                break;
            case SCIMConstants.OperationalConstants.REPLACE:
                if (patchedRole == null) {
                    patchedRole = (Role) PatchOperationUtil.doPatchReplace(operation, getDecoder(), oldRole, copyOfOldRole, roleSchema);
                } else {
                    patchedRole = (Role) PatchOperationUtil.doPatchReplace(operation, getDecoder(), patchedRole, copyOfOldRole, roleSchema);
                }
                copyOfOldRole = (Role) CopyUtil.deepCopy(patchedRole);
                break;
            default:
                throw new BadRequestException("Unknown operation.", ResponseCodeConstants.INVALID_SYNTAX);
        }
    }
    return (Role) ServerSideValidator.validateUpdatedSCIMObject(originalRole, patchedRole, roleSchema);
}
Also used : Role(org.wso2.charon3.core.objects.Role) PatchOperation(org.wso2.charon3.core.utils.codeutils.PatchOperation) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Example 57 with Role

use of org.wso2.charon3.core.objects.Role in project charon by wso2.

the class RoleResourceManager method updateWithPUTRole.

@Override
public SCIMResponse updateWithPUTRole(String id, String putRequest, RoleManager roleManager) {
    try {
        if (roleManager == null) {
            String error = "Provided role manager is null.";
            throw new InternalErrorException(error);
        }
        JSONEncoder encoder = getEncoder();
        JSONDecoder decoder = getDecoder();
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getRoleResourceSchema();
        Map<String, Boolean> requestAttributes = ResourceManagerUtil.getAllAttributeURIs(schema);
        Role role = decoder.decodeResource(putRequest, schema, new Role());
        Role updatedRole;
        // Retrieve the old object.
        Role oldRole = roleManager.getRole(id, requestAttributes);
        if (oldRole != null) {
            Role newRole = (Role) ServerSideValidator.validateUpdatedSCIMObject(oldRole, role, schema);
            updatedRole = roleManager.updateRole(oldRole, newRole);
        } else {
            String error = "No role exists with the given id: " + id;
            throw new NotFoundException(error);
        }
        return getScimResponse(encoder, updatedRole);
    } catch (NotFoundException | BadRequestException | CharonException | ConflictException | InternalErrorException | NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : ConflictException(org.wso2.charon3.core.exceptions.ConflictException) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) Role(org.wso2.charon3.core.objects.Role) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException)

Example 58 with Role

use of org.wso2.charon3.core.objects.Role in project charon by wso2.

the class RoleResourceManager method getScimResponse.

private SCIMResponse getScimResponse(JSONEncoder encoder, Role updatedRole) throws CharonException, NotFoundException, InternalErrorException {
    String encodedRole;
    Map<String, String> httpHeaders = new HashMap<>();
    if (updatedRole != null) {
        // Create a deep copy of the user object since we are going to change it.
        Role copiedRole = (Role) CopyUtil.deepCopy(updatedRole);
        encodedRole = encoder.encodeSCIMObject(copiedRole);
        // Add location header
        httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.ROLE_ENDPOINT) + "/" + updatedRole.getId());
        httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
    } else {
        String error = "Updated Role resource is null.";
        throw new InternalErrorException(error);
    }
    return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedRole, httpHeaders);
}
Also used : Role(org.wso2.charon3.core.objects.Role) HashMap(java.util.HashMap) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 59 with Role

use of org.wso2.charon3.core.objects.Role in project charon by wso2.

the class RoleResourceManager method createRole.

@Override
public SCIMResponse createRole(String postRequest, RoleManager roleManager) {
    try {
        if (roleManager == null) {
            String error = "Provided role manager is null.";
            throw new InternalErrorException(error);
        }
        JSONEncoder encoder = getEncoder();
        JSONDecoder decoder = getDecoder();
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getRoleResourceSchema();
        Role role = decoder.decodeResource(postRequest, schema, new Role());
        ServerSideValidator.validateCreatedSCIMObject(role, SCIMSchemaDefinitions.SCIM_ROLE_SCHEMA);
        Role createdRole = roleManager.createRole(role);
        String encodedRole;
        Map<String, String> httpHeaders = new HashMap<>();
        if (createdRole != null) {
            encodedRole = encoder.encodeSCIMObject(createdRole);
            httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.ROLE_ENDPOINT) + "/" + createdRole.getId());
            httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String message = "Newly created Role resource is null.";
            throw new InternalErrorException(message);
        }
        return new SCIMResponse(ResponseCodeConstants.CODE_CREATED, encodedRole, httpHeaders);
    } catch (InternalErrorException | BadRequestException | ConflictException | CharonException | NotFoundException | NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : HashMap(java.util.HashMap) ConflictException(org.wso2.charon3.core.exceptions.ConflictException) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) Role(org.wso2.charon3.core.objects.Role) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 60 with Role

use of org.wso2.charon3.core.objects.Role in project charon by wso2.

the class RoleResourceManager method getRole.

@Override
public SCIMResponse getRole(String id, RoleManager roleManager, String attributes, String excludeAttributes) {
    try {
        if (roleManager == null) {
            String error = "Provided role manager is null.";
            throw new InternalErrorException(error);
        }
        JSONEncoder encoder = getEncoder();
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getRoleResourceSchema();
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        Role role = roleManager.getRole(id, requiredAttributes);
        if (role == null) {
            String message = "Role id: " + id + " not found in the system.";
            throw new NotFoundException(message);
        }
        ServerSideValidator.validateRetrievedSCIMObject(role, schema, attributes, excludeAttributes);
        ServerSideValidator.validateRetrievedSCIMRoleObject(role, attributes, excludeAttributes);
        String encodedRole = encoder.encodeSCIMObject(role);
        Map<String, String> httpHeaders = new HashMap<>();
        httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedRole, httpHeaders);
    } catch (NotFoundException | BadRequestException | CharonException | NotImplementedException | InternalErrorException e) {
        return encodeSCIMException(e);
    }
}
Also used : HashMap(java.util.HashMap) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) Role(org.wso2.charon3.core.objects.Role) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Aggregations

Test (org.testng.annotations.Test)85 ArrayList (java.util.ArrayList)74 UserStoreException (org.wso2.carbon.user.api.UserStoreException)56 HashMap (java.util.HashMap)52 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)42 Connection (java.sql.Connection)36 SQLException (java.sql.SQLException)34 Role (org.wso2.charon3.core.objects.Role)33 AbstractUserStoreManager (org.wso2.carbon.user.core.common.AbstractUserStoreManager)31 CharonException (org.wso2.charon3.core.exceptions.CharonException)29 RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)26 PreparedStatement (java.sql.PreparedStatement)25 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)24 RoleMapping (org.wso2.carbon.identity.application.common.model.RoleMapping)24 ISIntegrationTest (org.wso2.identity.integration.common.utils.ISIntegrationTest)23 HashSet (java.util.HashSet)20 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)20 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)19 IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)19 Matchers.anyString (org.mockito.Matchers.anyString)18