Browse Source

权限管理类

tags/v1.0.0^2
chengwang 1 year ago
parent
commit
df47bc6f1d
8 changed files with 320 additions and 44 deletions
  1. +76
    -43
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/RoleController.java
  2. +26
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/UserRoleDto.java
  3. +19
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/mapper/OpRolePermissionMapper.java
  4. +2
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/mapper/UserRoleMapper.java
  5. +13
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/role/IRoleService.java
  6. +176
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/role/RoleServiceImpl.java
  7. +1
    -1
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/vo/RoleListVo.java
  8. +7
    -0
      tuoheng-service/tuoheng-admin/src/main/resources/mapper/UserRoleMapper.xml

+ 76
- 43
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/RoleController.java View File

@@ -1,6 +1,7 @@
package com.tuoheng.admin.controller;

import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.query.RoleQuery;
import com.tuoheng.admin.service.role.IRoleService;
import com.tuoheng.common.core.annotation.Log;
@@ -20,47 +21,79 @@ public class RoleController {
@Autowired
private IRoleService roleService;

// /**
// * 获取角色列表
// * @param roleQuery
// * @return
// */
// @GetMapping("/index")
// public JsonResult index(RoleQuery roleQuery){
// return roleService.getRoleList(roleQuery);
// }
//
// /**
// * 添加角色
// * @param entity
// * @return
// */
// @PostMapping("/add")
// public JsonResult add(@RequestBody Role entity){
// return roleService.editRole(entity);
// }
//
// /**
// * 编辑角色
// * @param entity
// * @return
// */
// @PutMapping("/edit")
// public JsonResult edit(@RequestBody Role entity){
// return roleService.editRole(entity);
// }
//
// /**
// * 删除角色
// *
// * @param roleIds 角色ID
// * @return
// */
// @Log(title = "角色管理", logType = LogType.DELETE)
//// @RequiresPermissions("sys:role:delete")
// @DeleteMapping("/delete/{roleIds}")
// public JsonResult delete(@PathVariable("roleIds") Integer[] roleIds) {
// return roleService.deleteByList(roleIds);
//
// }
/**
* 获取角色列表
* @param roleQuery
* @return
*/
@GetMapping("/index")
public JsonResult index(RoleQuery roleQuery){
return roleService.getRoleList(roleQuery);
}

/**
* 添加角色
* @param entity
* @return
*/
@PostMapping("/add")
public JsonResult add(@RequestBody Role entity){
return roleService.editRole(entity);
}

/**
* 编辑角色
* @param entity
* @return
*/
@PutMapping("/edit")
public JsonResult edit(@RequestBody Role entity){
return roleService.editRole(entity);
}

/**
* 删除角色
*
* @param roleIds 角色ID
* @return
*/
@Log(title = "角色管理", logType = LogType.DELETE)
// @RequiresPermissions("sys:role:delete")
@DeleteMapping("/delete/{roleIds}")
public JsonResult delete(@PathVariable("roleIds") Integer[] roleIds) {
return roleService.deleteByList(roleIds);
}

/**
* 获取角色列表
*
* @return
*/
@GetMapping("/getRoleList")
public JsonResult getRoleList() {
return roleService.getRoleLists();
}

/**
* 获取角色菜单列表
*
* @param query
* @return
*/
@GetMapping("/getMenuList")
public JsonResult getMenuList(RoleClientQuery query) {
return roleService.getMenuList(query);
}

/**
* 更新角色状态
* @param entity
* @return
*/
@PutMapping("/status")
public JsonResult updateStatus(@RequestBody Role entity){
return roleService.updateStatus(entity);
}


}

+ 26
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/UserRoleDto.java View File

@@ -0,0 +1,26 @@
package com.tuoheng.admin.dto;

import lombok.Data;

/**
* @Author xiaoying
* @Date 2022/11/24 19:08
*/
@Data
public class UserRoleDto {
/**
* 租户id
*/
private Integer tenantId;

/**
* 人员ID
*/
private Integer userId;

/**
* 角色ID
*/
private Integer roleId;

}

+ 19
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/mapper/OpRolePermissionMapper.java View File

@@ -0,0 +1,19 @@
package com.tuoheng.admin.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tuoheng.admin.entity.OpRolePermission;

/**
* @author 小影
* @description 针对表【op_role_permission(角色可操作的permission)】的数据库操作Mapper
* @createDate 2022-11-21 15:24:21
* @Entity com.tuoheng.system.entity.OpRolePermission
*/
public interface OpRolePermissionMapper extends BaseMapper<OpRolePermission> {

}





+ 2
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/mapper/UserRoleMapper.java View File

@@ -1,6 +1,7 @@
package com.tuoheng.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tuoheng.admin.dto.UserRoleDto;
import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.entity.UserRole;
import org.apache.ibatis.annotations.Param;
@@ -34,4 +35,5 @@ public interface UserRoleMapper extends BaseMapper<UserRole> {
*/
List<String> getUsersByURoleId(@Param("roleId") String roleId);

List<UserRoleDto> getListByRoleId(@Param("roleId") Integer roleId,@Param("tenantId") String tenantId);
}

+ 13
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/role/IRoleService.java View File

@@ -1,6 +1,7 @@
package com.tuoheng.admin.service.role;

import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.query.RoleQuery;
import com.tuoheng.common.core.common.IBaseService;
import com.tuoheng.common.core.utils.JsonResult;
@@ -11,4 +12,16 @@ import com.tuoheng.common.core.utils.JsonResult;
*/
public interface IRoleService {

JsonResult getRoleList(RoleQuery roleQuery);

JsonResult editRole(Role entity);

JsonResult deleteByList(Integer[] roleIds);

JsonResult getRoleLists();

JsonResult getMenuList(RoleClientQuery query);

JsonResult updateStatus(Role entity);

}

+ 176
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/role/RoleServiceImpl.java View File

@@ -1,11 +1,34 @@
package com.tuoheng.admin.service.role;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.admin.dto.UserRoleDto;
import com.tuoheng.admin.entity.OpRolePermission;
import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.entity.RoleMenu;
import com.tuoheng.admin.mapper.OpRolePermissionMapper;
import com.tuoheng.admin.mapper.RoleMapper;
import com.tuoheng.admin.mapper.RoleMenuMapper;
import com.tuoheng.admin.mapper.UserRoleMapper;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.query.RoleQuery;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.RoleListVo;
import com.tuoheng.common.core.common.BaseServiceImpl;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
* @Author ChengWang
@@ -14,4 +37,157 @@ import org.springframework.stereotype.Service;
@Service
public class RoleServiceImpl implements IRoleService {

@Autowired
private RoleMapper roleMapper;

@Autowired
private RoleMenuMapper roleMenuMapper;

@Autowired
private OpRolePermissionMapper opRolePermissionMapper;

@Autowired
private UserRoleMapper userRoleMapper;


@Override
public JsonResult getRoleList(RoleQuery roleQuery) {
IPage<Role> page = new Page<>(roleQuery.getPage(), roleQuery.getLimit());
// 查询条件
QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
// 租户ID
queryWrapper.eq("tenant_id", CurrentUserUtil.getTenantId());
// 角色名称
if (!StringUtils.isEmpty(roleQuery.getRoleName())) {
queryWrapper.like("role_name", roleQuery.getRoleName());
}
queryWrapper.eq("mark", 1);
//queryWrapper.orderByAsc("sort");
queryWrapper.orderByAsc("create_time");
queryWrapper.ne("code", "super");
// 查询分页数据
IPage<Role> pageData = roleMapper.selectPage(page, queryWrapper);
pageData.convert(x -> {
RoleListVo roleListVo = Convert.convert(RoleListVo.class, x);
// TODO...
return roleListVo;
});
return JsonResult.success(pageData);
}

@Override
public JsonResult editRole(Role entity) {
if (StringUtils.isNotNull(entity.getId()) && entity.getId() > 0) {
// 更新
Integer count = roleMapper.selectCount(new LambdaQueryWrapper<Role>()
.ne(Role::getId, entity.getId())
.eq(Role::getTenantId, CurrentUserUtil.getTenantId())
.eq(Role::getCode, entity.getCode())
.eq(Role::getMark, 1));
//当前用户
entity.setUpdateUser(CurrentUserUtil.getUserId());
entity.setUpdateTime(DateUtils.now());
if (count > 0) {
return JsonResult.error("系统中已存在相同的角色编码");
}
} else {
// 添加
Integer count = roleMapper.selectCount(new LambdaQueryWrapper<Role>()
.eq(Role::getTenantId, CurrentUserUtil.getTenantId())
.eq(Role::getCode, entity.getCode())
.eq(Role::getMark, 1));
if (count > 0) {
return JsonResult.error("系统中已存在相同的角色编码");
}
entity.setCreateUser(CurrentUserUtil.getUserId());
entity.setCreateTime(DateUtils.now());
entity.setTenantId(CurrentUserUtil.getTenantId());
}
if(entity == null){
return JsonResult.error("实体对象不存在");
}
if(entity.getId() !=null && entity.getId()>0){
//修改记录
int i = roleMapper.updateById(entity);
if(i<=0){
return JsonResult.error();
}
}else {
//新增记录
int count = roleMapper.insert(entity);
if(count<=0){
return JsonResult.error();
}
}
return JsonResult.success();
}

@Override
public JsonResult deleteByList(Integer[] roleIds) {
List<Role> roles = roleMapper.selectList(Wrappers.<Role>lambdaQuery().eq(Role::getStatus, 1).eq(Role::getMark, 1).eq(Role::getTenantId, CurrentUserUtil.getTenantId()));
//
List<Integer> collect = roles.stream().filter(t -> !t.getCode().equals("001"))
.filter(t -> t.getCode().equals("002"))
.filter(t -> t.getCode().equals("003")).map(t -> t.getId()).collect(Collectors.toList());
if (collect.contains(roleIds)) {
return JsonResult.error("初始化配置角色不能删除");
}
for (Integer roleId : roleIds) {
List<RoleMenu> roleMenus = roleMenuMapper.selectList(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getRoleId, roleId).eq(RoleMenu::getMark, 1));
if (roleMenus.size() > 0 && StringUtils.isNotEmpty(roleMenus)) {
return JsonResult.error("该角色已配置菜单不能删除!");
}
List<OpRolePermission> opRolePermissionList = opRolePermissionMapper.selectList(Wrappers.<OpRolePermission>lambdaQuery().eq(OpRolePermission::getRoleId,roleId).eq(OpRolePermission::getMark, 1));
if (opRolePermissionList.size() > 0 && StringUtils.isNotEmpty(opRolePermissionList)) {
return JsonResult.error("该角色已配置权限不能删除!");
}
List<UserRoleDto> userRoles = userRoleMapper.getListByRoleId(roleId, CurrentUserUtil.getTenantId());
if (userRoles.size() > 0 && StringUtils.isNotEmpty(userRoles)) {
return JsonResult.error("该角色已经分配用户不能删除!");
}
Role role = roleMapper.selectById(roleId);
role.setMark(0);
roleMapper.updateById(role);
}
return JsonResult.success();
}

@Override
public JsonResult getRoleLists() {
QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
// 租户ID
//不展示超管
queryWrapper.ne("code", "001");
queryWrapper.eq("tenant_id", CurrentUserUtil.getTenantId());
queryWrapper.eq("status", 1);
queryWrapper.eq("mark", 1);
queryWrapper.orderByAsc("create_time");
List<Role> rolesLists = roleMapper.selectList(queryWrapper);

return JsonResult.success(rolesLists);
}

/**
* 获取菜单列表
* @param query
* @return
*/
@Override
public JsonResult getMenuList(RoleClientQuery query) {

return null;
}

@Override
public JsonResult updateStatus(Role entity) {
Role role = roleMapper.selectById(entity.getId());
if (null == role) {
return JsonResult.error("该角色不存在");
}
role.setStatus(entity.getStatus());
roleMapper.updateById(role);
return JsonResult.success();
}


}

+ 1
- 1
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/vo/RoleListVo.java View File

@@ -15,7 +15,7 @@ public class RoleListVo {
/**
* 角色ID
*/
private String id;
private Integer id;

/**
* 角色名称

+ 7
- 0
tuoheng-service/tuoheng-admin/src/main/resources/mapper/UserRoleMapper.xml View File

@@ -14,4 +14,11 @@
SELECT user_id FROM th_user_role WHERE role_id = #{roleId};
</select>

<select id="getListByRoleId" resultType="com.tuoheng.admin.dto.UserRoleDto">
SELECT *
FROM sys_user_role
WHERE sys_user_role.role_id = #{roleId}
and sys_user_role.tenant_id = #{tenantId}
</select>

</mapper>

Loading…
Cancel
Save