Skip to content

Commit

Permalink
Added: Mailing Black List
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Jan 7, 2025
1 parent f911c91 commit a493324
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.0.1 - 2024-12-23]
## [0.0.1 - 2024-12-27]
### Added
- First version of the project
- Spring Application
Expand Down Expand Up @@ -183,3 +183,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Title and description in project configuration
- Annotation Ignore Project Configuration Match
- Send error email if error while sending query to bridgehead
- Mailing Black List
8 changes: 8 additions & 0 deletions src/main/java/de/samply/app/ProjectManagerConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public class ProjectManagerConst {
public final static String FETCH_PROJECT_ROLES_ACTION = "FETCH_PROJECT_ROLES";
public final static String SEND_EXPORT_FILES_TO_RESEARCH_ENVIRONMENT_ACTION = "SEND_EXPORT_FILES_TO_RESEARCH_ENVIRONMENT";
public final static String ARE_EXPORT_FILES_TRANSFERRED_TO_RESEARCH_ENVIRONMENT_ACTION = "ARE_EXPORT_FILES_TRANSFERRED_TO_RESEARCH_ENVIRONMENT";
public final static String ADD_USER_TO_MAILING_BLACK_LIST_ACTION = "ADD_USER_TO_MAILING_BLACK_LIST";
public final static String REMOVE_USER_FROM_MAILING_BLACK_LIST_ACTION = "REMOVE_USER_FROM_MAILING_BLACK_LIST";
public final static String FETCH_MAILING_BLACK_LIST_ACTION = "FETCH_MAILING_BLACK_LIST";
public final static String FETCH_USERS_FOR_AUTOCOMPLETE_IN_MAILING_BLACK_LIST_ACTION = "FETCH_USERS_FOR_AUTOCOMPLETE_IN_MAILING_BLACK_LIST";


// REST Services
Expand All @@ -135,6 +139,10 @@ public class ProjectManagerConst {
public final static String CREATE_PROJECT = "/create-project";
public final static String ACCEPT_PROJECT = "/accept-project";
public final static String REJECT_PROJECT = "/reject-project";
public final static String ADD_USER_TO_MAILING_BLACK_LIST = "/add-mailing-black-list";
public final static String REMOVE_USER_FROM_MAILING_BLACK_LIST = "/remove-mailing-black-list";
public final static String FETCH_MAILING_BLACK_LIST = "/mailing-black-list";
public final static String FETCH_USERS_FOR_AUTOCOMPLETE_IN_MAILING_BLACK_LIST = "/mailing-black-list-autocomplete";
public final static String ACCEPT_BRIDGEHEAD_PROJECT = "/accept-bridgehead-project";
public final static String REJECT_BRIDGEHEAD_PROJECT = "/reject-bridgehead-project";
public final static String ACCEPT_SCRIPT = "/accept-script";
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/de/samply/app/ProjectManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,44 @@ public ResponseEntity<String> rejectProject(
return convertToResponseEntity(() -> projectEventService.reject(projectCode));
}

@RoleConstraints(organisationRoles = OrganisationRole.PROJECT_MANAGER_ADMIN)
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE)
@FrontendAction(action = ProjectManagerConst.ADD_USER_TO_MAILING_BLACK_LIST_ACTION)
@PostMapping(value = ProjectManagerConst.ADD_USER_TO_MAILING_BLACK_LIST)
public ResponseEntity<String> addUserToMailingBlackList(
@RequestParam(name = ProjectManagerConst.EMAIL) String email
) {
return convertToResponseEntity(() -> userService.updateUserInMailingBlackList(email, true));
}

@RoleConstraints(organisationRoles = OrganisationRole.PROJECT_MANAGER_ADMIN)
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE)
@FrontendAction(action = ProjectManagerConst.REMOVE_USER_FROM_MAILING_BLACK_LIST_ACTION)
@PostMapping(value = ProjectManagerConst.REMOVE_USER_FROM_MAILING_BLACK_LIST)
public ResponseEntity<String> removeUserFromMailingBlackList(
@RequestParam(name = ProjectManagerConst.EMAIL) String email
) {
return convertToResponseEntity(() -> userService.updateUserInMailingBlackList(email, false));
}

@RoleConstraints(organisationRoles = OrganisationRole.PROJECT_MANAGER_ADMIN)
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE)
@FrontendAction(action = ProjectManagerConst.FETCH_MAILING_BLACK_LIST_ACTION)
@PostMapping(value = ProjectManagerConst.FETCH_MAILING_BLACK_LIST)
public ResponseEntity<String> fetchMailingBlackList() {
return convertToResponseEntity(() -> userService.fetchMailingBlackList());
}

@RoleConstraints(organisationRoles = OrganisationRole.PROJECT_MANAGER_ADMIN)
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE)
@FrontendAction(action = ProjectManagerConst.FETCH_USERS_FOR_AUTOCOMPLETE_IN_MAILING_BLACK_LIST_ACTION)
@PostMapping(value = ProjectManagerConst.FETCH_USERS_FOR_AUTOCOMPLETE_IN_MAILING_BLACK_LIST)
public ResponseEntity<String> fetchUsersForAutocompleteInMailingBlackList(
@RequestParam(name = ProjectManagerConst.EMAIL, required = false) String email
) {
return convertToResponseEntity(() -> userService.fetchUsersForAutocompleteInMailingBlackList(email));
}

@RoleConstraints(projectRoles = {ProjectRole.BRIDGEHEAD_ADMIN})
@StateConstraints(projectStates = {ProjectState.DEVELOP, ProjectState.PILOT, ProjectState.FINAL}, queryStates = {QueryState.FINISHED, QueryState.ERROR})
@EmailSender(templateType = EmailTemplateType.PROJECT_BRIDGEHEAD_ACCEPTED, recipients = {EmailRecipientType.PROJECT_MANAGER_ADMIN})
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/de/samply/db/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ public class User {
@Column(name = "last_name", nullable = false)
private String lastName;

@Column(name = "mailing_black_list", nullable = false)
private boolean isInMailingBlackList = false;

}
5 changes: 5 additions & 0 deletions src/main/java/de/samply/db/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long>
{
Optional<User> findByEmail(String email);

List<User> findByIsInMailingBlackListIsTrue();

List<User> findByEmailContainingIgnoreCase(String emailFragment);

}
4 changes: 2 additions & 2 deletions src/main/java/de/samply/email/EmailContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class EmailContext {
// If a key is Base64-encoded, it should start with the prefix "B64" to indicate its encoding.
@PostConstruct
public void init() {
log.info("Email Context initialized: ({})", context.entrySet().size());
log.debug("Email Context initialized: ({})", context.entrySet().size());
Map<String, String> tempContext = new HashMap<>();
context.entrySet().stream().forEach(keyValue -> {
String key = replaceHyphen(keyValue.getKey());
Expand All @@ -34,7 +34,7 @@ public void init() {
key = key.replaceFirst(ProjectManagerConst.BASE_64, "");
Base64Utils.decodeIfNecessary(keyValue.getValue()).ifPresent(value::set);
}
log.info("\t-\t{}: {}", key, value.get());
log.debug("\t-\t{}: {}", key, value.get());
tempContext.put(key, value.get());
});
context = tempContext;
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/de/samply/email/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.samply.app.ProjectManagerConst;
import de.samply.notification.NotificationService;
import de.samply.notification.OperationType;
import de.samply.user.UserService;
import de.samply.user.roles.ProjectRole;
import de.samply.utils.KeyTransformer;
import jakarta.mail.MessagingException;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class EmailService {
private final NotificationService notificationService;
private final EmailKeyValuesFactory emailKeyValuesFactory;
private final List<String> testMailDomains;
private final UserService userService;


public EmailService(
Expand All @@ -48,7 +50,8 @@ public EmailService(
EmailTemplates emailTemplates,
NotificationService notificationService,
EmailKeyValuesFactory emailKeyValuesFactory,
@Value(ProjectManagerConst.TEST_EMAIL_DOMAINS_SV) List<String> testMailDomains) {
@Value(ProjectManagerConst.TEST_EMAIL_DOMAINS_SV) List<String> testMailDomains,
UserService userService) {
this.emailFrom = emailFrom;
this.mailSender = mailSender;
this.testMailSender = testMailSender;
Expand All @@ -58,21 +61,17 @@ public EmailService(
this.notificationService = notificationService;
this.emailKeyValuesFactory = emailKeyValuesFactory;
this.testMailDomains = testMailDomains;
this.userService = userService;
}

@Async(ProjectManagerConst.ASYNC_EMAIL_SENDER_EXECUTOR)
public void sendEmail(@NotNull String emailTo, Optional<String> project, Optional<String> bridgehead, @NotNull ProjectRole role, @NotNull EmailTemplateType type) throws EmailServiceException {
if (enableEmails) {
sendEmail(emailTo, project, bridgehead, role, type, this.emailKeyValuesFactory.newInstance());
} else {
log.info("SMTP Server not enabled. Email to " + emailTo + " with role " + role + " for bridgehead " +
(bridgehead.isPresent() ? bridgehead.get() : "NONE") + " and type " + type + " could not be sent");
}
sendEmail(emailTo, project, bridgehead, role, type, this.emailKeyValuesFactory.newInstance());
}

@Async(ProjectManagerConst.ASYNC_EMAIL_SENDER_EXECUTOR)
public void sendEmail(@NotNull String emailTo, Optional<String> project, Optional<String> bridgehead, @NotNull ProjectRole role, @NotNull EmailTemplateType type, EmailKeyValues keyValues) throws EmailServiceException {
if (enableEmails) {
if (enableEmails && userService.isUserInMailingBlackList(emailTo)) {
project.ifPresent(keyValues::addProjectCode);
bridgehead.ifPresent(keyValues::addBridgehead);
Optional<MessageSubject> messageSubject = createEmailMessageAndSubject(role, type, keyValues);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/samply/frontend/dto/DtoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,8 @@ public Bridgehead convertToBridgehead(@NotNull String bridgehead) {
return humanReadable.isPresent() ? new Bridgehead(bridgehead, humanReadable.get()) : new Bridgehead(bridgehead, null);
}

public static User convert(de.samply.db.model.User user) {
return new User(user.getEmail(), user.getFirstName(), user.getLastName(), null, null, null, null);
}

}
32 changes: 27 additions & 5 deletions src/main/java/de/samply/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import de.samply.notification.OperationType;
import de.samply.project.state.UserProjectState;
import de.samply.security.SessionUser;
import de.samply.user.roles.OrganisationRole;
import de.samply.user.roles.OrganisationRoleToProjectRoleMapper;
import de.samply.user.roles.ProjectRole;
import de.samply.user.roles.UserProjectRoles;
Expand All @@ -17,10 +16,7 @@
import org.springframework.util.StringUtils;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -222,4 +218,30 @@ public void addUserInformationIfNotExists(String email, String firstName, String
}
}

public void updateUserInMailingBlackList(@NotNull String email, @NotNull boolean addedToBlackList) throws UserServiceException {
if (StringUtils.hasText(email)) {
Optional<de.samply.db.model.User> userOptional = userRepository.findByEmail(email);
if (userOptional.isPresent()) {
userOptional.get().setInMailingBlackList(addedToBlackList);
userRepository.save(userOptional.get());
}
}
}

public boolean isUserInMailingBlackList(@NotNull String email) throws UserServiceException {
if (StringUtils.hasText(email)) {
Optional<de.samply.db.model.User> userOptional = userRepository.findByEmail(email);
return userOptional.isPresent() && userOptional.get().isInMailingBlackList();
}
return false;
}

public List<User> fetchMailingBlackList(){
return userRepository.findByIsInMailingBlackListIsTrue().stream().map(DtoFactory::convert).collect(Collectors.toList());
}

public List<User> fetchUsersForAutocompleteInMailingBlackList(String email){
return userRepository.findByEmailContainingIgnoreCase(email).stream().map(DtoFactory::convert).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE SCHEMA IF NOT EXISTS samply;

SET
search_path TO samply;
search_path TO samply;

CREATE TABLE samply.query
(
Expand Down Expand Up @@ -129,10 +129,11 @@ CREATE TABLE samply.project_coder

CREATE TABLE samply.user
(
id SERIAL PRIMARY KEY,
email TEXT NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL
id SERIAL PRIMARY KEY,
email TEXT NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
mailing_black_list BOOLEAN NOT NULL
);

ALTER TABLE samply.project
Expand Down

0 comments on commit a493324

Please sign in to comment.