Skip to content

Commit

Permalink
Add Role Permission API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jzow committed Sep 28, 2023
1 parent 345db84 commit f1f3316
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wansensoft.dto.role.AddOrUpdateRoleDTO;
import com.wansensoft.dto.role.RoleListDTO;
import com.wansensoft.dto.role.RolePermissionDTO;
import com.wansensoft.service.role.ISysRoleService;
import com.wansensoft.service.role.KtSysRoleService;
import com.wansensoft.service.system.ISysMenuService;
Expand Down Expand Up @@ -75,4 +76,9 @@ public Response<String> addOrUpdateRole(@RequestBody AddOrUpdateRoleDTO addOrUpd
public Response<String> deleteRole(@RequestParam(value = "id") String id) {
return ktSysRoleService.deleteRole(id);
}

@PostMapping("permission")
public Response<String> rolePermission(@RequestBody RolePermissionDTO rolePermissionDTO) {
return ktSysRoleService.rolePermission(rolePermissionDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.IService
import com.wansensoft.dto.role.AddOrUpdateRoleDTO
import com.wansensoft.dto.role.RoleListDTO
import com.wansensoft.dto.role.RolePermissionDTO
import com.wansensoft.entities.role.SysRole
import com.wansensoft.utils.response.Response
import com.wansensoft.vo.RoleVO
Expand All @@ -19,4 +20,6 @@ interface KtSysRoleService : IService<SysRole> {
fun addOrUpdateRole(addOrUpdateRoleDTO : AddOrUpdateRoleDTO?) : Response<String>

fun deleteRole(id: String?): Response<String>

fun rolePermission(rolePermissionDTO: RolePermissionDTO): Response<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.wansensoft.dto.role.AddOrUpdateRoleDTO
import com.wansensoft.dto.role.RoleListDTO
import com.wansensoft.dto.role.RolePermissionDTO
import com.wansensoft.entities.role.SysRole
import com.wansensoft.entities.role.SysRoleMenuRel
import com.wansensoft.mappers.role.SysRoleMapper
Expand All @@ -22,16 +23,17 @@ import java.time.LocalDateTime

@Service
open class KtSysRoleServiceImpl(
private val roleMapper: SysRoleMapper,
private val roleMenuRelMapper: SysRoleMenuRelMapper
private val roleMapper: SysRoleMapper,
private val roleMenuRelMapper: SysRoleMenuRelMapper,
private val roleMenuRelService: ISysRoleMenuRelService
) : ServiceImpl<SysRoleMapper, SysRole>(), KtSysRoleService {

override fun roleList(): Response<List<RoleVO>> {
val roles = ArrayList<RoleVO>()

val sysRoles = lambdaQuery()
.eq(SysRole::getDeleteFlag, CommonConstants.NOT_DELETED)
.list()
.eq(SysRole::getDeleteFlag, CommonConstants.NOT_DELETED)
.list()
sysRoles.forEach { item ->
val roleVo = RoleVO()
BeanUtils.copyProperties(item, roleVo)
Expand Down Expand Up @@ -66,18 +68,18 @@ open class KtSysRoleServiceImpl(
}
listVo.forEach { roleVo ->
val roleMenuRelList = roleMenuRelMapper.selectList(
LambdaQueryWrapper<SysRoleMenuRel>()
.eq(SysRoleMenuRel::getRoleId, roleVo.id)
LambdaQueryWrapper<SysRoleMenuRel>()
.eq(SysRoleMenuRel::getRoleId, roleVo.id)
)
val menuList = ArrayList<Int>()
roleMenuRelList.forEach { roleMenuRel ->
val menuId = roleMenuRel.menuId
val regex = "\\d+".toRegex()
val matchResult = regex.findAll(menuId)
matchResult.forEach { match ->
menuList.add(match.value.toInt())
}
roleMenuRelList.forEach { roleMenuRel ->
val menuId = roleMenuRel.menuId
val regex = "\\d+".toRegex()
val matchResult = regex.findAll(menuId)
matchResult.forEach { match ->
menuList.add(match.value.toInt())
}
}
roleVo.menuIds = menuList
}
Page<RoleVO>().apply {
Expand All @@ -100,9 +102,9 @@ open class KtSysRoleServiceImpl(
}

val updateResult = lambdaUpdate()
.eq(SysRole::getId, id)
.set(SysRole::getStatus, status)
.update()
.eq(SysRole::getId, id)
.set(SysRole::getStatus, status)
.update()

return if (updateResult) {
Response.responseMsg(RoleCodeEnum.UPDATE_ROLE_STATUS_SUCCESS)
Expand Down Expand Up @@ -158,9 +160,9 @@ open class KtSysRoleServiceImpl(
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL)
}
val deleteResult = lambdaUpdate()
.eq(SysRole::getId, roleId)
.set(SysRole::getDeleteFlag, CommonConstants.DELETED)
.update()
.eq(SysRole::getId, roleId)
.set(SysRole::getDeleteFlag, CommonConstants.DELETED)
.update()

return if (deleteResult) {
Response.responseMsg(RoleCodeEnum.DELETE_ROLE_SUCCESS)
Expand All @@ -170,4 +172,26 @@ open class KtSysRoleServiceImpl(
}
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL)
}

// 帮我优化rolePermission方法

override fun rolePermission(rolePermissionDTO: RolePermissionDTO): Response<String> {
val roleId = rolePermissionDTO.id
val menuIds = rolePermissionDTO.menuIds
if (roleId == null || menuIds.isEmpty()) {
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL)
}

val roleMenuRel = SysRoleMenuRel()
val menuIdStr = menuIds.joinToString(separator = "") { "[${it}]" }
roleMenuRel.menuId = menuIdStr
roleMenuRel.roleId = roleId
// 进行saveOrUpdate操作
val saveBatchResult = roleMenuRelService.saveOrUpdate(roleMenuRel);
return if (saveBatchResult) {
Response.responseMsg(RoleCodeEnum.ROLE_PERMISSION_MENU_SUCCESS)
} else {
Response.responseMsg(RoleCodeEnum.ROLE_PERMISSION_MENU_ERROR)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public enum RoleCodeEnum {

DELETE_ROLE_SUCCESS("A0007", "删除角色成功"),

DELETE_ROLE_ERROR("A0210", "删除角色失败");
DELETE_ROLE_ERROR("A0210", "删除角色失败"),

ROLE_PERMISSION_MENU_SUCCESS("A0008", "赋予角色菜单权限成功"),

ROLE_PERMISSION_MENU_ERROR("A0211", "赋予角色菜单权限失败");

/**
* 响应状态码
Expand Down

0 comments on commit f1f3316

Please sign in to comment.