auth,username duplicate check,filter,change passwd, default passwd
parent
39a0d26008
commit
0f2da43c89
|
|
@ -1,7 +1,7 @@
|
||||||
import com.jfinal.plugin.activerecord.ActiveRecordPlugin
|
import com.jfinal.plugin.activerecord.ActiveRecordPlugin
|
||||||
import com.jfinal.plugin.druid.DruidPlugin
|
import com.jfinal.plugin.druid.DruidPlugin
|
||||||
import model.ModelMenu2
|
import model.JFinalModelMenu
|
||||||
import model.ModelUser2
|
import model.JFinalModelUser
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import ro.pippo.controller.ControllerApplication
|
import ro.pippo.controller.ControllerApplication
|
||||||
import ro.pippo.core.Pippo
|
import ro.pippo.core.Pippo
|
||||||
|
|
@ -10,6 +10,8 @@ import ro.pippo.session.SessionManager
|
||||||
import ro.pippo.session.SessionRequestResponseFactory
|
import ro.pippo.session.SessionRequestResponseFactory
|
||||||
import ro.pippo.session.cookie.CookieSessionDataStorage
|
import ro.pippo.session.cookie.CookieSessionDataStorage
|
||||||
import route.*
|
import route.*
|
||||||
|
import service.Service
|
||||||
|
import util.md5
|
||||||
|
|
||||||
|
|
||||||
object Main {
|
object Main {
|
||||||
|
|
@ -52,6 +54,25 @@ class BasicApplication : ControllerApplication() {
|
||||||
println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
|
println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ANY("/.*") { routeContext ->
|
||||||
|
if (routeContext.requestUri.contains("user/auth")) {
|
||||||
|
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)
|
||||||
|
if (user != null) {
|
||||||
|
if (token == md5(user.name + ":" + user.passwd)) {
|
||||||
|
routeContext.next()
|
||||||
|
return@ANY
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.runAsFinally()
|
||||||
|
|
||||||
addControllers(CtrlUser::class.java)
|
addControllers(CtrlUser::class.java)
|
||||||
addControllers(CrtlGroup::class.java)
|
addControllers(CrtlGroup::class.java)
|
||||||
addControllers(CtrlMenu::class.java)
|
addControllers(CtrlMenu::class.java)
|
||||||
|
|
@ -59,19 +80,21 @@ class BasicApplication : ControllerApplication() {
|
||||||
addControllers(CtrlRole::class.java)
|
addControllers(CtrlRole::class.java)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
val dp = DruidPlugin(
|
val dp = DruidPlugin(
|
||||||
"jdbc:mysql://rm-wz9n28sq10rz5b0u2o.mysql.rds.aliyuncs.com:3306/sh-upgram?serverTimezone=Asia/Shanghai",
|
"jdbc:mysql://rm-wz9n28sq10rz5b0u2o.mysql.rds.aliyuncs.com:3306/sh-upgram?serverTimezone=Asia/Shanghai",
|
||||||
"shzhyjxy",
|
"shzhyjxy",
|
||||||
"Admin111")
|
"Admin111")
|
||||||
val arp = ActiveRecordPlugin(dp)
|
val arp = ActiveRecordPlugin(dp)
|
||||||
arp.addMapping("menu", ModelMenu2::class.java)
|
arp.addMapping("menu", JFinalModelMenu::class.java)
|
||||||
arp.addMapping("user", ModelUser2::class.java)
|
arp.addMapping("user", JFinalModelUser::class.java)
|
||||||
dp.start()
|
dp.start()
|
||||||
arp.start()
|
arp.start()
|
||||||
|
|
||||||
PUT("/test") { context ->
|
PUT("/test") { context ->
|
||||||
context.send("ok")
|
context.send("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createRequestResponseFactory(): RequestResponseFactory {
|
override fun createRequestResponseFactory(): RequestResponseFactory {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ object Const {
|
||||||
|
|
||||||
val msgEmptyMsg = ""
|
val msgEmptyMsg = ""
|
||||||
val msgNotFoundById = "根据id没有找到对应资源"
|
val msgNotFoundById = "根据id没有找到对应资源"
|
||||||
|
val msgNotFound = "没有找到对应资源"
|
||||||
val msgInsertUserFailed = "新增用户失败,请检查参数,或许已有相同用户存在"
|
val msgInsertUserFailed = "新增用户失败,请检查参数,或许已有相同用户存在"
|
||||||
val msgUpdateUserFailed = "更新用户信息失败"
|
val msgUpdateUserFailed = "更新用户信息失败"
|
||||||
val msgUpdateRoleFailed = "更新角色信息失败"
|
val msgUpdateRoleFailed = "更新角色信息失败"
|
||||||
|
|
@ -17,4 +18,5 @@ object Const {
|
||||||
val msgInvalidParams = "参数错误"
|
val msgInvalidParams = "参数错误"
|
||||||
val msgOperationFailed = "操作失败"
|
val msgOperationFailed = "操作失败"
|
||||||
val msgCheckNameFailed = "用户名不存在,请重新输入"
|
val msgCheckNameFailed = "用户名不存在,请重新输入"
|
||||||
|
val msgDuplicateUserName = "用户名已存在"
|
||||||
}
|
}
|
||||||
|
|
@ -2,34 +2,152 @@ package model
|
||||||
|
|
||||||
import com.jfinal.plugin.activerecord.Model
|
import com.jfinal.plugin.activerecord.Model
|
||||||
import java.sql.Timestamp
|
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?) {
|
||||||
|
companion object {
|
||||||
|
fun fromJFinal(model: Model<*>): ModelUser {
|
||||||
|
return ModelUser(
|
||||||
|
id = model.getInt("id"),
|
||||||
|
name = model.getStr("name"),
|
||||||
|
passwd = model.getStr("passwd"),
|
||||||
|
groupId = model.getInt("groupId"),
|
||||||
|
roleId = model.getInt("roleId"),
|
||||||
|
createTime = model.getTimestamp("createTime")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toJFinal(): Model<*> {
|
||||||
|
val model = JFinalModelUser()
|
||||||
|
this::class.memberProperties.forEach {
|
||||||
|
model.set(it.name, it.getter.call(this))
|
||||||
|
}
|
||||||
|
return model
|
||||||
|
}
|
||||||
|
|
||||||
class ModelUser(val id: Int?, var name: String?, var passwd: String?, val groupId: Int/*default -1*/, val roleId: Int/*default -1*/, val createTime: Timestamp?) {
|
|
||||||
constructor() : this(null, null, null, -1, -1, null)
|
constructor() : this(null, null, null, -1, -1, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
data class ModelGroup(val id: Int?, val description: String, val createTime: Timestamp?) {
|
data class ModelGroup(val id: Int?, val description: String, val createTime: Timestamp?) {
|
||||||
|
companion object {
|
||||||
|
fun fromJFinal(model: Model<*>): ModelGroup {
|
||||||
|
return ModelGroup(
|
||||||
|
id = model.getInt("id"),
|
||||||
|
description = model.getStr("description"),
|
||||||
|
createTime = model.getTimestamp("createTime")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toJFinal(): Model<*> {
|
||||||
|
val model = JFinalModelGroup()
|
||||||
|
this::class.memberProperties.forEach {
|
||||||
|
model.set(it.name, it.getter.call(this))
|
||||||
|
}
|
||||||
|
return model
|
||||||
|
}
|
||||||
|
|
||||||
constructor() : this(null, "", null)
|
constructor() : this(null, "", null)
|
||||||
}
|
}
|
||||||
|
|
||||||
data class ModelPerm(val id: Int?, val description: String?, val perm: String?, val createTime: Timestamp?) {
|
data class ModelPerm(val id: Int?, val description: String?, val perm: String?, val createTime: Timestamp?) {
|
||||||
|
companion object {
|
||||||
|
fun fromJFinal(model: Model<*>): ModelPerm {
|
||||||
|
return ModelPerm(
|
||||||
|
id = model.getInt("id"),
|
||||||
|
description = model.getStr("description"),
|
||||||
|
perm = model.getStr("perm"),
|
||||||
|
createTime = model.getTimestamp("createTime")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toJFinal(): Model<*> {
|
||||||
|
val model = JFinalModelGroup()
|
||||||
|
this::class.memberProperties.forEach {
|
||||||
|
model.set(it.name, it.getter.call(this))
|
||||||
|
}
|
||||||
|
return model
|
||||||
|
}
|
||||||
|
|
||||||
constructor() : this(null, null, null, null)
|
constructor() : this(null, null, null, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
data class ModelMenu(val id: Int?, val description: String?, val url: String?, val permId: Int?, val parentId:Int?, val createTime: Timestamp?) {
|
data class ModelMenu(val id: Int?, val description: String?, val url: String?, val permId: Int?, val parentId: Int?, val createTime: Timestamp?) {
|
||||||
constructor() : this(null, null, null, -1, -1,null)
|
companion object {
|
||||||
}
|
fun fromJFinal(model: Model<*>): ModelMenu {
|
||||||
|
return ModelMenu(
|
||||||
|
id = model.getInt("id"),
|
||||||
|
description = model.getStr("description"),
|
||||||
|
url = model.getStr("url"),
|
||||||
|
permId = model.getInt("permId"),
|
||||||
|
parentId = model.getInt("parentId"),
|
||||||
|
createTime = model.getTimestamp("createTime")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class ModelMenu2() : Model<ModelMenu2>() {
|
fun toJFinal(): Model<*> {
|
||||||
companion object {
|
val model = JFinalModelMenu()
|
||||||
val DAO: ModelMenu2 = ModelMenu2()
|
this::class.memberProperties.forEach {
|
||||||
}
|
model.set(it.name, it.getter.call(this))
|
||||||
}
|
}
|
||||||
class ModelUser2() : Model<ModelUser2>() {
|
return model
|
||||||
companion object {
|
|
||||||
val DAO: ModelUser2 = ModelUser2()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constructor() : this(null, null, null, -1, -1, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
data class ModelRole(val id: Int?, val description: String?, val createTime: Timestamp?) {
|
data class ModelRole(val id: Int?, val description: String?, val createTime: Timestamp?) {
|
||||||
|
companion object {
|
||||||
|
fun fromJFinal(model: Model<*>): ModelRole {
|
||||||
|
return ModelRole(
|
||||||
|
id = model.getInt("id"),
|
||||||
|
description = model.getStr("description"),
|
||||||
|
createTime = model.getTimestamp("createTime")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toJFinal(): Model<*> {
|
||||||
|
val model = JFinalModelRole()
|
||||||
|
this::class.memberProperties.forEach {
|
||||||
|
model.set(it.name, it.getter.call(this))
|
||||||
|
}
|
||||||
|
return model
|
||||||
|
}
|
||||||
|
|
||||||
constructor() : this(null, null, null)
|
constructor() : this(null, null, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class JFinalModelUser() : Model<JFinalModelUser>() {
|
||||||
|
companion object {
|
||||||
|
val DAO: JFinalModelUser = JFinalModelUser()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JFinalModelGroup() : Model<JFinalModelGroup>() {
|
||||||
|
companion object {
|
||||||
|
val DAO: JFinalModelGroup = JFinalModelGroup()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JFinalModelRole() : Model<JFinalModelRole>() {
|
||||||
|
companion object {
|
||||||
|
val DAO: JFinalModelRole = JFinalModelRole()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JFinalModelMenu() : Model<JFinalModelMenu>() {
|
||||||
|
companion object {
|
||||||
|
val DAO: JFinalModelMenu = JFinalModelMenu()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JFinalModelPerm() : Model<JFinalModelPerm>() {
|
||||||
|
companion object {
|
||||||
|
val DAO: JFinalModelPerm = JFinalModelPerm()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import com.jfinal.plugin.activerecord.Db
|
||||||
import com.jfinal.plugin.activerecord.Record
|
import com.jfinal.plugin.activerecord.Record
|
||||||
import model.JSONResponse
|
import model.JSONResponse
|
||||||
import model.ModelMenu
|
import model.ModelMenu
|
||||||
import model.ModelMenu2
|
import model.JFinalModelMenu
|
||||||
import ro.pippo.controller.*
|
import ro.pippo.controller.*
|
||||||
import ro.pippo.controller.extractor.Param
|
import ro.pippo.controller.extractor.Param
|
||||||
import service.Service
|
import service.Service
|
||||||
|
|
@ -53,7 +53,7 @@ class CtrlMenu : Controller() {
|
||||||
if (menu == null) {
|
if (menu == null) {
|
||||||
return responseInvalidParams()
|
return responseInvalidParams()
|
||||||
}
|
}
|
||||||
val menu2 = ModelMenu2()
|
val menu2 = JFinalModelMenu()
|
||||||
menu2.put("description", menu.description)
|
menu2.put("description", menu.description)
|
||||||
menu2.put("url", menu.url)
|
menu2.put("url", menu.url)
|
||||||
menu2.put("permId", menu.permId)
|
menu2.put("permId", menu.permId)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package route
|
||||||
import com.jfinal.kit.JsonKit
|
import com.jfinal.kit.JsonKit
|
||||||
import com.jfinal.plugin.activerecord.Db
|
import com.jfinal.plugin.activerecord.Db
|
||||||
import com.jfinal.plugin.activerecord.Record
|
import com.jfinal.plugin.activerecord.Record
|
||||||
|
import config.Const
|
||||||
import model.JSONResponse
|
import model.JSONResponse
|
||||||
import model.ModelUser
|
import model.ModelUser
|
||||||
import ro.pippo.controller.*
|
import ro.pippo.controller.*
|
||||||
|
|
@ -42,12 +43,16 @@ class CtrlUser : Controller() {
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
return responseInvalidParams()
|
return responseInvalidParams()
|
||||||
}
|
}
|
||||||
val isSuccess = service.saveUser(user)
|
val (status, isSuccess) = service.saveUser(user)
|
||||||
return if (isSuccess) {
|
return if (status == Service.Status.DUPLICATED) {
|
||||||
user.passwd = ""
|
JSONResponse(Const.codeInvalidParams, Const.msgDuplicateUserName, null)
|
||||||
responseSuccess(user)
|
|
||||||
} else {
|
} else {
|
||||||
responseOperationFailed()
|
if (isSuccess) {
|
||||||
|
user.passwd = ""
|
||||||
|
responseSuccess(user)
|
||||||
|
} else {
|
||||||
|
responseOperationFailed()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,33 +74,36 @@ class CtrlUser : Controller() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//校验用户名是否存在
|
//校验用户名是否存在
|
||||||
@POST("/checkName/{name}")
|
@POST("/checkName/{name}")
|
||||||
@Produces(Produces.JSON)
|
@Produces(Produces.JSON)
|
||||||
fun checkName(@Param("name") name : String ) :JSONResponse{
|
fun checkName(@Param("name") name: String): JSONResponse {
|
||||||
if ( null == name || "" == name.trim()){
|
if (null == name || "" == name.trim()) {
|
||||||
responseCheckNameFailed()
|
responseCheckNameFailed()
|
||||||
}
|
}
|
||||||
service.getUserByName(name) ?: return responseCheckNameFailed()
|
service.getUserByName(name) ?: return responseCheckNameFailed()
|
||||||
return responseSuccess(true)
|
return responseSuccess(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
//更新 User
|
//更新 User
|
||||||
@POST("/update")
|
@POST("/update")
|
||||||
@Produces(Produces.JSON)
|
@Produces(Produces.JSON)
|
||||||
fun updateUser() :JSONResponse{
|
fun updateUser(): JSONResponse {
|
||||||
if (request.parameters["id"] != null) {
|
if (request.parameters["id"] != null) {
|
||||||
val record = Record()
|
val record = Record()
|
||||||
updateUtil(record,request)
|
updateUtil(record, request)
|
||||||
val isSuccess = Db.update("user","id",record)
|
val isSuccess = Db.update("user", "id", record)
|
||||||
return if (isSuccess){
|
return if (isSuccess) {
|
||||||
responseSuccess(true)
|
responseSuccess(true)
|
||||||
} else{
|
} else {
|
||||||
responseUpdateUserFailed()
|
responseUpdateUserFailed()
|
||||||
}
|
}
|
||||||
}else {
|
} else {
|
||||||
return responseUpdateUserFailed()
|
return responseUpdateUserFailed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST("/del/{id: [0-9]+}")
|
@POST("/del/{id: [0-9]+}")
|
||||||
@Produces(Produces.JSON)
|
@Produces(Produces.JSON)
|
||||||
fun delUser(@Param("id") id: Int): JSONResponse {
|
fun delUser(@Param("id") id: Int): JSONResponse {
|
||||||
|
|
@ -126,7 +134,7 @@ class CtrlUser : Controller() {
|
||||||
return if (user == null) {
|
return if (user == null) {
|
||||||
responseNotFoundById()
|
responseNotFoundById()
|
||||||
} else {
|
} else {
|
||||||
val isSuccess = service.changePasswd(user, passwd!!)
|
val (isSuccess, _) = service.changePasswd(user, passwd!!)
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
responseSuccess(true)
|
responseSuccess(true)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -135,6 +143,28 @@ class CtrlUser : Controller() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@POST("/auth")
|
||||||
|
@Produces(Produces.JSON)
|
||||||
|
fun auth(): JSONResponse {
|
||||||
|
val name = request.getQueryParameter("name")?.toString(null)
|
||||||
|
val passwd = request.getQueryParameter("passwd")?.toString(null)
|
||||||
|
|
||||||
|
if (name == null || passwd == null) {
|
||||||
|
return responseInvalidParams()
|
||||||
|
}
|
||||||
|
|
||||||
|
val user = service.getUserByName(name)
|
||||||
|
if (user == null) {
|
||||||
|
return JSONResponse(Const.codeResourceNotFound, Const.msgNotFound, null)
|
||||||
|
} else {
|
||||||
|
val session = request.getSession(true)
|
||||||
|
val token = md5(name + ":" + passwd)
|
||||||
|
session.put("name", name)
|
||||||
|
session.put("token", token)
|
||||||
|
return responseSuccess(token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@POST("/bindGroup/{userId: [0-9]+}/{groupId: [0-9]+}")
|
@POST("/bindGroup/{userId: [0-9]+}/{groupId: [0-9]+}")
|
||||||
@Produces(Produces.JSON)
|
@Produces(Produces.JSON)
|
||||||
fun bindGroup(@Param("userId") userId: Int, @Param("groupId") groupId: Int): JSONResponse {
|
fun bindGroup(@Param("userId") userId: Int, @Param("groupId") groupId: Int): JSONResponse {
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,261 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import com.jfinal.plugin.activerecord.Db
|
import com.jfinal.plugin.activerecord.Db
|
||||||
|
import com.jfinal.plugin.activerecord.Model
|
||||||
import com.jfinal.plugin.activerecord.Page
|
import com.jfinal.plugin.activerecord.Page
|
||||||
import com.jfinal.plugin.activerecord.Record
|
import com.jfinal.plugin.activerecord.Record
|
||||||
import model.*
|
import model.*
|
||||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException
|
import sun.reflect.generics.reflectiveObjects.NotImplementedException
|
||||||
import util.*
|
import util.*
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
import kotlin.reflect.full.companionObjectInstance
|
||||||
|
import kotlin.reflect.full.functions
|
||||||
|
|
||||||
class Service {
|
class Service {
|
||||||
|
/*
|
||||||
|
通用函数
|
||||||
|
*/
|
||||||
|
|
||||||
|
private fun getDAOFromClass(cls: KClass<*>): Model<*> {
|
||||||
|
return when (cls) {
|
||||||
|
ModelUser::class -> JFinalModelUser.DAO
|
||||||
|
ModelRole::class -> JFinalModelRole.DAO
|
||||||
|
ModelGroup::class -> JFinalModelGroup.DAO
|
||||||
|
ModelPerm::class -> JFinalModelPerm.DAO
|
||||||
|
ModelMenu::class -> JFinalModelMenu.DAO
|
||||||
|
else -> throw IllegalArgumentException()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> getById(cls: KClass<*>, id: Int): T? {
|
||||||
|
val dao = getDAOFromClass(cls)
|
||||||
|
val model = dao.findById(id) ?: return null
|
||||||
|
|
||||||
|
val companion = cls.companionObjectInstance!!
|
||||||
|
val func = companion::class.functions.filter { "fromJFinal" == it.name }.first()
|
||||||
|
val ret = func.call(companion, model)
|
||||||
|
return ret as T?
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> getByColumn(cls: KClass<*>, instance: Any) {
|
||||||
|
throw NotImplementedException()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun save(cls: KClass<*>, instance: Any): Boolean {
|
||||||
|
val model = instance::class.functions.filter { "toJFinal" == it.name }.first() as Model<*>
|
||||||
|
return model.save()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> save(cls: KClass<*>, instance: Any, modelNeedReturn: Boolean): Pair<T?, Boolean> {
|
||||||
|
if (modelNeedReturn) {
|
||||||
|
val model = instance::class.functions.filter { "toJFinal" == it.name }.first() as Model<*>
|
||||||
|
val companion = cls.companionObjectInstance!!
|
||||||
|
val func = companion::class.functions.filter { "fromJFinal" == it.name }.first()
|
||||||
|
val ret = func.call(companion, model)
|
||||||
|
return (ret as T) to model.save()
|
||||||
|
} else {
|
||||||
|
return null to save(cls, instance)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun update(cls: KClass<*>, instance: Any): Boolean {
|
||||||
|
val model = instance::class.functions.filter { "toJFinal" == it.name }.first() as Model<*>
|
||||||
|
return model.update()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> update(cls: KClass<*>, instance: Any, modelNeedReturn: Boolean): Pair<T?, Boolean> {
|
||||||
|
if (modelNeedReturn) {
|
||||||
|
val model = instance::class.functions.filter { "toJFinal" == it.name }.first() as Model<*>
|
||||||
|
val companion = cls.companionObjectInstance!!
|
||||||
|
val func = companion::class.functions.filter { "fromJFinal" == it.name }.first()
|
||||||
|
val ret = func.call(companion, model)
|
||||||
|
return (ret as T) to model.update()
|
||||||
|
} else {
|
||||||
|
return null to save(cls, instance)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteById(cls: KClass<*>, id: Int): Boolean {
|
||||||
|
val dao = getDAOFromClass(cls)
|
||||||
|
return dao.deleteById(id)
|
||||||
|
}
|
||||||
|
//分隔符
|
||||||
|
|
||||||
|
fun isUserNameDuplicate(name: String): Boolean {
|
||||||
|
val userModel = JFinalModelUser.DAO.findFirst("select * from `user` where name='$name'")
|
||||||
|
return userModel != null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun saveUser(user: ModelUser): Pair<Status, Boolean> {
|
||||||
|
var isDuplicated = false
|
||||||
|
if (user.name != null) {
|
||||||
|
isDuplicated = isUserNameDuplicate(user.name!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
return if (isDuplicated) {
|
||||||
|
Status.DUPLICATED to false
|
||||||
|
} else {
|
||||||
|
user.passwd = "111"
|
||||||
|
val result = save(ModelUser::class, user)
|
||||||
|
if (result) {
|
||||||
|
Status.SUCCESS to result
|
||||||
|
} else {
|
||||||
|
Status.FAILED_WITH_UNKNOWN_REASON to result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getGroupByUserId(userId: Int): ModelGroup? {
|
||||||
|
val userModel = JFinalModelUser.DAO.findById(userId) ?: return null
|
||||||
|
val user = ModelUser.fromJFinal(userModel)
|
||||||
|
|
||||||
|
val groupModel = JFinalModelGroup.DAO.findById(user.groupId) ?: return null
|
||||||
|
return ModelGroup.fromJFinal(groupModel)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun changePasswd(user: ModelUser, passwd: String): Pair<Boolean, ModelUser?> {
|
||||||
|
val userModel = user.toJFinal()
|
||||||
|
val result = userModel.set("passwd", passwd).update()
|
||||||
|
return if (result) {
|
||||||
|
true to ModelUser.fromJFinal(userModel.remove("passwd"))
|
||||||
|
} else {
|
||||||
|
false to null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bindUserToGroup(userId: Int, groupId: Int): Boolean {
|
||||||
|
JFinalModelGroup.DAO.findById(groupId) ?: return false
|
||||||
|
val userModel = JFinalModelUser.DAO.findById(userId) ?: return false
|
||||||
|
userModel.set("groupId", groupId)
|
||||||
|
return userModel.update()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unbindUserToGroup(userId: Int, groupId: Int): Boolean {
|
||||||
|
JFinalModelGroup.DAO.findById(groupId) ?: return false
|
||||||
|
val userModel = JFinalModelUser.DAO.findById(userId) ?: return false
|
||||||
|
userModel.set("groupId", -1)
|
||||||
|
return userModel.update()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getRoleByUserId(userId: Int): ModelRole? {
|
||||||
|
val userModel = JFinalModelUser.DAO.findById(userId) ?: return null
|
||||||
|
val user = ModelUser.fromJFinal(userModel)
|
||||||
|
|
||||||
|
val roleModel = JFinalModelRole.DAO.findById(user.roleId) ?: return null
|
||||||
|
return ModelRole.fromJFinal(roleModel)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bindUserToRole(userId: Int, roleId: Int): Boolean {
|
||||||
|
JFinalModelRole.DAO.findById(roleId) ?: return false
|
||||||
|
val userModel = JFinalModelUser.DAO.findById(userId) ?: return false
|
||||||
|
userModel.set("roleId", roleId)
|
||||||
|
return userModel.update()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unbindUserToRole(userId: Int, roleId: Int): Boolean {
|
||||||
|
JFinalModelGroup.DAO.findById(roleId) ?: return false
|
||||||
|
val userModel = JFinalModelUser.DAO.findById(userId) ?: return false
|
||||||
|
userModel.set("roleId", -1)
|
||||||
|
return userModel.update()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getPermByRoleId(roleId: Int): List<ModelPerm> {
|
||||||
|
val list = mutableListOf<ModelPerm>()
|
||||||
|
val perms = JFinalModelPerm.DAO.find(
|
||||||
|
"""
|
||||||
|
select * from perm t
|
||||||
|
where exists(
|
||||||
|
select * from mapping_perm_role m where m.roleid = ?
|
||||||
|
)
|
||||||
|
""".trim()
|
||||||
|
) ?: return list
|
||||||
|
perms.forEach {
|
||||||
|
list.add(ModelPerm.fromJFinal(it))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bindPermToRole(permId: Int, roleId: Int): Boolean {
|
||||||
|
JFinalModelRole.DAO.findById(roleId) ?: return false
|
||||||
|
JFinalModelPerm.DAO.findById(permId) ?: return false
|
||||||
|
val record = Db.findFirst("select * from mapping_perm_role where permId=$permId and roleId=$roleId")
|
||||||
|
return if (record != null) true
|
||||||
|
else Db.save("mapping_perm_role", Record().set("permId", permId).set("roleId", roleId))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unbindPermToRole(permId: Int, roleId: Int): Boolean {
|
||||||
|
JFinalModelRole.DAO.findById(roleId) ?: return false
|
||||||
|
JFinalModelPerm.DAO.findById(permId) ?: return false
|
||||||
|
Db.findFirst("select * from mapping_perm_role where permId=$permId and roleId=$roleId") ?: return true
|
||||||
|
return Db.delete("mapping_perm_role", Record().set("permId", permId).set("roleId", roleId))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getMenuByRole(roleId: Int): List<ModelMenu> {
|
||||||
|
//todo join
|
||||||
|
throw NotImplementedException()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bindMenuToPerm(menuId: Int, permId: Int): Boolean {
|
||||||
|
JFinalModelPerm.DAO.findById(permId) ?: return false
|
||||||
|
val menu = JFinalModelMenu.DAO.findById(menuId) ?: return false
|
||||||
|
menu.set("permId", permId)
|
||||||
|
return menu.save()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unbindMenuToPerm(menuId: Int, permId: Int): Boolean {
|
||||||
|
JFinalModelPerm.DAO.findById(permId) ?: return false
|
||||||
|
val menu = JFinalModelMenu.DAO.findById(menuId) ?: return false
|
||||||
|
menu.set("permId", -1)
|
||||||
|
return menu.save()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTableNameFromClass(cls: KClass<*>): String {
|
||||||
|
return when (cls) {
|
||||||
|
ModelUser::class -> "`user`"
|
||||||
|
ModelRole::class -> "`role`"
|
||||||
|
ModelGroup::class -> "`group`"
|
||||||
|
ModelPerm::class -> "`perm`"
|
||||||
|
ModelMenu::class -> "`menu`"
|
||||||
|
else -> throw IllegalArgumentException()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun simplePagi(cls: KClass<*>, page: Int, size: Int): Page<Record> {
|
||||||
|
val tableName = getTableNameFromClass(cls)
|
||||||
|
return Db.paginate(page, size, "select *", "from `$tableName`")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated("", ReplaceWith("simplePagi"))
|
||||||
|
fun pagiUser(page: Int, size: Int): Page<Record> {
|
||||||
|
return Db.paginate(page, size, "select *", "from `user`")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated("", ReplaceWith("simplePagi"))
|
||||||
|
fun pagiGroup(page: Int, size: Int): Page<Record> {
|
||||||
|
return Db.paginate(page, size, "select *", "from `group`")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated("", ReplaceWith("simplePagi"))
|
||||||
|
fun pagiMenu(page: Int, size: Int): Page<Record> {
|
||||||
|
return Db.paginate(page, size, "select *", "from `menu`")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated("", ReplaceWith("simplePagi"))
|
||||||
|
fun pagiPerm(page: Int, size: Int): Page<Record> {
|
||||||
|
return Db.paginate(page, size, "select *", "from `perm`")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated("", ReplaceWith("simplePagi"))
|
||||||
|
fun pagiRole(page: Int, size: Int): Page<Record> {
|
||||||
|
return Db.paginate(page, size, "select *", "from `role`")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun getUserById(id: Int): ModelUser? {
|
fun getUserById(id: Int): ModelUser? {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
val prep = conn.prepareStatement("select * from `user` where id=?")
|
val prep = conn.prepareStatement("select * from `user` where id=?")
|
||||||
prep.setInt(1,id)
|
prep.setInt(1, id)
|
||||||
val rs = prep.executeQuery()
|
val rs = prep.executeQuery()
|
||||||
return if (rs.next()) {
|
return if (rs.next()) {
|
||||||
val user = rsToUser(rs)
|
val user = rsToUser(rs)
|
||||||
|
|
@ -21,42 +265,34 @@ class Service {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun getUserByName(userName : String ) : ModelUser? {
|
|
||||||
|
@Deprecated("")
|
||||||
|
fun getUserByName(userName: String): ModelUser? {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
val prep = conn.prepareStatement("select * from `user` where name=?");
|
val prep = conn.prepareStatement("select * from `user` where name=?")
|
||||||
prep.setString(1,userName)
|
prep.setString(1, userName)
|
||||||
val rs = prep.executeQuery();
|
val rs = prep.executeQuery();
|
||||||
return if (rs.next()){
|
return if (rs.next()) {
|
||||||
val user = rsToUser(rs)
|
val user = rsToUser(rs)
|
||||||
conn.close()
|
conn.close()
|
||||||
return user
|
return user
|
||||||
}else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun saveUser(user: ModelUser): Boolean {
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
conn.createStatement()
|
|
||||||
.execute(userToInsertSQL(user))
|
|
||||||
|
|
||||||
return true
|
|
||||||
} catch (e: Exception) {
|
|
||||||
return false
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun updateUser(user: ModelUser): Boolean {
|
fun updateUser(user: ModelUser): Boolean {
|
||||||
throw NotImplementedException()
|
throw NotImplementedException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun deleteUser(userId: Int): Boolean {
|
fun deleteUser(userId: Int): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("delete from `user` where id=?")
|
val prep = conn.prepareStatement("delete from `user` where id=?")
|
||||||
prep.setInt(1,userId)
|
prep.setInt(1, userId)
|
||||||
prep.execute()
|
prep.execute()
|
||||||
return true
|
return true
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|
@ -66,30 +302,7 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun changePasswd(user: ModelUser, passwd: String): Boolean {
|
@Deprecated("")
|
||||||
throw NotImplementedException()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getGroupByUserId(userId: Int): ModelGroup? {
|
|
||||||
val user = getUserById(userId)
|
|
||||||
if (user == null) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("select * from `group` where id=?")
|
|
||||||
prep.setInt(1,user.groupId)
|
|
||||||
val rs = prep.executeQuery()
|
|
||||||
return if (rs.next()) {
|
|
||||||
return rsToGroup(rs)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun saveGroup(group: ModelGroup): Boolean {
|
fun saveGroup(group: ModelGroup): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
|
|
@ -105,15 +318,17 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun updateGroup(group: ModelGroup): Boolean {
|
fun updateGroup(group: ModelGroup): Boolean {
|
||||||
throw NotImplementedException()
|
throw NotImplementedException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun deleteGroup(groupId: Int): Boolean {
|
fun deleteGroup(groupId: Int): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("delete from `group` where id=?")
|
val prep = conn.prepareStatement("delete from `group` where id=?")
|
||||||
prep.setInt(1,groupId)
|
prep.setInt(1, groupId)
|
||||||
prep.execute()
|
prep.execute()
|
||||||
return true
|
return true
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|
@ -123,11 +338,12 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun getGroupById(groupId: Int): ModelGroup? {
|
fun getGroupById(groupId: Int): ModelGroup? {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("select * from `group` where id=?")
|
val prep = conn.prepareStatement("select * from `group` where id=?")
|
||||||
prep.setInt(1,groupId)
|
prep.setInt(1, groupId)
|
||||||
val rs = prep.executeQuery()
|
val rs = prep.executeQuery()
|
||||||
return if (rs.next()) {
|
return if (rs.next()) {
|
||||||
return rsToGroup(rs)
|
return rsToGroup(rs)
|
||||||
|
|
@ -139,39 +355,12 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun bindUserToGroup(userId: Int, groupId: Int): Boolean {
|
@Deprecated("")
|
||||||
val user = getUserById(userId)
|
|
||||||
if (user == null) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("update `user` set groupId=? where id=?")
|
|
||||||
prep.setInt(1,groupId)
|
|
||||||
prep.setInt(2,userId)
|
|
||||||
return prep.execute()
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun unbindUserToGroup(userId: Int, groupId: Int): Boolean {
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("update `user` set groupId=-1 where id=?")
|
|
||||||
prep.setInt(1,userId)
|
|
||||||
return prep.execute()
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getRoleById(roleId: Int): ModelRole? {
|
fun getRoleById(roleId: Int): ModelRole? {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("select * from `role` where id=?")
|
val prep = conn.prepareStatement("select * from `role` where id=?")
|
||||||
prep.setInt(1,roleId)
|
prep.setInt(1, roleId)
|
||||||
val rs = prep.executeQuery()
|
val rs = prep.executeQuery()
|
||||||
|
|
||||||
return if (rs.next()) {
|
return if (rs.next()) {
|
||||||
|
|
@ -183,41 +372,23 @@ class Service {
|
||||||
conn.close()
|
conn.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun getRoleByName(roleName : String ) : ModelRole? {
|
|
||||||
|
@Deprecated("")
|
||||||
|
fun getRoleByName(roleName: String): ModelRole? {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
val prep = conn.prepareStatement("select * from `role` where description=?")
|
val prep = conn.prepareStatement("select * from `role` where description=?")
|
||||||
prep.setString(1,roleName)
|
prep.setString(1, roleName)
|
||||||
val rs = prep.executeQuery();
|
val rs = prep.executeQuery();
|
||||||
return if (rs.next()){
|
return if (rs.next()) {
|
||||||
val role = rsToRole(rs)
|
val role = rsToRole(rs)
|
||||||
conn.close()
|
conn.close()
|
||||||
return role
|
return role
|
||||||
}else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getRoleByUserId(userId: Int): ModelRole? {
|
@Deprecated("")
|
||||||
val user = getUserById(userId)
|
|
||||||
if (user == null) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("select * from `role` where id=?")
|
|
||||||
prep.setInt(1,user.roleId)
|
|
||||||
val rs = prep.executeQuery()
|
|
||||||
|
|
||||||
return if (rs.next()) {
|
|
||||||
return rsToRole(rs)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun saveRole(role: ModelRole): Boolean {
|
fun saveRole(role: ModelRole): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
|
|
@ -231,11 +402,12 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun deleteRole(roleId: Int): Boolean {
|
fun deleteRole(roleId: Int): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("delete from `role` where id=?")
|
val prep = conn.prepareStatement("delete from `role` where id=?")
|
||||||
prep.setInt(1,roleId)
|
prep.setInt(1, roleId)
|
||||||
prep.execute()
|
prep.execute()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
@ -247,49 +419,17 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun updateRole(role: ModelRole): Boolean {
|
fun updateRole(role: ModelRole): Boolean {
|
||||||
throw NotImplementedException()
|
throw NotImplementedException()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun bindUserToRole(userId: Int, roleId: Int): Boolean {
|
@Deprecated("")
|
||||||
val user = getUserById(userId)
|
|
||||||
if (user == null) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("update `user` set roleId=? where id=?")
|
|
||||||
prep.setInt(1,roleId)
|
|
||||||
prep.setInt(2,userId)
|
|
||||||
return prep.execute()
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun unbindUserToRole(userId: Int, roleId: Int): Boolean {
|
|
||||||
val user = getUserById(userId)
|
|
||||||
if (user == null) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("update `user` set roleId=-1 where id=?")
|
|
||||||
prep.setInt(1,userId)
|
|
||||||
|
|
||||||
return prep.execute()
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getPermById(permId: Int): ModelPerm? {
|
fun getPermById(permId: Int): ModelPerm? {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("select * from `perm` where id=?")
|
val prep = conn.prepareStatement("select * from `perm` where id=?")
|
||||||
prep.setInt(1,permId)
|
prep.setInt(1, permId)
|
||||||
|
|
||||||
val rs = prep.executeQuery()
|
val rs = prep.executeQuery()
|
||||||
|
|
||||||
|
|
@ -302,41 +442,23 @@ class Service {
|
||||||
conn.close()
|
conn.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun getPremByName(permName : String ) : ModelPerm? {
|
|
||||||
|
@Deprecated("")
|
||||||
|
fun getPremByName(permName: String): ModelPerm? {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
val prep = conn.prepareStatement("select * from `perm` where description=?")
|
val prep = conn.prepareStatement("select * from `perm` where description=?")
|
||||||
prep.setString(1,permName)
|
prep.setString(1, permName)
|
||||||
val rs = prep.executeQuery()
|
val rs = prep.executeQuery()
|
||||||
return if (rs.next()){
|
return if (rs.next()) {
|
||||||
val perm = rsToPerm(rs)
|
val perm = rsToPerm(rs)
|
||||||
conn.close()
|
conn.close()
|
||||||
return perm
|
return perm
|
||||||
}else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun getPermByRoleId(roleId: Int): List<ModelPerm> {
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("""
|
|
||||||
select * from perm t
|
|
||||||
where exists(
|
|
||||||
select * from mapping_perm_role m where m.roleid = ?
|
|
||||||
)
|
|
||||||
""".trim())
|
|
||||||
prep.setInt(1,roleId)
|
|
||||||
|
|
||||||
val rs = prep.executeQuery()
|
|
||||||
val list = mutableListOf<ModelPerm>()
|
|
||||||
while (rs.next()) {
|
|
||||||
list.add(rsToPerm(rs))
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun savePerm(perm: ModelPerm): Boolean {
|
fun savePerm(perm: ModelPerm): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
|
|
@ -350,50 +472,17 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun updatePerm(perm: ModelPerm): Boolean {
|
fun updatePerm(perm: ModelPerm): Boolean {
|
||||||
throw NotImplementedException()
|
throw NotImplementedException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun deletePerm(permId: Int): Boolean {
|
fun deletePerm(permId: Int): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("delete from `perm` where id=?")
|
val prep = conn.prepareStatement("delete from `perm` where id=?")
|
||||||
prep.setInt(1,permId)
|
prep.setInt(1, permId)
|
||||||
prep.execute()
|
|
||||||
|
|
||||||
return true
|
|
||||||
} catch (e: Exception) {
|
|
||||||
return false
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun bindPermToRole(permId: Int, roleId: Int): Boolean {
|
|
||||||
val perm = getPermById(permId)
|
|
||||||
val role = getRoleById(roleId)
|
|
||||||
if (perm == null || role == null) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("insert into mapping_perm_role(?,?)")
|
|
||||||
prep.setInt(1,permId)
|
|
||||||
prep.setInt(2,roleId)
|
|
||||||
|
|
||||||
return prep.execute()
|
|
||||||
} finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun unbindPermToRole(permId: Int, roleId: Int): Boolean {
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("delete from mapping_perm_role where permId=? and roleId=?)")
|
|
||||||
prep.setInt(1,permId)
|
|
||||||
prep.setInt(2,roleId)
|
|
||||||
prep.execute()
|
prep.execute()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
@ -404,11 +493,12 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun getMenuById(menuId: Int): ModelMenu? {
|
fun getMenuById(menuId: Int): ModelMenu? {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("select * from `menu` where id=?")
|
val prep = conn.prepareStatement("select * from `menu` where id=?")
|
||||||
prep.setInt(1,menuId)
|
prep.setInt(1, menuId)
|
||||||
|
|
||||||
val rs = prep.executeQuery()
|
val rs = prep.executeQuery()
|
||||||
|
|
||||||
|
|
@ -422,11 +512,7 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getMenuByRole(roleId: Int): List<ModelMenu> {
|
@Deprecated("")
|
||||||
//todo join
|
|
||||||
throw NotImplementedException()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun saveMenu(menu: ModelMenu): Boolean {
|
fun saveMenu(menu: ModelMenu): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
|
|
@ -440,15 +526,17 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun updateMenu(menu: ModelMenu): Boolean {
|
fun updateMenu(menu: ModelMenu): Boolean {
|
||||||
throw NotImplementedException()
|
throw NotImplementedException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("")
|
||||||
fun deleteMenu(menuId: Int): Boolean {
|
fun deleteMenu(menuId: Int): Boolean {
|
||||||
val conn = DBUtil.getConnection()
|
val conn = DBUtil.getConnection()
|
||||||
try {
|
try {
|
||||||
val prep = conn.prepareStatement("delete from `menu` where id=?")
|
val prep = conn.prepareStatement("delete from `menu` where id=?")
|
||||||
prep.setInt(1,menuId)
|
prep.setInt(1, menuId)
|
||||||
prep.execute()
|
prep.execute()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
@ -459,52 +547,7 @@ class Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun bindMenuToPerm(menuId: Int, permId: Int): Boolean {
|
enum class Status {
|
||||||
val menu = getMenuById(menuId)
|
SUCCESS, FAILED_WITH_UNKNOWN_REASON, DUPLICATED
|
||||||
if (menu == null) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val conn = DBUtil.getConnection()
|
|
||||||
try {
|
|
||||||
val prep = conn.prepareStatement("update `menu` set permId=? where id=?")
|
|
||||||
prep.setInt(1,-1)
|
|
||||||
prep.setInt(2,-menuId)
|
|
||||||
prep.execute()
|
|
||||||
|
|
||||||
prep.setInt(1,permId)
|
|
||||||
prep.setInt(2,menuId)
|
|
||||||
|
|
||||||
val bool = prep.executeUpdate()
|
|
||||||
return bool == 1
|
|
||||||
}finally {
|
|
||||||
conn.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun unbindMenuToPerm(menuId: Int, permId: Int): Boolean {
|
|
||||||
val prep = DBUtil.getConnection().prepareStatement("update `menu` set permId=-1 where id=?")
|
|
||||||
prep.setInt(1,menuId)
|
|
||||||
return prep.execute()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun pagiUser(page: Int, size: Int): Page<Record> {
|
|
||||||
return Db.paginate(page, size, "select *", "from `user`")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun pagiGroup(page: Int, size: Int): Page<Record> {
|
|
||||||
return Db.paginate(page, size, "select *", "from `group`")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun pagiMenu(page: Int, size: Int): Page<Record> {
|
|
||||||
return Db.paginate(page, size, "select *", "from `menu`")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun pagiPerm(page: Int, size: Int): Page<Record> {
|
|
||||||
return Db.paginate(page, size, "select *", "from `perm`")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun pagiRole(page: Int, size: Int): Page<Record> {
|
|
||||||
return Db.paginate(page, size, "select *", "from `role`")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,8 @@ import config.Const
|
||||||
import model.*
|
import model.*
|
||||||
import ro.pippo.core.ParameterValue
|
import ro.pippo.core.ParameterValue
|
||||||
import ro.pippo.core.Request
|
import ro.pippo.core.Request
|
||||||
|
import java.security.MessageDigest
|
||||||
|
import java.security.NoSuchAlgorithmException
|
||||||
import java.sql.ResultSet
|
import java.sql.ResultSet
|
||||||
import java.sql.Timestamp
|
import java.sql.Timestamp
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
|
|
@ -18,24 +20,31 @@ fun responseNotFoundById(): JSONResponse {
|
||||||
fun responseOperationFailed(): JSONResponse {
|
fun responseOperationFailed(): JSONResponse {
|
||||||
return JSONResponse(Const.codeServiceOperationFailed, Const.msgOperationFailed, false)
|
return JSONResponse(Const.codeServiceOperationFailed, Const.msgOperationFailed, false)
|
||||||
}
|
}
|
||||||
fun responseUpdateUserFailed() : JSONResponse {
|
|
||||||
return JSONResponse(Const.codeServiceOperationFailed,Const.msgUpdateUserFailed,null)
|
fun responseUpdateUserFailed(): JSONResponse {
|
||||||
|
return JSONResponse(Const.codeServiceOperationFailed, Const.msgUpdateUserFailed, null)
|
||||||
}
|
}
|
||||||
fun responseUpdateRoleFailed() : JSONResponse {
|
|
||||||
return JSONResponse(Const.codeServiceOperationFailed,Const.msgUpdateRoleFailed,null)
|
fun responseUpdateRoleFailed(): JSONResponse {
|
||||||
|
return JSONResponse(Const.codeServiceOperationFailed, Const.msgUpdateRoleFailed, null)
|
||||||
}
|
}
|
||||||
fun responseUpdateGroupFailed() : JSONResponse {
|
|
||||||
return JSONResponse(Const.codeServiceOperationFailed,Const.msgUpdateGroupFailed,null)
|
fun responseUpdateGroupFailed(): JSONResponse {
|
||||||
|
return JSONResponse(Const.codeServiceOperationFailed, Const.msgUpdateGroupFailed, null)
|
||||||
}
|
}
|
||||||
fun responseUpdateMenuFailed() : JSONResponse {
|
|
||||||
return JSONResponse(Const.codeServiceOperationFailed,Const.msgUpdateMenuFailed,null)
|
fun responseUpdateMenuFailed(): JSONResponse {
|
||||||
|
return JSONResponse(Const.codeServiceOperationFailed, Const.msgUpdateMenuFailed, null)
|
||||||
}
|
}
|
||||||
fun responseUpdatePermFailed() : JSONResponse {
|
|
||||||
return JSONResponse(Const.codeServiceOperationFailed,Const.msgUpdatepermFailed,null)
|
fun responseUpdatePermFailed(): JSONResponse {
|
||||||
|
return JSONResponse(Const.codeServiceOperationFailed, Const.msgUpdatepermFailed, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun responseCheckNameFailed(): JSONResponse {
|
fun responseCheckNameFailed(): JSONResponse {
|
||||||
return JSONResponse(Const.codeSuccess, "", false)
|
return JSONResponse(Const.codeSuccess, "", false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun responseInvalidParams(): JSONResponse {
|
fun responseInvalidParams(): JSONResponse {
|
||||||
return JSONResponse(Const.codeInvalidParams, Const.msgInvalidParams, null)
|
return JSONResponse(Const.codeInvalidParams, Const.msgInvalidParams, null)
|
||||||
}
|
}
|
||||||
|
|
@ -126,13 +135,35 @@ fun menuToInsertSQL(menu: ModelMenu): String {
|
||||||
values('${menu.description}','${menu.description}','${menu.permId}','${Timestamp.from(Instant.now())}')
|
values('${menu.description}','${menu.description}','${menu.permId}','${Timestamp.from(Instant.now())}')
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
fun updateUtil(record:Record , request:Request) :Record {
|
|
||||||
|
|
||||||
val params : Map<String, ParameterValue> = request.parameters
|
fun updateUtil(record: Record, request: Request): Record {
|
||||||
params.forEach{
|
|
||||||
if ( null != it.value){
|
val params: Map<String, ParameterValue> = request.parameters
|
||||||
record.set(it.key, it.value.toString())
|
params.forEach {
|
||||||
|
if (null != it.value) {
|
||||||
|
record.set(it.key, it.value.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return record
|
return record
|
||||||
|
}
|
||||||
|
|
||||||
|
fun md5(str: String): String {
|
||||||
|
try {
|
||||||
|
val instance: MessageDigest = MessageDigest.getInstance("MD5")//获取md5加密对象
|
||||||
|
val digest: ByteArray = instance.digest(str.toByteArray())//对字符串加密,返回字节数组
|
||||||
|
val sb = StringBuffer()
|
||||||
|
for (b in digest) {
|
||||||
|
var i: Int = b.toInt() and 0xff//获取低八位有效值
|
||||||
|
var hexString = Integer.toHexString(i)//将整数转化为16进制
|
||||||
|
if (hexString.length < 2) {
|
||||||
|
hexString = "0" + hexString//如果是一位的话,补0
|
||||||
|
}
|
||||||
|
sb.append(hexString)
|
||||||
|
}
|
||||||
|
return sb.toString()
|
||||||
|
|
||||||
|
} catch (e: NoSuchAlgorithmException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue