fix: 优化角色编辑功能

This commit is contained in:
萌狼蓝天 2024-11-06 16:00:48 +08:00
parent efdde4e4e4
commit 19300abbd4
6 changed files with 116 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package com.guaiguailang.harmony.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.guaiguailang.harmony.domain.dto.ParamLogin;
import com.guaiguailang.harmony.domain.entity.SystemRole;
import com.guaiguailang.harmony.domain.vo.ResponseResult;
import com.guaiguailang.harmony.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
@ -87,4 +88,17 @@ public class AuthController {
StpUtil.logout();
return ResponseEntity.ok().build();
}
@Operation(
summary = "角色列表(所有)",
description = "获取所有角色列表,仅限管理员操作的接口",
tags = {"权限相关接口"}
)
@GetMapping("/role-all")
public ResponseEntity getRoleAll() {
return ResponseEntity.ok(authService.getRoleAll());
}
@PostMapping("/role-update")
public ResponseEntity updateRole(@RequestBody SystemRole role) {
return ResponseEntity.ok(authService.updateRole(role));
}
}

View File

@ -0,0 +1,14 @@
package com.guaiguailang.harmony.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.guaiguailang.harmony.domain.entity.SystemRole;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface RoleMapper extends BaseMapper<SystemRole> {
@Select("SELECT * FROM system_role")
List<SystemRole> getRoleAll();
}

View File

@ -26,9 +26,17 @@ public interface UserMapper extends BaseMapper<UserInfo> {
List<UserInfo> getUserListAll(int limit,int end, String merchantCode, Map<String, Object> queryConditions);
UserListNum getUserListNum(String merchantCode);
@Select("SELECT * FROM system_role WHERE level > (SELECT level FROM system_role WHERE role_id = #{roleId})")
@Select("SELECT * FROM system_role WHERE level > (SELECT level FROM system_role WHERE role_id = #{roleId} and status=1)")
List<SystemRole> getRoleListWhenAddUser(String role);
int addUser(ParamUserAdd userAddParam);
/**
* 获取用户 角色信息
* @param id 用户id
* @return 用户角色信息
*/
@Select("select * from system_role where role_id = (select role_id from user_info where user_info.id=#{id})")
SystemRole getRoleLevelByUserId(Long id);
}

View File

@ -1,6 +1,7 @@
package com.guaiguailang.harmony.service;
import com.guaiguailang.harmony.domain.dto.ParamLogin;
import com.guaiguailang.harmony.domain.entity.SystemRole;
import com.guaiguailang.harmony.domain.vo.ResponseResult;
import org.springframework.http.ResponseEntity;
@ -8,4 +9,8 @@ import java.security.NoSuchAlgorithmException;
public interface AuthService {
ResponseResult loginByAccount(ParamLogin loginParam) throws NoSuchAlgorithmException;
ResponseResult getRoleAll();
ResponseResult updateRole(SystemRole systemRole);
}

View File

@ -3,10 +3,12 @@ package com.guaiguailang.harmony.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.guaiguailang.harmony.domain.dto.ParamLogin;
import com.guaiguailang.harmony.domain.entity.SystemRole;
import com.guaiguailang.harmony.domain.entity.UserInfo;
import com.guaiguailang.harmony.domain.vo.ResponseResult;
import com.guaiguailang.harmony.domain.vo.UserInfoLogin;
import com.guaiguailang.harmony.mapper.AuthMapper;
import com.guaiguailang.harmony.mapper.RoleMapper;
import com.guaiguailang.harmony.mapper.UserMapper;
import com.guaiguailang.harmony.service.AuthService;
import com.guaiguailang.harmony.utils.PasswordEncryptor;
@ -32,9 +34,12 @@ public class AuthServiceImpl implements AuthService {
@Autowired
private final UserMapper userMapper;
public AuthServiceImpl(AuthMapper authMapper, UserMapper userMapper) {
private final RoleMapper roleMapper;
public AuthServiceImpl(AuthMapper authMapper, UserMapper userMapper, RoleMapper roleMapper) {
this.authMapper = authMapper;
this.userMapper = userMapper;
this.roleMapper = roleMapper;
}
@Override
@ -74,4 +79,33 @@ public class AuthServiceImpl implements AuthService {
userInfoLogin.setRoles(rs);
return ResponseResult.info(1,"ok",userInfoLogin);
}
@Override
public ResponseResult getRoleAll() {
List<SystemRole> result = roleMapper.getRoleAll();
return ResponseResult.success(result);
}
@Override
public ResponseResult updateRole(SystemRole systemRole) {
// todo 此处可以进行性能优化获取level从redis中获取
// 做一个校验更新的level不能小于操作人的level
Long id = Long.parseLong(StpUtil.getLoginId().toString());// 获取用户id
// 获取level
SystemRole systemRoleCreator = userMapper.getRoleLevelByUserId(id);
// if(userInfo.getLevel()!=1){
// // 仅限超级管理员(1)能操作所有
// }
// 算了先不给超级管理员特权
if(systemRole.getLevel()<=systemRoleCreator.getLevel()){
return ResponseResult.error("权限不足");
}
int result = roleMapper.updateById(systemRole);
if(result == 1) {
return ResponseResult.success(systemRole);
}else{
return ResponseResult.error("更新失败");
}
}
}

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.guaiguailang.harmony.mapper.RoleMapper">
<!-- &lt;!&ndash; 插入新角色 &ndash;&gt;-->
<!-- <insert id="insertRole" parameterType="com.guaiguailang.harmony.domain.entity.SystemRole">-->
<!-- INSERT INTO system_role (role_name, role_id, status, description, level)-->
<!-- VALUES (#{roleName}, #{roleId}, #{status}, #{description}, #{level})-->
<!-- </insert>-->
<!-- &lt;!&ndash; 更新角色信息 &ndash;&gt;-->
<!-- <update id="updateRole" parameterType="com.guaiguailang.harmony.domain.entity.SystemRole">-->
<!-- UPDATE system_role-->
<!-- SET role_name = #{roleName},-->
<!-- role_id = #{roleId},-->
<!-- status = #{status},-->
<!-- description = #{description},-->
<!-- level = #{level}-->
<!-- WHERE id = #{id}-->
<!-- </update>-->
<!-- &lt;!&ndash; 删除角色 &ndash;&gt;-->
<!-- <delete id="deleteRole" parameterType="long">-->
<!-- DELETE FROM system_role WHERE id = #{id}-->
<!-- </delete>-->
<!-- &lt;!&ndash; 查询所有角色 &ndash;&gt;-->
<!-- <select id="selectAllRoles" resultType="com.guaiguailang.harmony.domain.entity.SystemRole">-->
<!-- SELECT * FROM system_role-->
<!-- </select>-->
<!-- &lt;!&ndash; 根据ID查询角色 &ndash;&gt;-->
<!-- <select id="selectRoleById" parameterType="long" resultType="com.guaiguailang.harmony.domain.entity.SystemRole">-->
<!-- SELECT * FROM system_role WHERE id = #{id}-->
<!-- </select>-->
</mapper>