diff --git a/src/main/java/com/guaiguailang/harmony/controller/AuthController.java b/src/main/java/com/guaiguailang/harmony/controller/AuthController.java index a4c2ff5..b6d8c29 100644 --- a/src/main/java/com/guaiguailang/harmony/controller/AuthController.java +++ b/src/main/java/com/guaiguailang/harmony/controller/AuthController.java @@ -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)); + } } diff --git a/src/main/java/com/guaiguailang/harmony/mapper/RoleMapper.java b/src/main/java/com/guaiguailang/harmony/mapper/RoleMapper.java new file mode 100644 index 0000000..5374e63 --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/mapper/RoleMapper.java @@ -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 { + @Select("SELECT * FROM system_role") + List getRoleAll(); +} diff --git a/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java b/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java index b5295a4..447fc2e 100644 --- a/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java +++ b/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java @@ -26,9 +26,17 @@ public interface UserMapper extends BaseMapper { List getUserListAll(int limit,int end, String merchantCode, Map 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 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); } diff --git a/src/main/java/com/guaiguailang/harmony/service/AuthService.java b/src/main/java/com/guaiguailang/harmony/service/AuthService.java index 2f33b1a..286a366 100644 --- a/src/main/java/com/guaiguailang/harmony/service/AuthService.java +++ b/src/main/java/com/guaiguailang/harmony/service/AuthService.java @@ -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); } diff --git a/src/main/java/com/guaiguailang/harmony/service/impl/AuthServiceImpl.java b/src/main/java/com/guaiguailang/harmony/service/impl/AuthServiceImpl.java index 4be13ba..b123425 100644 --- a/src/main/java/com/guaiguailang/harmony/service/impl/AuthServiceImpl.java +++ b/src/main/java/com/guaiguailang/harmony/service/impl/AuthServiceImpl.java @@ -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 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("更新失败"); + } + } } diff --git a/src/main/resources/mapper/RoleMapper.xml b/src/main/resources/mapper/RoleMapper.xml new file mode 100644 index 0000000..54abf17 --- /dev/null +++ b/src/main/resources/mapper/RoleMapper.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file