diff --git a/pom.xml b/pom.xml index c8dc829..d633a16 100644 --- a/pom.xml +++ b/pom.xml @@ -163,6 +163,12 @@ 1.8.0 + + fr.recia.notifications + event-rest-client-kafka + 1.0.14-java-11 + + diff --git a/src/main/java/fr/recia/mce/api/escomceapi/aop/notification/NotificationAspect.java b/src/main/java/fr/recia/mce/api/escomceapi/aop/notification/NotificationAspect.java new file mode 100644 index 0000000..8150a27 --- /dev/null +++ b/src/main/java/fr/recia/mce/api/escomceapi/aop/notification/NotificationAspect.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2023 GIP-RECIA, Inc. + * + * 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 fr.recia.mce.api.escomceapi.aop.notification; + +import fr.recia.mce.api.escomceapi.aop.notification.configuration.NotificationAspectConfig; +import fr.recia.notifications.event_rest_client_kafka.HttpNotificationClient; +import fr.recia.notifications.model_kafka.model.*; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.stereotype.Component; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Locale; + + +@Component +@Aspect +@Slf4j +@Data +public class NotificationAspect { + private final HttpNotificationClient notificationClient; + private final NotificationAspectConfig notificationAspectConfig; + + + private static final DateTimeFormatter DATE_TIME_FORMATTER = + DateTimeFormatter.ofPattern("dd/MM/yyyy 'à' HH:mm", Locale.FRENCH); + + private String buildMessage(String template) { + return template + ZonedDateTime.now().format(DATE_TIME_FORMATTER); + } + + @After("execution(* fr.recia.mce.api.escomceapi.ldap.repository.LdapUserDaoImp.updatePassword(..)) && args(uid, newHashedPassword)") + public void notifPasswordChange(String uid, String newHashedPassword) { + try { + final String title = notificationAspectConfig.getTitleMdp(); + final String message = buildMessage(notificationAspectConfig.getMessageMdp()); + final List channel = List.of(Channel.WEB); + final String idLink = ""; + final Priority priority = Priority.HIGH; + final TargetType targetType = TargetType.UID; + notificationClient.sendNotification(title, message, idLink, uid, channel, priority, targetType); + log.debug("Notification pour changement de mot de passe de {} a été mis à jour", uid); + } catch (Exception e) { + log.error("Erreur: la notification n'a pas pu être envoyée", e); + } + } + + @After("execution(* fr.recia.mce.api.escomceapi.services.PersonneService.updateEmail(..)) && args(uid, ..)") + public void notifEmailChange(String uid) { + try { + final String title = notificationAspectConfig.getTitleMail(); + final String message = buildMessage(notificationAspectConfig.getMessageMail()); + final List channel = List.of(Channel.WEB); + final String idLink = ""; + final Priority priority = Priority.HIGH ; + final TargetType targetType = TargetType.UID; + notificationClient.sendNotification(title, message, idLink, uid, channel, priority, targetType); + log.info("Notification pour changement de mail {} a été envoyé", uid); + } catch (Exception e) { + log.error("Erreur: la notification n'a pas pu être envoyée", e); + } + } +} diff --git a/src/main/java/fr/recia/mce/api/escomceapi/aop/notification/configuration/NotificationAspectConfig.java b/src/main/java/fr/recia/mce/api/escomceapi/aop/notification/configuration/NotificationAspectConfig.java new file mode 100644 index 0000000..f8af189 --- /dev/null +++ b/src/main/java/fr/recia/mce/api/escomceapi/aop/notification/configuration/NotificationAspectConfig.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2023 GIP-RECIA, Inc. + * + * 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 fr.recia.mce.api.escomceapi.aop.notification.configuration; + +import fr.recia.notifications.event_rest_client_kafka.HttpNotificationClient; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Data +@Configuration +@ConfigurationProperties(prefix = "notification-api") +public class NotificationAspectConfig { + private String url; + private String serviceName; + private String apiKey; + private String titleMdp; + private String messageMdp; + private String titleMail; + private String messageMail; + + @Bean + public HttpNotificationClient notificationClient() { + return new HttpNotificationClient(url, serviceName, apiKey); + } +} diff --git a/src/main/resources/application.example.yml b/src/main/resources/application.example.yml index 61eeaff..55df396 100644 --- a/src/main/resources/application.example.yml +++ b/src/main/resources/application.example.yml @@ -185,5 +185,11 @@ mail: regexValideAddr: '[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z-]{2,4})' regexsDomainesExclus: 'netocentre.fr touraine-eschool.fr chercan.fr colleges41.fr mon-e-college.loiret.fr e-college.indre.fr colleges-eureliens.fr' verification: + base-url: http://localhost:8080/api/personne/mce/verify-email frontend-url: http://localhost:5173/verification-email - expiry-hours: 1 + expiry-hours: 24 + +notification-api: + url: "localhost:8079/event/emit" + service-name: "Service Name" + api-key: "MySecretApiKey"