diff --git a/support/cas-mgmt-support-delegated/src/main/java/org/apereo/cas/mgmt/controller/PullController.java b/support/cas-mgmt-support-delegated/src/main/java/org/apereo/cas/mgmt/controller/PullController.java index 3b520f0ca0..1666ba13e9 100644 --- a/support/cas-mgmt-support-delegated/src/main/java/org/apereo/cas/mgmt/controller/PullController.java +++ b/support/cas-mgmt-support-delegated/src/main/java/org/apereo/cas/mgmt/controller/PullController.java @@ -9,6 +9,7 @@ import org.apereo.cas.mgmt.exception.VersionControlException; import org.apereo.cas.mgmt.factory.RepositoryFactory; import org.apereo.cas.notifications.CommunicationsManager; +import org.apereo.cas.notifications.mail.EmailMessageRequest; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; @@ -141,15 +142,21 @@ private void sendAcceptMessage(final String submitName, final String email) { if (communicationsManager.isMailSenderDefined()) { val emailProps = managementProperties.getDelegated().getNotifications().getAccept(); emailProps.setSubject(MessageFormat.format(emailProps.getSubject(), submitName)); - communicationsManager.email(emailProps, email, MessageFormat.format(emailProps.getText(), submitName)); + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProps.getText(), submitName)) + .to(List.of(email)).build(); + communicationsManager.email(request); } } private void sendRejectMessage(final String submitName, final String note, final String email) { if (communicationsManager.isMailSenderDefined()) { val emailProps = managementProperties.getDelegated().getNotifications().getReject(); + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProps.getText(), submitName, note)) + .to(List.of(email)).build(); emailProps.setSubject(MessageFormat.format(emailProps.getSubject(), submitName)); - communicationsManager.email(emailProps, email, MessageFormat.format(emailProps.getText(), submitName, note)); + communicationsManager.email(request); } } } diff --git a/support/cas-mgmt-support-delegated/src/main/java/org/apereo/cas/mgmt/controller/SubmitController.java b/support/cas-mgmt-support-delegated/src/main/java/org/apereo/cas/mgmt/controller/SubmitController.java index bbec25fd3e..887d233eee 100644 --- a/support/cas-mgmt-support-delegated/src/main/java/org/apereo/cas/mgmt/controller/SubmitController.java +++ b/support/cas-mgmt-support-delegated/src/main/java/org/apereo/cas/mgmt/controller/SubmitController.java @@ -7,6 +7,7 @@ import org.apereo.cas.mgmt.domain.BranchData; import org.apereo.cas.mgmt.factory.RepositoryFactory; import org.apereo.cas.notifications.CommunicationsManager; +import org.apereo.cas.notifications.mail.EmailMessageRequest; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; @@ -127,7 +128,8 @@ private void sendSubmitMessage(final String submitName, final CasUserProfile use if (communicationsManager.isMailSenderDefined()) { val emailProps = managementProperties.getDelegated().getNotifications().getSubmit(); emailProps.setSubject(MessageFormat.format(emailProps.getSubject(), submitName)); - communicationsManager.email(emailProps, user.getEmail(), emailProps.getText()); + val request = EmailMessageRequest.builder().body(emailProps.getText()).to(List.of(user.getEmail())).build(); + communicationsManager.email(request); } } } diff --git a/support/cas-mgmt-support-register/src/main/java/org/apereo/cas/mgmt/BaseRegisterController.java b/support/cas-mgmt-support-register/src/main/java/org/apereo/cas/mgmt/BaseRegisterController.java index bb97048dde..91a2b62541 100644 --- a/support/cas-mgmt-support-register/src/main/java/org/apereo/cas/mgmt/BaseRegisterController.java +++ b/support/cas-mgmt-support-register/src/main/java/org/apereo/cas/mgmt/BaseRegisterController.java @@ -7,6 +7,7 @@ import org.apereo.cas.mgmt.factory.VersionControlManagerFactory; import org.apereo.cas.mgmt.util.CasManagementUtils; import org.apereo.cas.notifications.CommunicationsManager; +import org.apereo.cas.notifications.mail.EmailMessageRequest; import org.apereo.cas.services.RegexRegisteredService; import org.apereo.cas.services.RegisteredService; import org.apereo.cas.services.RegisteredServiceContact; @@ -80,7 +81,7 @@ public abstract class BaseRegisterController { public BaseRegisterController(final VersionControlManagerFactory managerFactory, final CasManagementConfigurationProperties managementProperties, final CommunicationsManager communicationsManager, - final ServicesManager published){ + final ServicesManager published) { this.managerFactory = managerFactory; this.managementProperties = managementProperties; this.communicationsManager = communicationsManager; @@ -92,15 +93,16 @@ public BaseRegisterController(final VersionControlManagerFactory managerFactory, * Mapped method that accepts a submitted service by end user and adds is to Submissions queue. * * @param authentication - the user - * @param service - the Service to be submitted + * @param service - the Service to be submitted */ @PostMapping @ResponseStatus(HttpStatus.OK) @SneakyThrows public void submit(final Authentication authentication, - @RequestBody final RegisteredService service) { - val id = service.getId() > 0 ? service.getId() : new Date().getTime(); - val path = Paths.get(managementProperties.getSubmissions().getSubmitDir() + "/submit-" + id +".json"); + @RequestBody + final RegisteredService service) { + val id = service.getId() > 0 ? service.getId() : System.nanoTime(); + val path = Paths.get(managementProperties.getSubmissions().getSubmitDir() + "/submit-" + id + ".json"); val out = Files.newOutputStream(path); CasManagementUtils.jsonTo(out, service); out.close(); @@ -109,7 +111,7 @@ public void submit(final Authentication authentication, sendMessage(casUserProfile, copyEmail(notifications.getSubmit()), service.getName(), service.getName()); } - private EmailProperties copyEmail(final EmailProperties source) { + private static EmailProperties copyEmail(final EmailProperties source) { val emailProps = new EmailProperties(); emailProps.setSubject(source.getSubject()); emailProps.setAttributeName(source.getAttributeName()); @@ -127,13 +129,14 @@ private EmailProperties copyEmail(final EmailProperties source) { * Mapped method to handle updating a service submitted by a user. * * @param authentication - the user - * @param pair - the Service to update + * @param pair - the Service to update */ @PatchMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) @SneakyThrows public void registerSave(final Authentication authentication, - @RequestBody final DataPair pair) { + @RequestBody + final DataPair pair) { val service = pair.getRight(); val id = pair.getLeft(); val casUserProfile = CasUserProfile.from(authentication); @@ -144,13 +147,14 @@ public void registerSave(final Authentication authentication, * Request to delete a service. * * @param authentication - the user - * @param id - the id + * @param id - the id */ @DeleteMapping("{id}") @ResponseStatus(HttpStatus.OK) @SneakyThrows public void remove(final Authentication authentication, - @PathVariable final String id) { + @PathVariable + final String id) { val casUserProfile = CasUserProfile.from(authentication); val manager = managerFactory.master(); val service = manager.findServiceBy(Long.parseLong(id)); @@ -166,13 +170,14 @@ public void remove(final Authentication authentication, * Mapped method that returns the RegisteredService by the passed Id form the master repo. * * @param authentication - the user - * @param id - assigned id of the service + * @param id - assigned id of the service * @return - the requested RegisteredService */ @GetMapping("{id}") @SneakyThrows public RegisteredService getRegisterService(final Authentication authentication, - @PathVariable final String id) { + @PathVariable + final String id) { val casUserProfile = CasUserProfile.from(authentication); val email = casUserProfile.getEmail(); val manager = managerFactory.master(); @@ -185,14 +190,15 @@ public RegisteredService getRegisterService(final Authentication authentication, * Method will cancel a pending submission. * * @param authentication - the user - * @param id - id of pending submission + * @param id - id of pending submission * @throws IllegalAccessException - Insufficient permissions - * @throws IOException - failed to delete file + * @throws IOException - failed to delete file */ @DeleteMapping("cancel") @ResponseStatus(HttpStatus.OK) public void cancel(final Authentication authentication, - @RequestParam final String id) throws IllegalAccessException, IOException { + @RequestParam + final String id) throws IllegalAccessException, IOException { val casUserProfile = CasUserProfile.from(authentication); val service = Paths.get(managementProperties.getSubmissions().getSubmitDir() + '/' + id); if (!isSubmitter(service, casUserProfile)) { @@ -204,13 +210,15 @@ public void cancel(final Authentication authentication, /** * Submits a request to promote a service. * - * @param id - the id + * @param id - the id * @param authentication - the user */ @GetMapping("promote/{id}") @SneakyThrows - public void promote(@PathVariable final Long id, - final Authentication authentication) { + public void promote( + @PathVariable + final Long id, + final Authentication authentication) { val casUserProfile = CasUserProfile.from(authentication); val manager = managerFactory.master(); val service = manager.findServiceBy(id); @@ -226,11 +234,11 @@ private static String[] getSubmitter(final Path path) { try { val email = new byte[MAX_EMAIL_LENGTH]; Files.getFileAttributeView(path, UserDefinedFileAttributeView.class) - .read("original_author", ByteBuffer.wrap(email)); + .read("original_author", ByteBuffer.wrap(email)); return new String(email, StandardCharsets.UTF_8).trim().split(":"); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); - return new String[] {StringUtils.EMPTY, StringUtils.EMPTY}; + return new String[]{StringUtils.EMPTY, StringUtils.EMPTY}; } } @@ -240,21 +248,24 @@ private void sendMessage(final CasUserProfile user, final String subjectArg) { if (communicationsManager.isMailSenderDefined()) { emailProperties.setSubject(MessageFormat.format(emailProperties.getSubject(), subjectArg)); - communicationsManager.email(emailProperties, user.getEmail(), MessageFormat.format(emailProperties.getText(), textArg)); + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProperties.getText(), textArg)) + .to(List.of(user.getEmail())).build(); + communicationsManager.email(request); } } /** * Saves a submitted service. * - * @param service - the service - * @param id - the id of the service + * @param service - the service + * @param id - the id of the service * @param casUserProfile - user profile * @throws IOException - failed to save file */ protected void saveService(final RegisteredService service, final String id, final CasUserProfile casUserProfile) throws IOException { val path = isNumber(id) ? Paths.get(managementProperties.getSubmissions().getSubmitDir() + "/edit-" + service.getId() + ".json") - : Paths.get(managementProperties.getSubmissions().getSubmitDir() + '/' + id); + : Paths.get(managementProperties.getSubmissions().getSubmitDir() + '/' + id); val out = Files.newOutputStream(path); CasManagementUtils.JSON_SERIALIZER.to(out, service); out.close(); @@ -266,7 +277,7 @@ protected void saveService(final RegisteredService service, final String id, fin * Returns the contact if the passed email is an owner of the service or null. * * @param contacts - List of contacts for a service. - * @param email - Email to search for + * @param email - Email to search for * @return - RegisteredServiceContact or null */ protected RegisteredServiceContact owner(final List contacts, final String email) { @@ -279,11 +290,11 @@ private void checkOwner(final List contacts, final Str } } - private static void setSubmitter(final Path path, final CasUserProfile casUserProfile) throws IOException{ + private static void setSubmitter(final Path path, final CasUserProfile casUserProfile) throws IOException { val payload = casUserProfile.getEmail() + ':' + casUserProfile.getFirstName() + ' ' - + casUserProfile.getFamilyName(); + + casUserProfile.getFamilyName(); Files.getFileAttributeView(path, UserDefinedFileAttributeView.class) - .write("original_author", ByteBuffer.wrap(payload.getBytes(StandardCharsets.UTF_8))); + .write("original_author", ByteBuffer.wrap(payload.getBytes(StandardCharsets.UTF_8))); } private static boolean isNumber(final String id) { @@ -298,6 +309,7 @@ private static boolean isNumber(final String id) { @Data private static class DataPair { private String left; + private RegisteredService right; } } diff --git a/support/cas-mgmt-support-register/src/main/java/org/apereo/cas/mgmt/BulkActionController.java b/support/cas-mgmt-support-register/src/main/java/org/apereo/cas/mgmt/BulkActionController.java index 99ffc4b63a..0030b30b32 100644 --- a/support/cas-mgmt-support-register/src/main/java/org/apereo/cas/mgmt/BulkActionController.java +++ b/support/cas-mgmt-support-register/src/main/java/org/apereo/cas/mgmt/BulkActionController.java @@ -5,6 +5,7 @@ import org.apereo.cas.mgmt.factory.RepositoryFactory; import org.apereo.cas.mgmt.factory.VersionControlManagerFactory; import org.apereo.cas.notifications.CommunicationsManager; +import org.apereo.cas.notifications.mail.EmailMessageRequest; import org.apereo.cas.services.RegisteredServiceContact; import lombok.RequiredArgsConstructor; @@ -37,22 +38,26 @@ public class BulkActionController { private final VersionControlManagerFactory managerFactory; + private final CasManagementConfigurationProperties managementProperties; + private final RepositoryFactory repositoryFactory; + private final CommunicationsManager communicationsManager; /** * Method used to remove the logged in user as contact from multiple services. * * @param authentication - the user - * @param services - Array of service IDs + * @param services - Array of service IDs * @throws IllegalAccessException - Does not own service - * @throws IllegalStateException - Service requires at least one contact + * @throws IllegalStateException - Service requires at least one contact */ @PostMapping("unclaim") @ResponseStatus(HttpStatus.OK) public void bulkUnclaim(final Authentication authentication, - @RequestBody final String[] services) throws IllegalStateException, IllegalAccessException { + @RequestBody + final String[] services) throws IllegalStateException, IllegalAccessException { val casUserProfile = CasUserProfile.from(authentication); val email = casUserProfile.getEmail(); val timestamp = new Date().getTime(); @@ -71,7 +76,7 @@ public void bulkUnclaim(final Authentication authentication, git.close(); removeClone(clone); throw new IllegalStateException("You are the only contact for service: '" + service.getName() - + "'. A second contact must be added before you can remove yourself."); + + "'. A second contact must be added before you can remove yourself."); } } if (git.scanWorkingDiffs().isEmpty()) { @@ -87,7 +92,10 @@ public void bulkUnclaim(final Authentication authentication, private void sendBulkRemoveMessage(final String services, final CasUserProfile user) { if (communicationsManager.isMailSenderDefined()) { val emailProps = managementProperties.getRegister().getBulkNotifications().getRemove(); - communicationsManager.email(emailProps, user.getEmail(), MessageFormat.format(emailProps.getText(), services)); + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProps.getText(), services)) + .to(List.of(user.getEmail())).build(); + communicationsManager.email(request); } } @@ -95,13 +103,14 @@ private void sendBulkRemoveMessage(final String services, final CasUserProfile u * Method will add the logged in user as contact to multiple services. * * @param authentication - the user - * @param services - Array of Service IDs + * @param services - Array of Service IDs * @throws IllegalStateException - failed */ @PostMapping("claim") @ResponseStatus(HttpStatus.OK) public void bulkclaim(final Authentication authentication, - @RequestBody final String[] services) throws IllegalStateException { + @RequestBody + final String[] services) throws IllegalStateException { val casUserProfile = CasUserProfile.from(authentication); val email = casUserProfile.getEmail(); val timestamp = new Date().getTime(); @@ -134,7 +143,10 @@ public void bulkclaim(final Authentication authentication, private void sendBulkAddMessage(final String services, final CasUserProfile user) { if (communicationsManager.isMailSenderDefined()) { val emailProps = managementProperties.getRegister().getBulkNotifications().getAdd(); - communicationsManager.email(emailProps, user.getEmail(), MessageFormat.format(emailProps.getText(), services)); + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProps.getText(), services)) + .to(List.of(user.getEmail())).build(); + communicationsManager.email(request); } } diff --git a/support/cas-mgmt-support-submissions/src/main/java/org/apereo/cas/mgmt/SubmissionController.java b/support/cas-mgmt-support-submissions/src/main/java/org/apereo/cas/mgmt/SubmissionController.java index 2108df70e3..984d6b3930 100644 --- a/support/cas-mgmt-support-submissions/src/main/java/org/apereo/cas/mgmt/SubmissionController.java +++ b/support/cas-mgmt-support-submissions/src/main/java/org/apereo/cas/mgmt/SubmissionController.java @@ -11,6 +11,7 @@ import org.apereo.cas.mgmt.factory.RepositoryFactory; import org.apereo.cas.mgmt.util.CasManagementUtils; import org.apereo.cas.notifications.CommunicationsManager; +import org.apereo.cas.notifications.mail.EmailMessageRequest; import org.apereo.cas.services.RegisteredService; import org.apereo.cas.services.ServicesManager; import org.apereo.cas.services.UnauthorizedServiceException; @@ -283,7 +284,10 @@ private void sendRejectMessage(final String submitName, final String note, final val notifications = managementProperties.getSubmissions().getNotifications(); val emailProps = isChange ? copyEmail(notifications.getRejectChange()) : copyEmail(notifications.getReject()); emailProps.setSubject(MessageFormat.format(emailProps.getSubject(), submitName)); - communicationsManager.email(emailProps, email, MessageFormat.format(emailProps.getText(), submitName, note)); + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProps.getText(), submitName, note)) + .to(List.of(email)).build(); + communicationsManager.email(request); } } @@ -310,7 +314,10 @@ private void sendAddedMessage(final String submitName, final String note, final if (communicationsManager.isMailSenderDefined()) { val emailProps = copyEmail(managementProperties.getSubmissions().getNotifications().getAdded()); emailProps.setSubject(MessageFormat.format(emailProps.getSubject(), submitName)); - communicationsManager.email(emailProps, email, MessageFormat.format(emailProps.getText(), submitName, note)); + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProps.getText(), submitName, note)) + .to(List.of(email)).build(); + communicationsManager.email(request); } } /** @@ -361,7 +368,10 @@ private void sendAcceptMessage(final String submitName, final String email) { if (communicationsManager.isMailSenderDefined()) { val emailProps = copyEmail(managementProperties.getSubmissions().getNotifications().getAccept()); emailProps.setSubject(MessageFormat.format(emailProps.getSubject(), submitName)); - communicationsManager.email(emailProps, email, MessageFormat.format(emailProps.getText(), submitName)); + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProps.getText(), submitName)) + .to(List.of(email)).build(); + communicationsManager.email(request); } } @@ -390,7 +400,11 @@ private void sendDeleteMessage(final String submitName, final String email) { if (communicationsManager.isMailSenderDefined()) { val emailProps = copyEmail(managementProperties.getSubmissions().getNotifications().getDelete()); emailProps.setSubject(MessageFormat.format(emailProps.getSubject(), submitName)); - communicationsManager.email(emailProps, email, MessageFormat.format(emailProps.getText(), submitName)); + + val request = EmailMessageRequest.builder() + .body(MessageFormat.format(emailProps.getText(), submitName)) + .to(List.of(email)).build(); + communicationsManager.email(request); } }