From 06d6cbd4e266f319264dde217062f0be73da1d7b Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Sat, 14 Sep 2024 12:00:19 +0800 Subject: [PATCH] feat: determine whether registration is allowed in saveUser --- .../sakurasou/exception/SignupNotAllowedException.kt | 12 ++++++++++++ .../io/sakurasou/service/user/UserServiceImpl.kt | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/io/sakurasou/exception/SignupNotAllowedException.kt diff --git a/app/src/main/kotlin/io/sakurasou/exception/SignupNotAllowedException.kt b/app/src/main/kotlin/io/sakurasou/exception/SignupNotAllowedException.kt new file mode 100644 index 00000000..c6cd1391 --- /dev/null +++ b/app/src/main/kotlin/io/sakurasou/exception/SignupNotAllowedException.kt @@ -0,0 +1,12 @@ +package io.sakurasou.exception + +/** + * @author Shiina Kin + * 2024/9/14 08:30 + */ +class SignupNotAllowedException : ServiceThrowable() { + override val code: Int + get() = 4001 + override val message: String + get() = "Signup is not allowed" +} \ No newline at end of file diff --git a/app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt b/app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt index 62b24e2c..f3ede4ff 100644 --- a/app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt +++ b/app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt @@ -2,6 +2,7 @@ package io.sakurasou.service.user import at.favre.lib.crypto.bcrypt.BCrypt import io.sakurasou.controller.request.UserInsertRequest +import io.sakurasou.exception.SignupNotAllowedException import io.sakurasou.model.DatabaseSingleton.dbQuery import io.sakurasou.model.dao.user.UserDao import io.sakurasou.model.dto.UserInsertDTO @@ -21,10 +22,12 @@ class UserServiceImpl( private val settingService: SettingService ) : UserService { override suspend fun saveUser(userInsertRequest: UserInsertRequest) { + val systemSetting = settingService.getSystemSetting() + if (!systemSetting.allowSignup) throw SignupNotAllowedException() + val rawPassword = userInsertRequest.password val encodePassword = BCrypt.withDefaults().hashToString(12, rawPassword.toCharArray()) - val systemSetting = settingService.getSystemSetting() val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()) val userInsertDTO = UserInsertDTO(