|
@@ -1,19 +1,37 @@
|
|
|
package com.yaoxiang.planning.service
|
|
|
|
|
|
+import com.yaoxiang.planning.config.PlanningConstants
|
|
|
+import com.yaoxiang.planning.config.PlanningProperties
|
|
|
+import com.yaoxiang.planning.domain.Department
|
|
|
import com.yaoxiang.planning.domain.UserInfo
|
|
|
import com.yaoxiang.planning.model.AuthUser
|
|
|
+import com.yaoxiang.planning.repository.DepartmentRepo
|
|
|
import com.yaoxiang.planning.repository.UserInfoRepo
|
|
|
+import mu.KotlinLogging
|
|
|
+import org.springframework.beans.factory.annotation.Autowired
|
|
|
import org.springframework.security.core.context.SecurityContextHolder
|
|
|
import org.springframework.stereotype.Service
|
|
|
+import org.springframework.transaction.annotation.Transactional
|
|
|
+import org.springframework.util.DigestUtils
|
|
|
+import org.springframework.util.StringUtils
|
|
|
import java.util.*
|
|
|
import javax.annotation.Resource
|
|
|
|
|
|
+private val logger = KotlinLogging.logger { }
|
|
|
+
|
|
|
@Service
|
|
|
class UserService {
|
|
|
|
|
|
@Resource
|
|
|
private lateinit var userInfoRepo: UserInfoRepo
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private lateinit var departmentRepo: DepartmentRepo
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private lateinit var planningProperties: PlanningProperties
|
|
|
+
|
|
|
+ @Transactional(readOnly = true)
|
|
|
fun findByUsername(username: String): Optional<UserInfo> {
|
|
|
return userInfoRepo.findByUsername(username)
|
|
|
}
|
|
@@ -31,4 +49,155 @@ class UserService {
|
|
|
}
|
|
|
return null
|
|
|
}
|
|
|
+
|
|
|
+ @Transactional(readOnly = true)
|
|
|
+ fun existsByUsername(username: String): Boolean {
|
|
|
+ return userInfoRepo.existsByUsername(username)
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(readOnly = true)
|
|
|
+ fun get(id: Long): Optional<UserInfo> {
|
|
|
+ return userInfoRepo.findById(id)
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ fun add(username: String, name: String, password: String, departmentId: Long?): Boolean {
|
|
|
+ if (existsByUsername(username)) {
|
|
|
+ logger.error { "用户名已存在,username=${username}" }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ var department: Department? = null
|
|
|
+ if (departmentId != null) {
|
|
|
+ val optional = departmentRepo.findById(departmentId)
|
|
|
+ if (!optional.isPresent) {
|
|
|
+ logger.error { "未找到指定部门,departmentId=${departmentId}" }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ department = optional.get()
|
|
|
+ }
|
|
|
+ val userInfo = UserInfo()
|
|
|
+ userInfo.enabled = true
|
|
|
+ userInfo.username = username
|
|
|
+ userInfo.name = name
|
|
|
+ userInfo.password = DigestUtils.md5DigestAsHex(password.toByteArray()).toUpperCase()
|
|
|
+ userInfo.departmentId = departmentId
|
|
|
+ userInfo.departmentName = department?.name
|
|
|
+ userInfoRepo.save(userInfo)
|
|
|
+ logger.info { "新增用户,user=${userInfo}" }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ fun updateInfo(id: Long, name: String, age: Int?, gender: Int?, email: String?): Boolean {
|
|
|
+ val optional = get(id)
|
|
|
+ if (!optional.isPresent) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val user = optional.get()
|
|
|
+ user.name = name
|
|
|
+ user.age = age
|
|
|
+ user.gender = gender
|
|
|
+ user.email = email
|
|
|
+ val result = update(user)
|
|
|
+ logger.info { "更新用户信息,user=${user}" }
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ fun updatePassword(id: Long, oldPassword: String, newPassword: String): Boolean {
|
|
|
+ val optional = get(id)
|
|
|
+ if (!optional.isPresent) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val user = optional.get()
|
|
|
+ if (user.password != DigestUtils.md5DigestAsHex(oldPassword.toByteArray()).toUpperCase()) {
|
|
|
+ logger.error { "密码不匹配,username=${user.username},oldPassword=${oldPassword},newPassword=${newPassword}" }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ user.password = DigestUtils.md5DigestAsHex(newPassword.toByteArray()).toUpperCase()
|
|
|
+ val result = update(user)
|
|
|
+ logger.info { "更新用户密码,id=${id},result=${result}" }
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ fun resetPassword(id: Long, secret: String?): Boolean {
|
|
|
+ val optional = get(id)
|
|
|
+ if (!optional.isPresent) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val cur = currentUser()
|
|
|
+ val hasText = StringUtils.hasText(secret)
|
|
|
+ if (!hasText && (cur == null || cur.username != PlanningConstants.ADMIN_USER)) {
|
|
|
+ logger.error { "未提供secret且用户不是admin,currentUserId=${cur?.id}" }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if (hasText && secret != planningProperties.secret) {
|
|
|
+ logger.error { "secret不匹配,currentUserId=${cur?.id}" }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val user = optional.get()
|
|
|
+ user.password = DigestUtils.md5DigestAsHex(planningProperties.defaultPassword.toByteArray()).toUpperCase()
|
|
|
+ val result = update(user)
|
|
|
+ logger.info { "重置密码,id=${id},result=${result}" }
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ fun delete(id: Long): Boolean {
|
|
|
+ val optional = get(id)
|
|
|
+ if (!optional.isPresent) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val user = optional.get()
|
|
|
+ if (user.username == PlanningConstants.ADMIN_USER) {
|
|
|
+ logger.error { "不允许删除admin,currentUserId=${currentUserId()}" }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val result = delete(user)
|
|
|
+ logger.info { "删除用户,id=${id},result=${result}" }
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun delete(userInfo: UserInfo): Boolean {
|
|
|
+ userInfoRepo.delete(userInfo)
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ fun clearDepartment(departmentIds: List<Long>): Boolean {
|
|
|
+ val users = userInfoRepo.findAllByDepartmentIdIn(departmentIds)
|
|
|
+ users.forEach {
|
|
|
+ it.departmentId = null
|
|
|
+ it.departmentName = null
|
|
|
+ update(it)
|
|
|
+ }
|
|
|
+ logger.info { "清除部门信息,departmentIds=${departmentIds}" }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ fun updateDepartment(id: Long, departmentId: Long): Boolean {
|
|
|
+ val optional = get(id)
|
|
|
+ if (!optional.isPresent) {
|
|
|
+ logger.error { "未找到用户,更新部门失败,id=${id},departmentId=${departmentId}" }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val optional1 = departmentRepo.findById(departmentId)
|
|
|
+ if (!optional1.isPresent) {
|
|
|
+ logger.error { "未找到部门,更新部门失败,id=${id},departmentId=${departmentId}" }
|
|
|
+ }
|
|
|
+ val user = optional.get()
|
|
|
+ user.departmentId = departmentId
|
|
|
+ user.departmentName = optional1.get().name
|
|
|
+ val result = update(user)
|
|
|
+ logger.info { "更新部门,id=${id},departmentId=${departmentId},result=${result}" }
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ private fun update(userInfo: UserInfo): Boolean {
|
|
|
+ userInfoRepo.save(userInfo)
|
|
|
+ return true
|
|
|
+ }
|
|
|
}
|