mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-06-02 00:16:11 +08:00
管理员查找所用用户加上角色
This commit is contained in:
parent
9209413483
commit
a21aadf283
@ -1,5 +1,6 @@
|
||||
package top.suyiiyii.sims.controller;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -7,10 +8,12 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import top.suyiiyii.sims.common.AuthAccess;
|
||||
import top.suyiiyii.sims.common.Result;
|
||||
import top.suyiiyii.sims.dto.RecordDto;
|
||||
import top.suyiiyii.sims.dto.UserDto;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.service.RoleService;
|
||||
import top.suyiiyii.sims.service.UserService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -28,13 +31,20 @@ public class AdminController {
|
||||
private RoleService roleService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
ModelMapper modelMapper;
|
||||
@AuthAccess(allowRoles = {"admin"})
|
||||
@GetMapping("/findAllUsersWithRoles")
|
||||
public Result findAllUsersWithRoles() {
|
||||
|
||||
List<User> userList = roleService.findAllUsersWithRoles();
|
||||
return Result.success(userList);
|
||||
public Result<List<UserDto>> findAllUsersWithRoles() {
|
||||
List<User> users = userService.selectAll();
|
||||
List<UserDto> UserDtos = new ArrayList<>();
|
||||
for (User user : users) {
|
||||
UserDto userDto = modelMapper.map(user, UserDto.class);
|
||||
userDto.setUserId(user.getStudentId());
|
||||
userDto.setRoles(roleService.getRolesById(user.getId()));
|
||||
UserDtos.add(userDto);
|
||||
}
|
||||
return Result.success(UserDtos);
|
||||
}
|
||||
|
||||
@AuthAccess(allowRoles = {"admin"})
|
||||
|
@ -16,14 +16,6 @@ import java.util.List;
|
||||
*/
|
||||
@Mapper
|
||||
public interface RoleMapper {
|
||||
@Insert("INSERT INTO role(name) VALUES(#{name}")
|
||||
void addRole(String name);
|
||||
|
||||
@Delete("DELETE FROM role WHERE name=#{name}")
|
||||
void deleteRole(String name);
|
||||
|
||||
@Update("UPDATE role SET name=#{newName} WHERE name=#{name}")
|
||||
void updateRole(String name, String newName);
|
||||
|
||||
/**
|
||||
* @param
|
||||
@ -32,25 +24,25 @@ public interface RoleMapper {
|
||||
* @Description: TODO 查询用户信息
|
||||
* @return: java.util.List<top.suyiiyii.sims.entity.User>
|
||||
*/
|
||||
@Select("SELECT u.username, u.name, u.userId, r.role_name " +
|
||||
@Select("SELECT u.username, u.student_id, r.role_name " +
|
||||
"FROM user u " +
|
||||
"LEFT JOIN user_role ur ON u.user_id = ur.user_id " +
|
||||
"LEFT JOIN role r ON ur.role_id = r.role_id")
|
||||
"LEFT JOIN user_role ur ON u.student_id = ur.user_id " +
|
||||
"LEFT JOIN role r ON ur.role_id = r.id")
|
||||
@Results({
|
||||
@Result(property = "username", column = "username"),
|
||||
@Result(property = "name", column = "name"),
|
||||
|
||||
@Result(property = "userId", column = "userId"),
|
||||
@Result(property = "group", column = "group"),
|
||||
@Result(property = "roles", column = "role_name", many = @Many(select = "selectRolesByUser"))
|
||||
@Result(property = "roles", column = "role_name", many = @Many(select = "selectRolesById"))
|
||||
})
|
||||
List<User> selectAllUsersWithRoles();
|
||||
|
||||
// 根据用户ID查询角色
|
||||
@Select("SELECT role_id, role_name " +
|
||||
"FROM role " +
|
||||
"WHERE role_id IN " +
|
||||
"(SELECT role_id FROM user_role WHERE user_id = #{user_id})")
|
||||
List<Role> selectRolesById(@Param("user_id") int id);
|
||||
"WHERE id IN " +
|
||||
"(SELECT role_id FROM user_role WHERE user_id = #{student_id})")
|
||||
List<Role> selectRolesById(@Param("student_id") int id);
|
||||
|
||||
@Select("SELECT role_name FROM role WHERE role_id=#{roleId}")
|
||||
List<String> selectRoleNamesByRoleId(Integer roleId);
|
||||
@ -62,4 +54,10 @@ public interface RoleMapper {
|
||||
Integer getIdByrolename(String roleName);
|
||||
@Select("SELECT student_id FROM user WHERE username=#{username}")
|
||||
Integer getStudentIdByUsername(String username);
|
||||
|
||||
@Select("SELECT role_name " +
|
||||
"FROM role " +
|
||||
"WHERE id IN " +
|
||||
"(SELECT role_id FROM user_role WHERE user_id = #{id})")
|
||||
List<String> getRolesById(int id);
|
||||
}
|
||||
|
@ -32,8 +32,8 @@ public class RoleService {
|
||||
* @Description: TODO 查看自己身份
|
||||
* @return: java.util.List<top.suyiiyii.sims.entity.Role>
|
||||
*/
|
||||
public List<Role> selectRolesById(int id) {
|
||||
return roleMapper.selectRolesById(id);
|
||||
public List<String> getRolesById(int id) {
|
||||
return roleMapper.getRolesById(id);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user