diff --git a/build.gradle.kts b/build.gradle.kts index 70e5b08..aaf8e58 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -21,7 +21,7 @@ repositories { val exposedVersion = "0.41.1" val postgresVersion = "42.5.4" val telegramBotVersion = "6.5.0" -val springBootVersion = "3.1.0" +val springBootVersion = "2.7.14" val serializationVersion = "1.5.0" val loggingVersion = "3.0.5" val securityTestVersion = "6.0.2" @@ -34,16 +34,9 @@ dependencies { implementation("org.telegram:telegrambotsextensions:$telegramBotVersion") implementation("org.telegram:telegrambots-spring-boot-starter:$telegramBotVersion") - implementation("org.springframework.boot:spring-boot-starter:$springBootVersion") { - exclude("org.springframework.boot:spring-boot-starter-tomcat") - } - implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion") { - exclude("org.springframework.boot:spring-boot-starter-tomcat") - } - implementation("org.springframework.boot:spring-boot-starter-security:$springBootVersion") { - exclude("org.springframework.boot:spring-boot-starter-tomcat") - } - implementation("org.springframework.boot:spring-boot-starter-jetty:$springBootVersion") + implementation("org.springframework.boot:spring-boot-starter:$springBootVersion") + implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion") + implementation("org.springframework.boot:spring-boot-starter-security:$springBootVersion") implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion") implementation("org.jetbrains.kotlin:kotlin-reflect") @@ -58,16 +51,6 @@ dependencies { testImplementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion") } -configurations.all { - resolutionStrategy { - eachDependency { - if (requested.group == "jakarta.servlet") { - useVersion("5.0.0") - } - } - } -} - tasks.withType { kotlinOptions { freeCompilerArgs = listOf("-Xjsr305=strict") diff --git a/src/main/kotlin/ru/sicamp/sicamphelper/api/controller/UsersController.kt b/src/main/kotlin/ru/sicamp/sicamphelper/api/controller/UsersController.kt index 8cc388b..f77cb24 100644 --- a/src/main/kotlin/ru/sicamp/sicamphelper/api/controller/UsersController.kt +++ b/src/main/kotlin/ru/sicamp/sicamphelper/api/controller/UsersController.kt @@ -1,7 +1,5 @@ package ru.sicamp.sicamphelper.api.controller -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.convertValue import mu.KLogging import org.springframework.web.bind.annotation.* import ru.sicamp.sicamphelper.model.dto.UserDto @@ -11,7 +9,6 @@ import ru.sicamp.sicamphelper.service.UserService @RestController class UsersController( private val userService: UserService, - private val objectMapper: ObjectMapper, ) { @PostMapping("/user/create") fun create(@RequestParam dto: UserDto): UserInfo { diff --git a/src/main/kotlin/ru/sicamp/sicamphelper/repository/UserRepository.kt b/src/main/kotlin/ru/sicamp/sicamphelper/repository/UserRepository.kt index 60993b6..faa4f66 100644 --- a/src/main/kotlin/ru/sicamp/sicamphelper/repository/UserRepository.kt +++ b/src/main/kotlin/ru/sicamp/sicamphelper/repository/UserRepository.kt @@ -1,15 +1,16 @@ package ru.sicamp.sicamphelper.repository import mu.KLogging +import org.jetbrains.exposed.dao.load import org.jetbrains.exposed.dao.with import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.transactions.transaction +import org.springframework.security.core.userdetails.UsernameNotFoundException import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.stereotype.Repository import ru.sicamp.sicamphelper.db.entity.User import ru.sicamp.sicamphelper.db.table.Users import ru.sicamp.sicamphelper.model.dto.UserDto -import ru.sicamp.sicamphelper.util.badArgument import java.time.Duration import java.time.LocalDateTime @@ -21,7 +22,10 @@ class UserRepository( User.find { (Users.tgId eq id) and (Users.tgUsername eq userName) - }.firstOrNull() + }.with( + User::supervisor, + User::group + ).firstOrNull() } fun findUserByUsername(username: String) = transaction { @@ -30,11 +34,14 @@ class UserRepository( }.with( User::supervisor, User::group - ).firstOrNull() ?: badArgument("Not found user with username $username") + ).firstOrNull() ?: throw UsernameNotFoundException("Not found user with username $username") } fun findUserById(id: Long) = transaction { - User.findById(id) + User.findById(id)?.load( + User::supervisor, + User::group + ) } fun createUser(userDto: UserDto): User = transaction { diff --git a/src/main/kotlin/ru/sicamp/sicamphelper/security/UserDetailsServiceImpl.kt b/src/main/kotlin/ru/sicamp/sicamphelper/security/UserDetailsServiceImpl.kt new file mode 100644 index 0000000..4760a12 --- /dev/null +++ b/src/main/kotlin/ru/sicamp/sicamphelper/security/UserDetailsServiceImpl.kt @@ -0,0 +1,16 @@ +package ru.sicamp.sicamphelper.security + +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.stereotype.Service +import ru.sicamp.sicamphelper.db.entity.User +import ru.sicamp.sicamphelper.repository.UserRepository +import ru.sicamp.sicamphelper.util.badArgument + +@Service +class UserDetailsServiceImpl( + private val userRepository: UserRepository +) : UserDetailsService { + override fun loadUserByUsername(username: String?): User = userRepository.findUserByUsername( + username = username ?: badArgument("Username shouldn't be null") + ) +} diff --git a/src/main/kotlin/ru/sicamp/sicamphelper/service/UserService.kt b/src/main/kotlin/ru/sicamp/sicamphelper/service/UserService.kt index 8e14da2..21d17bf 100644 --- a/src/main/kotlin/ru/sicamp/sicamphelper/service/UserService.kt +++ b/src/main/kotlin/ru/sicamp/sicamphelper/service/UserService.kt @@ -1,6 +1,5 @@ package ru.sicamp.sicamphelper.service -import org.springframework.security.core.userdetails.UserDetailsService import org.springframework.stereotype.Service import ru.sicamp.sicamphelper.db.entity.User import ru.sicamp.sicamphelper.model.dto.UserDto @@ -12,16 +11,12 @@ import ru.sicamp.sicamphelper.util.badArgument @Service class UserService( private val userRepository: UserRepository -) : UserDetailsService { +) { fun findUserByTgUser(user: TgUser): User? = userRepository.findUserByTgUsernameAndId( id = user.id, userName = user.userName ) - override fun loadUserByUsername(username: String?): User = userRepository.findUserByUsername( - username = username ?: badArgument("Username shouldn't be null") - ) - fun findUserInfoByLogin(login: String) = UserInfo.from( userRepository.findUserByUsername(login) ) ?: badArgument("Not found user with login=$login")