Skip to content

Commit

Permalink
Added new Notifications (#2841)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheshiriks authored Nov 3, 2023
1 parent 0875faf commit 64f6f3b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.saveourtool.save.evententities

import com.saveourtool.save.entities.User

/**
* @property user
**/
data class UserEvent(
var user: User
)

0 comments on commit 64f6f3b

Please sign in to comment.