use of io.gravitee.rest.api.model.NewExternalUserEntity in project gravitee-management-rest-api by gravitee-io.
the class UserCommandHandler method handle.
@Override
public Single<UserReply> handle(UserCommand command) {
UserPayload userPayload = command.getPayload();
GraviteeContext.setCurrentOrganization(userPayload.getOrganizationId());
try {
final UserEntity existingUser = userService.findBySource(COCKPIT_SOURCE, userPayload.getId(), false);
UpdateUserEntity updatedUser = new UpdateUserEntity();
updatedUser.setFirstname(userPayload.getFirstName());
updatedUser.setLastname(userPayload.getLastName());
updatedUser.setEmail(userPayload.getEmail());
updatedUser.setPicture(userPayload.getPicture());
updatedUser.setCustomFields(new HashMap<>());
if (userPayload.getAdditionalInformation() != null) {
updatedUser.getCustomFields().putAll(userPayload.getAdditionalInformation());
}
updatedUser.getCustomFields().computeIfAbsent(PICTURE, k -> userPayload.getPicture());
updatedUser.getCustomFields().computeIfAbsent(SUB, k -> userPayload.getUsername());
UserEntity cockpitUserEntity = userService.update(existingUser.getId(), updatedUser);
logger.info("User [{}] with APIM id [{}] updated.", userPayload.getUsername(), cockpitUserEntity.getId());
return Single.just(new UserReply(command.getId(), CommandStatus.SUCCEEDED));
} catch (UserNotFoundException unfe) {
NewExternalUserEntity newUser = new NewExternalUserEntity();
newUser.setSourceId(userPayload.getId());
newUser.setFirstname(userPayload.getFirstName());
newUser.setLastname(userPayload.getLastName());
newUser.setEmail(userPayload.getEmail());
newUser.setPicture(userPayload.getPicture());
newUser.setSource(COCKPIT_SOURCE);
newUser.setCustomFields(new HashMap<>());
if (userPayload.getAdditionalInformation() != null) {
newUser.getCustomFields().putAll(userPayload.getAdditionalInformation());
}
newUser.getCustomFields().computeIfAbsent(PICTURE, k -> userPayload.getPicture());
newUser.getCustomFields().computeIfAbsent(SUB, k -> userPayload.getUsername());
try {
UserEntity cockpitUserEntity = userService.create(newUser, false);
logger.info("User [{}] created with APIM id [{}].", userPayload.getUsername(), cockpitUserEntity.getId());
return Single.just(new UserReply(command.getId(), CommandStatus.SUCCEEDED));
} catch (Exception e) {
logger.info("Error occurred when creating user [{}] for organization [{}].", userPayload.getUsername(), userPayload.getOrganizationId(), e);
return Single.just(new UserReply(command.getId(), CommandStatus.ERROR));
}
} catch (Exception e) {
logger.info("Error occurred when updating user [{}] for organization [{}].", userPayload.getUsername(), userPayload.getOrganizationId(), e);
return Single.just(new UserReply(command.getId(), CommandStatus.ERROR));
} finally {
GraviteeContext.cleanContext();
}
}
use of io.gravitee.rest.api.model.NewExternalUserEntity in project gravitee-management-rest-api by gravitee-io.
the class UserMapperTest method testConvertRegisterUserInput.
@Test
public void testConvertRegisterUserInput() {
// init
RegisterUserInput input = new RegisterUserInput();
input.setEmail(USER_EMAIL);
input.setFirstname(USER_FIRSTNAME);
input.setLastname(USER_LASTNAME);
// Test
NewExternalUserEntity newExternalUserEntity = userMapper.convert(input);
assertNotNull(newExternalUserEntity);
assertEquals(USER_EMAIL, newExternalUserEntity.getEmail());
assertEquals(USER_FIRSTNAME, newExternalUserEntity.getFirstname());
assertEquals(USER_LASTNAME, newExternalUserEntity.getLastname());
}
use of io.gravitee.rest.api.model.NewExternalUserEntity in project gravitee-management-rest-api by gravitee-io.
the class UsersResourceTest method shouldNotCreateRegistration.
@Test
public void shouldNotCreateRegistration() {
// init
RegisterUserInput input = new RegisterUserInput().email("test@example.com").firstname("Firstname").lastname("LASTNAME");
NewExternalUserEntity newExternalUserEntity = new NewExternalUserEntity();
doReturn(newExternalUserEntity).when(userMapper).convert(input);
doReturn(null).when(userService).register(newExternalUserEntity, null);
// test
final Response response = target("registration").request().post(Entity.json(input));
assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR_500, response.getStatus());
Mockito.verify(userMapper).convert(input);
Mockito.verify(userService).register(newExternalUserEntity, null);
}
use of io.gravitee.rest.api.model.NewExternalUserEntity in project gravitee-management-rest-api by gravitee-io.
the class UsersResourceTest method shouldCreateRegistration.
@Test
public void shouldCreateRegistration() {
// init
RegisterUserInput input = new RegisterUserInput().email("test@example.com").firstname("Firstname").lastname("LASTNAME").confirmationPageUrl("HTTP://MY-CONFIRM-PAGE");
NewExternalUserEntity newExternalUserEntity = new NewExternalUserEntity();
doReturn(newExternalUserEntity).when(userMapper).convert(input);
doReturn(new UserEntity()).when(userService).register(newExternalUserEntity, "HTTP://MY-CONFIRM-PAGE");
// test
final Response response = target("registration").request().post(Entity.json(input));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
Mockito.verify(userMapper).convert(input);
Mockito.verify(userService).register(newExternalUserEntity, "HTTP://MY-CONFIRM-PAGE");
}
Aggregations