From 64f6f3bc3c1dcba579f917dcccd9a61f167960c0 Mon Sep 17 00:00:00 2001 From: Vladislav Frolov <50615459+Cheshiriks@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:37:47 +0300 Subject: [PATCH] Added new Notifications (#2841) --- .../event/VulnerabilityMetadataListener.kt | 30 ++++++++++++++++--- .../save/backend/event/UserListener.kt | 27 +++++++++++++---- .../backend/service/UserDetailsService.kt | 12 ++++---- .../save/evententities/UserEvent.kt | 10 +++++++ 4 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/evententities/UserEvent.kt diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/vulnerability/event/VulnerabilityMetadataListener.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/vulnerability/event/VulnerabilityMetadataListener.kt index 9c1e10287a..0fdd3bb072 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/vulnerability/event/VulnerabilityMetadataListener.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/vulnerability/event/VulnerabilityMetadataListener.kt @@ -23,12 +23,18 @@ class VulnerabilityMetadataListener( */ @EventListener fun createVulnerabilityMetadata(vulnerabilityEvent: VulnerabilityMetadataEvent) { - if (vulnerabilityEvent.vulnerabilityMetadata.status == VulnerabilityStatus.CREATED) { + val newMessage = when (vulnerabilityEvent.vulnerabilityMetadata.status) { + VulnerabilityStatus.CREATED -> messageNewVulnerabilityMetadata(vulnerabilityEvent.vulnerabilityMetadata) + VulnerabilityStatus.AUTO_APPROVED -> messageNewVulnerabilityMetadataAutoApproved(vulnerabilityEvent.vulnerabilityMetadata) + VulnerabilityStatus.PENDING_REVIEW -> messageUpdatedVulnerabilityMetadata(vulnerabilityEvent.vulnerabilityMetadata) + else -> null + } + newMessage?.let { message -> val recipients = userDetailsService.findByRole(Role.SUPER_ADMIN.asSpringSecurityRole()) - val notifications = recipients.map { + val notifications = recipients.map { user -> Notification( - message = messageNewVulnerabilityMetadata(vulnerabilityEvent.vulnerabilityMetadata), - user = it, + message = message, + user = user, ) } notificationService.saveAll(notifications) @@ -43,5 +49,21 @@ class VulnerabilityMetadataListener( fun messageNewVulnerabilityMetadata(vulnerability: VulnerabilityMetadata) = """ New vulnerability: ${vulnerability.identifier} is waiting for approve. """.trimIndent() + + /** + * @param vulnerability + * @return message + */ + fun messageNewVulnerabilityMetadataAutoApproved(vulnerability: VulnerabilityMetadata) = """ + New uploaded vulnerability: ${vulnerability.identifier} has been auto approved. + """.trimIndent() + + /** + * @param vulnerability + * @return message + */ + fun messageUpdatedVulnerabilityMetadata(vulnerability: VulnerabilityMetadata) = """ + Vulnerability: ${vulnerability.identifier} has been updated and waiting for approve. + """.trimIndent() } } diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/event/UserListener.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/event/UserListener.kt index b866aebcd2..dce1cdc571 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/event/UserListener.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/event/UserListener.kt @@ -5,6 +5,7 @@ import com.saveourtool.save.backend.service.UserDetailsService import com.saveourtool.save.domain.Role import com.saveourtool.save.entities.Notification import com.saveourtool.save.entities.User +import com.saveourtool.save.evententities.UserEvent import com.saveourtool.save.info.UserStatus import org.springframework.context.event.EventListener import org.springframework.stereotype.Component @@ -18,16 +19,22 @@ class UserListener( private val notificationService: NotificationService, ) { /** - * @param user new user + * @param userEvent new userEvent */ @EventListener - fun createUser(user: User) { - if (user.status == UserStatus.NOT_APPROVED) { + fun createUser(userEvent: UserEvent) { + val newMessage = when (userEvent.user.status) { + UserStatus.NOT_APPROVED -> messageNewUser(userEvent.user) + UserStatus.BANNED -> messageBanUser(userEvent.user) + else -> null + } + + newMessage?.let { message -> val recipients = userDetailsService.findByRole(Role.SUPER_ADMIN.asSpringSecurityRole()) - val notifications = recipients.map { + val notifications = recipients.map { user -> Notification( - message = messageNewUser(user), - user = it, + message = message, + user = user, ) } notificationService.saveAll(notifications) @@ -42,5 +49,13 @@ class UserListener( fun messageNewUser(user: User) = """ New user: ${user.name} is waiting for approve of his account. """.trimIndent() + + /** + * @param user + * @return message + */ + fun messageBanUser(user: User) = """ + User: ${user.name} has been banned. + """.trimIndent() } } diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/service/UserDetailsService.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/service/UserDetailsService.kt index e2d4b5bb1b..2e950d6b1b 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/service/UserDetailsService.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/service/UserDetailsService.kt @@ -11,6 +11,7 @@ import com.saveourtool.save.domain.Role import com.saveourtool.save.domain.UserSaveStatus import com.saveourtool.save.entities.OriginalLogin import com.saveourtool.save.entities.User +import com.saveourtool.save.evententities.UserEvent import com.saveourtool.save.info.UserStatus import com.saveourtool.save.utils.* import org.slf4j.Logger @@ -129,7 +130,7 @@ class UserDetailsService( */ @Transactional fun saveUser(newUser: User, oldName: String?, oldUserStatus: UserStatus): UserSaveStatus { - applicationEventPublisher.publishEvent(newUser) + applicationEventPublisher.publishEvent(UserEvent(newUser)) val isNameFreeAndNotTaken = userRepository.validateName(newUser.name) != 0L // if we are registering new user (updating just name and status to NOT_APPROVED): return if (oldUserStatus == UserStatus.CREATED && newUser.status == UserStatus.NOT_APPROVED) { @@ -272,11 +273,12 @@ class UserDetailsService( fun banUser( name: String, ): UserSaveStatus { - val user: User = userRepository.findByName(name).orNotFound() - - userRepository.save(user.apply { + val user: User = userRepository.findByName(name).orNotFound().apply { this.status = UserStatus.BANNED - }) + } + applicationEventPublisher.publishEvent(UserEvent(user)) + + userRepository.save(user) return UserSaveStatus.BANNED } diff --git a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/evententities/UserEvent.kt b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/evententities/UserEvent.kt new file mode 100644 index 0000000000..4e79e18b5b --- /dev/null +++ b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/evententities/UserEvent.kt @@ -0,0 +1,10 @@ +package com.saveourtool.save.evententities + +import com.saveourtool.save.entities.User + +/** + * @property user + **/ +data class UserEvent( + var user: User +)