Search in sources :

Example 1 with UpdateUserEntity

use of io.gravitee.rest.api.model.UpdateUserEntity in project gravitee-management-rest-api by gravitee-io.

the class UserResourceTest method shouldUpdateCurrentUser.

@Test
public void shouldUpdateCurrentUser() {
    UserInput userInput = new UserInput();
    final String newAvatar = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
    final String expectedAvatar = "data:image/gif;base64,R0lGODlhyADIAPAAAAAAAP///ywAAAAAyADIAEAC/4SPqcvtD6OctNqL" + "s968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5LL5jE6r1+y2+w2Py+" + "f0uv2Oz+v3/L7/DxgoOEhYaHiImKi4yNjo+AgZKTlJWWl5iZmpucnZ6fkJGio6SlpqeoqaqrrK2ur6ChsrO0tba3uLm6u7y9vr" + "+wscLDxMXGx8jJysvMzc7PwMHS09TV1tfY2drb3N3e39DR4uPk5ebn6Onq6+zt7u/g4fLz9PX29/j5+vv8/f7/8PMKDAgQQLGj" + "yIMKHChQwbOnwIMaLEiRQrWryIMaPGjQYcO3osUwAAOw==";
    userInput.setAvatar(newAvatar);
    userInput.setId(USER_NAME);
    when(userService.update(eq(USER_NAME), any())).thenReturn(new UserEntity());
    final Response response = target().request().put(Entity.json(userInput));
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<UpdateUserEntity> user = ArgumentCaptor.forClass(UpdateUserEntity.class);
    Mockito.verify(userService).update(eq(USER_NAME), user.capture());
    final UpdateUserEntity updateUserEntity = user.getValue();
    assertNotNull(updateUserEntity);
    assertEquals(expectedAvatar, updateUserEntity.getPicture());
    assertNull(updateUserEntity.getStatus());
    User updateUser = response.readEntity(User.class);
    assertNotNull(updateUser);
}
Also used : Response(javax.ws.rs.core.Response) User(io.gravitee.rest.api.portal.rest.model.User) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserInput(io.gravitee.rest.api.portal.rest.model.UserInput) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 2 with UpdateUserEntity

use of io.gravitee.rest.api.model.UpdateUserEntity in project gravitee-management-rest-api by gravitee-io.

the class DefaultUserStatusUpgrader method upgrade.

@Override
public boolean upgrade() {
    // Initialize default user status
    UpdateUserEntity updateUserEntity = new UpdateUserEntity();
    updateUserEntity.setStatus(UserStatus.ACTIVE.name());
    userService.search(new UserCriteria.Builder().noStatus().build(), new PageableImpl(1, Integer.MAX_VALUE)).getContent().forEach(userEntity -> {
        if (userEntity.getStatus() == null) {
            userService.update(userEntity.getId(), updateUserEntity);
        }
    });
    return true;
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity)

Example 3 with UpdateUserEntity

use of io.gravitee.rest.api.model.UpdateUserEntity 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)

Aggregations

UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)3 UserEntity (io.gravitee.rest.api.model.UserEntity)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 UserCriteria (io.gravitee.repository.management.api.search.UserCriteria)1 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)1 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)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 User (io.gravitee.rest.api.portal.rest.model.User)1 UserInput (io.gravitee.rest.api.portal.rest.model.UserInput)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