Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoayyed committed Sep 16, 2022
1 parent 2674e47 commit f15cbb3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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();
Expand All @@ -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)) {
Expand All @@ -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);
Expand All @@ -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};
}
}

Expand All @@ -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();
Expand All @@ -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<RegisteredServiceContact> contacts, final String email) {
Expand All @@ -279,11 +290,11 @@ private void checkOwner(final List<RegisteredServiceContact> 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) {
Expand All @@ -298,6 +309,7 @@ private static boolean isNumber(final String id) {
@Data
private static class DataPair {
private String left;

private RegisteredService right;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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()) {
Expand All @@ -87,21 +92,25 @@ 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);
}
}

/**
* 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();
Expand Down Expand Up @@ -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);
}
}

Expand Down
Loading

0 comments on commit f15cbb3

Please sign in to comment.