Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker-compose-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
volumes:
- maven-cache:/root/.m2
environment:
- ACTIVE_PROFILE
- APP_VERSION
- SERVER_URL
- JWT_SECRET=${JWT_SECRET}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
volumes:
- maven-cache:/root/.m2
environment:
- ACTIVE_PROFILE
- APP_VERSION
- SERVER_URL
- JWT_SECRET=${JWT_SECRET}
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
</properties>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
Expand Down
27 changes: 0 additions & 27 deletions src/main/java/com/api/twitter/common/events/UserCreatedEvent.java

This file was deleted.

14 changes: 14 additions & 0 deletions src/main/java/com/api/twitter/common/events/UserCreatedEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.api.twitter.common.events;

import org.springframework.context.ApplicationEvent;

import java.util.UUID;

data class UserCreatedEvent(
val userId: UUID,
val username: String,
val email: String,
private val objSource: Any
) : ApplicationEvent(objSource) {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.api.twitter.security.application.usecases

import com.api.twitter.common.events.UserCreatedEvent
import com.api.twitter.common.exception.BadRequestException
import com.api.twitter.common.model.UserRole
import com.api.twitter.security.domain.model.UserCredentials
import com.api.twitter.security.infrastructure.persistence.UserCredentialsRepository
import org.springframework.context.ApplicationEventPublisher
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Component
import java.time.LocalDateTime

@Component
class RegisterUserCredentialsUseCase(
private val userCredentialsRepository: UserCredentialsRepository,
private val passwordEncoder: PasswordEncoder,
private val applicationEventPublisher: ApplicationEventPublisher
) {
fun execute(username: String, email: String, password: String) {
verifyIfUsernameOrEmailIsAlreadyInUse(username, email)

val userCredentials = UserCredentials(
email = email,
rawPassword = password,
rawUsername = username,
role = UserRole.USER,
createdAt = LocalDateTime.now(),
).apply {
this.validate()
}.also {
it.rawPassword = passwordEncoder.encode(password)
?: throw RuntimeException("Invalid encrypted password")
}
val user = userCredentialsRepository.save<UserCredentials>(encodePassword(userCredentials))
publishUserCreatedEvent(user)
}

private fun verifyIfUsernameOrEmailIsAlreadyInUse(username: String, email: String) {
if (userCredentialsRepository.existsByUsername(username))
throw BadRequestException("This username is already in use")
if (userCredentialsRepository.existsByEmail(email))
throw BadRequestException("This email is already in use")
}

private fun encodePassword(userCredentials: UserCredentials): UserCredentials {
userCredentials.rawPassword = passwordEncoder.encode(userCredentials.rawPassword)
?: throw RuntimeException("Invalid encrypted password")
return userCredentials
}

private fun publishUserCreatedEvent(user: UserCredentials) {
applicationEventPublisher.publishEvent(
UserCreatedEvent(
userId = user.userId ?: throw BadRequestException("User id is null"),
username = user.rawUsername,
email = user.email,
objSource = this
)
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.api.twitter.security.application.usecases

import com.api.twitter.common.exception.BadRequestException
import com.api.twitter.security.domain.model.UserCredentials
import com.api.twitter.security.infrastructure.persistence.UserCredentialsRepository
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Component
import java.util.*
import java.util.function.Supplier
import kotlin.jvm.optionals.getOrNull

@Component
class UpdateUserCredentialsUseCase(
private val passwordEncoder: PasswordEncoder,
private val userCredentialsRepository: UserCredentialsRepository
) {

fun execute(userId: UUID, username: String, email: String) {
val userCredentials = userCredentialsRepository.findById(userId).getOrNull()
?: throw BadRequestException("User not found")

userCredentials.rawUsername = username
userCredentials.email = email
userCredentials.validate()
userCredentialsRepository.save(userCredentials)
}

fun updatePassword(userId: UUID, password: String) {
val userCredentials = userCredentialsRepository.findById(userId).getOrNull()
?: throw RuntimeException("User not found")

userCredentials.rawPassword = password
userCredentials.validatePassword()
userCredentials.rawPassword = this.passwordEncoder.encode(password)
?: throw BadRequestException("Invalid password")

userCredentialsRepository.save(userCredentials)
}
}
Loading