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;
}
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;
}
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;
}
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);
}
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())));
}
Aggregations