Search in sources :

Example 1 with UserConfig

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

the class UserResourceTest method shouldGetCurrentUserWithManagementConfigWithUrl.

@Test
public void shouldGetCurrentUserWithManagementConfigWithUrl() {
    when(userService.findByIdWithRoles(USER_NAME)).thenReturn(new UserEntity());
    when(permissionService.hasManagementRights(USER_NAME)).thenReturn(Boolean.TRUE);
    ConsoleSettingsEntity consoleConfigEntity = new ConsoleSettingsEntity();
    Management managementConfig = new Management();
    managementConfig.setUrl("URL");
    consoleConfigEntity.setManagement(managementConfig);
    when(configService.getConsoleSettings()).thenReturn(consoleConfigEntity);
    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);
    UserConfig config = user.getConfig();
    assertNotNull(config);
    assertEquals("URL", config.getManagementUrl());
    assertNotNull(user.getLinks());
}
Also used : Response(javax.ws.rs.core.Response) ConsoleSettingsEntity(io.gravitee.rest.api.model.settings.ConsoleSettingsEntity) 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) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 2 with UserConfig

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

Aggregations

Management (io.gravitee.rest.api.model.settings.Management)2 User (io.gravitee.rest.api.portal.rest.model.User)2 UserConfig (io.gravitee.rest.api.portal.rest.model.UserConfig)2 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)1 UserEntity (io.gravitee.rest.api.model.UserEntity)1 ConsoleSettingsEntity (io.gravitee.rest.api.model.settings.ConsoleSettingsEntity)1 UserNotFoundException (io.gravitee.rest.api.service.exceptions.UserNotFoundException)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1