From 95ea00e64383bd5d918283a08d33848e5faa900d Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Fri, 1 Dec 2023 17:21:42 +0800 Subject: [PATCH] Offline user when power change --- .../api/mapper/permission/UserMapper.kt | 6 +- .../permission/group/GroupUpdateParam.kt | 2 +- .../permission/role/RoleChangeStatusParam.kt | 2 +- .../param/permission/role/RoleUpdateParam.kt | 2 +- .../api/service/permission/IUserService.kt | 4 + .../permission/impl/GroupServiceImpl.kt | 23 +++- .../permission/impl/RoleServiceImpl.kt | 27 ++++- .../permission/impl/UserServiceImpl.kt | 4 + .../mapper/permission/UserMapper.xml | 112 +++++++++++------- 9 files changed, 128 insertions(+), 54 deletions(-) diff --git a/src/main/kotlin/top/fatweb/api/mapper/permission/UserMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/permission/UserMapper.kt index db9f519..069ec6e 100644 --- a/src/main/kotlin/top/fatweb/api/mapper/permission/UserMapper.kt +++ b/src/main/kotlin/top/fatweb/api/mapper/permission/UserMapper.kt @@ -18,9 +18,13 @@ interface UserMapper : BaseMapper { fun selectPage(page: IPage, searchType: String, searchValue: String?, searchRegex: Boolean): IPage - fun selectListWithRoleInfoByIds(userIds: List): List? + fun selectListWithRoleInfoByIds(userIds: List): List fun selectOneWithRoleInfoById(id: Long): User? fun selectListWithInfo(): List + + fun selectIdsWithRoleIds(roleIds: List): List + + fun selectIdsWithGroupIds(groupIds: List): List } diff --git a/src/main/kotlin/top/fatweb/api/param/permission/group/GroupUpdateParam.kt b/src/main/kotlin/top/fatweb/api/param/permission/group/GroupUpdateParam.kt index d539375..3a62411 100644 --- a/src/main/kotlin/top/fatweb/api/param/permission/group/GroupUpdateParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/permission/group/GroupUpdateParam.kt @@ -14,7 +14,7 @@ import jakarta.validation.constraints.NotNull data class GroupUpdateParam( @Schema(description = "用户组 ID") @field:NotNull(message = "ID can not be null") - val id: Long, + val id: Long?, @Schema(description = "用户组名称") @field:NotBlank(message = "Name can not be blank") diff --git a/src/main/kotlin/top/fatweb/api/param/permission/role/RoleChangeStatusParam.kt b/src/main/kotlin/top/fatweb/api/param/permission/role/RoleChangeStatusParam.kt index ba37b80..a6ce7cb 100644 --- a/src/main/kotlin/top/fatweb/api/param/permission/role/RoleChangeStatusParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/permission/role/RoleChangeStatusParam.kt @@ -13,7 +13,7 @@ import jakarta.validation.constraints.NotNull data class RoleChangeStatusParam( @Schema(description = "角色 ID") @field:NotNull(message = "Role id can not be null") - val id: Long, + val id: Long?, @Schema(description = "启用", allowableValues = ["true", "false"], defaultValue = "true") val enable: Boolean = true diff --git a/src/main/kotlin/top/fatweb/api/param/permission/role/RoleUpdateParam.kt b/src/main/kotlin/top/fatweb/api/param/permission/role/RoleUpdateParam.kt index 9b3ead9..f69fb91 100644 --- a/src/main/kotlin/top/fatweb/api/param/permission/role/RoleUpdateParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/permission/role/RoleUpdateParam.kt @@ -14,7 +14,7 @@ import jakarta.validation.constraints.NotNull data class RoleUpdateParam( @Schema(description = "角色 ID") @field:NotNull(message = "Role id can not be null") - val id: Long, + val id: Long?, @Schema(description = "角色名称") @field:NotBlank(message = "Name can not be blank") diff --git a/src/main/kotlin/top/fatweb/api/service/permission/IUserService.kt b/src/main/kotlin/top/fatweb/api/service/permission/IUserService.kt index 8731b61..5268641 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/IUserService.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/IUserService.kt @@ -35,4 +35,8 @@ interface IUserService : IService { fun deleteOne(id: Long) fun delete(userDeleteParam: UserDeleteParam) + + fun selectIdsWithRoleIds(roleIds: List): List + + fun selectIdsWithGroupIds(groupIds: List): List } diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/GroupServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/GroupServiceImpl.kt index 4ae7fcf..e6e7fcf 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/GroupServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/GroupServiceImpl.kt @@ -12,7 +12,10 @@ import top.fatweb.api.mapper.permission.GroupMapper import top.fatweb.api.param.permission.group.* import top.fatweb.api.service.permission.IGroupService import top.fatweb.api.service.permission.IRoleGroupService +import top.fatweb.api.service.permission.IUserService import top.fatweb.api.util.PageUtil +import top.fatweb.api.util.RedisUtil +import top.fatweb.api.util.WebUtil import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.permission.base.GroupVo import top.fatweb.api.vo.permission.GroupWithRoleVo @@ -25,7 +28,9 @@ import top.fatweb.api.vo.permission.GroupWithRoleVo */ @Service class GroupServiceImpl( - private val roleGroupService: IRoleGroupService + private val redisUtil: RedisUtil, + private val roleGroupService: IRoleGroupService, + private val userService: IUserService ) : ServiceImpl(), IGroupService { override fun getPage(groupGetParam: GroupGetParam?): PageVo { val groupIdsPage = Page(groupGetParam?.currentPage ?: 1, groupGetParam?.pageSize ?: 20) @@ -107,11 +112,19 @@ class GroupServiceImpl( }) } + groupUpdateParam.id?.let { offlineUser(it) } + return GroupConverter.groupToGroupVo(group) } override fun changeStatus(groupChangeStatusParam: GroupChangeStatusParam): Boolean { - return updateById(GroupConverter.groupChangeStatusParamToGroup(groupChangeStatusParam)) + updateById(GroupConverter.groupChangeStatusParamToGroup(groupChangeStatusParam)).let { + if (it && !groupChangeStatusParam.enable) { + groupChangeStatusParam.id?.let { id -> offlineUser(id) } + } + + return it + } } @Transactional @@ -123,5 +136,11 @@ class GroupServiceImpl( override fun delete(groupDeleteParam: GroupDeleteParam) { baseMapper.deleteBatchIds(groupDeleteParam.ids) roleGroupService.remove(KtQueryWrapper(RoleGroup()).`in`(RoleGroup::groupId, groupDeleteParam.ids)) + offlineUser(*groupDeleteParam.ids.toLongArray()) + } + + private fun offlineUser(vararg groupIds: Long) { + val userIds = userService.selectIdsWithGroupIds(groupIds.toList()) + WebUtil.offlineUser(redisUtil, *userIds.toLongArray()) } } diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt index 7f2b3ce..148c24e 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt @@ -10,11 +10,10 @@ import top.fatweb.api.entity.permission.PowerRole import top.fatweb.api.entity.permission.Role import top.fatweb.api.mapper.permission.RoleMapper import top.fatweb.api.param.permission.role.* -import top.fatweb.api.service.permission.IFuncService -import top.fatweb.api.service.permission.IMenuService -import top.fatweb.api.service.permission.IPowerRoleService -import top.fatweb.api.service.permission.IRoleService +import top.fatweb.api.service.permission.* import top.fatweb.api.util.PageUtil +import top.fatweb.api.util.RedisUtil +import top.fatweb.api.util.WebUtil import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.permission.base.RoleVo import top.fatweb.api.vo.permission.RoleWithPowerVo @@ -27,9 +26,11 @@ import top.fatweb.api.vo.permission.RoleWithPowerVo */ @Service class RoleServiceImpl( + private val redisUtil: RedisUtil, private val powerRoleService: IPowerRoleService, private val funcService: IFuncService, - private val menuService: IMenuService + private val menuService: IMenuService, + private val userService: IUserService ) : ServiceImpl(), IRoleService { override fun getPage(roleGetParam: RoleGetParam?): PageVo { val roleIdsPage = Page(roleGetParam?.currentPage ?: 1, roleGetParam?.pageSize ?: 20) @@ -118,11 +119,19 @@ class RoleServiceImpl( }) } + roleUpdateParam.id?.let { offlineUser(it) } + return RoleConverter.roleToRoleVo(role) } override fun changeStatus(roleChangeStatusParam: RoleChangeStatusParam): Boolean { - return updateById(RoleConverter.roleChangeStatusParamToRole(roleChangeStatusParam)) + updateById(RoleConverter.roleChangeStatusParamToRole(roleChangeStatusParam)).let { + if (it && !roleChangeStatusParam.enable) { + roleChangeStatusParam.id?.let { id -> offlineUser(id) } + } + + return it + } } @Transactional @@ -134,6 +143,7 @@ class RoleServiceImpl( override fun delete(roleDeleteParam: RoleDeleteParam) { baseMapper.deleteBatchIds(roleDeleteParam.ids) powerRoleService.remove(KtQueryWrapper(PowerRole()).`in`(PowerRole::roleId, roleDeleteParam.ids)) + offlineUser(*roleDeleteParam.ids.toLongArray()) } private fun getFullPowerIds(powerIds: List): Set { @@ -161,4 +171,9 @@ class RoleServiceImpl( getMenuParent(it, parentIds) } } + + private fun offlineUser(vararg roleIds: Long) { + val userIds = userService.selectIdsWithRoleIds(roleIds.toList()) + WebUtil.offlineUser(redisUtil, *userIds.toLongArray()) + } } diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt index 79a045c..fa90b5d 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt @@ -248,4 +248,8 @@ class UserServiceImpl( WebUtil.offlineUser(redisUtil, *ids.toLongArray()) } + + override fun selectIdsWithRoleIds(roleIds: List) = baseMapper.selectIdsWithRoleIds(roleIds) + + override fun selectIdsWithGroupIds(groupIds: List) = baseMapper.selectIdsWithGroupIds(groupIds) } diff --git a/src/main/resources/mapper/permission/UserMapper.xml b/src/main/resources/mapper/permission/UserMapper.xml index d35e579..cf2ff7c 100644 --- a/src/main/resources/mapper/permission/UserMapper.xml +++ b/src/main/resources/mapper/permission/UserMapper.xml @@ -39,7 +39,7 @@ t.id as operation_id, t.name as operation_name, t.code as operation_code, - t.func_id as operation_func_id + t.func_id as operation_func_id from t_user left join (select * from t_user_info where deleted = 0) as tui on t_user.id = tui.user_id left join (select * from t_user_group where deleted = 0) as tug on t_user.id = tug.user_id @@ -124,48 +124,48 @@ + + + +