Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent emails to be sent to non opted in user in trial instance
- Loading branch information
Showing
18 changed files
with
1,551 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...-apim-rest-api-model/src/main/java/io/gravitee/rest/api/model/settings/TrialInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.rest.api.model.settings; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import io.gravitee.rest.api.model.annotations.ParameterKey; | ||
import io.gravitee.rest.api.model.parameters.Key; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* @author Yann TAVERNIER (yann.tavernier at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@Getter | ||
@Setter | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class TrialInstance { | ||
|
||
@ParameterKey(Key.TRIAL_INSTANCE) | ||
private Boolean enabled; | ||
} |
41 changes: 41 additions & 0 deletions
41
...m-rest-api-service/src/main/java/io/gravitee/rest/api/service/EmailRecipientsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.rest.api.service; | ||
|
||
import io.gravitee.rest.api.service.common.ExecutionContext; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public interface EmailRecipientsService { | ||
/** | ||
* Process a list of templated recipients to extract it as a list of unique email addresses | ||
* | ||
* @param templatedRecipientsEmail a list of strings representing templated email. Each string can be itself a literal list of recipients separated by ' ' (whitespace) ',' or ';'. If an element contains '$', then it will be processed against templateData parameter with Freemarker processor | ||
* @param templateData is the dateset used to process emails. | ||
* @return a set of not empty emails. | ||
*/ | ||
Set<String> processTemplatedRecipients(Collection<String> templatedRecipientsEmail, Map<String, Object> templateData); | ||
|
||
/** | ||
* Checks that each email has an opted-in user attached to it. If it is not the case, then the email is excluded from the result. | ||
* @param executionContext | ||
* @param recipientsEmail is the list of recipients to verify if attached user has opted-in. This method assumes emails are valids. | ||
* @return a set of emails attached to an opted-in user. | ||
*/ | ||
Set<String> filterRegisteredUser(ExecutionContext executionContext, Collection<String> recipientsEmail); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...i-service/src/main/java/io/gravitee/rest/api/service/impl/EmailRecipientsServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.rest.api.service.impl; | ||
|
||
import static java.util.function.Predicate.not; | ||
|
||
import io.gravitee.apim.core.template.TemplateProcessor; | ||
import io.gravitee.apim.core.template.TemplateProcessorException; | ||
import io.gravitee.rest.api.model.UserEntity; | ||
import io.gravitee.rest.api.service.EmailRecipientsService; | ||
import io.gravitee.rest.api.service.UserService; | ||
import io.gravitee.rest.api.service.common.ExecutionContext; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class EmailRecipientsServiceImpl implements EmailRecipientsService { | ||
|
||
public static final Pattern SPLIT_PATTERN = Pattern.compile("[,;\\s]"); | ||
private final TemplateProcessor templateProcessor; | ||
private final UserService userService; | ||
|
||
@Override | ||
public Set<String> processTemplatedRecipients(Collection<String> templatedRecipientsEmail, final Map<String, Object> templateData) { | ||
return templatedRecipientsEmail | ||
.stream() | ||
.flatMap(splittableRecipientsStr -> | ||
Arrays | ||
.stream(SPLIT_PATTERN.split(splittableRecipientsStr)) | ||
.filter(not(String::isEmpty)) | ||
.map(recipient -> { | ||
if (recipient.contains("$")) { | ||
try { | ||
return Optional.ofNullable(templateProcessor.processInlineTemplate(recipient, templateData)); | ||
} catch (TemplateProcessorException e) { | ||
log.error("Error while processing template '{}' skipping this email", recipient, e); | ||
return Optional.<String>empty(); | ||
} | ||
} | ||
return Optional.of(recipient); | ||
}) | ||
) | ||
.flatMap(Optional::stream) | ||
.filter(not(StringUtils::isEmpty)) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public Set<String> filterRegisteredUser(ExecutionContext executionContext, Collection<String> recipientsEmail) { | ||
return recipientsEmail | ||
.stream() | ||
.map(email -> userService.findByEmail(executionContext, email)) | ||
.flatMap(Optional::stream) | ||
.filter(UserEntity::optedIn) | ||
.map(UserEntity::getEmail) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
Oops, something went wrong.