增加用户重置密码功能

This commit is contained in:
2020-05-28 17:02:13 +08:00
parent 3d3d6099fe
commit 2535ae08d0
8 changed files with 101 additions and 29 deletions

View File

@@ -1,5 +0,0 @@
package com.synebula.zeus.app.component
interface IUserAdded {
fun added(id: String, name: String)
}

View File

@@ -18,7 +18,7 @@ class SignInOutApp(var query: IUserQuery, override var logger: ILogger) : ISignI
@PostMapping("/in")
override fun signIn(name: String, password: String): HttpMessage {
return this.safeExecute("用户登录出现错误") {
it.load(this.query.signIn(name, password.toMd5()))
it.load(this.query.signIn(name, password))
}
}

View File

@@ -2,17 +2,13 @@ package com.synebula.zeus.app.controller.rbac
import com.synebula.gaea.app.UnionApp
import com.synebula.gaea.app.component.HttpMessage
import com.synebula.gaea.data.message.Status
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.query.IQuery
import com.synebula.zeus.app.component.IUserAdded
import com.synebula.zeus.domain.service.cmd.rbac.UserCmd
import com.synebula.zeus.domain.service.contr.rbac.IUserService
import com.synebula.zeus.query.view.UserView
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/users")
@@ -24,14 +20,6 @@ class UserApp(
"用户信息", UserView::class.java,
service, query, logger
) {
@Autowired
lateinit var userAdded: IUserAdded
override fun add(command: UserCmd): HttpMessage {
val msg = super.add(command)
userAdded.added(msg.data.toString(), command.name)
return msg
}
/**
* 激活用户
@@ -39,10 +27,32 @@ class UserApp(
* @param key 用户ID
*/
@GetMapping("/{key}/active")
fun active(@PathVariable key: String): String {
fun active(@PathVariable key: String, token: String): String {
this.safeExecute("激活用户出现异常") {
(this.service as IUserService).active(key)
(this.service as IUserService).active(key, token)
}
return "<html><body><div style='text-align: center; padding: 100px;'><h2>激活成功!</h2></div></body></html>"
}
@GetMapping("/{name}/forgot")
fun forgot(@PathVariable name: String): HttpMessage {
return this.safeExecute("遗忘用户密码出现异常") {
val users = this.query?.list(mapOf(Pair("name", name)), UserView::class.java)
if (users != null && users.isNotEmpty()) {
it.load((this.service as IUserService).forgotPassword(users[0].id))
} else {
it.status = Status.Failure
it.message = "找不到该用户信息"
}
}
}
@PutMapping("/{key}/password")
fun changePassword(@PathVariable key: String, password: String, token: String): HttpMessage {
return this.safeExecute("修改用户密码出现异常") {
it.load((this.service as IUserService).changePassword(key, password, token))
}
}
}

View File

@@ -8,4 +8,5 @@ class User(override var id: String? = null) : AggregateRoot<String>() {
var realName: String? = null
var phone: String? = null
var role: String = ""
var token: String? = null
}

View File

@@ -0,0 +1,7 @@
package com.synebula.zeus.domain.service.contr.component
interface IUserNotifier {
fun added(id: String, name: String, token: String)
fun forgot(id: String, name: String, token: String)
}

View File

@@ -1,5 +1,6 @@
package com.synebula.zeus.domain.service.contr.rbac
import com.synebula.gaea.data.message.Message
import com.synebula.gaea.domain.service.IService
interface IUserService : IService<String> {
@@ -7,6 +8,23 @@ interface IUserService : IService<String> {
* 激活用户
*
* @param key 用户ID
* @param token 激活密令
*/
fun active(key: String)
fun active(key: String, token: String): Message<Any>
/**
* 激活用户
*
* @param key 用户ID
* @param token 激活密令
*/
fun changePassword(key: String, password: String, token: String): Message<Any>
/**
* 激活用户
*
* @param key 用户ID
*/
fun forgotPassword(key: String): Message<String>
}

View File

@@ -2,21 +2,31 @@ package com.synebula.zeus.domain.service.impl.rbac
import com.synebula.gaea.data.IObjectConverter
import com.synebula.gaea.data.message.Message
import com.synebula.gaea.data.message.Status
import com.synebula.gaea.domain.repository.IRepository
import com.synebula.gaea.domain.service.ICommand
import com.synebula.gaea.domain.service.Service
import com.synebula.gaea.extension.*
import com.synebula.gaea.log.ILogger
import com.synebula.zeus.domain.model.rbac.User
import com.synebula.zeus.domain.service.contr.component.IUserNotifier
import com.synebula.zeus.domain.service.contr.rbac.IUserService
import java.util.*
class UserService(repository: IRepository, converter: IObjectConverter, logger: ILogger) :
class UserService(
repository: IRepository,
converter: IObjectConverter,
logger: ILogger,
var userNotifier: IUserNotifier
) :
Service<User, String>(User::class.java, repository, converter, logger), IUserService {
override fun add(command: ICommand): Message<String> {
val user = this.convert(command)
user.password = user.password.toMd5()
user.token = UUID.randomUUID().toString()
user.alive = false
this.repository.add(user, this.clazz)
userNotifier.added(user.id!!, user.name, user.token!!)
return Message(user.id!!)
}
@@ -25,11 +35,42 @@ class UserService(repository: IRepository, converter: IObjectConverter, logger:
*
* @param key 用户ID
*/
override fun active(key: String) {
override fun active(key: String, token: String): Message<Any> {
val user = this.repository.get(key, this.clazz)
if (!user.alive) {
user.alive = true
this.repository.update(user, this.clazz)
return if (user.alive) {
Message("用户${user.name}无需重复激活")
} else {
if (token == user.token) {
user.alive = true
user.token = null
this.repository.update(user, this.clazz)
Message(Status.Success, "用户${user.name}激活成功")
} else {
Message(Status.Failure, "用户${user.name}激活失败, 请从系统发送的邮件链接激活用户")
}
}
}
override fun changePassword(key: String, password: String, token: String): Message<Any> {
val user = this.repository.get(key, this.clazz)
return if (token == user.token) {
user.password = password.toMd5()
user.token = null
this.repository.update(user, this.clazz)
Message()
} else {
Message(Status.Failure, "用户密码修改失败, 请从系统发送的邮件链接中修改密码")
}
}
override fun forgotPassword(key: String): Message<String> {
val user = this.repository.get(key, this.clazz)
return if (user.alive) {
user.token = UUID.randomUUID().toString()
this.repository.update(user, this.clazz)
userNotifier.forgot(user.id!!, user.name, user.token!!)
Message()
} else
Message(Status.Failure, "用户还未激活, 请先激活用户")
}
}

View File

@@ -23,7 +23,7 @@ class UserQuery(template: MongoTemplate) :
.and("password").isEqualTo(password.toMd5())
.and("alive").isEqualTo(true)
)
val user = this.template.findOne(query, this.clazz!!)
val user = this.template.findOne(query, this.clazz!!, "user")
return if (user != null)
Message(SignUserView(user.id, user.name, user.role ?: ""))
else