Search in sources :

Example 36 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMRoleManagerTest method testListRolesWithGETPositive.

@Test(dataProvider = "dataProviderForListRolesWithGETPositive")
public void testListRolesWithGETPositive(String nodeType, Object count, String operation) throws CharonException, IdentityRoleManagementException, NotImplementedException, BadRequestException {
    Node rootNode = generateNodeBasedOnNodeType(nodeType, "name", operation);
    List<RoleBasicInfo> roleList = getDummyRoleBasicInfoList();
    when(mockRoleManagementService.getRoles(anyInt(), anyInt(), anyString(), anyString(), anyString())).thenAnswer(invocationOnMock -> roleList);
    when(mockRoleManagementService.getRoles(anyString(), anyInt(), anyInt(), anyString(), anyString(), anyString())).thenAnswer(invocationOnMock -> roleList);
    SCIMRoleManager roleManager = new SCIMRoleManager(mockRoleManagementService, SAMPLE_TENANT_DOMAIN);
    roleManager.listRolesWithGET(rootNode, 2, (Integer) count, null, null);
    assertTrue(true, "list roles works as expected");
}
Also used : OperationNode(org.wso2.charon3.core.utils.codeutils.OperationNode) ExpressionNode(org.wso2.charon3.core.utils.codeutils.ExpressionNode) Node(org.wso2.charon3.core.utils.codeutils.Node) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 37 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMRoleManager method doUpdateUsers.

private void doUpdateUsers(Role oldRole, Role newRole) throws CharonException, BadRequestException {
    if (log.isDebugEnabled()) {
        log.debug("Updating users of role: " + oldRole.getDisplayName());
    }
    Set<String> userIDsInOldRole = new HashSet<>(oldRole.getUsers());
    Set<String> userIDsInNewRole = new HashSet<>(newRole.getUsers());
    // Check for deleted users.
    Set<String> deletedUserIDList = getRemovedIDList(userIDsInOldRole, userIDsInNewRole);
    // Check for added users.
    Set<String> newUserIDList = getAddedIDList(userIDsInOldRole, userIDsInNewRole);
    // Update the role with added users and deleted users.
    if (isNotEmpty(newUserIDList) || isNotEmpty(deletedUserIDList)) {
        try {
            roleManagementService.updateUserListOfRole(oldRole.getId(), new ArrayList<>(newUserIDList), new ArrayList<>(deletedUserIDList), tenantDomain);
        } catch (IdentityRoleManagementException e) {
            if (StringUtils.equals(INVALID_REQUEST.getCode(), e.getErrorCode()) || StringUtils.equals(OPERATION_FORBIDDEN.getCode(), e.getErrorCode())) {
                throw new BadRequestException(e.getMessage());
            }
            throw new CharonException(String.format("Error occurred while updating users in the role: %s", newRole.getDisplayName()), e);
        }
    }
}
Also used : BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) CharonException(org.wso2.charon3.core.exceptions.CharonException) HashSet(java.util.HashSet) IdentityRoleManagementException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)

Example 38 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMRoleManager method doUpdatePermissions.

private void doUpdatePermissions(Role oldRole, Role newRole) throws BadRequestException, CharonException {
    if (log.isDebugEnabled()) {
        log.debug("Updating permissions of role: " + oldRole.getDisplayName());
    }
    List<String> oldRolePermissions = oldRole.getPermissions();
    List<String> newRolePermissions = newRole.getPermissions();
    // Update the role with specified permissions.
    if (hasPermissionsChanged(oldRolePermissions, newRolePermissions)) {
        if (log.isDebugEnabled()) {
            log.debug("Permissions have changed. Updating permissions of role: " + oldRole.getDisplayName());
        }
        try {
            roleManagementService.setPermissionsForRole(oldRole.getId(), newRolePermissions, tenantDomain);
        } catch (IdentityRoleManagementException e) {
            if (StringUtils.equals(INVALID_REQUEST.getCode(), e.getErrorCode())) {
                throw new BadRequestException(e.getMessage());
            } else if (StringUtils.equals(OPERATION_FORBIDDEN.getCode(), e.getErrorCode())) {
                throw new BadRequestException(e.getMessage());
            }
            throw new CharonException(String.format("Error occurred while updating permissions for role: %s", newRole.getDisplayName()), e);
        }
    }
}
Also used : BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) CharonException(org.wso2.charon3.core.exceptions.CharonException) IdentityRoleManagementException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)

Example 39 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMRoleManager method createRole.

@Override
public Role createRole(Role role) throws CharonException, ConflictException, BadRequestException {
    if (log.isDebugEnabled()) {
        log.debug("Creating role: " + role.getDisplayName());
    }
    try {
        // Check if the role already exists.
        if (roleManagementService.isExistingRole(role.getId(), tenantDomain)) {
            String error = "Role with name: " + role.getDisplayName() + " already exists in the tenantDomain: " + tenantDomain;
            throw new ConflictException(error);
        }
        RoleBasicInfo roleBasicInfo = roleManagementService.addRole(role.getDisplayName(), role.getUsers(), role.getGroups(), role.getPermissions(), tenantDomain);
        Role createdRole = new Role();
        createdRole.setId(roleBasicInfo.getId());
        String locationURI = SCIMCommonUtils.getSCIMRoleURL(roleBasicInfo.getId());
        createdRole.setLocation(locationURI);
        createdRole.setDisplayName(roleBasicInfo.getName());
        createdRole.setSchemas();
        return createdRole;
    } catch (IdentityRoleManagementException e) {
        if (StringUtils.equals(ROLE_ALREADY_EXISTS.getCode(), e.getErrorCode())) {
            throw new ConflictException(e.getMessage());
        } else if (StringUtils.equals(INVALID_REQUEST.getCode(), e.getErrorCode())) {
            throw new BadRequestException(e.getMessage());
        }
        throw new CharonException(String.format("Error occurred while adding a new role: %s", role.getDisplayName()), e);
    }
}
Also used : Role(org.wso2.charon3.core.objects.Role) ConflictException(org.wso2.charon3.core.exceptions.ConflictException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) CharonException(org.wso2.charon3.core.exceptions.CharonException) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo) IdentityRoleManagementException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)

Example 40 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMRoleManager method doUpdateRoleName.

private void doUpdateRoleName(Role oldRole, Role newRole) throws CharonException, ConflictException, NotFoundException, BadRequestException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Updating name of role %s to %s.", oldRole.getDisplayName(), newRole.getDisplayName()));
    }
    // Update name if it is changed.
    String oldRoleDisplayName = oldRole.getDisplayName();
    String newRoleDisplayName = newRole.getDisplayName();
    if (!StringUtils.equals(oldRoleDisplayName, newRoleDisplayName)) {
        // Update role name.
        try {
            roleManagementService.updateRoleName(oldRole.getId(), newRoleDisplayName, tenantDomain);
        } catch (IdentityRoleManagementException e) {
            if (StringUtils.equals(ROLE_NOT_FOUND.getCode(), e.getErrorCode())) {
                throw new NotFoundException(e.getMessage());
            } else if (StringUtils.equals(ROLE_ALREADY_EXISTS.getCode(), e.getErrorCode())) {
                throw new ConflictException(e.getMessage());
            } else if (StringUtils.equals(OPERATION_FORBIDDEN.getCode(), e.getErrorCode())) {
                throw new BadRequestException(e.getMessage());
            }
            throw new CharonException(String.format("Error occurred while updating role name from: %s to %s", oldRoleDisplayName, newRoleDisplayName), e);
        }
    }
}
Also used : ConflictException(org.wso2.charon3.core.exceptions.ConflictException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) CharonException(org.wso2.charon3.core.exceptions.CharonException) IdentityRoleManagementException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)

Aggregations

IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)29 IdentityRoleManagementServerException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException)23 RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Test (org.testng.annotations.Test)20 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)20 Connection (java.sql.Connection)19 SQLException (java.sql.SQLException)19 Matchers.anyString (org.mockito.Matchers.anyString)14 IdentityRoleManagementException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)14 Role (org.wso2.charon3.core.objects.Role)13 ResultSet (java.sql.ResultSet)12 RoleManagementEventPublisherProxy (org.wso2.carbon.identity.role.mgt.core.RoleManagementEventPublisherProxy)11 ArrayList (java.util.ArrayList)9 CharonException (org.wso2.charon3.core.exceptions.CharonException)8 ExpressionNode (org.wso2.charon3.core.utils.codeutils.ExpressionNode)8 Node (org.wso2.charon3.core.utils.codeutils.Node)8 OperationNode (org.wso2.charon3.core.utils.codeutils.OperationNode)8 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)6