Search in sources :

Example 66 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 testUpdateRoleUpdateRoleName.

@Test(dataProvider = "dataProviderForUpdateRoleUpdateRoleName")
public void testUpdateRoleUpdateRoleName(String roleId, String oldRoleName, String newRoleName, String tenantDomain, String type) throws IdentityRoleManagementException, BadRequestException, CharonException, ConflictException, NotFoundException {
    RoleBasicInfo roleBasicInfo = new RoleBasicInfo(roleId, newRoleName);
    Role[] oldAndNewRoles = getOldAndNewRoleDummies(roleId, oldRoleName, newRoleName, type);
    when(mockRoleManagementService.updateRoleName(anyString(), anyString(), anyString())).thenReturn(roleBasicInfo);
    when(mockRoleManagementService.updateUserListOfRole(eq(roleId), anyListOf(String.class), anyListOf(String.class), anyString())).thenReturn(roleBasicInfo);
    when(mockRoleManagementService.updateGroupListOfRole(eq(roleId), anyListOf(String.class), anyListOf(String.class), anyString())).thenReturn(roleBasicInfo);
    when(mockRoleManagementService.setPermissionsForRole(eq(roleId), anyListOf(String.class), anyString())).thenReturn(roleBasicInfo);
    SCIMRoleManager scimRoleManager = new SCIMRoleManager(mockRoleManagementService, tenantDomain);
    scimRoleManager.updateRole(oldAndNewRoles[0], oldAndNewRoles[1]);
    assertTrue(true, "updateRole execute successfully");
}
Also used : Role(org.wso2.charon3.core.objects.Role) Matchers.anyString(org.mockito.Matchers.anyString) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 67 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 testCreateRoleUnexpectedServerError.

@Test(dataProvider = "dataProviderForCreateRoleUnexpectedServerError")
public void testCreateRoleUnexpectedServerError(String roleId, String roleDisplayName, String tenantDomain, String sError) throws BadRequestException, CharonException, IdentityRoleManagementException {
    Role role = getDummyRole(roleId, roleDisplayName);
    when(mockRoleManagementService.addRole(anyString(), anyListOf(String.class), anyListOf(String.class), anyListOf(String.class), anyString())).thenThrow(unExpectedErrorThrower(tenantDomain, sError, "Error while creating the role: %s in the tenantDomain: %s", roleDisplayName));
    SCIMRoleManager scimRoleManager = new SCIMRoleManager(mockRoleManagementService, tenantDomain);
    assertThrows(CharonException.class, () -> scimRoleManager.createRole(role));
}
Also used : Role(org.wso2.charon3.core.objects.Role) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 68 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 testRoleUpdatePermissionListOfRoleThrowingErrors.

@Test(dataProvider = "dataProviderForRoleUpdatePermissionListOfRoleThrowingErrors", expectedExceptions = { BadRequestException.class, CharonException.class })
public void testRoleUpdatePermissionListOfRoleThrowingErrors(String roleId, String oldRoleName, String newRoleName, String tenantDomain, String permissionType, String sError) throws IdentityRoleManagementException, BadRequestException, CharonException, ConflictException, NotFoundException {
    RoleBasicInfo roleBasicInfo = new RoleBasicInfo(roleId, newRoleName);
    Role[] oldAndNewRoles = getOldAndNewRoleDummies(roleId, oldRoleName, newRoleName, permissionType);
    when(mockRoleManagementService.updateRoleName(anyString(), anyString(), anyString())).thenReturn(roleBasicInfo);
    when(mockRoleManagementService.setPermissionsForRole(anyString(), anyListOf(String.class), anyString())).thenAnswer(invocationOnMock -> {
        String roleIdArg = invocationOnMock.getArgumentAt(0, String.class);
        String tenantDomainArg = invocationOnMock.getArgumentAt(2, String.class);
        if (INVALID_ROLE_IDS.contains(roleIdArg)) {
            String errorMessage = "Invalid scenario. Multiple roles found for the given role name: " + roleIdArg + " and tenantDomain: " + tenantDomain;
            throw new IdentityRoleManagementClientException(INVALID_REQUEST.getCode(), errorMessage);
        }
        if (SYSTEM_ROLES.contains(oldRoleName)) {
            throw new IdentityRoleManagementClientException(RoleConstants.Error.OPERATION_FORBIDDEN.getCode(), "Invalid operation. Permissions cannot be modified in the role: " + oldRoleName + " since it's a read only system role.");
        }
        Throwable unExpectedErrors = unExpectedErrorThrower(tenantDomainArg, sError, "Error while updating users to the role: %s in the tenantDomain: %s", roleIdArg);
        if (unExpectedErrors != null)
            throw unExpectedErrors;
        return roleBasicInfo;
    });
    when(mockRoleManagementService.updateUserListOfRole(eq(roleId), anyListOf(String.class), anyListOf(String.class), anyString())).thenReturn(roleBasicInfo);
    when(mockRoleManagementService.updateGroupListOfRole(eq(roleId), anyListOf(String.class), anyListOf(String.class), anyString())).thenReturn(roleBasicInfo);
    SCIMRoleManager scimRoleManager = new SCIMRoleManager(mockRoleManagementService, tenantDomain);
    scimRoleManager.updateRole(oldAndNewRoles[0], oldAndNewRoles[1]);
}
Also used : Role(org.wso2.charon3.core.objects.Role) Matchers.anyString(org.mockito.Matchers.anyString) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo) IdentityRoleManagementClientException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 69 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 filterRolesBySingleAttribute.

/**
 * Get the list of roles based on the filter.
 *
 * @param node       Expression node.
 * @param startIndex Starting index.
 * @param count      Number of results required.
 * @param sortBy     SortBy.
 * @param sortOrder  Sorting order.
 * @return Filtered roles.
 * @throws CharonException Error filtering the roles.
 */
private List<Object> filterRolesBySingleAttribute(ExpressionNode node, Integer count, Integer startIndex, String sortBy, String sortOrder) throws CharonException, BadRequestException {
    String attributeName = node.getAttributeValue();
    String filterOperation = node.getOperation();
    String attributeValue = node.getValue();
    if (log.isDebugEnabled()) {
        log.debug("Filtering roles with filter: " + attributeName + " + " + filterOperation + " + " + attributeValue);
    }
    // Check whether the filter operation is supported for filtering in roles.
    if (isFilteringNotSupported(filterOperation)) {
        String errorMessage = "Filter operation: " + filterOperation + " is not supported for role filtering.";
        throw new BadRequestException(errorMessage);
    }
    List<Object> filteredRoles = new ArrayList<>();
    // 0th index is to store total number of results.
    filteredRoles.add(0);
    String searchFilter = getSearchFilter(filterOperation, attributeValue);
    if (log.isDebugEnabled()) {
        log.debug(String.format("Filtering roleNames from search filter: %s", searchFilter));
    }
    List<RoleBasicInfo> roles;
    try {
        roles = roleManagementService.getRoles(searchFilter, count, startIndex, sortBy, sortOrder, tenantDomain);
    } catch (IdentityRoleManagementException e) {
        throw new CharonException(String.format("Error occurred while listing roles based on the search filter: %s", searchFilter), e);
    }
    List<Object> scimRoles = getScimRolesList(roles);
    // Set total number of results to 0th index.
    filteredRoles.set(0, scimRoles.size());
    // Add the results list.
    filteredRoles.addAll(scimRoles);
    return filteredRoles;
}
Also used : ArrayList(java.util.ArrayList) 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 70 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 doUpdateGroups.

private void doUpdateGroups(Role oldRole, Role newRole) throws CharonException, BadRequestException {
    if (log.isDebugEnabled()) {
        log.debug("Updating groups of role: " + oldRole.getDisplayName());
    }
    Set<String> groupIDsInOldRole = new HashSet<>(oldRole.getGroups());
    Set<String> groupIDsInNewRole = new HashSet<>(newRole.getGroups());
    // Check for deleted groups.
    Set<String> deleteGroupIDList = getRemovedIDList(groupIDsInOldRole, groupIDsInNewRole);
    // Check for added groups.
    Set<String> newGroupIDList = getAddedIDList(groupIDsInOldRole, groupIDsInNewRole);
    // Update the role with added users and deleted users.
    if (isNotEmpty(newGroupIDList) || isNotEmpty(deleteGroupIDList)) {
        try {
            roleManagementService.updateGroupListOfRole(oldRole.getId(), new ArrayList<>(newGroupIDList), new ArrayList<>(deleteGroupIDList), 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 groups 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)

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