From 902fcef9b28551558039be0fdae3df4b28ef843d Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Mon, 4 Dec 2023 17:13:20 +0800 Subject: [PATCH] Add system settings management api --- .../controller/system/SettingController.kt | 32 ------- .../controller/system/SettingsController.kt | 33 +++++++ .../api/converter/system/SettingConverter.kt | 32 ------- .../api/converter/system/SettingsConverter.kt | 19 ++++ .../api/param/system/MailSettingsParam.kt | 9 ++ .../fatweb/api/param/system/SettingParam.kt | 13 --- .../api/service/system/ISettingsService.kt | 10 +++ .../api/service/system/ISysSettingService.kt | 10 --- .../system/impl/SettingsServiceImpl.kt | 24 +++++ .../system/impl/SysSettingServiceImpl.kt | 17 ---- .../top/fatweb/api/setting/SettingOperator.kt | 31 ------- .../top/fatweb/api/setting/SystemSetting.kt | 5 -- .../MailSettings.kt} | 7 +- .../fatweb/api/settings/SettingsOperator.kt | 90 +++++++++++++++++++ .../top/fatweb/api/settings/SystemSettings.kt | 8 ++ .../kotlin/top/fatweb/api/util/StrUtil.kt | 14 +++ .../vo/system/{SettingVo.kt => SettingsVo.kt} | 6 +- .../resources/db/migration/R__Basic_data.sql | 18 +++- 18 files changed, 229 insertions(+), 149 deletions(-) delete mode 100644 src/main/kotlin/top/fatweb/api/controller/system/SettingController.kt create mode 100644 src/main/kotlin/top/fatweb/api/controller/system/SettingsController.kt delete mode 100644 src/main/kotlin/top/fatweb/api/converter/system/SettingConverter.kt create mode 100644 src/main/kotlin/top/fatweb/api/converter/system/SettingsConverter.kt create mode 100644 src/main/kotlin/top/fatweb/api/param/system/MailSettingsParam.kt delete mode 100644 src/main/kotlin/top/fatweb/api/param/system/SettingParam.kt create mode 100644 src/main/kotlin/top/fatweb/api/service/system/ISettingsService.kt delete mode 100644 src/main/kotlin/top/fatweb/api/service/system/ISysSettingService.kt create mode 100644 src/main/kotlin/top/fatweb/api/service/system/impl/SettingsServiceImpl.kt delete mode 100644 src/main/kotlin/top/fatweb/api/service/system/impl/SysSettingServiceImpl.kt delete mode 100644 src/main/kotlin/top/fatweb/api/setting/SettingOperator.kt delete mode 100644 src/main/kotlin/top/fatweb/api/setting/SystemSetting.kt rename src/main/kotlin/top/fatweb/api/{setting/MailSetting.kt => settings/MailSettings.kt} (50%) create mode 100644 src/main/kotlin/top/fatweb/api/settings/SettingsOperator.kt create mode 100644 src/main/kotlin/top/fatweb/api/settings/SystemSettings.kt rename src/main/kotlin/top/fatweb/api/vo/system/{SettingVo.kt => SettingsVo.kt} (68%) diff --git a/src/main/kotlin/top/fatweb/api/controller/system/SettingController.kt b/src/main/kotlin/top/fatweb/api/controller/system/SettingController.kt deleted file mode 100644 index b8ba719..0000000 --- a/src/main/kotlin/top/fatweb/api/controller/system/SettingController.kt +++ /dev/null @@ -1,32 +0,0 @@ -package top.fatweb.api.controller.system - -import io.swagger.v3.oas.annotations.Operation -import io.swagger.v3.oas.annotations.tags.Tag -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RestController -import top.fatweb.api.entity.common.ResponseResult -import top.fatweb.api.param.system.SettingParam -import top.fatweb.api.service.system.ISysSettingService -import top.fatweb.api.vo.system.SettingVo - -/** - * System setting controller - * - * @author FatttSnake, fatttsnake@gmail.com - * @since 1.0.0 - */ -@Tag(name = "系统设置", description = "系统设置相关接口") -@RequestMapping("/system/setting") -@RestController -class SettingController( - private val sysSettingService: ISysSettingService -) { - @Operation(summary = "获取") - fun get(): ResponseResult = ResponseResult.success(data = sysSettingService.get()) - - @Operation(summary = "更新") - fun update(settingParam: SettingParam): ResponseResult { - sysSettingService.update(settingParam) - return ResponseResult.success() - } -} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/controller/system/SettingsController.kt b/src/main/kotlin/top/fatweb/api/controller/system/SettingsController.kt new file mode 100644 index 0000000..e458c95 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/controller/system/SettingsController.kt @@ -0,0 +1,33 @@ +package top.fatweb.api.controller.system + +import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.web.bind.annotation.* +import top.fatweb.api.entity.common.ResponseResult +import top.fatweb.api.param.system.MailSettingsParam +import top.fatweb.api.service.system.ISettingsService +import top.fatweb.api.vo.system.SettingsVo + +/** + * System settings controller + * + * @author FatttSnake, fatttsnake@gmail.com + * @since 1.0.0 + */ +@Tag(name = "系统设置", description = "系统设置相关接口") +@RequestMapping("/system/settings") +@RestController +class SettingsController( + private val settingsService: ISettingsService +) { + @Operation(summary = "获取全部设置") + @GetMapping + fun get(): ResponseResult = ResponseResult.success(data = settingsService.get()) + + @Operation(summary = "更新邮件设置") + @PutMapping("/mail") + fun updateMail(@RequestBody mailSettingsParam: MailSettingsParam): ResponseResult { + settingsService.updateMail(mailSettingsParam) + return ResponseResult.success() + } +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/converter/system/SettingConverter.kt b/src/main/kotlin/top/fatweb/api/converter/system/SettingConverter.kt deleted file mode 100644 index 5a920fa..0000000 --- a/src/main/kotlin/top/fatweb/api/converter/system/SettingConverter.kt +++ /dev/null @@ -1,32 +0,0 @@ -package top.fatweb.api.converter.system - -import top.fatweb.api.param.system.SettingParam -import top.fatweb.api.setting.MailSetting -import top.fatweb.api.setting.SystemSetting -import top.fatweb.api.vo.system.SettingVo - -object SettingConverter { - fun mailSettingToMailSettingVo(mailSetting: MailSetting) = SettingVo.MailSettingVo( - host = mailSetting.host, - port = mailSetting.port, - username = mailSetting.username, - password = mailSetting.password, - from = mailSetting.from - ) - - fun systemSettingToSettingVo(sysSetting: SystemSetting) = SettingVo( - mail = sysSetting.mail?.let { mailSettingToMailSettingVo(it) } - ) - - fun mailSettingParamToMailSetting(mailSettingParam: SettingParam.MailSettingParam) = MailSetting( - host = mailSettingParam.host, - port = mailSettingParam.port, - username = mailSettingParam.username, - password = mailSettingParam.password, - from = mailSettingParam.from - ) - - fun settingParamToSystemSetting(settingParam: SettingParam) = SystemSetting( - mail = settingParam.mail?.let { mailSettingParamToMailSetting(it) } - ) -} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/converter/system/SettingsConverter.kt b/src/main/kotlin/top/fatweb/api/converter/system/SettingsConverter.kt new file mode 100644 index 0000000..7055f64 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/converter/system/SettingsConverter.kt @@ -0,0 +1,19 @@ +package top.fatweb.api.converter.system + +import top.fatweb.api.settings.MailSettings +import top.fatweb.api.settings.SystemSettings +import top.fatweb.api.vo.system.SettingsVo + +object SettingsConverter { + private fun mailSettingsToMailSettingsVo(mailSettings: MailSettings) = SettingsVo.MailSettingsVo( + host = mailSettings.host, + port = mailSettings.port, + username = mailSettings.username, + password = mailSettings.password, + from = mailSettings.from + ) + + fun systemSettingsToSettingsVo(systemSettings: SystemSettings) = SettingsVo( + mail = systemSettings.mail?.let { mailSettingsToMailSettingsVo(it) } + ) +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/param/system/MailSettingsParam.kt b/src/main/kotlin/top/fatweb/api/param/system/MailSettingsParam.kt new file mode 100644 index 0000000..6467b39 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/system/MailSettingsParam.kt @@ -0,0 +1,9 @@ +package top.fatweb.api.param.system + +data class MailSettingsParam ( + val host: String?, + val port: Int?, + val username: String?, + val password: String?, + val from: String? +) \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/param/system/SettingParam.kt b/src/main/kotlin/top/fatweb/api/param/system/SettingParam.kt deleted file mode 100644 index f98b4ae..0000000 --- a/src/main/kotlin/top/fatweb/api/param/system/SettingParam.kt +++ /dev/null @@ -1,13 +0,0 @@ -package top.fatweb.api.param.system - -data class SettingParam ( - val mail: MailSettingParam? -) { - data class MailSettingParam ( - val host: String, - val port: Int, - val username: String, - val password: String, - val from: String - ) -} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/ISettingsService.kt b/src/main/kotlin/top/fatweb/api/service/system/ISettingsService.kt new file mode 100644 index 0000000..8a7cb2a --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/service/system/ISettingsService.kt @@ -0,0 +1,10 @@ +package top.fatweb.api.service.system + +import top.fatweb.api.param.system.MailSettingsParam +import top.fatweb.api.vo.system.SettingsVo + +interface ISettingsService { + fun get(): SettingsVo + + fun updateMail(mailSettingsParam: MailSettingsParam) +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/ISysSettingService.kt b/src/main/kotlin/top/fatweb/api/service/system/ISysSettingService.kt deleted file mode 100644 index 2cea670..0000000 --- a/src/main/kotlin/top/fatweb/api/service/system/ISysSettingService.kt +++ /dev/null @@ -1,10 +0,0 @@ -package top.fatweb.api.service.system - -import top.fatweb.api.param.system.SettingParam -import top.fatweb.api.vo.system.SettingVo - -interface ISysSettingService { - fun get(): SettingVo - - fun update(settingParam: SettingParam) -} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/SettingsServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/SettingsServiceImpl.kt new file mode 100644 index 0000000..af32740 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/service/system/impl/SettingsServiceImpl.kt @@ -0,0 +1,24 @@ +package top.fatweb.api.service.system.impl + +import org.springframework.stereotype.Service +import top.fatweb.api.converter.system.SettingsConverter +import top.fatweb.api.param.system.MailSettingsParam +import top.fatweb.api.service.system.ISettingsService +import top.fatweb.api.settings.MailSettings +import top.fatweb.api.settings.SettingsOperator +import top.fatweb.api.vo.system.SettingsVo + +@Service +class SettingsServiceImpl : ISettingsService { + override fun get(): SettingsVo = SettingsConverter.systemSettingsToSettingsVo(SettingsOperator.settings()) + + override fun updateMail(mailSettingsParam: MailSettingsParam) { + mailSettingsParam.apply { + SettingsOperator.setMailValue(MailSettings::host, host) + SettingsOperator.setMailValue(MailSettings::port, port) + SettingsOperator.setMailValue(MailSettings::username, username) + SettingsOperator.setMailValue(MailSettings::password, password) + SettingsOperator.setMailValue(MailSettings::from, from) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/SysSettingServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/SysSettingServiceImpl.kt deleted file mode 100644 index 50c32c9..0000000 --- a/src/main/kotlin/top/fatweb/api/service/system/impl/SysSettingServiceImpl.kt +++ /dev/null @@ -1,17 +0,0 @@ -package top.fatweb.api.service.system.impl - -import org.springframework.stereotype.Service -import top.fatweb.api.param.system.SettingParam -import top.fatweb.api.service.system.ISysSettingService -import top.fatweb.api.vo.system.SettingVo - -@Service -class SysSettingServiceImpl : ISysSettingService { - override fun get(): SettingVo { - TODO("Not yet implemented") - } - - override fun update(settingParam: SettingParam) { - TODO("Not yet implemented") - } -} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/setting/SettingOperator.kt b/src/main/kotlin/top/fatweb/api/setting/SettingOperator.kt deleted file mode 100644 index c01b5dc..0000000 --- a/src/main/kotlin/top/fatweb/api/setting/SettingOperator.kt +++ /dev/null @@ -1,31 +0,0 @@ -package top.fatweb.api.setting - -import com.fasterxml.jackson.dataformat.yaml.YAMLMapper -import com.fasterxml.jackson.module.kotlin.readValue -import java.io.File -import java.io.IOException - -object SettingOperator { - const val SETTING_FILE_NAME = "data/config/setting.yaml" - private val yamlMapper = YAMLMapper() - - var systemSetting: SystemSetting? = null - - fun init() { - - } - - fun getFromSettingFile(): SystemSetting { - val settingFile = File(SETTING_FILE_NAME) - if (!settingFile.isFile) { - if (!settingFile.parentFile.isDirectory) { - if (!settingFile.parentFile.mkdirs()) { - throw IOException("Can not make directory: ${settingFile.parentFile.absolutePath}") - } - } - settingFile.delete() - settingFile.createNewFile() - } - return yamlMapper.readValue(settingFile) - } -} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/setting/SystemSetting.kt b/src/main/kotlin/top/fatweb/api/setting/SystemSetting.kt deleted file mode 100644 index 4dbe09e..0000000 --- a/src/main/kotlin/top/fatweb/api/setting/SystemSetting.kt +++ /dev/null @@ -1,5 +0,0 @@ -package top.fatweb.api.setting - -data class SystemSetting( - var mail: MailSetting? = null -) diff --git a/src/main/kotlin/top/fatweb/api/setting/MailSetting.kt b/src/main/kotlin/top/fatweb/api/settings/MailSettings.kt similarity index 50% rename from src/main/kotlin/top/fatweb/api/setting/MailSetting.kt rename to src/main/kotlin/top/fatweb/api/settings/MailSettings.kt index 1b993ce..43fbcb5 100644 --- a/src/main/kotlin/top/fatweb/api/setting/MailSetting.kt +++ b/src/main/kotlin/top/fatweb/api/settings/MailSettings.kt @@ -1,6 +1,9 @@ -package top.fatweb.api.setting +package top.fatweb.api.settings -data class MailSetting( +import com.fasterxml.jackson.annotation.JsonInclude + +@JsonInclude(JsonInclude.Include.NON_EMPTY) +data class MailSettings( var host: String? = null, var port: Int? = null, var username: String? = null, diff --git a/src/main/kotlin/top/fatweb/api/settings/SettingsOperator.kt b/src/main/kotlin/top/fatweb/api/settings/SettingsOperator.kt new file mode 100644 index 0000000..4b20dff --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/settings/SettingsOperator.kt @@ -0,0 +1,90 @@ +package top.fatweb.api.settings + +import com.fasterxml.jackson.databind.exc.MismatchedInputException +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper +import com.fasterxml.jackson.module.kotlin.readValue +import top.fatweb.api.util.StrUtil +import java.io.File +import java.io.IOException +import kotlin.reflect.KMutableProperty1 + +object SettingsOperator { + private const val SETTINGS_FILE_NAME = "data/config/settings.yaml" + + private val yamlMapper = YAMLMapper() + private val settingFile = File(SETTINGS_FILE_NAME) + private lateinit var systemSettings: SystemSettings + + init { + getFromSettingsFile() + } + + @Throws(IOException::class, MismatchedInputException::class) + private fun getFromSettingsFile() { + if (settingFile.isDirectory) { + throw IOException("'${settingFile.absoluteFile}' is a directory, cannot create settings file") + } + + if (!settingFile.isFile) { + if (settingFile.parentFile.isFile) { + throw IOException("'${settingFile.parentFile.absoluteFile}' is a file, cannot create settings file") + } + + if (!settingFile.parentFile.isDirectory) { + if (!settingFile.parentFile.mkdirs()) { + throw IOException("Cannot make directory: ${settingFile.parentFile.absolutePath}") + } + } + settingFile.delete() + settingFile.createNewFile() + } + + if (settingFile.length() == 0L) { + systemSettings = SystemSettings() + saveSettingsToFile() + } + + systemSettings = yamlMapper.readValue(settingFile) + } + + private fun saveSettingsToFile() { + yamlMapper.writeValue(settingFile, systemSettings) + } + + fun setMailValue(field: KMutableProperty1, value: V?) { + systemSettings.mail?.let { + if (field == MailSettings::password) { + getMailValue(MailSettings::password)?.let { password -> + if (StrUtil.md5(password) == value) { + return + } + } + } + field.set(it, value) + } ?: { + MailSettings().also { + field.set(it, value) + systemSettings.mail = it + } + } + + saveSettingsToFile() + } + + fun getMailValue(field: KMutableProperty1): V? = systemSettings.mail?.let(field) + + fun settings() = systemSettings.copy( + mail = systemSettings.mail?.copy() + ).apply { + mail?.apply { + password = password?.let { + StrUtil.md5(it) + } + } + } + + fun overwrite(systemSettings: SystemSettings) { + this.systemSettings = systemSettings + saveSettingsToFile() + } +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/settings/SystemSettings.kt b/src/main/kotlin/top/fatweb/api/settings/SystemSettings.kt new file mode 100644 index 0000000..b0f741e --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/settings/SystemSettings.kt @@ -0,0 +1,8 @@ +package top.fatweb.api.settings + +import com.fasterxml.jackson.annotation.JsonInclude + +@JsonInclude(JsonInclude.Include.NON_EMPTY) +data class SystemSettings( + var mail: MailSettings? = null +) diff --git a/src/main/kotlin/top/fatweb/api/util/StrUtil.kt b/src/main/kotlin/top/fatweb/api/util/StrUtil.kt index 958c9fb..0aef8cf 100644 --- a/src/main/kotlin/top/fatweb/api/util/StrUtil.kt +++ b/src/main/kotlin/top/fatweb/api/util/StrUtil.kt @@ -1,5 +1,6 @@ package top.fatweb.api.util +import java.security.MessageDigest import java.util.regex.Pattern /** @@ -49,4 +50,17 @@ object StrUtil { return password.toString() } + + fun md5(content: String): String { + val hash = MessageDigest.getInstance("MD5").digest(content.toByteArray()) + val hex = StringBuilder(hash.size * 2) + for (b in hash) { + var str = Integer.toHexString(b.toInt()) + if (b < 0x10) { + str = "0$str" + } + hex.append(str.substring(str.length -2)) + } + return hex.toString() + } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/vo/system/SettingVo.kt b/src/main/kotlin/top/fatweb/api/vo/system/SettingsVo.kt similarity index 68% rename from src/main/kotlin/top/fatweb/api/vo/system/SettingVo.kt rename to src/main/kotlin/top/fatweb/api/vo/system/SettingsVo.kt index ba5e9c5..85f19c9 100644 --- a/src/main/kotlin/top/fatweb/api/vo/system/SettingVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/system/SettingsVo.kt @@ -1,9 +1,9 @@ package top.fatweb.api.vo.system -data class SettingVo( - val mail: MailSettingVo? +data class SettingsVo( + val mail: MailSettingsVo? ) { - data class MailSettingVo( + data class MailSettingsVo( val host: String?, val port: Int?, val username: String?, diff --git a/src/main/resources/db/migration/R__Basic_data.sql b/src/main/resources/db/migration/R__Basic_data.sql index 6c1892d..dd102e3 100644 --- a/src/main/resources/db/migration/R__Basic_data.sql +++ b/src/main/resources/db/migration/R__Basic_data.sql @@ -12,6 +12,7 @@ insert into t_power (id, type_id) (1020000, 2), (1030000, 2), (1040000, 2), + (1050000, 2), (1010100, 3), (1010200, 3), (1010300, 3), @@ -25,6 +26,8 @@ insert into t_power (id, type_id) (1030300, 3), (1030400, 3), (1040100, 3), + (1050100, 3), + (1050300, 3), (1010101, 4), (1010102, 4), (1010103, 4), @@ -49,7 +52,9 @@ insert into t_power (id, type_id) (1030302, 4), (1030401, 4), (1030402, 4), - (1040103, 4) + (1040103, 4), + (1050101, 4), + (1050301, 4) as new_value on duplicate key update type_id = new_value.type_id; @@ -63,7 +68,8 @@ insert into t_menu (id, name, url, parent_id, module_id) (1020000, '角色管理', '/system/role', 1990000, 1000000), (1030000, '用户组管理', '/system/group', 1990000, 1000000), (1040000, '权限管理', '/system/power', 1990000, 1000000), - (1510000, '日志管理', '/system/log', 1990000, 1000000) as new_value + (1510000, '日志管理', '/system/log', 1990000, 1000000), + (1520000, '系统设置', '/system/settings', 1990000, 1000000) as new_value on duplicate key update name =new_value.name, url =new_value.url, parent_id =new_value.parent_id; @@ -82,7 +88,9 @@ insert into t_func(id, name, menu_id, parent_id) (1030300, '修改', 1030000, null), (1030400, '删除', 1030000, null), (1040100, '查询', 1040000, null), - (1510100, '查询', 1510000, null) as new_value + (1510100, '查询', 1510000, null), + (1520100, '查询', 1520000, null), + (1520300, '修改', 1520000, null) as new_value on duplicate key update name = new_value.name, menu_id = new_value.menu_id, parent_id = new_value.parent_id; @@ -113,7 +121,9 @@ insert into t_operation(id, name, code, func_id) (1030401, '单个', 'system:group:delete:one', 1030400), (1030402, '多个', 'system:group:delete:multiple', 1030400), (1040103, '列表', 'system:power:query:list', 1040100), - (1510101, '列表', 'system:log:query:all', 1510100) as new_value + (1510101, '列表', 'system:log:query:all', 1510100), + (1520101, '全部', 'system:settings:query:all', 1520100), + (1520301, '全部', 'system:settings:modify:all', 1520300) as new_value on duplicate key update name=new_value.name, code=new_value.code, func_id=new_value.func_id; \ No newline at end of file