Search in sources :

Example 1 with NewExternalUserEntity

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();
    }
}
Also used : UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) Logger(org.slf4j.Logger) PICTURE(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE) SUB(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) LoggerFactory(org.slf4j.LoggerFactory) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) HashMap(java.util.HashMap) Single(io.reactivex.Single) Command(io.gravitee.cockpit.api.command.Command) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Component(org.springframework.stereotype.Component) UserService(io.gravitee.rest.api.service.UserService) CommandHandler(io.gravitee.cockpit.api.command.CommandHandler) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) UserEntity(io.gravitee.rest.api.model.UserEntity) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) HashMap(java.util.HashMap) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply)

Example 2 with NewExternalUserEntity

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());
}
Also used : NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) RegisterUserInput(io.gravitee.rest.api.portal.rest.model.RegisterUserInput) Test(org.junit.Test)

Example 3 with NewExternalUserEntity

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);
}
Also used : Response(javax.ws.rs.core.Response) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) Test(org.junit.Test)

Example 4 with NewExternalUserEntity

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");
}
Also used : Response(javax.ws.rs.core.Response) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) RegisterUserEntity(io.gravitee.rest.api.model.RegisterUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Aggregations

NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)4 Test (org.junit.Test)3 UserEntity (io.gravitee.rest.api.model.UserEntity)2 Response (javax.ws.rs.core.Response)2 Command (io.gravitee.cockpit.api.command.Command)1 CommandHandler (io.gravitee.cockpit.api.command.CommandHandler)1 CommandStatus (io.gravitee.cockpit.api.command.CommandStatus)1 UserCommand (io.gravitee.cockpit.api.command.user.UserCommand)1 UserPayload (io.gravitee.cockpit.api.command.user.UserPayload)1 UserReply (io.gravitee.cockpit.api.command.user.UserReply)1 RegisterUserEntity (io.gravitee.rest.api.model.RegisterUserEntity)1 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)1 PICTURE (io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE)1 SUB (io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB)1 RegisterUserInput (io.gravitee.rest.api.portal.rest.model.RegisterUserInput)1 UserService (io.gravitee.rest.api.service.UserService)1 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)1 UserNotFoundException (io.gravitee.rest.api.service.exceptions.UserNotFoundException)1 Single (io.reactivex.Single)1 HashMap (java.util.HashMap)1