Search in sources :

Example 11 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 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 12 with User

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

the class ApiMapper method convert.

public Api convert(ApiEntity api) {
    final Api apiItem = new Api();
    apiItem.setDescription(api.getDescription());
    List<ApiEntrypointEntity> apiEntrypoints = api.getEntrypoints();
    if (apiEntrypoints != null) {
        List<String> entrypoints = apiEntrypoints.stream().map(ApiEntrypointEntity::getTarget).collect(Collectors.toList());
        apiItem.setEntrypoints(entrypoints);
    }
    apiItem.setDraft(api.getLifecycleState() == ApiLifecycleState.UNPUBLISHED || api.getLifecycleState() == ApiLifecycleState.CREATED);
    apiItem.setRunning(api.getState() == Lifecycle.State.STARTED);
    apiItem.setPublic(api.getVisibility() == Visibility.PUBLIC);
    apiItem.setId(api.getId());
    List<String> apiLabels = api.getLabels();
    if (apiLabels != null) {
        apiItem.setLabels(new ArrayList<>(apiLabels));
    } else {
        apiItem.setLabels(new ArrayList<>());
    }
    apiItem.setName(api.getName());
    PrimaryOwnerEntity primaryOwner = api.getPrimaryOwner();
    if (primaryOwner != null) {
        User owner = new User();
        owner.setId(primaryOwner.getId());
        owner.setDisplayName(primaryOwner.getDisplayName());
        owner.setEmail(primaryOwner.getEmail());
        apiItem.setOwner(owner);
    }
    apiItem.setPages(null);
    apiItem.setPlans(null);
    if (ratingService.isEnabled()) {
        final RatingSummaryEntity ratingSummaryEntity = ratingService.findSummaryByApi(api.getId());
        RatingSummary ratingSummary = new RatingSummary().average(ratingSummaryEntity.getAverageRate()).count(BigDecimal.valueOf(ratingSummaryEntity.getNumberOfRatings()));
        apiItem.setRatingSummary(ratingSummary);
    }
    if (api.getUpdatedAt() != null) {
        apiItem.setUpdatedAt(api.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    }
    apiItem.setVersion(api.getVersion());
    boolean isCategoryModeEnabled = this.parameterService.findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
    if (isCategoryModeEnabled && api.getCategories() != null) {
        apiItem.setCategories(api.getCategories().stream().filter(categoryId -> {
            try {
                categoryService.findNotHiddenById(categoryId);
                return true;
            } catch (CategoryNotFoundException v) {
                return false;
            }
        }).collect(Collectors.toList()));
    } else {
        apiItem.setCategories(new ArrayList<>());
    }
    return apiItem;
}
Also used : RatingSummary(io.gravitee.rest.api.portal.rest.model.RatingSummary) ApiEntrypointEntity(io.gravitee.rest.api.model.api.ApiEntrypointEntity) User(io.gravitee.rest.api.portal.rest.model.User) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) Api(io.gravitee.rest.api.portal.rest.model.Api)

Example 13 with User

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

the class MemberMapper method convert.

public Member convert(MemberEntity member, UriInfo uriInfo) {
    final Member memberItem = new Member();
    memberItem.setCreatedAt(member.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
    UserEntity userEntity = userService.findById(member.getId());
    User memberUser = userMapper.convert(userEntity);
    memberUser.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), userEntity.getId()), userEntity.getUpdatedAt()));
    memberItem.setUser(memberUser);
    if (member.getRoles() != null && !member.getRoles().isEmpty()) {
        memberItem.setRole(member.getRoles().get(0).getName());
    }
    memberItem.setUpdatedAt(member.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    return memberItem;
}
Also used : User(io.gravitee.rest.api.portal.rest.model.User) Member(io.gravitee.rest.api.portal.rest.model.Member) UserEntity(io.gravitee.rest.api.model.UserEntity)

Example 14 with User

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

the class ApplicationMapperTest method checkApplication.

private void checkApplication(Instant now, Application responseApplication, AppSettingsEnum appSettingsType) {
    assertNotNull(responseApplication);
    assertNull(responseApplication.getLinks());
    assertEquals(APPLICATION_TYPE, responseApplication.getApplicationType());
    assertEquals(now.toEpochMilli(), responseApplication.getCreatedAt().toInstant().toEpochMilli());
    assertEquals(APPLICATION_DESCRIPTION, responseApplication.getDescription());
    assertEquals(APPLICATION_ID, responseApplication.getId());
    assertEquals(APPLICATION_NAME, responseApplication.getName());
    assertEquals(now.toEpochMilli(), responseApplication.getUpdatedAt().toInstant().toEpochMilli());
    List<Group> groups = responseApplication.getGroups();
    assertNotNull(groups);
    assertEquals(1, groups.size());
    assertEquals(APPLICATION_GROUP_ID, groups.get(0).getId());
    assertEquals(APPLICATION_GROUP_NAME, groups.get(0).getName());
    User owner = responseApplication.getOwner();
    assertNotNull(owner);
    assertEquals(APPLICATION_USER_DISPLAYNAME, owner.getDisplayName());
    assertEquals(APPLICATION_USER_EMAIL, owner.getEmail());
    assertEquals(APPLICATION_USER_ID, owner.getId());
    assertEquals("environments/DEFAULT/users/" + APPLICATION_USER_ID + "/avatar?", owner.getLinks().getAvatar());
    io.gravitee.rest.api.portal.rest.model.ApplicationSettings applicationSettings = responseApplication.getSettings();
    if (AppSettingsEnum.NO_SETTINGS == appSettingsType) {
        assertNull(applicationSettings);
    } else {
        io.gravitee.rest.api.portal.rest.model.SimpleApplicationSettings sas = applicationSettings.getApp();
        io.gravitee.rest.api.portal.rest.model.OAuthClientSettings oacs = applicationSettings.getOauth();
        if (AppSettingsEnum.OAUTH_SETTINGS == appSettingsType) {
            assertNull(sas);
            assertNotNull(oacs);
            assertEquals(APPLICATION_OAUTH_APPLICATION_TYPE, oacs.getApplicationType());
            assertEquals(APPLICATION_OAUTH_CLIENT_ID, oacs.getClientId());
            assertEquals(APPLICATION_OAUTH_CLIENT_SECRET, oacs.getClientSecret());
            assertEquals(APPLICATION_OAUTH_CLIENT_URI, oacs.getClientUri());
            assertEquals(APPLICATION_OAUTH_LOGO_URI, oacs.getLogoUri());
            assertEquals(Boolean.TRUE, oacs.getRenewClientSecretSupported());
            final List<String> grantTypes = oacs.getGrantTypes();
            assertNotNull(grantTypes);
            assertFalse(grantTypes.isEmpty());
            assertEquals(APPLICATION_OAUTH_GRANT_TYPE, grantTypes.get(0));
            final List<String> redirectUris = oacs.getRedirectUris();
            assertNotNull(redirectUris);
            assertFalse(redirectUris.isEmpty());
            assertEquals(APPLICATION_OAUTH_REDIRECT_URI, redirectUris.get(0));
            final List<String> responseTypes = oacs.getResponseTypes();
            assertNotNull(responseTypes);
            assertFalse(responseTypes.isEmpty());
            assertEquals(APPLICATION_OAUTH_RESPONSE_TYPE, responseTypes.get(0));
            assertEquals(responseApplication.getHasClientId(), true);
        } else if (AppSettingsEnum.SIMPLE_SETTINGS == appSettingsType) {
            assertNotNull(sas);
            assertNull(oacs);
            assertEquals(APPLICATION_SIMPLE_CLIENT_ID, sas.getClientId());
            assertEquals(APPLICATION_SIMPLE_TYPE, sas.getType());
            assertEquals(responseApplication.getHasClientId(), true);
        }
    }
}
Also used : Group(io.gravitee.rest.api.portal.rest.model.Group) User(io.gravitee.rest.api.portal.rest.model.User) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 15 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 testConvertUserEntityWithPermissions.

@Test
public void testConvertUserEntityWithPermissions() throws Exception {
    Instant now = Instant.now();
    Date nowDate = Date.from(now);
    // init
    UserEntity userEntity = new UserEntity();
    UserRoleEntity userRoleEntityOrganization = new UserRoleEntity();
    userRoleEntityOrganization.setId("org-id");
    userRoleEntityOrganization.setScope(RoleScope.ORGANIZATION);
    HashMap<String, char[]> organizationPermissions = new HashMap<>();
    organizationPermissions.put("USER", new char[] { 'C', 'R', 'U', 'D' });
    organizationPermissions.put("ENVIRONMENT", new char[] { 'C', 'R', 'U', 'D' });
    userRoleEntityOrganization.setPermissions(organizationPermissions);
    UserRoleEntity userRoleEntityEnvironment = new UserRoleEntity();
    userRoleEntityEnvironment.setScope(RoleScope.ENVIRONMENT);
    userRoleEntityEnvironment.setId("env-id");
    HashMap<String, char[]> environmentPermissions = new HashMap<>();
    environmentPermissions.put("APPLICATION", new char[] { 'C' });
    userRoleEntityEnvironment.setPermissions(environmentPermissions);
    userEntity.setCreatedAt(nowDate);
    userEntity.setEmail(USER_EMAIL);
    userEntity.setFirstname(USER_FIRSTNAME);
    userEntity.setId(USER_ID);
    userEntity.setLastConnectionAt(nowDate);
    userEntity.setLastname(USER_LASTNAME);
    userEntity.setPassword(USER_PASSWORD);
    userEntity.setPicture(USER_PICTURE);
    userEntity.setRoles(new HashSet<>(Arrays.asList(userRoleEntityOrganization, userRoleEntityEnvironment)));
    userEntity.setSource(USER_SOURCE);
    userEntity.setSourceId(USER_SOURCE_ID);
    userEntity.setStatus(USER_STATUS);
    userEntity.setUpdatedAt(nowDate);
    // Test
    User responseUser = userMapper.convert(userEntity);
    assertNotNull(responseUser);
    assertEquals(USER_ID, responseUser.getId());
    assertEquals(USER_EMAIL, responseUser.getEmail());
    assertEquals(USER_FIRSTNAME, responseUser.getFirstName());
    assertEquals(USER_LASTNAME, responseUser.getLastName());
    assertEquals(USER_FIRSTNAME + ' ' + USER_LASTNAME, responseUser.getDisplayName());
    assertTrue(responseUser.getPermissions().getAPPLICATION().containsAll(Arrays.asList("C")));
}
Also used : UserRoleEntity(io.gravitee.rest.api.model.UserRoleEntity) SearchableUser(io.gravitee.rest.api.idp.api.identity.SearchableUser) User(io.gravitee.rest.api.portal.rest.model.User) HashMap(java.util.HashMap) Instant(java.time.Instant) Date(java.util.Date) 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

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