From 4850a3d06e64ad02acd00341195473eb98f0ef01 Mon Sep 17 00:00:00 2001 From: djuarezgf Date: Fri, 27 Dec 2024 10:53:26 +0100 Subject: [PATCH] Added: Mailing Black List --- CHANGELOG.md | 3 ++- .../de/samply/app/ProjectManagerConst.java | 4 ++++ .../samply/app/ProjectManagerController.java | 24 +++++++++++++++++++ src/main/java/de/samply/db/model/User.java | 3 +++ .../java/de/samply/email/EmailService.java | 15 ++++++------ src/main/java/de/samply/user/UserService.java | 18 ++++++++++++++ .../V001__initialize_schema_and_tables.sql | 11 +++++---- 7 files changed, 64 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2edf0b..c98ebec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/src/main/java/de/samply/app/ProjectManagerConst.java b/src/main/java/de/samply/app/ProjectManagerConst.java index bd43c42..98178d6 100644 --- a/src/main/java/de/samply/app/ProjectManagerConst.java +++ b/src/main/java/de/samply/app/ProjectManagerConst.java @@ -111,6 +111,8 @@ 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"; // REST Services @@ -135,6 +137,8 @@ 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 ACCEPT_BRIDGEHEAD_PROJECT = "/accept-bridgehead-project"; public final static String REJECT_BRIDGEHEAD_PROJECT = "/reject-bridgehead-project"; public final static String ACCEPT_SCRIPT = "/accept-script"; diff --git a/src/main/java/de/samply/app/ProjectManagerController.java b/src/main/java/de/samply/app/ProjectManagerController.java index 5ffaf80..b3f9738 100644 --- a/src/main/java/de/samply/app/ProjectManagerController.java +++ b/src/main/java/de/samply/app/ProjectManagerController.java @@ -500,6 +500,30 @@ public ResponseEntity rejectProject( return convertToResponseEntity(() -> projectEventService.reject(projectCode)); } + @RoleConstraints(projectRoles = {ProjectRole.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 addUserToMailingBlackList( + @ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode, + @Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD) String bridgehead, + @RequestParam(name = ProjectManagerConst.EMAIL) String email + ) { + return convertToResponseEntity(() -> userService.updateUserInMailingBlackList(email, true)); + } + + @RoleConstraints(projectRoles = {ProjectRole.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 removeUserFromMailingBlackList( + @ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode, + @Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD) String bridgehead, + @RequestParam(name = ProjectManagerConst.EMAIL) String email + ) { + return convertToResponseEntity(() -> userService.updateUserInMailingBlackList(email, false)); + } + @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}) diff --git a/src/main/java/de/samply/db/model/User.java b/src/main/java/de/samply/db/model/User.java index d633a28..202152a 100644 --- a/src/main/java/de/samply/db/model/User.java +++ b/src/main/java/de/samply/db/model/User.java @@ -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; + } diff --git a/src/main/java/de/samply/email/EmailService.java b/src/main/java/de/samply/email/EmailService.java index ec5cecd..2232a41 100644 --- a/src/main/java/de/samply/email/EmailService.java +++ b/src/main/java/de/samply/email/EmailService.java @@ -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; @@ -37,6 +38,7 @@ public class EmailService { private final NotificationService notificationService; private final EmailKeyValuesFactory emailKeyValuesFactory; private final List testMailDomains; + private final UserService userService; public EmailService( @@ -48,7 +50,8 @@ public EmailService( EmailTemplates emailTemplates, NotificationService notificationService, EmailKeyValuesFactory emailKeyValuesFactory, - @Value(ProjectManagerConst.TEST_EMAIL_DOMAINS_SV) List testMailDomains) { + @Value(ProjectManagerConst.TEST_EMAIL_DOMAINS_SV) List testMailDomains, + UserService userService) { this.emailFrom = emailFrom; this.mailSender = mailSender; this.testMailSender = testMailSender; @@ -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 project, Optional 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 project, Optional 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 = createEmailMessageAndSubject(role, type, keyValues); diff --git a/src/main/java/de/samply/user/UserService.java b/src/main/java/de/samply/user/UserService.java index 26fad57..f1ddf00 100644 --- a/src/main/java/de/samply/user/UserService.java +++ b/src/main/java/de/samply/user/UserService.java @@ -222,4 +222,22 @@ public void addUserInformationIfNotExists(String email, String firstName, String } } + public void updateUserInMailingBlackList(@NotNull String email, @NotNull boolean addedToBlackList) throws UserServiceException { + if (StringUtils.hasText(email)) { + Optional 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 userOptional = userRepository.findByEmail(email); + return userOptional.isPresent() && userOptional.get().isInMailingBlackList(); + } + return false; + } + } diff --git a/src/main/resources/db/migration/V001__initialize_schema_and_tables.sql b/src/main/resources/db/migration/V001__initialize_schema_and_tables.sql index 81110bd..b723420 100644 --- a/src/main/resources/db/migration/V001__initialize_schema_and_tables.sql +++ b/src/main/resources/db/migration/V001__initialize_schema_and_tables.sql @@ -1,7 +1,7 @@ CREATE SCHEMA IF NOT EXISTS samply; SET -search_path TO samply; + search_path TO samply; CREATE TABLE samply.query ( @@ -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