use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.
the class AccountsRoute method create.
public void create(final Context context) {
final String idempotentKey = IdempotencyHeader.getKeyOrFail(context);
final CreateAccountRequestDTO request = accountRequestBodyHandler.getValidated(context);
if (!canPerform(context, request)) {
context.status(403).json(new Error("", "An auth client violated its restrictions in the request"));
return;
}
final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).source(context.ip()).build();
final Optional<AccountDTO> createdAccount = Optional.of(restMapper.toBO(request)).map(accountBO -> accountsService.create(accountBO, requestContext)).map(restMapper::toDTO);
if (createdAccount.isPresent()) {
context.status(201).json(createdAccount.get());
} else {
context.status(400).json(new Error("400", "Failed to create account"));
}
}
use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.
the class AccountsRoute method getByEmail.
@Override
public void getByEmail(final Context context) {
final String domain = context.pathParam("domain");
final String email = context.pathParam("email");
final Optional<AccountDTO> account = accountsService.getByEmail(email, domain).map(restMapper::toDTO);
if (account.isPresent()) {
context.status(200).json(account.get());
} else {
context.status(404).json(new Error(ErrorCode.ACCOUNT_DOES_NOT_EXIST.getCode(), "Account not found"));
}
}
use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.
the class AccountsRoute method updateRoles.
public void updateRoles(final Context context) {
final String accountId = context.pathParam("id");
final RolesRequestDTO request = rolesRequestBodyHandler.getValidated(context);
final AccountDTO updatedAccount;
if (request.getAction() == RolesRequest.Action.GRANT) {
updatedAccount = restMapper.toDTO(accountsService.grantRoles(accountId, request.getRoles()));
} else {
updatedAccount = restMapper.toDTO(accountsService.revokeRoles(accountId, request.getRoles()));
}
context.json(updatedAccount);
}
use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.
the class AccountsApiTest method create.
@Test
void create() {
final CreateAccountRequestDTO requestDTO = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
final AccountBO accountBO = mapper().toBO(requestDTO);
final AccountBO serviceResponse = accountBO.withId(UUID.randomUUID().toString());
Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenReturn(serviceResponse);
LOG.info("Request {}", requestDTO);
final ValidatableResponse httpResponse = given().body(requestDTO).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url()).then().statusCode(201).contentType(ContentType.JSON);
final AccountDTO response = httpResponse.extract().response().getBody().as(AccountDTO.class);
assertThat(response).isEqualToIgnoringGivenFields(requestDTO, "id", "deleted", "createdAt", "lastModified", "social", "identityProvider");
assertThat(response.getId()).isEqualTo(serviceResponse.getId());
}
use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.
the class AccountsRoute method getById.
public void getById(final Context context) {
final String accountId = context.pathParam("id");
final Optional<AccountDTO> account = accountsService.getById(accountId).map(restMapper::toDTO);
if (account.isPresent()) {
context.status(200).json(account.get());
} else {
context.status(404).json(new Error(ErrorCode.ACCOUNT_DOES_NOT_EXIST.getCode(), "Account not found"));
}
}
Aggregations