use of io.gravitee.rest.api.service.search.query.Query in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method resetPassword.
private void resetPassword(final String id, final String resetPageUrl) {
try {
LOGGER.debug("Resetting password of user id {}", id);
Optional<User> optionalUser = userRepository.findById(id);
if (!optionalUser.isPresent()) {
throw new UserNotFoundException(id);
}
final User user = optionalUser.get();
if (!isInternalUser(user)) {
throw new UserNotInternallyManagedException(id);
}
// do not perform this check if the request comes from an authenticated user (ie. admin or someone with right permission)
if (!isAuthenticated() || !canResetPassword()) {
AuditQuery query = new AuditQuery();
query.setEvents(Arrays.asList(User.AuditEvent.PASSWORD_RESET.name()));
query.setFrom(Instant.now().minus(1, ChronoUnit.HOURS).toEpochMilli());
query.setPage(1);
query.setSize(100);
MetadataPage<AuditEntity> events = auditService.search(query);
if (events != null) {
if (events.getContent().size() == 100) {
LOGGER.warn("More than 100 reset password received in less than 1 hour", user.getId());
}
Optional<AuditEntity> optReset = events.getContent().stream().filter(evt -> user.getId().equals(evt.getProperties().get(USER.name()))).findFirst();
if (optReset.isPresent()) {
LOGGER.warn("Multiple reset password received for user '{}' in less than 1 hour", user.getId());
throw new PasswordAlreadyResetException();
}
}
}
final Map<String, Object> params = getTokenRegistrationParams(convert(user, false), RESET_PASSWORD_PATH, RESET_PASSWORD, resetPageUrl);
notifierService.trigger(PortalHook.PASSWORD_RESET, params);
auditService.createOrganizationAuditLog(Collections.singletonMap(USER, user.getId()), User.AuditEvent.PASSWORD_RESET, new Date(), null, null);
emailService.sendAsyncEmailNotification(new EmailNotificationBuilder().to(user.getEmail()).template(EmailNotificationBuilder.EmailTemplate.TEMPLATES_FOR_ACTION_USER_PASSWORD_RESET).params(params).build(), GraviteeContext.getCurrentContext());
} catch (TechnicalException ex) {
final String message = "An error occurs while trying to reset password for user " + id;
LOGGER.error(message, ex);
throw new TechnicalManagementException(message, ex);
}
}
use of io.gravitee.rest.api.service.search.query.Query in project gravitee-management-rest-api by gravitee-io.
the class ApiServiceImpl method findApisByUser.
private List<Api> findApisByUser(String userId, ApiQuery apiQuery, boolean portal) {
// get all public apis
List<Api> publicApis;
if (portal) {
publicApis = apiRepository.search(queryToCriteria(apiQuery).visibility(PUBLIC).build());
} else {
publicApis = emptyList();
}
List<Api> userApis = emptyList();
List<Api> groupApis = emptyList();
List<Api> subscribedApis = emptyList();
// for others API, user must be authenticated
if (userId != null) {
// get user apis
final String[] userApiIds = membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userId, MembershipReferenceType.API).stream().map(MembershipEntity::getReferenceId).filter(apiId -> {
if (apiQuery != null && !CollectionUtils.isEmpty(apiQuery.getIds())) {
// We already have api ids to focus on.
return apiQuery.getIds().contains(apiId);
} else {
return true;
}
}).toArray(String[]::new);
if (userApiIds.length > 0) {
userApis = apiRepository.search(queryToCriteria(apiQuery).ids(userApiIds).build());
}
// get user groups apis
final String[] groupIds = membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userId, MembershipReferenceType.GROUP).stream().filter(m -> {
final RoleEntity roleInGroup = roleService.findById(m.getRoleId());
if (!portal) {
return (m.getRoleId() != null && roleInGroup.getScope().equals(RoleScope.API) && canManageApi(roleInGroup.getPermissions()));
}
return m.getRoleId() != null && roleInGroup.getScope().equals(RoleScope.API);
}).map(MembershipEntity::getReferenceId).toArray(String[]::new);
if (groupIds.length > 0 && groupIds[0] != null) {
groupApis = apiRepository.search(queryToCriteria(apiQuery).groups(groupIds).build());
}
// get user subscribed apis, useful when an API becomes private and an app owner is not anymore in members.
if (portal) {
final Set<String> applications = applicationService.findByUser(userId).stream().map(ApplicationListItem::getId).collect(toSet());
if (!applications.isEmpty()) {
final SubscriptionQuery query = new SubscriptionQuery();
query.setApplications(applications);
final Collection<SubscriptionEntity> subscriptions = subscriptionService.search(query);
if (subscriptions != null && !subscriptions.isEmpty()) {
subscribedApis = apiRepository.search(queryToCriteria(apiQuery).ids(subscriptions.stream().map(SubscriptionEntity::getApi).distinct().toArray(String[]::new)).build());
}
}
}
}
List<Api> allApis = new ArrayList<>();
allApis.addAll(publicApis);
allApis.addAll(userApis);
allApis.addAll(groupApis);
allApis.addAll(subscribedApis);
return allApis.stream().distinct().collect(toList());
}
Aggregations