Search in sources :

Example 21 with UserEntity

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

the class ApplicationMapper method convert.

public Application convert(ApplicationEntity applicationEntity, UriInfo uriInfo) {
    final Application application = new Application();
    application.setApplicationType(applicationEntity.getType());
    application.setCreatedAt(applicationEntity.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
    application.setDescription(applicationEntity.getDescription());
    Set<String> groupEntities = applicationEntity.getGroups();
    if (groupEntities != null && !groupEntities.isEmpty()) {
        List<Group> groups = groupEntities.stream().map(groupService::findById).map(groupEntity -> new Group().id(groupEntity.getId()).name(groupEntity.getName())).collect(Collectors.toList());
        application.setGroups(groups);
    }
    application.setId(applicationEntity.getId());
    application.setName(applicationEntity.getName());
    UserEntity primaryOwnerUserEntity = userService.findById(applicationEntity.getPrimaryOwner().getId());
    User owner = userMapper.convert(primaryOwnerUserEntity);
    owner.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), primaryOwnerUserEntity.getId()), primaryOwnerUserEntity.getUpdatedAt()));
    application.setOwner(owner);
    application.setUpdatedAt(applicationEntity.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    application.setPicture(applicationEntity.getPicture());
    application.setBackground(applicationEntity.getBackground());
    final ApplicationSettings applicationEntitySettings = applicationEntity.getSettings();
    if (applicationEntitySettings != null) {
        io.gravitee.rest.api.portal.rest.model.ApplicationSettings appSettings = new io.gravitee.rest.api.portal.rest.model.ApplicationSettings();
        final SimpleApplicationSettings simpleAppEntitySettings = applicationEntitySettings.getApp();
        if (simpleAppEntitySettings != null) {
            appSettings.app(new io.gravitee.rest.api.portal.rest.model.SimpleApplicationSettings().clientId(simpleAppEntitySettings.getClientId()).type(simpleAppEntitySettings.getType()));
            application.setHasClientId(simpleAppEntitySettings.getClientId() != null);
        } else {
            final OAuthClientSettings oAuthClientEntitySettings = applicationEntitySettings.getoAuthClient();
            appSettings.oauth(new io.gravitee.rest.api.portal.rest.model.OAuthClientSettings().applicationType(oAuthClientEntitySettings.getApplicationType()).clientId(oAuthClientEntitySettings.getClientId()).clientSecret(oAuthClientEntitySettings.getClientSecret()).clientUri(oAuthClientEntitySettings.getClientUri()).logoUri(oAuthClientEntitySettings.getLogoUri()).grantTypes(oAuthClientEntitySettings.getGrantTypes()).redirectUris(oAuthClientEntitySettings.getRedirectUris()).responseTypes(oAuthClientEntitySettings.getResponseTypes()).renewClientSecretSupported(oAuthClientEntitySettings.isRenewClientSecretSupported()));
            application.setHasClientId(oAuthClientEntitySettings.getClientId() != null);
        }
        application.setSettings(appSettings);
    }
    return application;
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) OAuthClientSettings(io.gravitee.rest.api.model.application.OAuthClientSettings) Group(io.gravitee.rest.api.portal.rest.model.Group) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) GroupService(io.gravitee.rest.api.service.GroupService) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) User(io.gravitee.rest.api.portal.rest.model.User) Collectors(java.util.stream.Collectors) Application(io.gravitee.rest.api.portal.rest.model.Application) List(java.util.List) Component(org.springframework.stereotype.Component) OffsetDateTime(java.time.OffsetDateTime) UserService(io.gravitee.rest.api.service.UserService) ZoneOffset(java.time.ZoneOffset) UriInfo(javax.ws.rs.core.UriInfo) PortalApiLinkHelper.usersURL(io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper.usersURL) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationLinks(io.gravitee.rest.api.portal.rest.model.ApplicationLinks) Group(io.gravitee.rest.api.portal.rest.model.Group) User(io.gravitee.rest.api.portal.rest.model.User) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) OAuthClientSettings(io.gravitee.rest.api.model.application.OAuthClientSettings) Application(io.gravitee.rest.api.portal.rest.model.Application)

Example 22 with UserEntity

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

the class AuditServiceImpl method getMetadata.

private Map<String, String> getMetadata(List<AuditEntity> content) {
    Map<String, String> metadata = new HashMap<>();
    for (AuditEntity auditEntity : content) {
        // add user's display name
        String metadataKey = "USER:" + auditEntity.getUser() + ":name";
        try {
            UserEntity user = userService.findById(auditEntity.getUser());
            metadata.put(metadataKey, user.getDisplayName());
        } catch (TechnicalManagementException e) {
            LOGGER.error("Error finding metadata {}", auditEntity.getUser());
        } catch (UserNotFoundException unfe) {
            metadata.put(metadataKey, auditEntity.getUser());
        }
        if (Audit.AuditReferenceType.API.name().equals(auditEntity.getReferenceType())) {
            metadataKey = "API:" + auditEntity.getReferenceId() + ":name";
            if (!metadata.containsKey(metadataKey)) {
                try {
                    Optional<Api> optApi = apiRepository.findById(auditEntity.getReferenceId());
                    if (optApi.isPresent()) {
                        metadata.put(metadataKey, optApi.get().getName());
                    }
                } catch (TechnicalException e) {
                    LOGGER.error("Error finding metadata {}", metadataKey);
                    metadata.put(metadataKey, auditEntity.getReferenceId());
                }
            }
        } else if (Audit.AuditReferenceType.APPLICATION.name().equals(auditEntity.getReferenceType())) {
            metadataKey = "APPLICATION:" + auditEntity.getReferenceId() + ":name";
            if (!metadata.containsKey(metadataKey)) {
                try {
                    Optional<Application> optApp = applicationRepository.findById(auditEntity.getReferenceId());
                    if (optApp.isPresent()) {
                        metadata.put(metadataKey, optApp.get().getName());
                    }
                } catch (TechnicalException e) {
                    LOGGER.error("Error finding metadata {}", metadataKey);
                    metadata.put(metadataKey, auditEntity.getReferenceId());
                }
            }
        }
        // add property metadata
        String name;
        if (auditEntity.getProperties() != null) {
            for (Map.Entry<String, String> property : auditEntity.getProperties().entrySet()) {
                metadataKey = new StringJoiner(":").add(property.getKey()).add(property.getValue()).add("name").toString();
                if (!metadata.containsKey(metadataKey)) {
                    name = property.getValue();
                    try {
                        switch(Audit.AuditProperties.valueOf(property.getKey())) {
                            case API:
                                Optional<Api> optApi = apiRepository.findById(property.getValue());
                                if (optApi.isPresent()) {
                                    name = optApi.get().getName();
                                }
                                break;
                            case APPLICATION:
                                Optional<Application> optApp = applicationRepository.findById(property.getValue());
                                if (optApp.isPresent()) {
                                    name = optApp.get().getName();
                                }
                                break;
                            case PAGE:
                                Optional<io.gravitee.repository.management.model.Page> optPage = pageRepository.findById(property.getValue());
                                if (optPage.isPresent()) {
                                    name = optPage.get().getName();
                                }
                                break;
                            case PLAN:
                                Optional<Plan> optPlan = planRepository.findById(property.getValue());
                                if (optPlan.isPresent()) {
                                    name = optPlan.get().getName();
                                }
                                break;
                            case METADATA:
                                MetadataReferenceType refType = (Audit.AuditReferenceType.API.name().equals(auditEntity.getReferenceType())) ? MetadataReferenceType.API : (Audit.AuditReferenceType.APPLICATION.name().equals(auditEntity.getReferenceType())) ? MetadataReferenceType.APPLICATION : MetadataReferenceType.DEFAULT;
                                String refId = refType.equals(MetadataReferenceType.DEFAULT) ? getDefaultReferenceId() : auditEntity.getReferenceId();
                                Optional<Metadata> optMetadata = metadataRepository.findById(property.getValue(), refId, refType);
                                if (optMetadata.isPresent()) {
                                    name = optMetadata.get().getName();
                                }
                                break;
                            case GROUP:
                                Optional<Group> optGroup = groupRepository.findById(property.getValue());
                                if (optGroup.isPresent()) {
                                    name = optGroup.get().getName();
                                }
                                break;
                            case USER:
                                try {
                                    UserEntity user = userService.findById(property.getValue());
                                    name = user.getDisplayName();
                                } catch (UserNotFoundException unfe) {
                                    name = property.getValue();
                                }
                            default:
                                break;
                        }
                    } catch (TechnicalException e) {
                        LOGGER.error("Error finding metadata {}", metadataKey);
                        name = property.getValue();
                    }
                    metadata.put(metadataKey, name);
                }
            }
        }
    }
    return metadata;
}
Also used : UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Page(io.gravitee.common.data.domain.Page) MetadataPage(io.gravitee.common.data.domain.MetadataPage) UuidString(io.gravitee.rest.api.service.common.UuidString) UserEntity(io.gravitee.rest.api.model.UserEntity) AuditEntity(io.gravitee.rest.api.model.audit.AuditEntity) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 23 with UserEntity

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

the class RatingMapper method convert.

public Rating convert(RatingEntity ratingEntity, UriInfo uriInfo) {
    final Rating rating = new Rating();
    UserEntity authorEntity = userService.findById(ratingEntity.getUser());
    User author = userMapper.convert(authorEntity);
    author.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), authorEntity.getId()), authorEntity.getUpdatedAt()));
    rating.setAuthor(author);
    rating.setTitle(ratingEntity.getTitle());
    rating.setComment(ratingEntity.getComment());
    if (ratingEntity.getCreatedAt() != null) {
        rating.setDate(ratingEntity.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
    }
    rating.setId(ratingEntity.getId());
    rating.setValue(Integer.valueOf(ratingEntity.getRate()));
    if (ratingEntity.getAnswers() != null) {
        List<RatingAnswer> ratingsAnswer = ratingEntity.getAnswers().stream().sorted(Comparator.comparing(RatingAnswerEntity::getCreatedAt)).map(rae -> {
            UserEntity answerAuthorEntity = userService.findById(rae.getUser());
            User answerAuthor = userMapper.convert(answerAuthorEntity);
            answerAuthor.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), answerAuthorEntity.getId()), answerAuthorEntity.getUpdatedAt()));
            return new RatingAnswer().id(rae.getId()).author(answerAuthor).comment(rae.getComment()).date(rae.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
        }).collect(Collectors.toList());
        rating.setAnswers(ratingsAnswer);
    }
    return rating;
}
Also used : RatingEntity(io.gravitee.rest.api.model.RatingEntity) Autowired(org.springframework.beans.factory.annotation.Autowired) User(io.gravitee.rest.api.portal.rest.model.User) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) UserService(io.gravitee.rest.api.service.UserService) Rating(io.gravitee.rest.api.portal.rest.model.Rating) RatingAnswer(io.gravitee.rest.api.portal.rest.model.RatingAnswer) ZoneOffset(java.time.ZoneOffset) UriInfo(javax.ws.rs.core.UriInfo) Comparator(java.util.Comparator) PortalApiLinkHelper.usersURL(io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper.usersURL) RatingAnswerEntity(io.gravitee.rest.api.model.RatingAnswerEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) RatingAnswer(io.gravitee.rest.api.portal.rest.model.RatingAnswer) User(io.gravitee.rest.api.portal.rest.model.User) Rating(io.gravitee.rest.api.portal.rest.model.Rating) UserEntity(io.gravitee.rest.api.model.UserEntity)

Example 24 with UserEntity

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

the class OAuth2AuthenticationResource method processUser.

private Response processUser(final SocialIdentityProviderEntity socialProvider, final HttpServletResponse servletResponse, final String userInfo, final String state, final String accessToken, final String idToken) {
    UserEntity user = userService.createOrUpdateUserFromSocialIdentityProvider(socialProvider, userInfo);
    String userId = user.getId();
    final Set<GrantedAuthority> authorities = authoritiesProvider.retrieveAuthorities(user.getId());
    // set user to Authentication Context
    UserDetails userDetails = new UserDetails(userId, "", authorities);
    userDetails.setEmail(user.getEmail());
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, authorities));
    return connectUser(userId, state, servletResponse, accessToken, idToken);
}
Also used : UserDetails(io.gravitee.rest.api.idp.api.authentication.UserDetails) GrantedAuthority(org.springframework.security.core.GrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserEntity(io.gravitee.rest.api.model.UserEntity)

Example 25 with UserEntity

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

the class ApiSubscribersResourceTest method init.

@Before
public void init() throws IOException {
    resetAllMocks();
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    UserEntity user = new UserEntity();
    user.setId(USER_NAME);
    PrimaryOwnerEntity primaryOwner = new PrimaryOwnerEntity(user);
    mockApi.setPrimaryOwner(primaryOwner);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any(), argThat(q -> singletonList(API).equals(q.getIds())));
}
Also used : java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) OK_200(io.gravitee.common.http.HttpStatusCode.OK_200) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Error(io.gravitee.rest.api.portal.rest.model.Error) Collections.singletonList(java.util.Collections.singletonList) Application(io.gravitee.rest.api.portal.rest.model.Application) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Collections.emptySet(java.util.Collections.emptySet) NOT_FOUND_404(io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404) IOException(java.io.IOException) Test(org.junit.Test) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Mockito(org.mockito.Mockito) Response(javax.ws.rs.core.Response) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) Assert(org.junit.Assert) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Before(org.junit.Before)

Aggregations

UserEntity (io.gravitee.rest.api.model.UserEntity)57 Test (org.junit.Test)36 User (io.gravitee.rest.api.portal.rest.model.User)14 Response (javax.ws.rs.core.Response)14 RoleEntity (io.gravitee.rest.api.model.RoleEntity)13 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)12 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)12 Date (java.util.Date)10 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)9 UserService (io.gravitee.rest.api.service.UserService)9 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)8 List (java.util.List)8 Instant (java.time.Instant)7 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)6 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)6 MembershipService (io.gravitee.rest.api.service.MembershipService)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)5 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)5