Search in sources :

Example 16 with User

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

the class UserMapperTest method testConvertSearchableUser.

@Test
public void testConvertSearchableUser() {
    // init
    SearchableUser searchableUser = Mockito.mock(SearchableUser.class);
    Mockito.when(searchableUser.getDisplayName()).thenReturn(SEARCHABLE_USER_DISPLAY_NAME);
    Mockito.when(searchableUser.getEmail()).thenReturn(USER_EMAIL);
    Mockito.when(searchableUser.getFirstname()).thenReturn(USER_FIRSTNAME);
    Mockito.when(searchableUser.getId()).thenReturn(USER_ID);
    Mockito.when(searchableUser.getLastname()).thenReturn(USER_LASTNAME);
    Mockito.when(searchableUser.getReference()).thenReturn(SEARCHABLE_USER_REFERENCE);
    // Test
    User responseUser = userMapper.convert(searchableUser);
    assertNotNull(responseUser);
    assertEquals(USER_ID, responseUser.getId());
    assertEquals(USER_EMAIL, responseUser.getEmail());
    assertEquals(USER_FIRSTNAME, responseUser.getFirstName());
    assertEquals(USER_LASTNAME, responseUser.getLastName());
    assertEquals(SEARCHABLE_USER_DISPLAY_NAME, responseUser.getDisplayName());
    assertEquals(SEARCHABLE_USER_REFERENCE, responseUser.getReference());
}
Also used : SearchableUser(io.gravitee.rest.api.idp.api.identity.SearchableUser) SearchableUser(io.gravitee.rest.api.idp.api.identity.SearchableUser) User(io.gravitee.rest.api.portal.rest.model.User) Test(org.junit.Test)

Example 17 with User

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

the class UserResource method updateCurrentUser.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateCurrentUser(@Valid @NotNull(message = "Input must not be null.") UserInput user) {
    if (!getAuthenticatedUser().equals(user.getId())) {
        throw new UnauthorizedAccessException();
    }
    UserEntity existingUser = userService.findById(getAuthenticatedUser());
    UpdateUserEntity updateUserEntity = new UpdateUserEntity();
    // the avatar picture
    if (user.getAvatar() != null && !user.getAvatar().startsWith("http")) {
        updateUserEntity.setPicture(checkAndScaleImage(user.getAvatar()));
    } else {
        updateUserEntity.setPicture(existingUser.getPicture());
    }
    if (user.getEmail() != null) {
        updateUserEntity.setEmail(user.getEmail());
    }
    if (user.getFirstName() != null) {
        updateUserEntity.setFirstname(user.getFirstName());
    }
    if (user.getLastName() != null) {
        updateUserEntity.setLastname(user.getLastName());
    }
    updateUserEntity.setCustomFields(user.getCustomFields());
    UserEntity updatedUser = userService.update(user.getId(), updateUserEntity);
    final User currentUser = userMapper.convert(updatedUser);
    currentUser.setLinks(userMapper.computeUserLinks(userURL(uriInfo.getBaseUriBuilder()), updatedUser.getUpdatedAt()));
    return Response.ok(currentUser).build();
}
Also used : User(io.gravitee.rest.api.portal.rest.model.User) UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException)

Example 18 with User

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

the class UserResource method getCurrentUser.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getCurrentUser() {
    final String authenticatedUser = getAuthenticatedUser();
    try {
        UserEntity userEntity = userService.findByIdWithRoles(authenticatedUser);
        User currentUser = userMapper.convert(userEntity);
        boolean withManagement = (authenticatedUser != null && permissionService.hasManagementRights(authenticatedUser));
        if (withManagement) {
            Management managementConfig = this.configService.getConsoleSettings().getManagement();
            if (managementConfig != null && managementConfig.getUrl() != null) {
                UserConfig userConfig = new UserConfig();
                userConfig.setManagementUrl(managementConfig.getUrl());
                currentUser.setConfig(userConfig);
            }
        }
        currentUser.setLinks(userMapper.computeUserLinks(userURL(uriInfo.getBaseUriBuilder()), userEntity.getUpdatedAt()));
        return Response.ok(currentUser).build();
    } catch (final UserNotFoundException unfe) {
        response.addCookie(cookieGenerator.generate(null));
        return status(Response.Status.UNAUTHORIZED).build();
    }
}
Also used : UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) User(io.gravitee.rest.api.portal.rest.model.User) Management(io.gravitee.rest.api.model.settings.Management) UserConfig(io.gravitee.rest.api.portal.rest.model.UserConfig)

Example 19 with User

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

the class UserResourceTest method init.

@Before
public void init() throws IOException, URISyntaxException {
    resetAllMocks();
    doReturn(new User()).when(userMapper).convert(nullable(UserEntity.class));
    doReturn(new UserLinks()).when(userMapper).computeUserLinks(any(), any());
    InlinePictureEntity mockImage = new InlinePictureEntity();
    byte[] apiLogoContent = Files.readAllBytes(Paths.get(this.getClass().getClassLoader().getResource("media/logo.svg").toURI()));
    mockImage.setContent(apiLogoContent);
    mockImage.setType("image/svg");
    doReturn(mockImage).when(userService).getPicture(any());
}
Also used : UserLinks(io.gravitee.rest.api.portal.rest.model.UserLinks) User(io.gravitee.rest.api.portal.rest.model.User) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) Before(org.junit.Before)

Example 20 with User

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

the class UserResourceTest method shouldGetCurrentUserWithoutConfig.

@Test
public void shouldGetCurrentUserWithoutConfig() {
    when(userService.findByIdWithRoles(USER_NAME)).thenReturn(new UserEntity());
    when(permissionService.hasManagementRights(USER_NAME)).thenReturn(Boolean.FALSE);
    final Response response = target().request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<String> userId = ArgumentCaptor.forClass(String.class);
    Mockito.verify(userService).findByIdWithRoles(userId.capture());
    assertEquals(USER_NAME, userId.getValue());
    User user = response.readEntity(User.class);
    assertNotNull(user);
    assertNull(user.getConfig());
    assertNotNull(user.getLinks());
}
Also used : Response(javax.ws.rs.core.Response) User(io.gravitee.rest.api.portal.rest.model.User) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Aggregations

User (io.gravitee.rest.api.portal.rest.model.User)20 UserEntity (io.gravitee.rest.api.model.UserEntity)15 Test (org.junit.Test)11 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)6 Instant (java.time.Instant)5 Response (javax.ws.rs.core.Response)5 Date (java.util.Date)4 SearchableUser (io.gravitee.rest.api.idp.api.identity.SearchableUser)3 ConsoleSettingsEntity (io.gravitee.rest.api.model.settings.ConsoleSettingsEntity)3 Group (io.gravitee.rest.api.portal.rest.model.Group)3 PortalApiLinkHelper.usersURL (io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper.usersURL)3 UserService (io.gravitee.rest.api.service.UserService)3 ZoneOffset (java.time.ZoneOffset)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 UriInfo (javax.ws.rs.core.UriInfo)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Component (org.springframework.stereotype.Component)3 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)2 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)2