mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-06-03 12:56:10 +08:00
权限校验1
This commit is contained in:
parent
ffd79f9727
commit
2659fe98c1
@ -0,0 +1,41 @@
|
||||
package top.suyiiyii.sims.controller;
|
||||
|
||||
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 top.suyiiyii.sims.common.Result;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.service.RoleService;
|
||||
import top.suyiiyii.sims.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author tortoise
|
||||
* @Date 2024/8/14 13:57
|
||||
* @PackageName:top.suyiiyii.sims.controller
|
||||
* @ClassName: AdminController
|
||||
* @Description: TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin")
|
||||
public class AdminController {
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/findAllUsersWithRoles")
|
||||
public Result findAllUsersWithRoles() {
|
||||
List<User> userList = roleService.findAllUsersWithRoles();
|
||||
return Result.success(userList);
|
||||
}
|
||||
@GetMapping("/selectAll")
|
||||
public Result selectAll() {
|
||||
List<User> users = userService.selectAll();
|
||||
return Result.success(users);
|
||||
}
|
||||
|
||||
}
|
@ -7,6 +7,7 @@ import top.suyiiyii.sims.common.AuthAccess;
|
||||
import top.suyiiyii.sims.common.Result;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.exception.ServiceException;
|
||||
import top.suyiiyii.sims.service.RoleService;
|
||||
import top.suyiiyii.sims.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
@ -25,6 +26,8 @@ import java.util.List;
|
||||
public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
@Autowired
|
||||
RoleService roleService;
|
||||
@AuthAccess
|
||||
@GetMapping("/")
|
||||
public Result hello(){
|
||||
@ -62,16 +65,10 @@ public class UserController {
|
||||
|
||||
|
||||
|
||||
@GetMapping("/selectAll")
|
||||
public Result selectAll() {
|
||||
List<User> users = userService.selectAll();
|
||||
return Result.success(users);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public Result add(@RequestBody User user) {
|
||||
|
||||
userService.addUser(user);
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
@PostMapping("/delete")
|
||||
@ -79,6 +76,13 @@ public class UserController {
|
||||
userService.deleteUser(user.getId());
|
||||
return Result.success("删除成功");
|
||||
}
|
||||
/**
|
||||
* @author: tortoise
|
||||
* @date: 2024/8/14 13:34
|
||||
* @Description: 更新用户信息,自己改的(不包括密码)
|
||||
* @param user
|
||||
* @return: top.suyiiyii.sims.common.Result
|
||||
*/
|
||||
@PostMapping("/update")
|
||||
public Result update(@RequestBody User user) {
|
||||
userService.updateUser(user);
|
||||
@ -87,10 +91,45 @@ public class UserController {
|
||||
@PostMapping("/select")
|
||||
public Result select(@RequestBody User user) {
|
||||
return Result.success(userService.selectById(user.getId()));
|
||||
|
||||
}
|
||||
@PostMapping("/selectByUsername")
|
||||
public Result selectByUsername(@RequestBody User user) {
|
||||
return Result.success(userService.selectByUsername(user.getUsername()));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @author: tortoise
|
||||
* @date: 2024/8/14 13:48
|
||||
* @Description: TODO 用户更新密码
|
||||
* @param user
|
||||
* @return: top.suyiiyii.sims.common.Result
|
||||
*/
|
||||
@PostMapping("/updatePassword")
|
||||
public Result updatePassword(@RequestBody User user) {
|
||||
if(StrUtil.isBlank(user.getPassword())){
|
||||
return Result.error("密码不能为空");
|
||||
}
|
||||
User user1 = userService.selectByUsername(user.getUsername());
|
||||
//验证原密码是否正确
|
||||
if(!user.getPassword().equals(user1.getPassword())){
|
||||
return Result.error("原密码错误");
|
||||
}
|
||||
userService.updatePassword(user);
|
||||
return Result.success("更新成功");
|
||||
}
|
||||
/**
|
||||
* @author: tortoise
|
||||
* @date: 2024/8/14 13:48
|
||||
* @Description: TODO 管理员修改密码
|
||||
* @param user
|
||||
* @return: top.suyiiyii.sims.common.Result
|
||||
*/
|
||||
@PostMapping("/updatePasswordByAdmin")
|
||||
public Result updatePasswordByAdmin(@RequestBody User user) {
|
||||
if(StrUtil.isBlank(user.getPassword())){
|
||||
return Result.error("密码不能为空");
|
||||
}
|
||||
userService.updatePassword(user);
|
||||
return Result.success("更新成功");
|
||||
}
|
||||
|
||||
}
|
@ -7,6 +7,8 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @Author tortoise
|
||||
* @Date 2024/8/9 14:03
|
||||
@ -24,7 +26,21 @@ public class Permissions {
|
||||
private Integer id;
|
||||
//权限id
|
||||
private Integer permissionId;
|
||||
|
||||
private String path;
|
||||
// 权限描述
|
||||
private String description;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Permissions that = (Permissions) o;
|
||||
return Objects.equals(path, that.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(path);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import lombok.NoArgsConstructor;
|
||||
* @Author tortoise
|
||||
* @Date 2024/8/9 14:02
|
||||
* @PackageName:top.suyiiyii.sims.entity
|
||||
* @ClassName: Role
|
||||
* @ClassName: RoleMapper
|
||||
* @Description: TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
|
@ -8,6 +8,8 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @Author tortoise
|
||||
* @Date 2024/8/9 14:02
|
||||
@ -17,7 +19,7 @@ import lombok.NoArgsConstructor;
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
/*@Table*/
|
||||
@Table
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class User {
|
||||
@ -31,4 +33,6 @@ public class User {
|
||||
private String group;
|
||||
@TableField(exist = false)
|
||||
private String token;
|
||||
@TableField(exist = false)
|
||||
private Set<Permissions> permissions;
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package top.suyiiyii.sims.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import top.suyiiyii.sims.entity.Permissions;
|
||||
import top.suyiiyii.sims.entity.RolePermission;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author tortoise
|
||||
* @Date 2024/8/14 16:14
|
||||
* @PackageName:top.suyiiyii.sims.mapper
|
||||
* @ClassName: PermissionsMapper
|
||||
* @Description: TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Mapper
|
||||
public interface PermissionsMapper {
|
||||
@Select("SELECT * FROM role_permission WHERE role_id = #{id}")
|
||||
List<RolePermission> getRolePerminsionByRoleId(Integer id);
|
||||
@Select("SELECT * FROM permissions WHERE permission_id = #{permissionId}")
|
||||
Permissions selectById(Integer permissionId);
|
||||
}
|
57
src/main/java/top/suyiiyii/sims/mapper/RoleMapper.java
Normal file
57
src/main/java/top/suyiiyii/sims/mapper/RoleMapper.java
Normal file
@ -0,0 +1,57 @@
|
||||
package top.suyiiyii.sims.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import lombok.Data;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
import top.suyiiyii.sims.entity.Permissions;
|
||||
import top.suyiiyii.sims.entity.Role;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.entity.UserRole;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author tortoise
|
||||
* @Date 2024/8/14 14:13
|
||||
* @PackageName:top.suyiiyii.sims.mapper
|
||||
* @ClassName: RoleMapper
|
||||
* @Description: TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@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);
|
||||
/**
|
||||
* @author: tortoise
|
||||
* @date: 2024/8/14 14:23
|
||||
* @Description: TODO 查询用户信息
|
||||
* @param
|
||||
* @return: java.util.List<top.suyiiyii.sims.entity.User>
|
||||
*/
|
||||
@Select("SELECT u.username, u.name, u.userId, 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")
|
||||
@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"))
|
||||
})
|
||||
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<UserRole> selectRolesById(@Param("user_id") int id);
|
||||
|
||||
|
||||
}
|
@ -2,6 +2,7 @@ package top.suyiiyii.sims.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
import top.suyiiyii.sims.entity.Role;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
@ -40,7 +41,6 @@ public interface UserMapper extends BaseMapper<User> {
|
||||
@Update("UPDATE user SET " +
|
||||
"user_id = #{userId}, " +
|
||||
"username = #{username}, " +
|
||||
"password = #{password}, " +
|
||||
"name = #{name}, " +
|
||||
"email = #{email}, " +
|
||||
"`group` = #{group} " +
|
||||
@ -71,5 +71,7 @@ public interface UserMapper extends BaseMapper<User> {
|
||||
|
||||
@Select("select * from user where username = #{username}")
|
||||
User selectByUserName(@Param("username") String username);
|
||||
@Update("update user set password = #{password} where username = #{username}")
|
||||
void updatePassword(User user);
|
||||
|
||||
}
|
||||
|
49
src/main/java/top/suyiiyii/sims/service/RoleService.java
Normal file
49
src/main/java/top/suyiiyii/sims/service/RoleService.java
Normal file
@ -0,0 +1,49 @@
|
||||
package top.suyiiyii.sims.service;
|
||||
|
||||
import org.checkerframework.checker.units.qual.A;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import top.suyiiyii.sims.entity.Role;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.entity.UserRole;
|
||||
import top.suyiiyii.sims.mapper.RoleMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author tortoise
|
||||
* @Date 2024/8/14 14:14
|
||||
* @PackageName:top.suyiiyii.sims.service
|
||||
* @ClassName: RoleService
|
||||
* @Description: TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class RoleService {
|
||||
@Autowired
|
||||
RoleMapper roleMapper;
|
||||
public void addRole(String name){
|
||||
roleMapper.addRole(name);
|
||||
}
|
||||
public void deleteRole(String name){
|
||||
roleMapper.deleteRole(name);
|
||||
}
|
||||
public void updateRole(String name,String newName){
|
||||
roleMapper.updateRole(name,newName);
|
||||
}
|
||||
public List<User> findAllUsersWithRoles(){
|
||||
return roleMapper.selectAllUsersWithRoles();
|
||||
}
|
||||
/**
|
||||
* @author: tortoise
|
||||
* @date: 2024/8/14 14:39
|
||||
* @Description: TODO 查看自己身份
|
||||
* @param Id
|
||||
* @return: java.util.List<top.suyiiyii.sims.entity.Role>
|
||||
*/
|
||||
List<UserRole> selectRolesById(int id){
|
||||
return roleMapper.selectRolesById(id);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -6,11 +6,16 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import top.suyiiyii.sims.common.Result;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.entity.*;
|
||||
import top.suyiiyii.sims.exception.ServiceException;
|
||||
import top.suyiiyii.sims.mapper.PermissionsMapper;
|
||||
import top.suyiiyii.sims.mapper.RoleMapper;
|
||||
import top.suyiiyii.sims.mapper.UserMapper;
|
||||
import top.suyiiyii.sims.utils.JwtUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -25,6 +30,10 @@ import java.util.List;
|
||||
public class UserService {
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
RoleMapper roleMapper;
|
||||
@Autowired
|
||||
PermissionsMapper permissionsMapper;
|
||||
|
||||
public void addUser(User user) {
|
||||
userMapper.addUser(user);
|
||||
@ -54,6 +63,21 @@ public class UserService {
|
||||
if (!dbUser.getPassword().equals(user.getPassword())) {
|
||||
throw new ServiceException("密码或用户名错误");
|
||||
}
|
||||
HashSet<Permissions> permissionsSet = new HashSet<>();
|
||||
Integer id = dbUser.getId();
|
||||
List<UserRole> UserRoles = roleMapper.selectRolesById(id);
|
||||
for (UserRole userRole : UserRoles) {
|
||||
//根据roleid找所有permissionId
|
||||
List<RolePermission> rolePerminsion = permissionsMapper.getRolePerminsionByRoleId(userRole.getRoleId());
|
||||
for (RolePermission rolePermission : rolePerminsion) {
|
||||
Integer permissionId = rolePermission.getPermissionId();
|
||||
//根据permissionId找permission
|
||||
Permissions permissions = permissionsMapper.selectById(permissionId);
|
||||
permissionsSet.add(permissions);
|
||||
}
|
||||
}
|
||||
dbUser.setPermissions(permissionsSet);
|
||||
|
||||
String token = JwtUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
|
||||
dbUser.setToken(token);
|
||||
return dbUser;
|
||||
@ -89,4 +113,9 @@ public class UserService {
|
||||
public User selectByUsername(String username) {
|
||||
return userMapper.selectByUserName(username);
|
||||
}
|
||||
|
||||
public void updatePassword(User user) {
|
||||
userMapper.updatePassword(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user