feat: add school admin backend management
parent
a8f6857cc7
commit
bb7ebef4c3
@ -0,0 +1,38 @@
|
||||
package com.sztzjy.linkCommerce.controller.common;
|
||||
|
||||
import com.sztzjy.linkCommerce.util.file.IFileUtil;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("common")
|
||||
public class CommonUploadController {
|
||||
@Resource
|
||||
private IFileUtil fileUtil;
|
||||
|
||||
@PostMapping("/upload")
|
||||
public Map<String, Object> upload(@RequestPart("file") MultipartFile file) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
if (file == null || file.isEmpty()) {
|
||||
result.put("code", 400);
|
||||
result.put("msg", "文件不能为空");
|
||||
return result;
|
||||
}
|
||||
|
||||
String filePath = fileUtil.upload(file);
|
||||
String publicPath = "/file" + filePath;
|
||||
result.put("code", 200);
|
||||
result.put("msg", "上传成功");
|
||||
result.put("fileName", publicPath);
|
||||
result.put("url", publicPath);
|
||||
result.put("originalFilename", file.getOriginalFilename());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,271 @@
|
||||
package com.sztzjy.linkCommerce.controller.platformadmin;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.config.security.TokenProvider;
|
||||
import com.sztzjy.linkCommerce.entity.*;
|
||||
import com.sztzjy.linkCommerce.mapper.*;
|
||||
import com.sztzjy.linkCommerce.service.PlatformAdminService;
|
||||
import com.sztzjy.linkCommerce.service.UserInfoService;
|
||||
import com.sztzjy.linkCommerce.util.ResultEntity;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Api(tags = "平台超管管理")
|
||||
@RequestMapping("api/platform-admin")
|
||||
@RestController
|
||||
public class PlatformAdminController {
|
||||
@Autowired
|
||||
private PlatformAdminService platformAdminService;
|
||||
@Autowired
|
||||
private UserInfoService userInfoService;
|
||||
@Autowired
|
||||
private SchoolMapper schoolMapper;
|
||||
@Autowired
|
||||
private SchoolFacultyMapper schoolFacultyMapper;
|
||||
@Autowired
|
||||
private SchoolMajorMapper schoolMajorMapper;
|
||||
@Autowired
|
||||
private SchoolClassMapper schoolClassMapper;
|
||||
@Autowired
|
||||
private UserinfoMapper userinfoMapper;
|
||||
@Autowired
|
||||
private TaskAllocationMapper taskAllocationMapper;
|
||||
|
||||
@GetMapping("/schools")
|
||||
@ApiOperation("平台超管-学校列表")
|
||||
public ResultEntity<PageInfo<School>> listSchools(@RequestParam Integer index,
|
||||
@RequestParam Integer size,
|
||||
@RequestParam(required = false) String schoolName,
|
||||
HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
PageHelper.startPage(index, size);
|
||||
SchoolExample example = new SchoolExample();
|
||||
if (StringUtils.isNotBlank(schoolName)) {
|
||||
example.createCriteria().andSchoolNameLike("%" + schoolName + "%");
|
||||
}
|
||||
example.setOrderByClause("create_time desc");
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", new PageInfo<>(schoolMapper.selectByExample(example)));
|
||||
}
|
||||
|
||||
@GetMapping("/schools/{id}")
|
||||
@ApiOperation("平台超管-学校详情")
|
||||
public ResultEntity<School> getSchool(@PathVariable String id, HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", requireSchool(id));
|
||||
}
|
||||
|
||||
@PostMapping("/schools")
|
||||
@ApiOperation("平台超管-新增学校")
|
||||
public ResultEntity addSchool(@RequestBody School school, HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
if (StringUtils.isBlank(school.getSchoolName())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "学校名称不能为空");
|
||||
}
|
||||
if (StringUtils.isBlank(school.getSchoolId())) {
|
||||
school.setSchoolId(UUID.randomUUID().toString());
|
||||
}
|
||||
if (school.getCreateTime() == null) {
|
||||
school.setCreateTime(new Date());
|
||||
}
|
||||
int count = schoolMapper.insertSelective(school);
|
||||
return writeResult(count, "新增成功", "新增失败");
|
||||
}
|
||||
|
||||
@PutMapping("/schools/{id}")
|
||||
@ApiOperation("平台超管-编辑学校")
|
||||
public ResultEntity updateSchool(@PathVariable String id,
|
||||
@RequestBody School school,
|
||||
HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
requireSchool(id);
|
||||
school.setSchoolId(id);
|
||||
int count = schoolMapper.updateByPrimaryKeySelective(school);
|
||||
return writeResult(count, "编辑成功", "编辑失败");
|
||||
}
|
||||
|
||||
@DeleteMapping("/schools/{id}")
|
||||
@ApiOperation("平台超管-删除学校")
|
||||
public ResultEntity deleteSchool(@PathVariable String id, HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
requireSchool(id);
|
||||
if (hasSchoolBusinessData(id)) {
|
||||
return new ResultEntity<>(HttpStatus.ACCEPTED, "该学校下存在业务数据,不能删除");
|
||||
}
|
||||
int count = schoolMapper.deleteByPrimaryKey(id);
|
||||
return writeResult(count, "删除成功", "删除失败");
|
||||
}
|
||||
|
||||
@GetMapping("/school-admins")
|
||||
@ApiOperation("平台超管-学校管理员列表")
|
||||
public ResultEntity<PageInfo<Userinfo>> listSchoolAdmins(@RequestParam Integer index,
|
||||
@RequestParam Integer size,
|
||||
@RequestParam(required = false) String schoolId,
|
||||
@RequestParam(required = false) String name,
|
||||
@RequestParam(required = false) String username,
|
||||
@RequestParam(required = false) String phone,
|
||||
HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
PageHelper.startPage(index, size);
|
||||
UserinfoExample example = new UserinfoExample();
|
||||
UserinfoExample.Criteria criteria = example.createCriteria();
|
||||
criteria.andRoleEqualTo(2);
|
||||
if (StringUtils.isNotBlank(schoolId)) {
|
||||
criteria.andSchoolIdEqualTo(schoolId);
|
||||
}
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
criteria.andNameLike("%" + name + "%");
|
||||
}
|
||||
if (StringUtils.isNotBlank(username)) {
|
||||
criteria.andUsernameLike("%" + username + "%");
|
||||
}
|
||||
if (StringUtils.isNotBlank(phone)) {
|
||||
criteria.andPhoneLike("%" + phone + "%");
|
||||
}
|
||||
example.setOrderByClause("create_time desc");
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", new PageInfo<>(userinfoMapper.selectByExample(example)));
|
||||
}
|
||||
|
||||
@GetMapping("/school-admins/{id}")
|
||||
@ApiOperation("平台超管-学校管理员详情")
|
||||
public ResultEntity<Userinfo> getSchoolAdmin(@PathVariable String id, HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", requireSchoolAdminUser(id));
|
||||
}
|
||||
|
||||
@PostMapping("/school-admins")
|
||||
@ApiOperation("平台超管-新增学校管理员")
|
||||
public ResultEntity addSchoolAdmin(@RequestBody Userinfo schoolAdmin, HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
if (StringUtils.isBlank(schoolAdmin.getUsername()) || StringUtils.isBlank(schoolAdmin.getName())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "学校管理员姓名和账号不能为空");
|
||||
}
|
||||
String schoolId = platformAdminService.requireSchoolId(schoolAdmin.getSchoolId());
|
||||
requireSchool(schoolId);
|
||||
if (userInfoService.existsByUserName(schoolAdmin.getUsername())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "账号已存在");
|
||||
}
|
||||
schoolAdmin.setUserId(UUID.randomUUID().toString());
|
||||
if (StringUtils.isBlank(schoolAdmin.getPassword())) {
|
||||
schoolAdmin.setPassword("123qwe");
|
||||
}
|
||||
schoolAdmin.setCreateTime(new Date());
|
||||
platformAdminService.prepareSchoolAdminForSave(schoolAdmin, schoolId);
|
||||
int count = userinfoMapper.insertSelective(schoolAdmin);
|
||||
return writeResult(count, "新增成功", "新增失败");
|
||||
}
|
||||
|
||||
@PutMapping("/school-admins/{id}")
|
||||
@ApiOperation("平台超管-编辑学校管理员")
|
||||
public ResultEntity updateSchoolAdmin(@PathVariable String id,
|
||||
@RequestBody Userinfo schoolAdmin,
|
||||
HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
requireSchoolAdminUser(id);
|
||||
String schoolId = platformAdminService.requireSchoolId(schoolAdmin.getSchoolId());
|
||||
requireSchool(schoolId);
|
||||
schoolAdmin.setUserId(id);
|
||||
platformAdminService.prepareSchoolAdminForSave(schoolAdmin, schoolId);
|
||||
int count = userinfoMapper.updateByPrimaryKeySelective(schoolAdmin);
|
||||
return writeResult(count, "编辑成功", "编辑失败");
|
||||
}
|
||||
|
||||
@DeleteMapping("/school-admins/{id}")
|
||||
@ApiOperation("平台超管-删除学校管理员")
|
||||
public ResultEntity deleteSchoolAdmin(@PathVariable String id, HttpServletRequest request) {
|
||||
requirePlatformAdmin(request);
|
||||
requireSchoolAdminUser(id);
|
||||
int count = userinfoMapper.deleteByPrimaryKey(id);
|
||||
return writeResult(count, "删除成功", "删除失败");
|
||||
}
|
||||
|
||||
private void requirePlatformAdmin(HttpServletRequest request) {
|
||||
JwtUser user = TokenProvider.getJWTUser(request);
|
||||
platformAdminService.requirePlatformAdmin(user);
|
||||
}
|
||||
|
||||
private School requireSchool(String schoolId) {
|
||||
if (StringUtils.isBlank(schoolId)) {
|
||||
throw new IllegalArgumentException("学校ID不能为空");
|
||||
}
|
||||
School school = schoolMapper.selectByPrimaryKey(schoolId);
|
||||
if (school == null) {
|
||||
throw new IllegalArgumentException("学校不存在");
|
||||
}
|
||||
return school;
|
||||
}
|
||||
|
||||
private Userinfo requireSchoolAdminUser(String userId) {
|
||||
if (StringUtils.isBlank(userId)) {
|
||||
throw new IllegalArgumentException("用户ID不能为空");
|
||||
}
|
||||
Userinfo userinfo = userinfoMapper.selectByPrimaryKey(userId);
|
||||
if (userinfo == null || !Integer.valueOf(2).equals(userinfo.getRole())) {
|
||||
throw new IllegalArgumentException("学校管理员不存在");
|
||||
}
|
||||
return userinfo;
|
||||
}
|
||||
|
||||
private ResultEntity writeResult(int count, String success, String fail) {
|
||||
if (count > 0) {
|
||||
return new ResultEntity<>(HttpStatus.OK, success);
|
||||
}
|
||||
return new ResultEntity<>(HttpStatus.ACCEPTED, fail);
|
||||
}
|
||||
|
||||
private boolean hasSchoolBusinessData(String schoolId) {
|
||||
return hasFacultyInSchool(schoolId)
|
||||
|| hasMajorInSchool(schoolId)
|
||||
|| hasClassInSchool(schoolId)
|
||||
|| hasUserInSchool(schoolId)
|
||||
|| hasTaskInSchool(schoolId);
|
||||
}
|
||||
|
||||
private boolean hasFacultyInSchool(String schoolId) {
|
||||
SchoolFacultyExample example = new SchoolFacultyExample();
|
||||
example.createCriteria().andSchoolIdEqualTo(schoolId);
|
||||
return schoolFacultyMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private boolean hasMajorInSchool(String schoolId) {
|
||||
SchoolFacultyExample facultyExample = new SchoolFacultyExample();
|
||||
facultyExample.createCriteria().andSchoolIdEqualTo(schoolId);
|
||||
List<SchoolFaculty> faculties = schoolFacultyMapper.selectByExample(facultyExample);
|
||||
for (SchoolFaculty faculty : faculties) {
|
||||
SchoolMajorExample majorExample = new SchoolMajorExample();
|
||||
majorExample.createCriteria().andSchoolFacultyIdEqualTo(faculty.getSchoolFacultyId());
|
||||
if (schoolMajorMapper.countByExample(majorExample) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean hasClassInSchool(String schoolId) {
|
||||
SchoolClassExample example = new SchoolClassExample();
|
||||
example.createCriteria().andSchoolIdEqualTo(schoolId);
|
||||
return schoolClassMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private boolean hasUserInSchool(String schoolId) {
|
||||
UserinfoExample example = new UserinfoExample();
|
||||
example.createCriteria().andSchoolIdEqualTo(schoolId);
|
||||
return userinfoMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private boolean hasTaskInSchool(String schoolId) {
|
||||
TaskAllocationExample example = new TaskAllocationExample();
|
||||
example.createCriteria().andSchoolIdEqualTo(schoolId);
|
||||
return taskAllocationMapper.countByExample(example) > 0;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,456 @@
|
||||
package com.sztzjy.linkCommerce.controller.schooladmin;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.config.security.TokenProvider;
|
||||
import com.sztzjy.linkCommerce.entity.*;
|
||||
import com.sztzjy.linkCommerce.entity.dto.SchoolClassDto;
|
||||
import com.sztzjy.linkCommerce.entity.dto.SchoolFacultyDto;
|
||||
import com.sztzjy.linkCommerce.entity.dto.SchoolMajorDto;
|
||||
import com.sztzjy.linkCommerce.mapper.*;
|
||||
import com.sztzjy.linkCommerce.service.SchoolAdminService;
|
||||
import com.sztzjy.linkCommerce.service.SchoolService;
|
||||
import com.sztzjy.linkCommerce.service.UserInfoService;
|
||||
import com.sztzjy.linkCommerce.util.ResultEntity;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Api(tags = "学校管理员管理")
|
||||
@RequestMapping("api/school-admin")
|
||||
@RestController
|
||||
public class SchoolAdminController {
|
||||
@Autowired
|
||||
private SchoolAdminService schoolAdminService;
|
||||
@Autowired
|
||||
private SchoolService schoolService;
|
||||
@Autowired
|
||||
private UserInfoService userInfoService;
|
||||
@Autowired
|
||||
private SchoolMapper schoolMapper;
|
||||
@Autowired
|
||||
private SchoolFacultyMapper schoolFacultyMapper;
|
||||
@Autowired
|
||||
private SchoolMajorMapper schoolMajorMapper;
|
||||
@Autowired
|
||||
private SchoolClassMapper schoolClassMapper;
|
||||
@Autowired
|
||||
private UserinfoMapper userinfoMapper;
|
||||
@Autowired
|
||||
private TaskAllocationMapper taskAllocationMapper;
|
||||
|
||||
@GetMapping("/school")
|
||||
@ApiOperation("学校管理员-当前学校详情")
|
||||
public ResultEntity<School> getSchool(HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
School school = schoolMapper.selectByPrimaryKey(schoolId);
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", school);
|
||||
}
|
||||
|
||||
@PutMapping("/school")
|
||||
@ApiOperation("学校管理员-编辑当前学校")
|
||||
public ResultEntity updateSchool(@RequestBody School school, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
school.setSchoolId(schoolId);
|
||||
int count = schoolMapper.updateByPrimaryKeySelective(school);
|
||||
return writeResult(count, "编辑成功", "编辑失败");
|
||||
}
|
||||
|
||||
@GetMapping("/faculties")
|
||||
@ApiOperation("学校管理员-院系列表")
|
||||
public ResultEntity<PageInfo<SchoolFacultyDto>> listFaculties(@RequestParam Integer index,
|
||||
@RequestParam Integer size,
|
||||
@RequestParam(required = false) String schoolFacultyName,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
PageInfo<SchoolFacultyDto> pageInfo = schoolService.seleteSchoolFaculty(index, size, schoolId, schoolFacultyName);
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", pageInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/faculties")
|
||||
@ApiOperation("学校管理员-新增院系")
|
||||
public ResultEntity addFaculty(@RequestBody SchoolFaculty faculty, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
if (StringUtils.isBlank(faculty.getSchoolFacultyName())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "院系名称不能为空");
|
||||
}
|
||||
faculty.setSchoolFacultyId(UUID.randomUUID().toString());
|
||||
faculty.setSchoolId(schoolId);
|
||||
faculty.setCreateTime(new Date());
|
||||
int count = schoolFacultyMapper.insertSelective(faculty);
|
||||
return writeResult(count, "新增成功", "新增失败");
|
||||
}
|
||||
|
||||
@PutMapping("/faculties/{id}")
|
||||
@ApiOperation("学校管理员-编辑院系")
|
||||
public ResultEntity updateFaculty(@PathVariable String id,
|
||||
@RequestBody SchoolFaculty faculty,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
SchoolFaculty existing = requireFaculty(id, schoolId);
|
||||
faculty.setSchoolFacultyId(existing.getSchoolFacultyId());
|
||||
faculty.setSchoolId(schoolId);
|
||||
int count = schoolFacultyMapper.updateByPrimaryKeySelective(faculty);
|
||||
return writeResult(count, "编辑成功", "编辑失败");
|
||||
}
|
||||
|
||||
@DeleteMapping("/faculties/{id}")
|
||||
@ApiOperation("学校管理员-删除院系")
|
||||
public ResultEntity deleteFaculty(@PathVariable String id, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireFaculty(id, schoolId);
|
||||
if (hasMajorInFaculty(id) || hasUserByFaculty(id)) {
|
||||
return new ResultEntity<>(HttpStatus.ACCEPTED, "院系下存在专业或用户,不能删除");
|
||||
}
|
||||
int count = schoolFacultyMapper.deleteByPrimaryKey(id);
|
||||
return writeResult(count, "删除成功", "删除失败");
|
||||
}
|
||||
|
||||
@GetMapping("/majors")
|
||||
@ApiOperation("学校管理员-专业列表")
|
||||
public ResultEntity<PageInfo<SchoolMajorDto>> listMajors(@RequestParam Integer index,
|
||||
@RequestParam Integer size,
|
||||
@RequestParam(required = false) String schoolMajorName,
|
||||
@RequestParam(required = false) String schoolFacultyId,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
if (StringUtils.isNotBlank(schoolFacultyId)) {
|
||||
requireFaculty(schoolFacultyId, schoolId);
|
||||
}
|
||||
PageInfo<SchoolMajorDto> pageInfo = schoolService.seleteSchoolMajor(index, size, schoolId, schoolMajorName, schoolFacultyId);
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", pageInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/majors")
|
||||
@ApiOperation("学校管理员-新增专业")
|
||||
public ResultEntity addMajor(@RequestBody SchoolMajor major, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
if (StringUtils.isBlank(major.getSchoolMajorName())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "专业名称不能为空");
|
||||
}
|
||||
requireFaculty(major.getSchoolFacultyId(), schoolId);
|
||||
major.setSchoolMajorId(UUID.randomUUID().toString());
|
||||
major.setCreateTime(new Date());
|
||||
int count = schoolMajorMapper.insertSelective(major);
|
||||
return writeResult(count, "新增成功", "新增失败");
|
||||
}
|
||||
|
||||
@PutMapping("/majors/{id}")
|
||||
@ApiOperation("学校管理员-编辑专业")
|
||||
public ResultEntity updateMajor(@PathVariable String id,
|
||||
@RequestBody SchoolMajor major,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireMajor(id, schoolId);
|
||||
if (StringUtils.isNotBlank(major.getSchoolFacultyId())) {
|
||||
requireFaculty(major.getSchoolFacultyId(), schoolId);
|
||||
}
|
||||
major.setSchoolMajorId(id);
|
||||
int count = schoolMajorMapper.updateByPrimaryKeySelective(major);
|
||||
return writeResult(count, "编辑成功", "编辑失败");
|
||||
}
|
||||
|
||||
@DeleteMapping("/majors/{id}")
|
||||
@ApiOperation("学校管理员-删除专业")
|
||||
public ResultEntity deleteMajor(@PathVariable String id, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireMajor(id, schoolId);
|
||||
if (hasClassInMajor(id) || hasUserByMajor(id)) {
|
||||
return new ResultEntity<>(HttpStatus.ACCEPTED, "专业下存在班级或用户,不能删除");
|
||||
}
|
||||
int count = schoolMajorMapper.deleteByPrimaryKey(id);
|
||||
return writeResult(count, "删除成功", "删除失败");
|
||||
}
|
||||
|
||||
@GetMapping("/classes")
|
||||
@ApiOperation("学校管理员-班级列表")
|
||||
public ResultEntity<PageInfo<SchoolClassDto>> listClasses(@RequestParam Integer index,
|
||||
@RequestParam Integer size,
|
||||
@RequestParam(required = false) String className,
|
||||
@RequestParam(required = false) String classSn,
|
||||
@RequestParam(required = false) String schoolMajorId,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
if (StringUtils.isNotBlank(schoolMajorId)) {
|
||||
requireMajor(schoolMajorId, schoolId);
|
||||
}
|
||||
PageInfo<SchoolClassDto> pageInfo = schoolService.seleteSchoolClass(index, size, schoolId, className, classSn, schoolMajorId);
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", pageInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/classes")
|
||||
@ApiOperation("学校管理员-新增班级")
|
||||
public ResultEntity addClass(@RequestBody SchoolClass schoolClass, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
if (StringUtils.isBlank(schoolClass.getClassName())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "班级名称不能为空");
|
||||
}
|
||||
requireMajor(schoolClass.getSchoolMajorId(), schoolId);
|
||||
schoolClass.setSchoolClassId(UUID.randomUUID().toString());
|
||||
schoolClass.setSchoolId(schoolId);
|
||||
schoolClass.setCreateTime(new Date());
|
||||
int count = schoolClassMapper.insertSelective(schoolClass);
|
||||
return writeResult(count, "新增成功", "新增失败");
|
||||
}
|
||||
|
||||
@PutMapping("/classes/{id}")
|
||||
@ApiOperation("学校管理员-编辑班级")
|
||||
public ResultEntity updateClass(@PathVariable String id,
|
||||
@RequestBody SchoolClass schoolClass,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireClass(id, schoolId);
|
||||
if (StringUtils.isNotBlank(schoolClass.getSchoolMajorId())) {
|
||||
requireMajor(schoolClass.getSchoolMajorId(), schoolId);
|
||||
}
|
||||
schoolClass.setSchoolClassId(id);
|
||||
schoolClass.setSchoolId(schoolId);
|
||||
int count = schoolClassMapper.updateByPrimaryKeySelective(schoolClass);
|
||||
return writeResult(count, "编辑成功", "编辑失败");
|
||||
}
|
||||
|
||||
@DeleteMapping("/classes/{id}")
|
||||
@ApiOperation("学校管理员-删除班级")
|
||||
public ResultEntity deleteClass(@PathVariable String id, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireClass(id, schoolId);
|
||||
if (hasStudentInClass(id) || hasTaskInClass(id, schoolId)) {
|
||||
return new ResultEntity<>(HttpStatus.ACCEPTED, "班级下存在学生或任务,不能删除");
|
||||
}
|
||||
int count = schoolClassMapper.deleteByPrimaryKey(id);
|
||||
return writeResult(count, "删除成功", "删除失败");
|
||||
}
|
||||
|
||||
@GetMapping("/teachers")
|
||||
@ApiOperation("学校管理员-教师列表")
|
||||
public ResultEntity<PageInfo<Userinfo>> listTeachers(@RequestParam Integer index,
|
||||
@RequestParam Integer size,
|
||||
@RequestParam(required = false) String name,
|
||||
@RequestParam(required = false) String username,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
PageInfo<Userinfo> pageInfo = listUsers(index, size, schoolId, 3, name, username, null);
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", pageInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/teachers")
|
||||
@ApiOperation("学校管理员-新增教师")
|
||||
public ResultEntity addTeacher(@RequestBody Userinfo teacher, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
if (StringUtils.isBlank(teacher.getUsername()) || StringUtils.isBlank(teacher.getName())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "教师姓名和工号不能为空");
|
||||
}
|
||||
if (userInfoService.existsByUserName(teacher.getUsername())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "账号已存在");
|
||||
}
|
||||
teacher.setUserId(UUID.randomUUID().toString());
|
||||
if (StringUtils.isBlank(teacher.getPassword())) {
|
||||
teacher.setPassword("123qwe");
|
||||
}
|
||||
teacher.setCreateTime(new Date());
|
||||
schoolAdminService.prepareTeacherForSave(teacher, schoolId);
|
||||
int count = userinfoMapper.insertSelective(teacher);
|
||||
return writeResult(count, "新增成功", "新增失败");
|
||||
}
|
||||
|
||||
@PutMapping("/teachers/{id}")
|
||||
@ApiOperation("学校管理员-编辑教师")
|
||||
public ResultEntity updateTeacher(@PathVariable String id,
|
||||
@RequestBody Userinfo teacher,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireUser(id, schoolId, 3);
|
||||
teacher.setUserId(id);
|
||||
schoolAdminService.prepareTeacherForSave(teacher, schoolId);
|
||||
int count = userinfoMapper.updateByPrimaryKeySelective(teacher);
|
||||
return writeResult(count, "编辑成功", "编辑失败");
|
||||
}
|
||||
|
||||
@DeleteMapping("/teachers/{id}")
|
||||
@ApiOperation("学校管理员-删除教师")
|
||||
public ResultEntity deleteTeacher(@PathVariable String id, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireUser(id, schoolId, 3);
|
||||
int count = userinfoMapper.deleteByPrimaryKey(id);
|
||||
return writeResult(count, "删除成功", "删除失败");
|
||||
}
|
||||
|
||||
@GetMapping("/students")
|
||||
@ApiOperation("学校管理员-学生列表")
|
||||
public ResultEntity<PageInfo<Userinfo>> listStudents(@RequestParam Integer index,
|
||||
@RequestParam Integer size,
|
||||
@RequestParam(required = false) String name,
|
||||
@RequestParam(required = false) String username,
|
||||
@RequestParam(required = false) String schoolClassId,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
if (StringUtils.isNotBlank(schoolClassId)) {
|
||||
requireClass(schoolClassId, schoolId);
|
||||
}
|
||||
PageInfo<Userinfo> pageInfo = listUsers(index, size, schoolId, 4, name, username, schoolClassId);
|
||||
return new ResultEntity<>(HttpStatus.OK, "查询成功", pageInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/students")
|
||||
@ApiOperation("学校管理员-新增学生")
|
||||
public ResultEntity addStudent(@RequestBody Userinfo student, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
if (StringUtils.isBlank(student.getUsername()) || StringUtils.isBlank(student.getName())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "学生姓名和学号不能为空");
|
||||
}
|
||||
if (userInfoService.existsByUserName(student.getUsername())) {
|
||||
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "账号已存在");
|
||||
}
|
||||
if (StringUtils.isNotBlank(student.getSchoolClassId())) {
|
||||
requireClass(student.getSchoolClassId(), schoolId);
|
||||
}
|
||||
student.setUserId(UUID.randomUUID().toString());
|
||||
if (StringUtils.isBlank(student.getPassword())) {
|
||||
student.setPassword("123qwe");
|
||||
}
|
||||
student.setCreateTime(new Date());
|
||||
schoolAdminService.prepareStudentForSave(student, schoolId);
|
||||
int count = userinfoMapper.insertSelective(student);
|
||||
return writeResult(count, "新增成功", "新增失败");
|
||||
}
|
||||
|
||||
@PutMapping("/students/{id}")
|
||||
@ApiOperation("学校管理员-编辑学生")
|
||||
public ResultEntity updateStudent(@PathVariable String id,
|
||||
@RequestBody Userinfo student,
|
||||
HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireUser(id, schoolId, 4);
|
||||
if (StringUtils.isNotBlank(student.getSchoolClassId())) {
|
||||
requireClass(student.getSchoolClassId(), schoolId);
|
||||
}
|
||||
student.setUserId(id);
|
||||
schoolAdminService.prepareStudentForSave(student, schoolId);
|
||||
int count = userinfoMapper.updateByPrimaryKeySelective(student);
|
||||
return writeResult(count, "编辑成功", "编辑失败");
|
||||
}
|
||||
|
||||
@DeleteMapping("/students/{id}")
|
||||
@ApiOperation("学校管理员-删除学生")
|
||||
public ResultEntity deleteStudent(@PathVariable String id, HttpServletRequest request) {
|
||||
String schoolId = currentSchoolId(request);
|
||||
requireUser(id, schoolId, 4);
|
||||
int count = userinfoMapper.deleteByPrimaryKey(id);
|
||||
return writeResult(count, "删除成功", "删除失败");
|
||||
}
|
||||
|
||||
private String currentSchoolId(HttpServletRequest request) {
|
||||
JwtUser user = TokenProvider.getJWTUser(request);
|
||||
return schoolAdminService.requireSchoolAdmin(user);
|
||||
}
|
||||
|
||||
private ResultEntity writeResult(int count, String success, String fail) {
|
||||
if (count > 0) {
|
||||
return new ResultEntity<>(HttpStatus.OK, success);
|
||||
}
|
||||
return new ResultEntity<>(HttpStatus.ACCEPTED, fail);
|
||||
}
|
||||
|
||||
private SchoolFaculty requireFaculty(String facultyId, String schoolId) {
|
||||
if (StringUtils.isBlank(facultyId)) {
|
||||
throw new IllegalArgumentException("院系ID不能为空");
|
||||
}
|
||||
SchoolFaculty faculty = schoolFacultyMapper.selectByPrimaryKey(facultyId);
|
||||
if (faculty == null || !schoolId.equals(faculty.getSchoolId())) {
|
||||
throw new IllegalArgumentException("院系不存在或无权限");
|
||||
}
|
||||
return faculty;
|
||||
}
|
||||
|
||||
private SchoolMajor requireMajor(String majorId, String schoolId) {
|
||||
if (StringUtils.isBlank(majorId)) {
|
||||
throw new IllegalArgumentException("专业ID不能为空");
|
||||
}
|
||||
SchoolMajor major = schoolMajorMapper.selectByPrimaryKey(majorId);
|
||||
if (major == null) {
|
||||
throw new IllegalArgumentException("专业不存在");
|
||||
}
|
||||
requireFaculty(major.getSchoolFacultyId(), schoolId);
|
||||
return major;
|
||||
}
|
||||
|
||||
private SchoolClass requireClass(String classId, String schoolId) {
|
||||
if (StringUtils.isBlank(classId)) {
|
||||
throw new IllegalArgumentException("班级ID不能为空");
|
||||
}
|
||||
SchoolClass schoolClass = schoolClassMapper.selectByPrimaryKey(classId);
|
||||
if (schoolClass == null || !schoolId.equals(schoolClass.getSchoolId())) {
|
||||
throw new IllegalArgumentException("班级不存在或无权限");
|
||||
}
|
||||
return schoolClass;
|
||||
}
|
||||
|
||||
private Userinfo requireUser(String userId, String schoolId, int role) {
|
||||
Userinfo userinfo = userinfoMapper.selectByPrimaryKey(userId);
|
||||
if (userinfo == null || !schoolId.equals(userinfo.getSchoolId()) || userinfo.getRole() == null || userinfo.getRole() != role) {
|
||||
throw new IllegalArgumentException("用户不存在或无权限");
|
||||
}
|
||||
return userinfo;
|
||||
}
|
||||
|
||||
private PageInfo<Userinfo> listUsers(Integer index, Integer size, String schoolId, int role, String name, String username, String classId) {
|
||||
PageHelper.startPage(index, size);
|
||||
UserinfoExample example = new UserinfoExample();
|
||||
UserinfoExample.Criteria criteria = example.createCriteria();
|
||||
criteria.andSchoolIdEqualTo(schoolId).andRoleEqualTo(role);
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
criteria.andNameLike("%" + name + "%");
|
||||
}
|
||||
if (StringUtils.isNotBlank(username)) {
|
||||
criteria.andUsernameLike("%" + username + "%");
|
||||
}
|
||||
if (StringUtils.isNotBlank(classId)) {
|
||||
criteria.andSchoolClassIdEqualTo(classId);
|
||||
}
|
||||
return new PageInfo<>(userinfoMapper.selectByExample(example));
|
||||
}
|
||||
|
||||
private boolean hasMajorInFaculty(String facultyId) {
|
||||
SchoolMajorExample example = new SchoolMajorExample();
|
||||
example.createCriteria().andSchoolFacultyIdEqualTo(facultyId);
|
||||
return schoolMajorMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private boolean hasClassInMajor(String majorId) {
|
||||
SchoolClassExample example = new SchoolClassExample();
|
||||
example.createCriteria().andSchoolMajorIdEqualTo(majorId);
|
||||
return schoolClassMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private boolean hasStudentInClass(String classId) {
|
||||
UserinfoExample example = new UserinfoExample();
|
||||
example.createCriteria().andSchoolClassIdEqualTo(classId).andRoleEqualTo(4);
|
||||
return userinfoMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private boolean hasTaskInClass(String classId, String schoolId) {
|
||||
TaskAllocationExample example = new TaskAllocationExample();
|
||||
example.createCriteria().andClassIdEqualTo(classId).andSchoolIdEqualTo(schoolId);
|
||||
return taskAllocationMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private boolean hasUserByFaculty(String facultyId) {
|
||||
UserinfoExample example = new UserinfoExample();
|
||||
example.createCriteria().andSchoolFacultyIdEqualTo(facultyId);
|
||||
return userinfoMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private boolean hasUserByMajor(String majorId) {
|
||||
UserinfoExample example = new UserinfoExample();
|
||||
example.createCriteria().andSchoolMajorIdEqualTo(majorId);
|
||||
return userinfoMapper.countByExample(example) > 0;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.sztzjy.linkCommerce.service;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.Userinfo;
|
||||
|
||||
public interface PlatformAdminService {
|
||||
void requirePlatformAdmin(JwtUser user);
|
||||
|
||||
void prepareSchoolAdminForSave(Userinfo userinfo, String schoolId);
|
||||
|
||||
String requireSchoolId(String schoolId);
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.sztzjy.linkCommerce.service;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.Userinfo;
|
||||
|
||||
public interface SchoolAdminService {
|
||||
String requireSchoolAdmin(JwtUser user);
|
||||
|
||||
void requireSameSchool(JwtUser user, String targetSchoolId);
|
||||
|
||||
void prepareTeacherForSave(Userinfo userinfo, String schoolId);
|
||||
|
||||
void prepareStudentForSave(Userinfo userinfo, String schoolId);
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package com.sztzjy.linkCommerce.service.impl;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.exception.UnAuthorizedException;
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.Userinfo;
|
||||
import com.sztzjy.linkCommerce.service.PlatformAdminService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PlatformAdminServiceImpl implements PlatformAdminService {
|
||||
@Override
|
||||
public void requirePlatformAdmin(JwtUser user) {
|
||||
if (user == null || user.getRoleId() != 1) {
|
||||
throw new UnAuthorizedException("仅平台超管可操作");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareSchoolAdminForSave(Userinfo userinfo, String schoolId) {
|
||||
userinfo.setSchoolId(requireSchoolId(schoolId));
|
||||
userinfo.setRole(2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String requireSchoolId(String schoolId) {
|
||||
if (StringUtils.isBlank(schoolId)) {
|
||||
throw new IllegalArgumentException("请选择所属学校");
|
||||
}
|
||||
return schoolId;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package com.sztzjy.linkCommerce.service.impl;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.exception.UnAuthorizedException;
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.Userinfo;
|
||||
import com.sztzjy.linkCommerce.service.SchoolAdminService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class SchoolAdminServiceImpl implements SchoolAdminService {
|
||||
@Override
|
||||
public String requireSchoolAdmin(JwtUser user) {
|
||||
if (user == null || user.getRoleId() != 2) {
|
||||
throw new UnAuthorizedException("仅学校管理员可操作");
|
||||
}
|
||||
if (user.getSchoolId() == null) {
|
||||
throw new UnAuthorizedException("学校管理员缺少学校信息");
|
||||
}
|
||||
return String.valueOf(user.getSchoolId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requireSameSchool(JwtUser user, String targetSchoolId) {
|
||||
String schoolId = requireSchoolAdmin(user);
|
||||
if (StringUtils.isBlank(targetSchoolId) || !schoolId.equals(targetSchoolId)) {
|
||||
throw new UnAuthorizedException("无权操作其他学校数据");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareTeacherForSave(Userinfo userinfo, String schoolId) {
|
||||
userinfo.setSchoolId(schoolId);
|
||||
userinfo.setRole(3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareStudentForSave(Userinfo userinfo, String schoolId) {
|
||||
userinfo.setSchoolId(schoolId);
|
||||
userinfo.setRole(4);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package com.sztzjy.linkCommerce.service.impl;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.exception.UnAuthorizedException;
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.Userinfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class PlatformAdminServiceImplTest {
|
||||
@Test
|
||||
void requirePlatformAdminRejectsSchoolAdmin() {
|
||||
PlatformAdminServiceImpl service = new PlatformAdminServiceImpl();
|
||||
JwtUser user = new JwtUser();
|
||||
user.setRoleId(2);
|
||||
|
||||
assertThrows(UnAuthorizedException.class, () -> service.requirePlatformAdmin(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requirePlatformAdminAllowsPlatformAdmin() {
|
||||
PlatformAdminServiceImpl service = new PlatformAdminServiceImpl();
|
||||
JwtUser user = new JwtUser();
|
||||
user.setRoleId(1);
|
||||
|
||||
service.requirePlatformAdmin(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void prepareSchoolAdminForSaveForcesRoleAndSchool() {
|
||||
PlatformAdminServiceImpl service = new PlatformAdminServiceImpl();
|
||||
Userinfo userinfo = new Userinfo();
|
||||
userinfo.setRole(1);
|
||||
userinfo.setSchoolId("other-school");
|
||||
|
||||
service.prepareSchoolAdminForSave(userinfo, "school-uuid-1");
|
||||
|
||||
assertEquals(2, userinfo.getRole());
|
||||
assertEquals("school-uuid-1", userinfo.getSchoolId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void prepareSchoolAdminForSaveRejectsMissingSchool() {
|
||||
PlatformAdminServiceImpl service = new PlatformAdminServiceImpl();
|
||||
Userinfo userinfo = new Userinfo();
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> service.prepareSchoolAdminForSave(userinfo, " "));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,77 @@
|
||||
package com.sztzjy.linkCommerce.service.impl;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.exception.UnAuthorizedException;
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.Userinfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class SchoolAdminServiceImplTest {
|
||||
@Test
|
||||
void requireSchoolAdminRejectsTeacher() {
|
||||
SchoolAdminServiceImpl service = new SchoolAdminServiceImpl();
|
||||
JwtUser user = new JwtUser();
|
||||
user.setRoleId(3);
|
||||
user.setSchoolId("school-uuid-1");
|
||||
|
||||
assertThrows(UnAuthorizedException.class, () -> service.requireSchoolAdmin(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireSchoolAdminReturnsSchoolIdForSchoolAdmin() {
|
||||
SchoolAdminServiceImpl service = new SchoolAdminServiceImpl();
|
||||
JwtUser user = new JwtUser();
|
||||
user.setRoleId(2);
|
||||
user.setSchoolId("school-uuid-1");
|
||||
|
||||
assertEquals("school-uuid-1", service.requireSchoolAdmin(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireSameSchoolRejectsOtherSchool() {
|
||||
SchoolAdminServiceImpl service = new SchoolAdminServiceImpl();
|
||||
JwtUser user = new JwtUser();
|
||||
user.setRoleId(2);
|
||||
user.setSchoolId("school-uuid-1");
|
||||
|
||||
assertThrows(UnAuthorizedException.class, () -> service.requireSameSchool(user, "school-uuid-2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireSameSchoolAllowsSameSchool() {
|
||||
SchoolAdminServiceImpl service = new SchoolAdminServiceImpl();
|
||||
JwtUser user = new JwtUser();
|
||||
user.setRoleId(2);
|
||||
user.setSchoolId("school-uuid-1");
|
||||
|
||||
service.requireSameSchool(user, "school-uuid-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void prepareTeacherForSaveForcesSchoolAndTeacherRole() {
|
||||
SchoolAdminServiceImpl service = new SchoolAdminServiceImpl();
|
||||
Userinfo userinfo = new Userinfo();
|
||||
userinfo.setSchoolId("other-school");
|
||||
userinfo.setRole(2);
|
||||
|
||||
service.prepareTeacherForSave(userinfo, "school-uuid-1");
|
||||
|
||||
assertEquals("school-uuid-1", userinfo.getSchoolId());
|
||||
assertEquals(3, userinfo.getRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void prepareStudentForSaveForcesSchoolAndStudentRole() {
|
||||
SchoolAdminServiceImpl service = new SchoolAdminServiceImpl();
|
||||
Userinfo userinfo = new Userinfo();
|
||||
userinfo.setSchoolId("other-school");
|
||||
userinfo.setRole(2);
|
||||
|
||||
service.prepareStudentForSave(userinfo, "school-uuid-1");
|
||||
|
||||
assertEquals("school-uuid-1", userinfo.getSchoolId());
|
||||
assertEquals(4, userinfo.getRole());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue