diff --git a/src/main/kotlin/org/js/lorca_core/business/mappers/ClientMapper.kt b/src/main/kotlin/org/js/lorca_core/business/mappers/ClientMapper.kt deleted file mode 100644 index 05e0677..0000000 --- a/src/main/kotlin/org/js/lorca_core/business/mappers/ClientMapper.kt +++ /dev/null @@ -1,8 +0,0 @@ -package org.js.lorca_core.business.mappers - -import org.js.lorca_core.business.models.Client -import org.js.lorca_core.db.entities.ClientEntity -import org.mapstruct.Mapper - -@Mapper(componentModel = "spring") -interface ClientMapper : GenericMapper \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/web/dtos/ClientDto.kt b/src/main/kotlin/org/js/lorca_core/common/dtos/ClientDto.kt similarity index 57% rename from src/main/kotlin/org/js/lorca_core/web/dtos/ClientDto.kt rename to src/main/kotlin/org/js/lorca_core/common/dtos/ClientDto.kt index 579aa47..ac70e2b 100644 --- a/src/main/kotlin/org/js/lorca_core/web/dtos/ClientDto.kt +++ b/src/main/kotlin/org/js/lorca_core/common/dtos/ClientDto.kt @@ -1,6 +1,6 @@ -package org.js.lorca_core.web.dtos +package org.js.lorca_core.common.dtos -import org.js.lorca_core.web.dtos.validators.NameValidator +import org.js.lorca_core.common.dtos.validators.NameValidator data class ClientDto( @field:NameValidator.Validate diff --git a/src/main/kotlin/org/js/lorca_core/common/dtos/UserAuthDto.kt b/src/main/kotlin/org/js/lorca_core/common/dtos/UserAuthDto.kt new file mode 100644 index 0000000..0d651e8 --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/common/dtos/UserAuthDto.kt @@ -0,0 +1,11 @@ +package org.js.lorca_core.common.dtos + +import org.js.lorca_core.common.dtos.validators.B64Validator +import org.js.lorca_core.common.dtos.validators.UsernameValidator + +data class UserAuthDto( + @field:UsernameValidator.Validate + val username: String, + @field:B64Validator.Validate + val password: String +) \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/web/dtos/validators/B64Validator.kt b/src/main/kotlin/org/js/lorca_core/common/dtos/validators/B64Validator.kt similarity index 95% rename from src/main/kotlin/org/js/lorca_core/web/dtos/validators/B64Validator.kt rename to src/main/kotlin/org/js/lorca_core/common/dtos/validators/B64Validator.kt index a7f81a1..5d5af44 100644 --- a/src/main/kotlin/org/js/lorca_core/web/dtos/validators/B64Validator.kt +++ b/src/main/kotlin/org/js/lorca_core/common/dtos/validators/B64Validator.kt @@ -1,4 +1,4 @@ -package org.js.lorca_core.web.dtos.validators +package org.js.lorca_core.common.dtos.validators import jakarta.validation.Constraint import jakarta.validation.ConstraintValidator diff --git a/src/main/kotlin/org/js/lorca_core/web/dtos/validators/NameValidator.kt b/src/main/kotlin/org/js/lorca_core/common/dtos/validators/NameValidator.kt similarity index 96% rename from src/main/kotlin/org/js/lorca_core/web/dtos/validators/NameValidator.kt rename to src/main/kotlin/org/js/lorca_core/common/dtos/validators/NameValidator.kt index 00d198d..dbd973c 100644 --- a/src/main/kotlin/org/js/lorca_core/web/dtos/validators/NameValidator.kt +++ b/src/main/kotlin/org/js/lorca_core/common/dtos/validators/NameValidator.kt @@ -1,4 +1,4 @@ -package org.js.lorca_core.web.dtos.validators +package org.js.lorca_core.common.dtos.validators import jakarta.validation.Constraint import jakarta.validation.ConstraintValidator diff --git a/src/main/kotlin/org/js/lorca_core/web/dtos/validators/UsernameValidator.kt b/src/main/kotlin/org/js/lorca_core/common/dtos/validators/UsernameValidator.kt similarity index 95% rename from src/main/kotlin/org/js/lorca_core/web/dtos/validators/UsernameValidator.kt rename to src/main/kotlin/org/js/lorca_core/common/dtos/validators/UsernameValidator.kt index 2e4028d..9d5da84 100644 --- a/src/main/kotlin/org/js/lorca_core/web/dtos/validators/UsernameValidator.kt +++ b/src/main/kotlin/org/js/lorca_core/common/dtos/validators/UsernameValidator.kt @@ -1,4 +1,4 @@ -package org.js.lorca_core.web.dtos.validators +package org.js.lorca_core.common.dtos.validators import jakarta.validation.Constraint import jakarta.validation.ConstraintValidator diff --git a/src/main/kotlin/org/js/lorca_core/common/enums/EBusinessException.kt b/src/main/kotlin/org/js/lorca_core/common/enums/EBusinessException.kt index ce02438..70b8826 100644 --- a/src/main/kotlin/org/js/lorca_core/common/enums/EBusinessException.kt +++ b/src/main/kotlin/org/js/lorca_core/common/enums/EBusinessException.kt @@ -1,6 +1,7 @@ package org.js.lorca_core.common.enums enum class EBusinessException(val msg: String) { + USER_ALREADY_EXISTS("Username: '%s' already exists"), USER_NOT_FOUND("Username: '%s' not found"), ENTITY_WITH_ID_NOT_FOUND("%s with id %s not found"), INVALID_REQUEST("Invalid request for %s with reason: %s") diff --git a/src/main/kotlin/org/js/lorca_core/common/mappers/ClientMapper.kt b/src/main/kotlin/org/js/lorca_core/common/mappers/ClientMapper.kt new file mode 100644 index 0000000..7b8ec89 --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/common/mappers/ClientMapper.kt @@ -0,0 +1,8 @@ +package org.js.lorca_core.common.mappers + +import org.js.lorca_core.common.models.Client +import org.js.lorca_core.db.entities.ClientEntity +import org.mapstruct.Mapper + +@Mapper(componentModel = "spring") +interface ClientMapper : GenericMapper \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/business/mappers/GenericMapper.kt b/src/main/kotlin/org/js/lorca_core/common/mappers/GenericMapper.kt similarity index 88% rename from src/main/kotlin/org/js/lorca_core/business/mappers/GenericMapper.kt rename to src/main/kotlin/org/js/lorca_core/common/mappers/GenericMapper.kt index 65d1638..6ba717f 100644 --- a/src/main/kotlin/org/js/lorca_core/business/mappers/GenericMapper.kt +++ b/src/main/kotlin/org/js/lorca_core/common/mappers/GenericMapper.kt @@ -1,4 +1,4 @@ -package org.js.lorca_core.business.mappers +package org.js.lorca_core.common.mappers interface GenericMapper { diff --git a/src/main/kotlin/org/js/lorca_core/business/mappers/UserAuthClaimMapper.kt b/src/main/kotlin/org/js/lorca_core/common/mappers/UserAuthClaimMapper.kt similarity index 61% rename from src/main/kotlin/org/js/lorca_core/business/mappers/UserAuthClaimMapper.kt rename to src/main/kotlin/org/js/lorca_core/common/mappers/UserAuthClaimMapper.kt index a9203fd..3318638 100644 --- a/src/main/kotlin/org/js/lorca_core/business/mappers/UserAuthClaimMapper.kt +++ b/src/main/kotlin/org/js/lorca_core/common/mappers/UserAuthClaimMapper.kt @@ -1,6 +1,6 @@ -package org.js.lorca_core.business.mappers +package org.js.lorca_core.common.mappers -import org.js.lorca_core.business.models.UserAuthClaim +import org.js.lorca_core.common.models.UserAuthClaim import org.js.lorca_core.db.entities.UserAuthClaimEntity import org.mapstruct.Mapper diff --git a/src/main/kotlin/org/js/lorca_core/common/mappers/WorkCategoryMapper.kt b/src/main/kotlin/org/js/lorca_core/common/mappers/WorkCategoryMapper.kt new file mode 100644 index 0000000..7ede26d --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/common/mappers/WorkCategoryMapper.kt @@ -0,0 +1,8 @@ +package org.js.lorca_core.common.mappers + +import org.js.lorca_core.common.models.WorkerCategory +import org.js.lorca_core.db.entities.WorkerCategoryEntity +import org.mapstruct.Mapper + +@Mapper(componentModel = "spring") +interface WorkCategoryMapper : GenericMapper \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/common/mappers/WorkerMapper.kt b/src/main/kotlin/org/js/lorca_core/common/mappers/WorkerMapper.kt new file mode 100644 index 0000000..22b8bfb --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/common/mappers/WorkerMapper.kt @@ -0,0 +1,8 @@ +package org.js.lorca_core.common.mappers + +import org.js.lorca_core.common.models.Worker +import org.js.lorca_core.db.entities.WorkerEntity +import org.mapstruct.Mapper + +@Mapper(componentModel = "spring") +interface WorkerMapper : GenericMapper \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/business/models/Client.kt b/src/main/kotlin/org/js/lorca_core/common/models/Client.kt similarity index 66% rename from src/main/kotlin/org/js/lorca_core/business/models/Client.kt rename to src/main/kotlin/org/js/lorca_core/common/models/Client.kt index efc5dc6..cb2f85d 100644 --- a/src/main/kotlin/org/js/lorca_core/business/models/Client.kt +++ b/src/main/kotlin/org/js/lorca_core/common/models/Client.kt @@ -1,4 +1,4 @@ -package org.js.lorca_core.business.models +package org.js.lorca_core.common.models data class Client( var id: Long?, diff --git a/src/main/kotlin/org/js/lorca_core/business/models/UserAuthClaim.kt b/src/main/kotlin/org/js/lorca_core/common/models/UserAuthClaim.kt similarity index 73% rename from src/main/kotlin/org/js/lorca_core/business/models/UserAuthClaim.kt rename to src/main/kotlin/org/js/lorca_core/common/models/UserAuthClaim.kt index 6704f2b..e97100c 100644 --- a/src/main/kotlin/org/js/lorca_core/business/models/UserAuthClaim.kt +++ b/src/main/kotlin/org/js/lorca_core/common/models/UserAuthClaim.kt @@ -1,4 +1,4 @@ -package org.js.lorca_core.business.models +package org.js.lorca_core.common.models import org.js.lorca_core.common.enums.EUserRoles diff --git a/src/main/kotlin/org/js/lorca_core/common/models/Worker.kt b/src/main/kotlin/org/js/lorca_core/common/models/Worker.kt new file mode 100644 index 0000000..74c311c --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/common/models/Worker.kt @@ -0,0 +1,8 @@ +package org.js.lorca_core.common.models + +data class Worker( + var id: Long, + var name: String, + var surname: String, + val category: WorkerCategory +) \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/common/models/WorkerCategory.kt b/src/main/kotlin/org/js/lorca_core/common/models/WorkerCategory.kt new file mode 100644 index 0000000..5af680c --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/common/models/WorkerCategory.kt @@ -0,0 +1,8 @@ +package org.js.lorca_core.common.models + +import org.js.lorca_core.common.enums.EWorkerCategory + +data class WorkerCategory( + var id: Long, + var name: EWorkerCategory +) \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/common/responses/LoginResponse.kt b/src/main/kotlin/org/js/lorca_core/common/responses/LoginResponse.kt new file mode 100644 index 0000000..3dbfc05 --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/common/responses/LoginResponse.kt @@ -0,0 +1,5 @@ +package org.js.lorca_core.common.responses + +data class LoginResponse( + val token: String +) \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/config/auth/JwtAuthConfiguration.kt b/src/main/kotlin/org/js/lorca_core/config/auth/JwtAuthConfiguration.kt index d0614d2..c28188b 100644 --- a/src/main/kotlin/org/js/lorca_core/config/auth/JwtAuthConfiguration.kt +++ b/src/main/kotlin/org/js/lorca_core/config/auth/JwtAuthConfiguration.kt @@ -1,6 +1,6 @@ package org.js.lorca_core.config.auth -import org.js.lorca_core.business.repositories.UserAuthRepository +import org.js.lorca_core.repositories.UserAuthRepository import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.security.authentication.AuthenticationManager diff --git a/src/main/kotlin/org/js/lorca_core/db/UserAuthJpa.kt b/src/main/kotlin/org/js/lorca_core/db/UserAuthJpa.kt index 4e36b3c..353db51 100644 --- a/src/main/kotlin/org/js/lorca_core/db/UserAuthJpa.kt +++ b/src/main/kotlin/org/js/lorca_core/db/UserAuthJpa.kt @@ -6,4 +6,5 @@ import java.util.* interface UserAuthJpa : JpaRepository { fun findByUsr(username: String): Optional + fun existsByUsr(username: String): Boolean } \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/db/WorkerJpa.kt b/src/main/kotlin/org/js/lorca_core/db/WorkerJpa.kt index bf7ffd9..34b1b50 100644 --- a/src/main/kotlin/org/js/lorca_core/db/WorkerJpa.kt +++ b/src/main/kotlin/org/js/lorca_core/db/WorkerJpa.kt @@ -1,11 +1,9 @@ package org.js.lorca_core.db -import org.js.lorca_core.common.enums.EWorkerCategory import org.js.lorca_core.db.entities.WorkerEntity import org.springframework.data.jpa.repository.JpaRepository -import org.springframework.data.repository.query.Param +import java.util.* interface WorkerJpa : JpaRepository { - - fun findAllByCategoryName(@Param("category") category: EWorkerCategory): MutableList + fun findByAuthUserUsr(username: String): Optional } \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/db/entities/UserAuthEntity.kt b/src/main/kotlin/org/js/lorca_core/db/entities/UserAuthEntity.kt index 18a0f0d..04213a5 100644 --- a/src/main/kotlin/org/js/lorca_core/db/entities/UserAuthEntity.kt +++ b/src/main/kotlin/org/js/lorca_core/db/entities/UserAuthEntity.kt @@ -5,12 +5,12 @@ import org.springframework.security.core.GrantedAuthority import org.springframework.security.core.userdetails.UserDetails @Entity(name = "user_auth") -class UserAuthEntity : UserDetails { +class UserAuthEntity(usr: String, pwd: String) : UserDetails { @Id @GeneratedValue var id: Long? = null - var usr: String? = null - var pwd: String? = null + var usr: String? = usr + var pwd: String? = pwd @ManyToMany(cascade = [CascadeType.DETACH]) var claims: MutableList = mutableListOf() diff --git a/src/main/kotlin/org/js/lorca_core/db/entities/WorkerEntity.kt b/src/main/kotlin/org/js/lorca_core/db/entities/WorkerEntity.kt index bbc5847..decd606 100644 --- a/src/main/kotlin/org/js/lorca_core/db/entities/WorkerEntity.kt +++ b/src/main/kotlin/org/js/lorca_core/db/entities/WorkerEntity.kt @@ -9,5 +9,7 @@ data class WorkerEntity( var name: String, var surname: String, @ManyToOne(fetch = FetchType.LAZY) - val category: WorkerCategoryEntity + val category: WorkerCategoryEntity, + @OneToOne(fetch = FetchType.LAZY) + val authUser: UserAuthEntity ) \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/business/repositories/ClientRepository.kt b/src/main/kotlin/org/js/lorca_core/repositories/ClientRepository.kt similarity index 60% rename from src/main/kotlin/org/js/lorca_core/business/repositories/ClientRepository.kt rename to src/main/kotlin/org/js/lorca_core/repositories/ClientRepository.kt index 1044a9a..f395d82 100644 --- a/src/main/kotlin/org/js/lorca_core/business/repositories/ClientRepository.kt +++ b/src/main/kotlin/org/js/lorca_core/repositories/ClientRepository.kt @@ -1,10 +1,10 @@ -package org.js.lorca_core.business.repositories +package org.js.lorca_core.repositories -import org.js.lorca_core.business.models.Client +import org.js.lorca_core.common.models.Client interface ClientRepository { fun createClient(client: Client): Client fun getAll(): MutableList fun getById(id: Long): Client -} \ No newline at end of file +} diff --git a/src/main/kotlin/org/js/lorca_core/business/repositories/UserAuthRepository.kt b/src/main/kotlin/org/js/lorca_core/repositories/UserAuthRepository.kt similarity index 61% rename from src/main/kotlin/org/js/lorca_core/business/repositories/UserAuthRepository.kt rename to src/main/kotlin/org/js/lorca_core/repositories/UserAuthRepository.kt index a2cb5c8..f964272 100644 --- a/src/main/kotlin/org/js/lorca_core/business/repositories/UserAuthRepository.kt +++ b/src/main/kotlin/org/js/lorca_core/repositories/UserAuthRepository.kt @@ -1,10 +1,11 @@ -package org.js.lorca_core.business.repositories +package org.js.lorca_core.repositories import org.js.lorca_core.common.enums.EUserRoles import org.js.lorca_core.db.entities.UserAuthEntity interface UserAuthRepository { + fun existsByUsername(username: String): Boolean fun getByUsername(username: String): UserAuthEntity fun save(userAuth: UserAuthEntity): UserAuthEntity - fun getClaimsForuser(user: UserAuthEntity): List + fun getClaimsForUser(user: UserAuthEntity): List } \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/repositories/WorkerRepository.kt b/src/main/kotlin/org/js/lorca_core/repositories/WorkerRepository.kt new file mode 100644 index 0000000..74c1c26 --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/repositories/WorkerRepository.kt @@ -0,0 +1,7 @@ +package org.js.lorca_core.repositories + +import org.js.lorca_core.common.models.Worker + +interface WorkerRepository { + fun getByUsername(username: String): Worker? +} diff --git a/src/main/kotlin/org/js/lorca_core/business/repositories/impl/ClientRepositoryImpl.kt b/src/main/kotlin/org/js/lorca_core/repositories/impl/ClientRepositoryImpl.kt similarity index 80% rename from src/main/kotlin/org/js/lorca_core/business/repositories/impl/ClientRepositoryImpl.kt rename to src/main/kotlin/org/js/lorca_core/repositories/impl/ClientRepositoryImpl.kt index 48d8e4f..7c66b1c 100644 --- a/src/main/kotlin/org/js/lorca_core/business/repositories/impl/ClientRepositoryImpl.kt +++ b/src/main/kotlin/org/js/lorca_core/repositories/impl/ClientRepositoryImpl.kt @@ -1,11 +1,11 @@ -package org.js.lorca_core.business.repositories.impl +package org.js.lorca_core.repositories.impl -import org.js.lorca_core.business.mappers.ClientMapper -import org.js.lorca_core.business.models.Client -import org.js.lorca_core.business.repositories.ClientRepository import org.js.lorca_core.common.enums.EBusinessException import org.js.lorca_core.common.exceptions.LorcaException +import org.js.lorca_core.common.mappers.ClientMapper +import org.js.lorca_core.common.models.Client import org.js.lorca_core.db.ClientJpa +import org.js.lorca_core.repositories.ClientRepository import org.springframework.stereotype.Component @Component diff --git a/src/main/kotlin/org/js/lorca_core/business/repositories/impl/UserAuthRepositoryImpl.kt b/src/main/kotlin/org/js/lorca_core/repositories/impl/UserAuthRepositoryImpl.kt similarity index 72% rename from src/main/kotlin/org/js/lorca_core/business/repositories/impl/UserAuthRepositoryImpl.kt rename to src/main/kotlin/org/js/lorca_core/repositories/impl/UserAuthRepositoryImpl.kt index 8ed7ac8..8745c5d 100644 --- a/src/main/kotlin/org/js/lorca_core/business/repositories/impl/UserAuthRepositoryImpl.kt +++ b/src/main/kotlin/org/js/lorca_core/repositories/impl/UserAuthRepositoryImpl.kt @@ -1,12 +1,12 @@ -package org.js.lorca_core.business.repositories.impl +package org.js.lorca_core.repositories.impl -import org.js.lorca_core.business.mappers.UserAuthClaimMapper -import org.js.lorca_core.business.repositories.UserAuthRepository import org.js.lorca_core.common.enums.EBusinessException import org.js.lorca_core.common.enums.EUserRoles import org.js.lorca_core.common.exceptions.LorcaException +import org.js.lorca_core.common.mappers.UserAuthClaimMapper import org.js.lorca_core.db.UserAuthJpa import org.js.lorca_core.db.entities.UserAuthEntity +import org.js.lorca_core.repositories.UserAuthRepository import org.springframework.stereotype.Component @Component @@ -23,8 +23,12 @@ class UserAuthRepositoryImpl( return jpa.save(userAuth) } - override fun getClaimsForuser(user: UserAuthEntity): List { + override fun getClaimsForUser(user: UserAuthEntity): List { return claimMapper.toModels(user.claims).map { it.name }.toList() } + override fun existsByUsername(username: String): Boolean { + return jpa.existsByUsr(username) + } + } \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/repositories/impl/WorkerRepositoryImpl.kt b/src/main/kotlin/org/js/lorca_core/repositories/impl/WorkerRepositoryImpl.kt new file mode 100644 index 0000000..fcd8469 --- /dev/null +++ b/src/main/kotlin/org/js/lorca_core/repositories/impl/WorkerRepositoryImpl.kt @@ -0,0 +1,20 @@ +package org.js.lorca_core.repositories.impl + +import org.js.lorca_core.common.mappers.WorkerMapper +import org.js.lorca_core.common.models.Worker +import org.js.lorca_core.db.WorkerJpa +import org.js.lorca_core.repositories.WorkerRepository +import org.springframework.stereotype.Component +import kotlin.jvm.optionals.getOrNull + +@Component +class WorkerRepositoryImpl( + val jpa: WorkerJpa, + val mapper: WorkerMapper +) : WorkerRepository { + override fun getByUsername(username: String): Worker? { + val user = jpa.findByAuthUserUsr(username).getOrNull() + return if (user != null) mapper.toModel(user) else null + } + +} \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/services/AuthenticationService.kt b/src/main/kotlin/org/js/lorca_core/services/AuthenticationService.kt index 1003930..481991a 100644 --- a/src/main/kotlin/org/js/lorca_core/services/AuthenticationService.kt +++ b/src/main/kotlin/org/js/lorca_core/services/AuthenticationService.kt @@ -1,13 +1,10 @@ package org.js.lorca_core.services -import org.js.lorca_core.common.enums.EUserRoles -import org.js.lorca_core.db.entities.UserAuthEntity -import org.js.lorca_core.web.dtos.UserAuthDto +import org.js.lorca_core.common.dtos.UserAuthDto +import org.js.lorca_core.common.responses.LoginResponse interface AuthenticationService { - fun register(input: UserAuthDto): Boolean + fun register(dto: UserAuthDto): Boolean - fun login(input: UserAuthDto): UserAuthEntity - - fun getClaimsForUser(user: UserAuthEntity): List + fun login(dto: UserAuthDto): LoginResponse } \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/services/ClientService.kt b/src/main/kotlin/org/js/lorca_core/services/ClientService.kt index 01dc115..7b22f30 100644 --- a/src/main/kotlin/org/js/lorca_core/services/ClientService.kt +++ b/src/main/kotlin/org/js/lorca_core/services/ClientService.kt @@ -1,7 +1,7 @@ package org.js.lorca_core.services -import org.js.lorca_core.business.models.Client -import org.js.lorca_core.web.dtos.ClientDto +import org.js.lorca_core.common.dtos.ClientDto +import org.js.lorca_core.common.models.Client interface ClientService { fun getAllClients(name: String?, surname: String?): MutableList diff --git a/src/main/kotlin/org/js/lorca_core/services/impl/AuthenticationServiceImpl.kt b/src/main/kotlin/org/js/lorca_core/services/impl/AuthenticationServiceImpl.kt index 6dfc0f2..9f76fa4 100644 --- a/src/main/kotlin/org/js/lorca_core/services/impl/AuthenticationServiceImpl.kt +++ b/src/main/kotlin/org/js/lorca_core/services/impl/AuthenticationServiceImpl.kt @@ -1,10 +1,13 @@ package org.js.lorca_core.services.impl -import org.js.lorca_core.business.repositories.UserAuthRepository -import org.js.lorca_core.common.enums.EUserRoles +import org.js.lorca_core.common.dtos.UserAuthDto +import org.js.lorca_core.common.enums.EBusinessException +import org.js.lorca_core.common.exceptions.LorcaException +import org.js.lorca_core.common.responses.LoginResponse import org.js.lorca_core.db.entities.UserAuthEntity +import org.js.lorca_core.repositories.UserAuthRepository import org.js.lorca_core.services.AuthenticationService -import org.js.lorca_core.web.dtos.UserAuthDto +import org.js.lorca_core.services.JwtService import org.springframework.security.authentication.AuthenticationManager import org.springframework.security.authentication.UsernamePasswordAuthenticationToken import org.springframework.security.crypto.password.PasswordEncoder @@ -15,30 +18,31 @@ import org.springframework.stereotype.Service class AuthenticationServiceImpl( private val userAuthRepository: UserAuthRepository, private val authenticationManager: AuthenticationManager, - private val passwordEncoder: PasswordEncoder + private val passwordEncoder: PasswordEncoder, + private val jwtService: JwtService ) : AuthenticationService { - override fun register(input: UserAuthDto): Boolean { - val user = UserAuthEntity() - user.username = input.username - user.password = passwordEncoder.encode(input.password) - userAuthRepository.save(user) + override fun register(dto: UserAuthDto): Boolean { + if (userAuthRepository.existsByUsername(dto.username)) { + throw LorcaException(EBusinessException.USER_ALREADY_EXISTS, dto.username) + } + + userAuthRepository.save(UserAuthEntity(dto.username, passwordEncoder.encode(dto.password))) return true } - override fun login(input: UserAuthDto): UserAuthEntity { + override fun login(dto: UserAuthDto): LoginResponse { authenticationManager.authenticate( UsernamePasswordAuthenticationToken( - input.username, - input.password + dto.username, + dto.password ) ) - return userAuthRepository.getByUsername(input.username) - } - - override fun getClaimsForUser(user: UserAuthEntity): List { - return userAuthRepository.getClaimsForuser(user) + val user = userAuthRepository.getByUsername(dto.username) + return LoginResponse( + jwtService.generateToken(user) + ) } } \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/services/impl/ClientServiceImpl.kt b/src/main/kotlin/org/js/lorca_core/services/impl/ClientServiceImpl.kt index 4a87346..ad5cf1f 100644 --- a/src/main/kotlin/org/js/lorca_core/services/impl/ClientServiceImpl.kt +++ b/src/main/kotlin/org/js/lorca_core/services/impl/ClientServiceImpl.kt @@ -1,9 +1,9 @@ package org.js.lorca_core.services.impl -import org.js.lorca_core.business.models.Client -import org.js.lorca_core.business.repositories.ClientRepository +import org.js.lorca_core.common.dtos.ClientDto +import org.js.lorca_core.common.models.Client +import org.js.lorca_core.repositories.ClientRepository import org.js.lorca_core.services.ClientService -import org.js.lorca_core.web.dtos.ClientDto import org.springframework.stereotype.Service @Service diff --git a/src/main/kotlin/org/js/lorca_core/services/impl/JwtServiceImpl.kt b/src/main/kotlin/org/js/lorca_core/services/impl/JwtServiceImpl.kt index 6441b30..b829174 100644 --- a/src/main/kotlin/org/js/lorca_core/services/impl/JwtServiceImpl.kt +++ b/src/main/kotlin/org/js/lorca_core/services/impl/JwtServiceImpl.kt @@ -4,6 +4,10 @@ import io.jsonwebtoken.Claims import io.jsonwebtoken.Jwts import io.jsonwebtoken.io.Decoders import io.jsonwebtoken.security.Keys +import org.js.lorca_core.common.enums.EUserRoles +import org.js.lorca_core.db.entities.UserAuthEntity +import org.js.lorca_core.repositories.UserAuthRepository +import org.js.lorca_core.repositories.WorkerRepository import org.js.lorca_core.services.JwtService import org.springframework.beans.factory.annotation.Value import org.springframework.security.core.userdetails.UserDetails @@ -13,7 +17,10 @@ import java.util.function.Function import javax.crypto.SecretKey @Service -class JwtServiceImpl : JwtService { +class JwtServiceImpl( + private val userAuthRepository: UserAuthRepository, + private val workerRepository: WorkerRepository +) : JwtService { @Value("\${security.jwt.secret-key}") private lateinit var secretKey: String @@ -31,7 +38,29 @@ class JwtServiceImpl : JwtService { } override fun generateToken(userDetails: UserDetails): String { - return generateToken(emptyMap(), userDetails) + val extraClaims: Map = mapOf() + getClaimsForUser(userAuthRepository.getByUsername(userDetails.username)).let { claims -> + if (claims.isNotEmpty()) { + extraClaims.plus( + Pair( + "roles", + claims + ) + ) + } + } + workerRepository.getByUsername(userDetails.username)?.let { + extraClaims.plus( + Pair( + "user", + it + ) + ) + } + + return generateToken( + extraClaims, userDetails + ) } override fun generateToken(extraClaims: Map, userDetails: UserDetails): String { @@ -81,4 +110,8 @@ class JwtServiceImpl : JwtService { val keyBytes = Decoders.BASE64.decode(secretKey) return Keys.hmacShaKeyFor(keyBytes) } + + private fun getClaimsForUser(user: UserAuthEntity): List { + return userAuthRepository.getClaimsForUser(user) + } } diff --git a/src/main/kotlin/org/js/lorca_core/web/advices/BaseAdvice.kt b/src/main/kotlin/org/js/lorca_core/web/advices/BaseAdvice.kt index 922e533..50988e7 100644 --- a/src/main/kotlin/org/js/lorca_core/web/advices/BaseAdvice.kt +++ b/src/main/kotlin/org/js/lorca_core/web/advices/BaseAdvice.kt @@ -28,6 +28,7 @@ class BaseAdvice { private fun deductStatus(ex: EBusinessException): HttpStatus { return when (ex) { + EBusinessException.USER_ALREADY_EXISTS -> HttpStatus.UNPROCESSABLE_ENTITY EBusinessException.USER_NOT_FOUND -> HttpStatus.NOT_FOUND EBusinessException.ENTITY_WITH_ID_NOT_FOUND -> HttpStatus.NOT_FOUND EBusinessException.INVALID_REQUEST -> HttpStatus.BAD_REQUEST diff --git a/src/main/kotlin/org/js/lorca_core/web/controllers/AuthController.kt b/src/main/kotlin/org/js/lorca_core/web/controllers/AuthController.kt index acf3360..e7fcb32 100644 --- a/src/main/kotlin/org/js/lorca_core/web/controllers/AuthController.kt +++ b/src/main/kotlin/org/js/lorca_core/web/controllers/AuthController.kt @@ -2,11 +2,10 @@ package org.js.lorca_core.web.controllers import jakarta.validation.Valid import lombok.AllArgsConstructor +import org.js.lorca_core.common.dtos.UserAuthDto +import org.js.lorca_core.common.responses.LoginResponse import org.js.lorca_core.services.AuthenticationService -import org.js.lorca_core.services.JwtService import org.js.lorca_core.web.advices.WebResponse -import org.js.lorca_core.web.dtos.UserAuthDto -import org.js.lorca_core.web.responses.LoginResponse import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping @@ -17,8 +16,7 @@ import org.springframework.web.bind.annotation.RestController @RequestMapping("/auth") @AllArgsConstructor class AuthController( - val authenticationService: AuthenticationService, - val jwtService: JwtService + val authenticationService: AuthenticationService ) { @PostMapping("/register") @@ -29,13 +27,8 @@ class AuthController( @PostMapping("/login") fun authenticate(@Valid @RequestBody loginUserDto: UserAuthDto): WebResponse { - val user = authenticationService.login(loginUserDto) return WebResponse.ok( - LoginResponse( - jwtService.generateToken(user), - jwtService.getExpirationTime(), - authenticationService.getClaimsForUser(user) - ) + authenticationService.login(loginUserDto) ) } diff --git a/src/main/kotlin/org/js/lorca_core/web/controllers/ClientController.kt b/src/main/kotlin/org/js/lorca_core/web/controllers/ClientController.kt index ad5a950..069e84b 100644 --- a/src/main/kotlin/org/js/lorca_core/web/controllers/ClientController.kt +++ b/src/main/kotlin/org/js/lorca_core/web/controllers/ClientController.kt @@ -2,10 +2,10 @@ package org.js.lorca_core.web.controllers import jakarta.validation.Valid import lombok.AllArgsConstructor -import org.js.lorca_core.business.models.Client +import org.js.lorca_core.common.dtos.ClientDto +import org.js.lorca_core.common.models.Client import org.js.lorca_core.services.ClientService import org.js.lorca_core.web.advices.WebResponse -import org.js.lorca_core.web.dtos.ClientDto import org.springframework.web.bind.annotation.* @RestController @@ -32,5 +32,5 @@ class ClientController( fun getClient(@PathVariable("id") id: String): WebResponse { return WebResponse.ok(clientService.getClientById(id.toLong())) } - + } \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/web/dtos/UserAuthDto.kt b/src/main/kotlin/org/js/lorca_core/web/dtos/UserAuthDto.kt deleted file mode 100644 index 828613d..0000000 --- a/src/main/kotlin/org/js/lorca_core/web/dtos/UserAuthDto.kt +++ /dev/null @@ -1,11 +0,0 @@ -package org.js.lorca_core.web.dtos - -import org.js.lorca_core.web.dtos.validators.B64Validator -import org.js.lorca_core.web.dtos.validators.UsernameValidator - -data class UserAuthDto( - @field:UsernameValidator.Validate - val username: String, - @field:B64Validator.Validate - val password: String -) \ No newline at end of file diff --git a/src/main/kotlin/org/js/lorca_core/web/responses/LoginResponse.kt b/src/main/kotlin/org/js/lorca_core/web/responses/LoginResponse.kt deleted file mode 100644 index 2f76d5e..0000000 --- a/src/main/kotlin/org/js/lorca_core/web/responses/LoginResponse.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.js.lorca_core.web.responses - -import org.js.lorca_core.common.enums.EUserRoles - -data class LoginResponse( - val token: String, - val expiresIn: Long, - val claims: List -) \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9cde38f..33a8d27 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,9 @@ server.servlet.context-path=/api spring.application.name=lorca-gestionale-be +# +# +# DATABASE +# spring.datasource.url=jdbc:postgresql://0.0.0.0:5432/lorca_db spring.datasource.username=lorca_usr spring.datasource.password=lorca_pwd @@ -7,8 +11,12 @@ spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=update -# HOW TO GENERATE THIS KEY AT : +# +# +# HOW TO GENERATE THIS KEY AT : https://www.devglan.com/online-tools/hmac-sha256-online?ref=blog.tericcabrel.com # PLAIN TEXT: Boh che cazzo ne so bel testo peró complimenti # SECRET KEY: Ma che minchia ne so quale puo essere una bella chiave segreta dio negraccio security.jwt.secret-key=93d5326c5ae622c9332f291c6a9868d237e6b41fc47c5f2581448d4d90e90a1a -security.jwt.expiration-time=3600000 \ No newline at end of file +security.jwt.expiration-time=3600000 +# +#