大致权限控制完成,新增优化权限控制代码
parent
a89b72fca1
commit
1ba81beb0a
@ -0,0 +1,52 @@
|
||||
package cn.zyp.stusystem.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.zyp.stusystem.common.Result;
|
||||
import cn.zyp.stusystem.dto.UserSimpleDTO;
|
||||
import cn.zyp.stusystem.entity.User;
|
||||
import cn.zyp.stusystem.service.UserService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@SaCheckLogin
|
||||
@SaCheckPermission("class:edit") // 或者更宽松的权限
|
||||
@GetMapping("/teachers")
|
||||
public Result<List<UserSimpleDTO>> listTeachers() {
|
||||
List<UserSimpleDTO> teachers = userService.list(
|
||||
new LambdaQueryWrapper<User>()
|
||||
.in(User::getRole, Arrays.asList("head_teacher", "teacher"))
|
||||
.eq(User::getStatus, 1)
|
||||
.select(User::getId, User::getName)
|
||||
).stream()
|
||||
.map(u -> new UserSimpleDTO(u.getId(), u.getName()))
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(teachers);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的权限列表
|
||||
*/
|
||||
@GetMapping("/permissions")
|
||||
public Result<List<String>> getUserPermissions() {
|
||||
// 从Token中获取当前用户ID
|
||||
Long userId = StpUtil.getLoginIdAsLong();
|
||||
List<String> permissions = userService.getUserPermissions(userId);
|
||||
return Result.success(permissions);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package cn.zyp.stusystem.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UserSimpleDTO {
|
||||
private Long id;
|
||||
private String name;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue