Search in sources :

Example 1 with UsernameUpdateException

use of org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException in project identity-governance by wso2-extensions.

the class ForgetMeToolExecutor method execute.

private int execute(String existingUsername, String newUsername, String userStoreDomain, String tenantDomain, int tenantId) throws UsernameUpdateException {
    String scriptName = getScriptForOS();
    if (scriptName == null) {
        throw new UsernameUpdateException(String.format("The operating system '%s' doesn't support the forget-me tool script.", System.getProperty(SYSTEM_PROPERTY_OS_NAME)));
    }
    String scriptPath = Paths.get(System.getProperty(SYSTEM_PROPERTY_CARBON_HOME)).resolve(Paths.get(FORGETME_TOOL_HOME, "bin", scriptName)).toString();
    if (log.isDebugEnabled()) {
        log.debug("Resolved forget-me tool script path to: " + scriptPath);
    }
    String arguments = buildForgetMeToolArguments(existingUsername, newUsername, userStoreDomain, tenantDomain, tenantId);
    Process process = null;
    try {
        process = Runtime.getRuntime().exec(scriptPath + " " + arguments);
        if (log.isInfoEnabled()) {
            log.info(IOUtils.toString(process.getInputStream()));
        }
        process.waitFor();
        return process.exitValue();
    } catch (IOException | InterruptedException e) {
        throw new UsernameUpdateException("Error while executing username update process for user: " + existingUsername + " in tenant: " + tenantDomain, e);
    }
}
Also used : IOException(java.io.IOException) UsernameUpdateException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException)

Example 2 with UsernameUpdateException

use of org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException in project identity-governance by wso2-extensions.

the class UsernameUpdateServiceImpl method updateUsername.

@Override
public StatusDTO updateUsername(UserDTO userDTO) throws UsernameUpdateException {
    validate(userDTO);
    String tenantDomain = userDTO.getTenantDomain();
    String domainQualifiedUsername = UserCoreUtil.addDomainToName(userDTO.getExistingUsername(), userDTO.getUserStoreDomain());
    UserStoreManager userStoreManager = getUserStoreManager(tenantDomain);
    if (isExistingUser(domainQualifiedUsername, tenantDomain, userStoreManager)) {
        performUpdate(userDTO, userStoreManager);
    } else {
        throw new UsernameUpdateClientException(String.format(ERROR_USER_NOT_FOUND.getMessage(), tenantDomain, domainQualifiedUsername), ERROR_USER_NOT_FOUND.getCode(), UsernameUpdateClientException.ErrorType.NOT_FOUND);
    }
    return UpdateUsernameServiceUtil.buildStatus(STATUS_SUCCESS.getCode(), String.format(STATUS_SUCCESS.getMessage(), userDTO.getExistingUsername(), userDTO.getNewUsername()));
}
Also used : UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) UsernameUpdateClientException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateClientException)

Example 3 with UsernameUpdateException

use of org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException in project identity-governance by wso2-extensions.

the class UpdateUsernameApiServiceImpl method updateUsernamePut.

/**
 * Username update service method.
 *
 * @param user UsernameUpdateRequestDTO{@link UsernameUpdateRequestDTO} including update request parameters
 * @return Response status. Returns 200 upon successful update
 */
@Override
public Response updateUsernamePut(UsernameUpdateRequestDTO user) {
    try {
        UserDTO userDTO = new UserDTO();
        userDTO.setExistingUsername(user.getExistingUsername());
        userDTO.setNewUsername(user.getNewUsername());
        userDTO.setUserStoreDomain(user.getRealm());
        userDTO.setTenantDomain(getTenantDomainFromContext());
        UsernameUpdateService usernameUpdateService = Utils.getUsernameUpdateService();
        usernameUpdateService.updateUsername(userDTO);
    } catch (UsernameUpdateClientException e) {
        handleClientError(e);
    } catch (UsernameUpdateException e) {
        handleServerError(e);
    } catch (Throwable e) {
        handleUnexpectedServerError(e);
    }
    return Response.ok().status(Response.Status.OK).build();
}
Also used : UsernameUpdateService(org.wso2.carbon.identity.user.rename.core.service.UsernameUpdateService) UserDTO(org.wso2.carbon.identity.user.rename.core.dto.UserDTO) UsernameUpdateClientException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateClientException) UsernameUpdateException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException)

Example 4 with UsernameUpdateException

use of org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException in project identity-governance by wso2-extensions.

the class UpdateUsernameApiServiceImplTest method testServerErrorStatusOfUpdateUsername.

@Test
public void testServerErrorStatusOfUpdateUsername() throws Exception {
    mockedUtils.when(Utils::getUsernameUpdateService).thenReturn(usernameUpdateService);
    Mockito.when(usernameUpdateService.updateUsername(any(UserDTO.class))).thenThrow(new UsernameUpdateException(ERROR_MSG, ERROR_CODE));
    // The test method executes the lines but does not throw the 500 code.
    Assert.assertEquals(usernameApiService.updateUsernamePut(usernameUpdateRequestDTO).getStatus(), 200);
}
Also used : UserDTO(org.wso2.carbon.identity.user.rename.core.dto.UserDTO) UsernameUpdateException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException) Test(org.testng.annotations.Test)

Aggregations

UsernameUpdateException (org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException)3 UserDTO (org.wso2.carbon.identity.user.rename.core.dto.UserDTO)2 UsernameUpdateClientException (org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateClientException)2 IOException (java.io.IOException)1 Test (org.testng.annotations.Test)1 UsernameUpdateService (org.wso2.carbon.identity.user.rename.core.service.UsernameUpdateService)1 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)1