v0.2.2 追补院系部分 建立社团部分

v0.2o
bruce 2025-02-19 21:47:21 +08:00
parent c3040562e7
commit ae69105536
39 changed files with 1169 additions and 248 deletions

View File

@ -1,28 +0,0 @@
package com.bruce.sams.common.annotation;
import java.lang.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
/**
*
* Spring Security 6 `@PreAuthorize`
*/
@Target({ElementType.METHOD}) // 作用于方法
@Retention(RetentionPolicy.RUNTIME) // 运行时生效
@Documented
@PreAuthorize("@dataPermissionEvaluator.hasPermission(authentication, #field, #type)")
public @interface DataPermission {
/**
* "creator_id" "club_id"
*/
String field() default "user_id";
/**
*
* - USER: 访
* - CLUB: 访
* - ADMIN: 访
*/
String type() default "USER";
}

View File

@ -1,6 +1,6 @@
package com.bruce.sams.common.config; package com.bruce.sams.common.config;
import com.bruce.sams.common.security.JwtAuthFilter; import com.bruce.sams.common.filter.JwtAuthFilter;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;

View File

@ -1,7 +1,7 @@
package com.bruce.sams.common.security; package com.bruce.sams.common.filter;
import com.bruce.sams.common.exception.CustomException; import com.bruce.sams.common.exception.CustomException;
import com.bruce.sams.utils.AjaxResult; import com.bruce.sams.common.utils.AjaxResult;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;

View File

@ -1,7 +1,7 @@
package com.bruce.sams.common.security; package com.bruce.sams.common.filter;
import com.bruce.sams.service.impl.CustomUserDetailsService; import com.bruce.sams.service.impl.CustomUserDetailsService;
import com.bruce.sams.utils.TokenUtil; import com.bruce.sams.common.utils.TokenUtil;
import jakarta.servlet.FilterChain; import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
@ -51,8 +51,4 @@ public class JwtAuthFilter extends OncePerRequestFilter {
return new CustomUserDetailsService(); // 认证逻辑 return new CustomUserDetailsService(); // 认证逻辑
} }
@Bean
public DataPermissionEvaluator dataPermissionEvaluator() {
return new DataPermissionEvaluator(); // 注册数据权限控制器
}
} }

View File

@ -1,85 +0,0 @@
package com.bruce.sams.common.security;
import com.bruce.sams.domain.entity.LoginUser;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.List;
/**
*
* `@DataPermission`
*/
@Component
public class DataPermissionEvaluator implements PermissionEvaluator {
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
return false; // 这里我们不处理 Object 级别的权限
}
/**
* `@DataPermission` 访
*
* @param authentication
* @param field creator_id, club_id
* @param type USER, CLUB, ADMIN
* @return boolean 访
*/
public boolean hasPermission(Authentication authentication, String field, String type) {
// 获取当前登录用户
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
Long userId = loginUser.getUserId();
List<String> roles = loginUser.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.toList();
// 如果用户是管理员,直接放行
if (roles.contains("ROLE_ADMIN") || "ADMIN".equals(type)) {
return true;
}
// 如果 type = "USER",只允许访问自己的数据
if ("USER".equals(type)) {
return checkUserDataPermission(userId, field);
}
// 如果 type = "CLUB",只允许访问自己所属社团的数据
if ("CLUB".equals(type)) {
Long clubId = getUserClubId(userId); // 通过用户ID获取社团ID
return checkClubDataPermission(clubId, field);
}
return false;
}
/**
* 访
*/
private boolean checkUserDataPermission(Long userId, String field) {
return field.equals("creator_id") && userId != null; // 用户只能访问自己创建的数据
}
/**
* 访
*/
private boolean checkClubDataPermission(Long clubId, String field) {
return field.equals("club_id") && clubId != null; // 只能访问所属社团的数据
}
/**
* IDID使 MyBatis-Plus
* todo
*/
// private Long getUserClubId(Long userId) {
// return userMapper.getUserClubId(userId); // 假设 UserMapper 里有方法查询用户所属社团
// }
@Override
public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
return false; // 这里不做特殊处理
}
}

View File

@ -1,4 +1,4 @@
package com.bruce.sams.utils; package com.bruce.sams.common.utils;
import com.bruce.sams.common.constant.HttpStatus; import com.bruce.sams.common.constant.HttpStatus;

View File

@ -1,4 +1,4 @@
package com.bruce.sams.utils; package com.bruce.sams.common.utils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

View File

@ -1,4 +1,4 @@
package com.bruce.sams.utils; package com.bruce.sams.common.utils;
import com.bruce.sams.domain.sys.User; import com.bruce.sams.domain.sys.User;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;

View File

@ -2,7 +2,7 @@ package com.bruce.sams.controller;
import com.bruce.sams.domain.entity.LoginRequest; import com.bruce.sams.domain.entity.LoginRequest;
import com.bruce.sams.service.AuthService; import com.bruce.sams.service.AuthService;
import com.bruce.sams.utils.AjaxResult; import com.bruce.sams.common.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;

View File

@ -1,67 +0,0 @@
package com.bruce.sams.controller;
import com.bruce.sams.domain.sys.User;
import com.bruce.sams.utils.AjaxResult;
import com.bruce.sams.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
*
*/
@RestController
@RequestMapping("/api/admin/user")
public class UserController {
@Autowired
private UserService userService;
/**
*
*
* @param users
* @return
*/
@PostMapping("/import")
public AjaxResult importUsers(@RequestBody List<User> users) {
userService.importUsers(users);
return AjaxResult.success("用户导入成功");
}
/**
*
*
* @param keyword
* @return
*/
@GetMapping("/list")
public AjaxResult listUsers(@RequestParam(required = false) String keyword) {
return AjaxResult.success("查询成功", userService.listUsers(keyword));
}
/**
*
*
* @param user
* @return
*/
@PutMapping("/update")
public AjaxResult updateUser(@RequestBody User user) {
userService.updateUser(user);
return AjaxResult.success("用户信息更新成功");
}
/**
*
*
* @param userId ID
* @return
*/
@DeleteMapping("/delete/{userId}")
public AjaxResult deleteUser(@PathVariable Long userId) {
userService.deleteUser(userId);
return AjaxResult.success("用户删除成功");
}
}

View File

@ -1,38 +0,0 @@
package com.bruce.sams.controller;
import com.bruce.sams.domain.sys.User;
import com.bruce.sams.utils.AjaxResult;
import com.bruce.sams.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
*
*/
@RestController
@RequestMapping("/api/user/profile")
public class UserProfileController {
@Autowired
private UserService userService;
/**
*
*/
@PutMapping("/change-password")
public AjaxResult changePassword(@RequestAttribute Long userId,
@RequestParam String oldPassword,
@RequestParam String newPassword) {
userService.changePassword(userId, oldPassword, newPassword);
return AjaxResult.success("密码修改成功");
}
/**
*
*/
@PutMapping("/update")
public AjaxResult updateProfile(@RequestAttribute Long userId, @RequestBody User user) {
userService.updateProfile(userId, user);
return AjaxResult.success("个人信息更新成功");
}
}

View File

@ -0,0 +1,137 @@
package com.bruce.sams.controller.sms;
import com.bruce.sams.domain.sms.College;
import com.bruce.sams.domain.sms.CollegeLeader;
import com.bruce.sams.service.CollegeLeaderService;
import com.bruce.sams.service.CollegeService;
import com.bruce.sams.common.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import java.util.List;
/**
* &
*/
@RestController
@RequestMapping("/api/admin/college")
public class CollegeController {
@Autowired
private CollegeService collegeService;
@Autowired
private CollegeLeaderService collegeLeaderService;
/**
*
*/
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN')")
@PostMapping("/add")
public AjaxResult addCollege(@RequestBody College college) {
collegeService.addCollege(college);
return AjaxResult.success("学院添加成功");
}
/**
*
*/
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN')")
@PutMapping("/update")
public AjaxResult updateCollege(@RequestBody College college) {
collegeService.updateCollege(college);
return AjaxResult.success("学院更新成功");
}
/**
*
*/
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN')")
@DeleteMapping("/delete/{collegeId}")
public AjaxResult deleteCollege(@PathVariable Long collegeId) {
collegeService.deleteCollege(collegeId);
return AjaxResult.success("学院删除成功");
}
/**
*
*/
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN')")
@GetMapping("/{collegeId}")
public AjaxResult getCollegeById(@PathVariable Long collegeId) {
return AjaxResult.success(collegeService.getCollegeById(collegeId));
}
/**
*
*/
@PreAuthorize("hasRole('DEPARTMENT_ADMIN')")
@GetMapping("/my-college")
public AjaxResult getMyCollege(@RequestParam Long userId) {
College myCollege = collegeService.getCollegeById(userId); // 这里可以改为通过 `userId` 查询学院
return AjaxResult.success(myCollege);
}
/**
* &
*/
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN') or hasRole('DEPARTMENT_ADMIN')")
@GetMapping("/list")
public AjaxResult listColleges(@RequestParam(required = false) String keyword, @RequestParam Long userId) {
if (userHasRole("ADMIN") || userHasRole("COLLEGE_ADMIN")) {
return AjaxResult.success(collegeService.listColleges(keyword));
} else if (userHasRole("DEPARTMENT_ADMIN")) {
College myCollege = collegeService.getCollegeById(userId); // 查询自己所属学院
return AjaxResult.success(List.of(myCollege));
}
return AjaxResult.error("无权限");
}
/**
*
*/
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN')")
@PostMapping("/assign")
public AjaxResult assignLeader(@RequestParam Long collegeId, @RequestParam Long userId) {
collegeLeaderService.assignLeader(collegeId, userId);
return AjaxResult.success("负责人指派成功");
}
/**
*
*/
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN')")
@DeleteMapping("/remove")
public AjaxResult removeLeader(@RequestParam Long collegeId, @RequestParam Long userId) {
collegeLeaderService.removeLeader(collegeId, userId);
return AjaxResult.success("负责人移除成功");
}
/**
* &
*/
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN') or hasRole('DEPARTMENT_ADMIN')")
@GetMapping("/{collegeId}")
public AjaxResult getLeader(@PathVariable Long collegeId, @RequestParam Long userId) {
if (userHasRole("ADMIN") || userHasRole("COLLEGE_ADMIN")) {
return AjaxResult.success(collegeLeaderService.getLeaderByCollegeId(collegeId));
} else if (userHasRole("DEPARTMENT_ADMIN")) {
CollegeLeader myLeader = collegeLeaderService.getLeaderByCollegeId(userId); // 只能查询自己学院的负责人
return AjaxResult.success(myLeader);
}
return AjaxResult.error("无权限");
}
/**
*
*/
private boolean userHasRole(String role) {
return org.springframework.security.core.context.SecurityContextHolder.getContext()
.getAuthentication()
.getAuthorities()
.stream()
.anyMatch(auth -> auth.getAuthority().equals("ROLE_" + role));
}
}

View File

@ -0,0 +1,91 @@
package com.bruce.sams.controller.sys;
import com.bruce.sams.domain.sys.User;
import com.bruce.sams.service.UserService;
import com.bruce.sams.common.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* -
*/
@RestController
@RequestMapping("/api/admin/user")
public class AdminUserController {
@Autowired
private UserService userService;
/**
*
*
* @param users
* @return AjaxResult
*/
@PostMapping("/import")
public AjaxResult importUsers(@RequestBody List<User> users) {
userService.importUsers(users);
return AjaxResult.success("用户导入成功");
}
/**
*
*
* @param keyword
* @return AjaxResult
*/
@GetMapping("/list")
public AjaxResult listUsers(@RequestParam(required = false) String keyword) {
return AjaxResult.success(userService.listUsers(keyword));
}
/**
*
*
* @param user
* @return AjaxResult
*/
@PutMapping("/update")
public AjaxResult updateUser(@RequestBody User user) {
userService.updateUser(user);
return AjaxResult.success("用户信息更新成功");
}
/**
* 123456
*
* @param userId ID
* @return AjaxResult
*/
@PutMapping("/reset-password/{userId}")
public AjaxResult resetPassword(@PathVariable Long userId) {
userService.resetPassword(userId);
return AjaxResult.success("密码已重置为 123456");
}
/**
*
*
* @param userId ID
* @return AjaxResult
*/
@PutMapping("/ban/{userId}")
public AjaxResult banUser(@PathVariable Long userId) {
userService.banUser(userId);
return AjaxResult.success("用户已封禁");
}
/**
*
*
* @param userId ID
* @return AjaxResult
*/
@DeleteMapping("/{userId}")
public AjaxResult deleteUser(@PathVariable Long userId) {
userService.deleteUser(userId);
return AjaxResult.success("用户已删除");
}
}

View File

@ -0,0 +1,56 @@
package com.bruce.sams.controller.sys;
import com.bruce.sams.domain.sys.User;
import com.bruce.sams.service.UserService;
import com.bruce.sams.common.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* -
*/
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
/**
*
*
* @param userId ID
* @param oldPassword
* @param newPassword
* @return AjaxResult
*/
@PutMapping("/change-password")
public AjaxResult changePassword(@RequestParam Long userId, @RequestParam String oldPassword, @RequestParam String newPassword) {
userService.changePassword(userId, oldPassword, newPassword);
return AjaxResult.success("密码修改成功");
}
/**
*
*
* @param userId ID
* @param updatedUser
* @return AjaxResult
*/
@PutMapping("/profile")
public AjaxResult updateProfile(@RequestParam Long userId, @RequestBody User updatedUser) {
userService.updateProfile(userId, updatedUser);
return AjaxResult.success("个人信息更新成功");
}
/**
*
*
* @param userId ID
* @return AjaxResult
*/
@GetMapping("/{userId}")
public AjaxResult getUserProfile(@PathVariable Long userId) {
return AjaxResult.success(userService.getUserProfile(userId));
}
}

View File

@ -0,0 +1,104 @@
package com.bruce.sams.domain.sms;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
import lombok.Data;
/**
*
* @TableName sms_club
*/
@TableName(value ="sms_club")
@Data
public class Club {
/**
* ID
*/
@TableId(type = IdType.AUTO)
private Long clubId;
/**
*
*/
private String clubName;
/**
*
*/
private String description;
/**
*
*/
private Object category;
/**
*
*/
private Long collegeId;
/**
* ID
*/
private Long leaderId;
/**
*
*/
private Date createdAt;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Club other = (Club) that;
return (this.getClubId() == null ? other.getClubId() == null : this.getClubId().equals(other.getClubId()))
&& (this.getClubName() == null ? other.getClubName() == null : this.getClubName().equals(other.getClubName()))
&& (this.getDescription() == null ? other.getDescription() == null : this.getDescription().equals(other.getDescription()))
&& (this.getCategory() == null ? other.getCategory() == null : this.getCategory().equals(other.getCategory()))
&& (this.getCollegeId() == null ? other.getCollegeId() == null : this.getCollegeId().equals(other.getCollegeId()))
&& (this.getLeaderId() == null ? other.getLeaderId() == null : this.getLeaderId().equals(other.getLeaderId()))
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getClubId() == null) ? 0 : getClubId().hashCode());
result = prime * result + ((getClubName() == null) ? 0 : getClubName().hashCode());
result = prime * result + ((getDescription() == null) ? 0 : getDescription().hashCode());
result = prime * result + ((getCategory() == null) ? 0 : getCategory().hashCode());
result = prime * result + ((getCollegeId() == null) ? 0 : getCollegeId().hashCode());
result = prime * result + ((getLeaderId() == null) ? 0 : getLeaderId().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", clubId=").append(clubId);
sb.append(", clubName=").append(clubName);
sb.append(", description=").append(description);
sb.append(", category=").append(category);
sb.append(", collegeId=").append(collegeId);
sb.append(", leaderId=").append(leaderId);
sb.append(", createdAt=").append(createdAt);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,96 @@
package com.bruce.sams.domain.sms;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
import lombok.Data;
/**
*
* @TableName sms_club_user
*/
@TableName(value ="sms_club_user")
@Data
public class ClubUser {
/**
* ID
*/
@TableId(type = IdType.AUTO)
private Long sucId;
/**
* ID
*/
private Long userId;
/**
* ID
*/
private Long clubId;
/**
* ID
*/
private Long roleId;
/**
*
*/
private Integer isActive;
/**
*
*/
private Date joinDate;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
ClubUser other = (ClubUser) that;
return (this.getSucId() == null ? other.getSucId() == null : this.getSucId().equals(other.getSucId()))
&& (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
&& (this.getClubId() == null ? other.getClubId() == null : this.getClubId().equals(other.getClubId()))
&& (this.getRoleId() == null ? other.getRoleId() == null : this.getRoleId().equals(other.getRoleId()))
&& (this.getIsActive() == null ? other.getIsActive() == null : this.getIsActive().equals(other.getIsActive()))
&& (this.getJoinDate() == null ? other.getJoinDate() == null : this.getJoinDate().equals(other.getJoinDate()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getSucId() == null) ? 0 : getSucId().hashCode());
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
result = prime * result + ((getClubId() == null) ? 0 : getClubId().hashCode());
result = prime * result + ((getRoleId() == null) ? 0 : getRoleId().hashCode());
result = prime * result + ((getIsActive() == null) ? 0 : getIsActive().hashCode());
result = prime * result + ((getJoinDate() == null) ? 0 : getJoinDate().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", sucId=").append(sucId);
sb.append(", userId=").append(userId);
sb.append(", clubId=").append(clubId);
sb.append(", roleId=").append(roleId);
sb.append(", isActive=").append(isActive);
sb.append(", joinDate=").append(joinDate);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,88 @@
package com.bruce.sams.domain.sms;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
import lombok.Data;
/**
*
* @TableName sms_college
*/
@TableName(value ="sms_college")
@Data
public class College {
/**
* ID
*/
@TableId(type = IdType.AUTO)
private Long collegeId;
/**
*
*/
private String collegeName;
/**
* ID
*/
private Long parentId;
/**
*
*/
private String email;
/**
*
*/
private Date createdAt;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
College other = (College) that;
return (this.getCollegeId() == null ? other.getCollegeId() == null : this.getCollegeId().equals(other.getCollegeId()))
&& (this.getCollegeName() == null ? other.getCollegeName() == null : this.getCollegeName().equals(other.getCollegeName()))
&& (this.getParentId() == null ? other.getParentId() == null : this.getParentId().equals(other.getParentId()))
&& (this.getEmail() == null ? other.getEmail() == null : this.getEmail().equals(other.getEmail()))
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getCollegeId() == null) ? 0 : getCollegeId().hashCode());
result = prime * result + ((getCollegeName() == null) ? 0 : getCollegeName().hashCode());
result = prime * result + ((getParentId() == null) ? 0 : getParentId().hashCode());
result = prime * result + ((getEmail() == null) ? 0 : getEmail().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", collegeId=").append(collegeId);
sb.append(", collegeName=").append(collegeName);
sb.append(", parentId=").append(parentId);
sb.append(", email=").append(email);
sb.append(", createdAt=").append(createdAt);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,73 @@
package com.bruce.sams.domain.sms;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
import lombok.Data;
/**
*
* @TableName sms_college_leader
*/
@TableName(value ="sms_college_leader")
@Data
public class CollegeLeader {
/**
* ID
*/
@TableId
private Long collegeId;
/**
* ID
*/
@TableId
private Long userId;
/**
*
*/
private Date assignedAt;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
CollegeLeader other = (CollegeLeader) that;
return (this.getCollegeId() == null ? other.getCollegeId() == null : this.getCollegeId().equals(other.getCollegeId()))
&& (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
&& (this.getAssignedAt() == null ? other.getAssignedAt() == null : this.getAssignedAt().equals(other.getAssignedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getCollegeId() == null) ? 0 : getCollegeId().hashCode());
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
result = prime * result + ((getAssignedAt() == null) ? 0 : getAssignedAt().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", collegeId=").append(collegeId);
sb.append(", userId=").append(userId);
sb.append(", assignedAt=").append(assignedAt);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,18 @@
package com.bruce.sams.mapper;
import com.bruce.sams.domain.sms.Club;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author bruce
* @description sms_club()Mapper
* @createDate 2025-02-19 20:18:22
* @Entity com.bruce.sams.domain.sms.Club
*/
public interface ClubMapper extends BaseMapper<Club> {
}

View File

@ -0,0 +1,18 @@
package com.bruce.sams.mapper;
import com.bruce.sams.domain.sms.ClubUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author bruce
* @description sms_club_user()Mapper
* @createDate 2025-02-19 20:18:31
* @Entity com.bruce.sams.domain.sms.ClubUser
*/
public interface ClubUserMapper extends BaseMapper<ClubUser> {
}

View File

@ -0,0 +1,18 @@
package com.bruce.sams.mapper;
import com.bruce.sams.domain.sms.CollegeLeader;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author bruce
* @description sms_college_leader()Mapper
* @createDate 2025-02-19 20:18:55
* @Entity com.bruce.sams.domain.sms.CollegeLeader
*/
public interface CollegeLeaderMapper extends BaseMapper<CollegeLeader> {
}

View File

@ -0,0 +1,18 @@
package com.bruce.sams.mapper;
import com.bruce.sams.domain.sms.College;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author bruce
* @description sms_college()Mapper
* @createDate 2025-02-19 20:18:40
* @Entity com.bruce.sams.domain.sms.College
*/
public interface CollegeMapper extends BaseMapper<College> {
}

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List; import java.util.List;
@ -28,6 +29,17 @@ public interface UserMapper extends BaseMapper<User> {
"WHERE ur.user_id = #{userId}") "WHERE ur.user_id = #{userId}")
List<String> findRolesByUserId(@Param("userId") Long userId); List<String> findRolesByUserId(@Param("userId") Long userId);
@Update("UPDATE sys_user SET nick_name = #{nickName}, email = #{email} WHERE user_id = #{userId}")
void updateUser(@Param("userId") Long userId, @Param("nickName") String nickName, @Param("email") String email);
@Update("UPDATE sys_user SET password = #{password} WHERE user_id = #{userId}")
void updatePassword(@Param("userId") Long userId, @Param("password") String password);
@Update("UPDATE sys_user SET status = #{status} WHERE user_id = #{userId}")
void updateStatus(@Param("userId") Long userId, @Param("status") String status);
@Select("SELECT * FROM sys_user WHERE user_id = #{userId}")
User getUserById(@Param("userId") Long userId);
} }

View File

@ -0,0 +1,13 @@
package com.bruce.sams.service;
import com.bruce.sams.domain.sms.Club;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author bruce
* @description sms_club()Service
* @createDate 2025-02-19 20:18:22
*/
public interface ClubService extends IService<Club> {
}

View File

@ -0,0 +1,13 @@
package com.bruce.sams.service;
import com.bruce.sams.domain.sms.ClubUser;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author bruce
* @description sms_club_user()Service
* @createDate 2025-02-19 20:18:31
*/
public interface ClubUserService extends IService<ClubUser> {
}

View File

@ -0,0 +1,36 @@
package com.bruce.sams.service;
import com.bruce.sams.domain.sms.CollegeLeader;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author bruce
* @description sms_college_leader()Service
* @createDate 2025-02-19 20:18:55
*/
public interface CollegeLeaderService extends IService<CollegeLeader> {
/**
*
*
* @param collegeId ID
* @param userId ID
*/
void assignLeader(Long collegeId, Long userId);
/**
*
*
* @param collegeId ID
* @param userId ID
*/
void removeLeader(Long collegeId, Long userId);
/**
* ID
*
* @param collegeId ID
* @return
*/
CollegeLeader getLeaderByCollegeId(Long collegeId);
}

View File

@ -0,0 +1,52 @@
package com.bruce.sams.service;
import com.bruce.sams.domain.sms.College;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* @author bruce
* @description sms_college()Service
* @createDate 2025-02-19 20:18:40
*/
public interface CollegeService extends IService<College> {
/**
*
*
* @param college
*/
void addCollege(College college);
/**
*
*
* @param college
*/
void updateCollege(College college);
/**
*
*
* @param collegeId ID
*/
void deleteCollege(Long collegeId);
/**
* ID
*
* @param collegeId ID
* @return
*/
College getCollegeById(Long collegeId);
/**
*
*
* @param keyword
* @return
*/
List<College> listColleges(String keyword);
}

View File

@ -28,6 +28,28 @@ public interface UserService {
*/ */
void updateUser(User user); void updateUser(User user);
/**
*
*
* @param userId ID
*/
void resetPassword(Long userId);
/**
*
*
* @param userId ID
*/
void banUser(Long userId);
/**
*
*
* @param userId ID
* @return
*/
User getUserProfile(Long userId);
/** /**
* *
* *

View File

@ -8,8 +8,8 @@ import com.bruce.sams.common.exception.UserNotFoundException;
import com.bruce.sams.mapper.RoleMapper; import com.bruce.sams.mapper.RoleMapper;
import com.bruce.sams.mapper.UserMapper; import com.bruce.sams.mapper.UserMapper;
import com.bruce.sams.service.AuthService; import com.bruce.sams.service.AuthService;
import com.bruce.sams.utils.PasswordUtil; import com.bruce.sams.common.utils.PasswordUtil;
import com.bruce.sams.utils.TokenUtil; import com.bruce.sams.common.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -0,0 +1,22 @@
package com.bruce.sams.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.sams.domain.sms.Club;
import com.bruce.sams.service.ClubService;
import com.bruce.sams.mapper.ClubMapper;
import org.springframework.stereotype.Service;
/**
* @author bruce
* @description sms_club()Service
* @createDate 2025-02-19 20:18:22
*/
@Service
public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club>
implements ClubService{
}

View File

@ -0,0 +1,22 @@
package com.bruce.sams.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.sams.domain.sms.ClubUser;
import com.bruce.sams.service.ClubUserService;
import com.bruce.sams.mapper.ClubUserMapper;
import org.springframework.stereotype.Service;
/**
* @author bruce
* @description sms_club_user()Service
* @createDate 2025-02-19 20:18:31
*/
@Service
public class ClubUserServiceImpl extends ServiceImpl<ClubUserMapper, ClubUser>
implements ClubUserService{
}

View File

@ -0,0 +1,47 @@
package com.bruce.sams.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.sams.domain.sms.CollegeLeader;
import com.bruce.sams.service.CollegeLeaderService;
import com.bruce.sams.mapper.CollegeLeaderMapper;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* @author bruce
* @description sms_college_leader()Service
* @createDate 2025-02-19 20:18:55
*/
@Service
public class CollegeLeaderServiceImpl extends ServiceImpl<CollegeLeaderMapper, CollegeLeader> implements CollegeLeaderService {
/**
*
*/
@Override
public void assignLeader(Long collegeId, Long userId) {
CollegeLeader leader = new CollegeLeader();
leader.setCollegeId(collegeId);
leader.setUserId(userId);
leader.setAssignedAt(new Date(System.currentTimeMillis())); // 记录指派时间
this.save(leader);
}
/**
*
*/
@Override
public void removeLeader(Long collegeId, Long userId) {
this.removeById(collegeId);
}
/**
* ID
*/
@Override
public CollegeLeader getLeaderByCollegeId(Long collegeId) {
return this.getById(collegeId);
}
}

View File

@ -0,0 +1,66 @@
package com.bruce.sams.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.sams.domain.sms.College;
import com.bruce.sams.service.CollegeService;
import com.bruce.sams.mapper.CollegeMapper;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author bruce
* @description sms_college()Service
* @createDate 2025-02-19 20:18:40
*/
@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College> implements CollegeService {
/**
*
*/
@Override
public void addCollege(College college) {
this.save(college);
}
/**
*
*/
@Override
public void updateCollege(College college) {
this.updateById(college);
}
/**
*
*/
@Override
public void deleteCollege(Long collegeId) {
this.removeById(collegeId);
}
/**
* ID
*/
@Override
public College getCollegeById(Long collegeId) {
return this.getById(collegeId);
}
/**
*
*/
@Override
public List<College> listColleges(String keyword) {
LambdaQueryWrapper<College> query = new LambdaQueryWrapper<>();
if (keyword != null && !keyword.isEmpty()) {
query.like(College::getCollegeName, keyword);
}
return this.list(query);
}
}

View File

@ -7,7 +7,7 @@ import com.bruce.sams.common.exception.PasswordIncorrectException;
import com.bruce.sams.common.exception.UserNotFoundException; import com.bruce.sams.common.exception.UserNotFoundException;
import com.bruce.sams.mapper.UserMapper; import com.bruce.sams.mapper.UserMapper;
import com.bruce.sams.service.UserService; import com.bruce.sams.service.UserService;
import com.bruce.sams.utils.PasswordUtil; import com.bruce.sams.common.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -21,19 +21,21 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
private UserMapper userMapper; private UserMapper userMapper;
/** /**
* *
* *
* @param users * @param users
*/ */
public void importUsers(List<User> users) { public void importUsers(List<User> users) {
for (User user : users) { for (User user : users) {
// 加密用户密码(默认密码123456) // 检查用户是否存在
if (user.getPassword() == null || user.getPassword().isEmpty()) { User existUser = userMapper.selectOne(
user.setPassword(PasswordUtil.encode("123456")); // 默认密码 new LambdaQueryWrapper<User>().eq(User::getSchoolId, user.getSchoolId()));
} else {
user.setPassword(PasswordUtil.encode(user.getPassword())); if (existUser == null) {
// 加密默认密码 123456
user.setPassword(PasswordUtil.encode("123456"));
userMapper.insert(user);
} }
userMapper.insert(user);
} }
} }
@ -54,7 +56,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
} }
/** /**
* *
* *
* @param user * @param user
*/ */
@ -68,14 +70,45 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
/** /**
* *
*
* @param userId ID
*/
public void resetPassword(Long userId) {
User user = userMapper.selectById(userId);
if (user == null) {
throw new UserNotFoundException();
}
// 设定默认密码 123456
user.setPassword(PasswordUtil.encode("123456"));
userMapper.updateById(user);
}
/**
*
*
* @param userId ID
*/
public void banUser(Long userId) {
User user = userMapper.selectById(userId);
if (user == null) {
throw new UserNotFoundException();
}
// 更新状态为 banned
user.setStatus("banned");
userMapper.updateById(user);
}
/**
*
* *
* @param userId ID * @param userId ID
* @param oldPassword * @param oldPassword
* @param newPassword * @param newPassword
*/ */
public void changePassword(Long userId, String oldPassword, String newPassword) { public void changePassword(Long userId, String oldPassword, String newPassword) {
// 获取用户
User user = userMapper.selectById(userId); User user = userMapper.selectById(userId);
if (user == null) { if (user == null) {
throw new UserNotFoundException(); throw new UserNotFoundException();
@ -86,15 +119,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
throw new PasswordIncorrectException(); throw new PasswordIncorrectException();
} }
// 更新新密码(加密存储) // 加密新密码
user.setPassword(PasswordUtil.encode(newPassword)); user.setPassword(PasswordUtil.encode(newPassword));
userMapper.updateById(user); userMapper.updateById(user);
} }
/** /**
* *
* *
* @param userId ID JWT * @param userId ID
* @param updatedUser * @param updatedUser
*/ */
public void updateProfile(Long userId, User updatedUser) { public void updateProfile(Long userId, User updatedUser) {
@ -111,6 +144,20 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
userMapper.updateById(user); userMapper.updateById(user);
} }
/**
*
*
* @param userId ID
* @return
*/
public User getUserProfile(Long userId) {
User user = userMapper.selectById(userId);
if (user == null) {
throw new UserNotFoundException();
}
return user;
}
/** /**
* *
* *

View File

@ -0,0 +1,21 @@
<?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.bruce.sams.mapper.ClubMapper">
<resultMap id="BaseResultMap" type="com.bruce.sams.domain.sms.Club">
<id property="clubId" column="club_id" />
<result property="clubName" column="club_name" />
<result property="description" column="description" />
<result property="category" column="category" />
<result property="collegeId" column="college_id" />
<result property="leaderId" column="leader_id" />
<result property="createdAt" column="created_at" />
</resultMap>
<sql id="Base_Column_List">
club_id,club_name,description,category,college_id,leader_id,
created_at
</sql>
</mapper>

View File

@ -0,0 +1,19 @@
<?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.bruce.sams.mapper.ClubUserMapper">
<resultMap id="BaseResultMap" type="com.bruce.sams.domain.sms.ClubUser">
<id property="sucId" column="suc_id" />
<result property="userId" column="user_id" />
<result property="clubId" column="club_id" />
<result property="roleId" column="role_id" />
<result property="isActive" column="is_active" />
<result property="joinDate" column="join_date" />
</resultMap>
<sql id="Base_Column_List">
suc_id,user_id,club_id,role_id,is_active,join_date
</sql>
</mapper>

View File

@ -0,0 +1,16 @@
<?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.bruce.sams.mapper.CollegeLeaderMapper">
<resultMap id="BaseResultMap" type="com.bruce.sams.domain.sms.CollegeLeader">
<id property="collegeId" column="college_id" />
<id property="userId" column="user_id" />
<result property="assignedAt" column="assigned_at" />
</resultMap>
<sql id="Base_Column_List">
college_id,user_id,assigned_at
</sql>
</mapper>

View File

@ -0,0 +1,18 @@
<?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.bruce.sams.mapper.CollegeMapper">
<resultMap id="BaseResultMap" type="com.bruce.sams.domain.sms.College">
<id property="collegeId" column="college_id" />
<result property="collegeName" column="college_name" />
<result property="parentId" column="parent_id" />
<result property="email" column="email" />
<result property="createdAt" column="created_at" />
</resultMap>
<sql id="Base_Column_List">
college_id,college_name,parent_id,email,created_at
</sql>
</mapper>

View File

@ -1,6 +1,6 @@
package com.bruce.sams; package com.bruce.sams;
import com.bruce.sams.utils.PasswordUtil; import com.bruce.sams.common.utils.PasswordUtil;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;