master
parent
71d59d1a43
commit
62b578c7c5
|
|
@ -58,12 +58,22 @@ class BasicApplication : ControllerApplication() {
|
|||
routeContext.next()
|
||||
return@ANY
|
||||
} else {
|
||||
val token = routeContext.getSession<String?>("token")
|
||||
val name = routeContext.getSession<String?>("name")
|
||||
if (token != null && name != null) {
|
||||
val user = Service().getUserByName(name)
|
||||
var token = routeContext.getSession<String?>("token")
|
||||
var phone = routeContext.getSession<String?>("phone")
|
||||
if (token != null && phone != null) {
|
||||
val user = Service().getUserByPhone(phone)
|
||||
if (user != null) {
|
||||
if (token == md5(user.name + ":" + user.passwd)) {
|
||||
if (token == md5(user.phone + ":" + user.passwd)) {
|
||||
routeContext.next()
|
||||
return@ANY
|
||||
}
|
||||
}
|
||||
} else {
|
||||
token = routeContext.getHeader("token")
|
||||
phone = routeContext.getHeader("phone")
|
||||
val user = Service().getUserByPhone(phone)
|
||||
if (user != null) {
|
||||
if (token == md5(user.phone + ":" + user.passwd)) {
|
||||
routeContext.next()
|
||||
return@ANY
|
||||
}
|
||||
|
|
@ -71,7 +81,7 @@ class BasicApplication : ControllerApplication() {
|
|||
}
|
||||
}
|
||||
// routeContext.status(403)
|
||||
routeContext.json().send(JSONResponse(410,"",null))
|
||||
routeContext.json().send(JSONResponse(410, "", null))
|
||||
}.runAsFinally()
|
||||
|
||||
addControllers(CtrlUser::class.java)
|
||||
|
|
@ -81,7 +91,6 @@ class BasicApplication : ControllerApplication() {
|
|||
addControllers(CtrlRole::class.java)
|
||||
|
||||
|
||||
|
||||
val dp = DruidPlugin(
|
||||
"jdbc:mysql://rm-wz9n28sq10rz5b0u2o.mysql.rds.aliyuncs.com:3306/sh-upgram?serverTimezone=Asia/Shanghai",
|
||||
"shzhyjxy",
|
||||
|
|
|
|||
|
|
@ -4,12 +4,13 @@ import com.jfinal.plugin.activerecord.Model
|
|||
import java.sql.Timestamp
|
||||
import kotlin.reflect.full.memberProperties
|
||||
|
||||
data class ModelUser(val id: Int?, var name: String?, var passwd: String?, val groupId: Int/*default -1*/, val roleId: Int/*default -1*/, val createTime: Timestamp?) {
|
||||
data class ModelUser(val id: Int?, var name: String?, var phone: String?, var passwd: String?, val groupId: Int/*default -1*/, val roleId: Int/*default -1*/, val createTime: Timestamp?) {
|
||||
companion object {
|
||||
fun fromJFinal(model: Model<*>): ModelUser {
|
||||
return ModelUser(
|
||||
id = model.getInt("id"),
|
||||
name = model.getStr("name"),
|
||||
phone = model.getStr("phone"),
|
||||
passwd = model.getStr("passwd"),
|
||||
groupId = model.getInt("groupId"),
|
||||
roleId = model.getInt("roleId"),
|
||||
|
|
@ -26,7 +27,7 @@ data class ModelUser(val id: Int?, var name: String?, var passwd: String?, val g
|
|||
return model
|
||||
}
|
||||
|
||||
constructor() : this(null, null, null, -1, -1, null)
|
||||
constructor() : this(null, null, null, null, -1, -1, null)
|
||||
}
|
||||
|
||||
data class ModelGroup(val id: Int?, val description: String, val parentId: Int?, val createTime: Timestamp?) {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import ro.pippo.controller.*
|
|||
import ro.pippo.controller.extractor.Param
|
||||
import service.Service
|
||||
import util.*
|
||||
import java.net.URLDecoder
|
||||
|
||||
|
||||
@Path("/user")
|
||||
|
|
@ -146,29 +147,49 @@ class CtrlUser : Controller() {
|
|||
@POST("/auth")
|
||||
@Produces(Produces.JSON)
|
||||
fun auth(): JSONResponse {
|
||||
val name = request.getQueryParameter("name")?.toString(null)
|
||||
val phone = request.getQueryParameter("phone")?.toString(null)
|
||||
val passwd = request.getQueryParameter("passwd")?.toString(null)
|
||||
|
||||
if (name == null || passwd == null) {
|
||||
if (phone == null || passwd == null) {
|
||||
return responseInvalidParams()
|
||||
}
|
||||
|
||||
val user = service.getUserByName(name)
|
||||
val user = service.getUserByPhone(phone)
|
||||
if (user == null) {
|
||||
return JSONResponse(Const.codeResourceNotFound, Const.msgNotFound, null)
|
||||
} else {
|
||||
if (user.passwd == passwd) {
|
||||
user.passwd = null
|
||||
val session = request.getSession(true)
|
||||
val token = md5(name + ":" + passwd)
|
||||
session.put("name", name)
|
||||
val token = md5(phone + ":" + passwd)
|
||||
session.put("phone", phone)
|
||||
session.put("token", token)
|
||||
return responseSuccess(token)
|
||||
return responseSuccess(mapOf(
|
||||
"token" to token,
|
||||
"user" to user
|
||||
))
|
||||
} else {
|
||||
return JSONResponse(403, "密码错误", null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@GET("/hasPermission/{userId: [0-9]+}/{perm}")
|
||||
@Produces(Produces.JSON)
|
||||
fun hasPermission(@Param("userId") userId: Int, @Param("perm") perm: String): JSONResponse {
|
||||
val role = service.getRoleByUserId(userId)
|
||||
if (role != null) {
|
||||
val perms = service.getPermByRoleId(role.id!!)
|
||||
val hasPermission = perms.any {
|
||||
it.perm == URLDecoder.decode(perm)
|
||||
}
|
||||
if (hasPermission) {
|
||||
return responseSuccess(true)
|
||||
}
|
||||
}
|
||||
return responseSuccess(false)
|
||||
}
|
||||
|
||||
@POST("/bindGroup/{userId: [0-9]+}/{groupId: [0-9]+}")
|
||||
@Produces(Produces.JSON)
|
||||
fun bindGroup(@Param("userId") userId: Int, @Param("groupId") groupId: Int): JSONResponse {
|
||||
|
|
|
|||
|
|
@ -270,6 +270,14 @@ class Service {
|
|||
}
|
||||
}
|
||||
|
||||
fun getUserByPhone(phone:String):ModelUser? {
|
||||
val user = JFinalModelUser.DAO.findFirst("select * from `user` where phone='$phone'")
|
||||
return if (user == null) {
|
||||
null
|
||||
} else {
|
||||
ModelUser.fromJFinal(user)
|
||||
}
|
||||
}
|
||||
@Deprecated("")
|
||||
fun getUserByName(userName: String): ModelUser? {
|
||||
val conn = DBUtil.getConnection()
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ fun rsToUser(rs: ResultSet): ModelUser {
|
|||
return ModelUser(
|
||||
id = rs.getInt("id"),
|
||||
name = rs.getString("name"),
|
||||
phone = rs.getString("phone"),
|
||||
passwd = rs.getString("passwd"),
|
||||
groupId = rs.getInt("groupId"),
|
||||
roleId = rs.getInt("roleId"),
|
||||
|
|
@ -65,13 +66,6 @@ fun rsToUser(rs: ResultSet): ModelUser {
|
|||
)
|
||||
}
|
||||
|
||||
fun userToInsertSQL(user: ModelUser): String {
|
||||
return """
|
||||
insert into `user`(name,passwd,createTime)
|
||||
values('${user.name}','${user.passwd}','${Timestamp.from(Instant.now())}')
|
||||
"""
|
||||
}
|
||||
|
||||
fun rsToGroup(rs: ResultSet): ModelGroup {
|
||||
return ModelGroup(
|
||||
id = rs.getInt("id"),
|
||||
|
|
@ -81,13 +75,6 @@ fun rsToGroup(rs: ResultSet): ModelGroup {
|
|||
)
|
||||
}
|
||||
|
||||
fun groupToInsertSQL(group: ModelGroup): String {
|
||||
return """
|
||||
insert into `group`(description,createTime)
|
||||
values('${group.description}','${Timestamp.from(Instant.now())}')
|
||||
"""
|
||||
}
|
||||
|
||||
fun rsToRole(rs: ResultSet): ModelRole {
|
||||
return ModelRole(
|
||||
id = rs.getInt("id"),
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Subproject commit e8deba38aa7c1125be08c8f0610ad66a9df29ace
|
||||
Loading…
Reference in New Issue