From 168584f25877193a3be16d45e77ba617d23052f9 Mon Sep 17 00:00:00 2001 From: Ftz123456 <485913144@qq.com> Date: Sat, 12 Oct 2024 15:56:08 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=9D=83=E9=99=90=E7=9A=84=E5=A2=9E?= =?UTF-8?q?=E5=88=A0=E6=94=B9=E6=9F=A5):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改,角色的联表查询 --- .../zsc/edu/bill/FirstTimeInitializer.java | 12 +-- .../security/JpaUserDetailsServiceImpl.java | 16 ++-- .../security/SpringSecurityConfig.java | 2 +- .../framework/security/UserDetailsImpl.java | 6 +- .../bills/controller/BillController.java | 8 +- .../system/controller/RoleController.java | 4 +- .../system/controller/UserController.java | 13 +-- .../bill/modules/system/entity/Authority.java | 89 ++++++++++++------- .../modules/system/entity/RoleAuthority.java | 11 ++- .../bill/modules/system/entity/UserRole.java | 2 +- .../bill/modules/system/query/UserQuery.java | 2 - .../repo/RoleAuthoritiesRepository.java | 7 +- .../modules/system/repo/RoleRepository.java | 5 ++ .../modules/system/repo/UserRepository.java | 3 - ...Reposity.java => UserRolesRepository.java} | 5 +- .../modules/system/service/DeptService.java | 2 - .../system/service/RoleAuthService.java | 4 +- .../modules/system/service/RoleService.java | 3 +- .../modules/system/service/UserService.java | 1 - .../service/impl/RoleAuthServiceImpl.java | 19 ++-- .../system/service/impl/RoleServiceImpl.java | 25 +++--- .../bill/modules/system/utils/sendMail.java | 2 - .../bill/modules/system/vo/UserDetail.java | 5 +- src/main/resources/application-dev.yml | 4 +- src/main/resources/db/study.sql | 24 ++++- .../mappers/RoleAuthoritiesReposity.xml | 1 - src/main/resources/mappers/RoleMapper.xml | 34 ++++++- .../com/zsc/edu/bill/MockMvcConfigBase.java | 11 +-- .../zsc/edu/bill/rest/DeptControllerTest.java | 7 ++ .../zsc/edu/bill/rest/RoleControllerTest.java | 7 ++ .../zsc/edu/bill/service/RoleServiceTest.java | 12 ++- .../zsc/edu/bill/service/UserServiceTest.java | 9 +- 32 files changed, 230 insertions(+), 125 deletions(-) rename src/main/java/com/zsc/edu/bill/modules/system/repo/{UserRolesReposity.java => UserRolesRepository.java} (63%) diff --git a/src/main/java/com/zsc/edu/bill/FirstTimeInitializer.java b/src/main/java/com/zsc/edu/bill/FirstTimeInitializer.java index f4e9d55..73b55b8 100644 --- a/src/main/java/com/zsc/edu/bill/FirstTimeInitializer.java +++ b/src/main/java/com/zsc/edu/bill/FirstTimeInitializer.java @@ -6,20 +6,14 @@ import com.zsc.edu.bill.modules.system.entity.*; import com.zsc.edu.bill.modules.system.repo.DeptRepository; import com.zsc.edu.bill.modules.system.repo.RoleRepository; import com.zsc.edu.bill.modules.system.repo.UserRepository; -import com.zsc.edu.bill.modules.system.repo.UserRolesReposity; +import com.zsc.edu.bill.modules.system.repo.UserRolesRepository; import com.zsc.edu.bill.modules.system.service.RoleService; -import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Profile; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.stream.Collectors; - @RequiredArgsConstructor @Component @Profile("!test") @@ -28,7 +22,7 @@ public class FirstTimeInitializer implements CommandLineRunner { private final DeptRepository deptRepo; private final RoleRepository roleRepo; private final RoleService roleService; - private final UserRolesReposity userRolesRepo; + private final UserRolesRepository userRolesRepo; private final UserRepository userRepo; private final PasswordEncoder passwordEncoder; @@ -46,7 +40,7 @@ public class FirstTimeInitializer implements CommandLineRunner { if (roleRepo.selectCount(new QueryWrapper<>()) == 0) { RoleDto dto = new RoleDto(); dto.setName("超级管理员"); - dto.setAuthorities(new HashSet<>(Arrays.asList(Authority.values()))); +// dto.setAuthorities(new HashSet<>(Arrays.asList(Authority.values()))); role = roleService.create(dto); } if (userRepo.selectCount(new QueryWrapper<>()) == 0) { diff --git a/src/main/java/com/zsc/edu/bill/framework/security/JpaUserDetailsServiceImpl.java b/src/main/java/com/zsc/edu/bill/framework/security/JpaUserDetailsServiceImpl.java index 877acec..4c2df3e 100644 --- a/src/main/java/com/zsc/edu/bill/framework/security/JpaUserDetailsServiceImpl.java +++ b/src/main/java/com/zsc/edu/bill/framework/security/JpaUserDetailsServiceImpl.java @@ -5,16 +5,21 @@ import com.zsc.edu.bill.exception.StateException; import com.zsc.edu.bill.modules.system.entity.Authority; import com.zsc.edu.bill.modules.system.entity.RoleAuthority; import com.zsc.edu.bill.modules.system.entity.User; +import com.zsc.edu.bill.modules.system.repo.AuthorityRepository; import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesRepository; import com.zsc.edu.bill.modules.system.repo.UserRepository; import lombok.AllArgsConstructor; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; /** @@ -25,7 +30,7 @@ import java.util.stream.Collectors; public class JpaUserDetailsServiceImpl implements UserDetailsService { private final UserRepository userRepo; - private final RoleAuthoritiesRepository roleAuthoritiesRepository; + private final AuthorityRepository authorityRepo; @Override @Transactional(rollbackFor = Exception.class) @@ -35,12 +40,13 @@ public class JpaUserDetailsServiceImpl implements UserDetailsService { throw new StateException("用户 '" + username + "' 已被禁用!请联系管理员"); } - List roleAuthorities= roleAuthoritiesRepository.selectByRoleId(user.getRoleId()); - user.role.authorities=roleAuthorities.stream() - .map(i -> Authority.valueOf(i.getAuthority())) - .collect(Collectors.toSet()); + user.role.authorities=authorityRepo.selectAuthoritiesByRoleId(user.getRoleId()); +// List authorities = new ArrayList<>(); +// authorities=authorityList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()); +// return authorities; + // .orElseThrow(() -> // new UsernameNotFoundException("用户 '" + username + "' 不存在!") diff --git a/src/main/java/com/zsc/edu/bill/framework/security/SpringSecurityConfig.java b/src/main/java/com/zsc/edu/bill/framework/security/SpringSecurityConfig.java index 864ff6a..e843f09 100644 --- a/src/main/java/com/zsc/edu/bill/framework/security/SpringSecurityConfig.java +++ b/src/main/java/com/zsc/edu/bill/framework/security/SpringSecurityConfig.java @@ -91,7 +91,7 @@ public class SpringSecurityConfig { .rememberMe(rememberMe -> rememberMe .userDetailsService(userDetailsService) .tokenRepository(persistentTokenRepository())) - .csrf(csrf -> csrf.ignoringRequestMatchers("/api/internal/**", "/api/rest/user/logout","/api/rest/user/register")) + .csrf(csrf -> csrf.ignoringRequestMatchers("/api/internal/**", "/api/rest/user/logout","/api/rest/user/login","/api/rest/user/register")) .sessionManagement(session -> session .maximumSessions(3) .sessionRegistry(sessionRegistry) diff --git a/src/main/java/com/zsc/edu/bill/framework/security/UserDetailsImpl.java b/src/main/java/com/zsc/edu/bill/framework/security/UserDetailsImpl.java index 6c5a509..93bb63e 100644 --- a/src/main/java/com/zsc/edu/bill/framework/security/UserDetailsImpl.java +++ b/src/main/java/com/zsc/edu/bill/framework/security/UserDetailsImpl.java @@ -7,6 +7,7 @@ import com.zsc.edu.bill.modules.system.entity.User; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.*; import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import java.util.Collection; @@ -32,7 +33,7 @@ public class UserDetailsImpl implements UserDetails { public Dept dept; public Role role; - public Set authorities; + public Set authorities; public UserDetailsImpl(Long id, String username, String password, String nickName, Boolean enabled, Dept dept, Role role, Set authorities) { this.id = id; @@ -42,7 +43,7 @@ public class UserDetailsImpl implements UserDetails { this.nickName = nickName; this.dept = dept; this.role = role; - this.authorities = authorities; + this.authorities = authorities.stream().map(authority -> new SimpleGrantedAuthority(authority.getName())).collect(Collectors.toSet());; } @@ -58,7 +59,6 @@ public class UserDetailsImpl implements UserDetails { user.dept, user.role, user.role.authorities - ); } diff --git a/src/main/java/com/zsc/edu/bill/modules/bills/controller/BillController.java b/src/main/java/com/zsc/edu/bill/modules/bills/controller/BillController.java index 6ae98aa..c427dcd 100644 --- a/src/main/java/com/zsc/edu/bill/modules/bills/controller/BillController.java +++ b/src/main/java/com/zsc/edu/bill/modules/bills/controller/BillController.java @@ -75,9 +75,9 @@ public class BillController { * 删除票据 * @return ture/false */ - @DeleteMapping + @DeleteMapping("{id}") @PreAuthorize("hasAuthority('BILL_DELETE')") - public Boolean delete(Long id){ + public Boolean delete(@PathVariable Long id){ return service.removeById(id); } @@ -101,8 +101,8 @@ public class BillController { public Bill detail(@PathVariable Long id){ return service.getById(id); } - /* - *审核票据 + /** + * 审核票据 **/ @PatchMapping("audit/{id}") @PreAuthorize("hasAuthority('BILL_AUDIT')") diff --git a/src/main/java/com/zsc/edu/bill/modules/system/controller/RoleController.java b/src/main/java/com/zsc/edu/bill/modules/system/controller/RoleController.java index abd8f23..61f5483 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/controller/RoleController.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/controller/RoleController.java @@ -1,12 +1,12 @@ package com.zsc.edu.bill.modules.system.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO; import com.zsc.edu.bill.modules.system.dto.RoleDto; import com.zsc.edu.bill.modules.system.entity.Role; import com.zsc.edu.bill.modules.system.mapper.RoleMapper; import com.zsc.edu.bill.modules.system.query.RoleQuery; import com.zsc.edu.bill.modules.system.service.RoleService; +import com.zsc.edu.bill.modules.system.vo.RoleVo; import lombok.AllArgsConstructor; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; @@ -85,7 +85,7 @@ public class RoleController { */ @GetMapping("{id}") @PreAuthorize("hasAuthority('ROLE_QUERY')") - public Role detail(@PathVariable Long id) { + public RoleVo detail(@PathVariable Long id) { return service.detail(id); } diff --git a/src/main/java/com/zsc/edu/bill/modules/system/controller/UserController.java b/src/main/java/com/zsc/edu/bill/modules/system/controller/UserController.java index b85a2a8..7a3647c 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/controller/UserController.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/controller/UserController.java @@ -8,13 +8,11 @@ import com.zsc.edu.bill.modules.system.dto.UserCreateDto; import com.zsc.edu.bill.modules.system.dto.UserSelfUpdateDto; import com.zsc.edu.bill.modules.system.dto.UserSelfUpdatePasswordDto; import com.zsc.edu.bill.modules.system.dto.UserUpdateDto; +import com.zsc.edu.bill.modules.system.entity.Authority; import com.zsc.edu.bill.modules.system.entity.Role; import com.zsc.edu.bill.modules.system.entity.User; import com.zsc.edu.bill.modules.system.query.UserQuery; -import com.zsc.edu.bill.modules.system.service.DeptService; -import com.zsc.edu.bill.modules.system.service.RoleAuthService; -import com.zsc.edu.bill.modules.system.service.RoleService; -import com.zsc.edu.bill.modules.system.service.UserService; +import com.zsc.edu.bill.modules.system.service.*; import com.zsc.edu.bill.modules.system.vo.UserDetail; import com.zsc.edu.bill.modules.system.vo.UserVo; import lombok.AllArgsConstructor; @@ -26,6 +24,7 @@ import org.springframework.web.bind.annotation.*; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; /** * 用户Controller @@ -42,6 +41,7 @@ public class UserController { private final RoleService roleService; private final DeptService deptService; private final RoleAuthService roleAuthService; + private final AuthorityService authorityService; /** * 登录前,获取csrfToken信息 @@ -72,7 +72,8 @@ public class UserController { user.dept = deptService.getById(user.deptId); Role role= roleService.getOne(new QueryWrapper().eq("id",user.roleId)); userDetail.setPermissions(role.getName()); - userDetail.setAuthorities(roleAuthService.getAuthorityByUserId(role.getId())); + Set authorities = authorityService.selectAuthoritiesByRoleId(role.getId()); + userDetail.setAuthorities(authorities.stream().toList()); userDetail.setUser(user); @@ -113,7 +114,7 @@ public class UserController { * @return 分页用户信息 */ @GetMapping - @PreAuthorize("hasAuthority('USER_QUERY')") + @PreAuthorize("hasAuthority('USER_QUERY·')") public Page query(UserQuery query, PageDTO page) { return service.page(query, page); } diff --git a/src/main/java/com/zsc/edu/bill/modules/system/entity/Authority.java b/src/main/java/com/zsc/edu/bill/modules/system/entity/Authority.java index 0e60454..f0d0334 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/entity/Authority.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/entity/Authority.java @@ -1,49 +1,70 @@ package com.zsc.edu.bill.modules.system.entity; -import org.springframework.security.core.GrantedAuthority; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +import java.util.Date; /** * 操作权限 * * @author harry yao */ -public enum Authority implements GrantedAuthority { - +@Getter +@Setter + @EqualsAndHashCode(callSuper = false) +@TableName("sys_authority") +public class Authority extends BaseEntity { /** - * 部门管理 + * 权限名 */ - DEPT_QUERY, - DEPT_CREATE, - DEPT_UPDATE, - DEPT_DELETE, - + public String name; /** - * 角色管理 + * 启用状态 */ - ROLE_CREATE, - ROLE_QUERY, - ROLE_UPDATE, - + private Boolean enabled = true; /** - * 用户管理 + * 备注 */ - USER_QUERY, - USER_CREATE, - USER_UPDATE, - USER_DELETE, - /** - * 票据管理 - * */ - BILL_QUERY, - BILL_CREATE, - BILL_UPDATE, - BILL_AUDIT, - BILL_CHOOSE_AUDITOR, - BILL_DELETE; - - - @Override - public String getAuthority() { - return name(); - } + private String remark; +// +// /** +// * 部门管理 +// */ +// DEPT_QUERY, +// DEPT_CREATE, +// DEPT_UPDATE, +// DEPT_DELETE, +// +// /** +// * 角色管理 +// */ +// ROLE_CREATE, +// ROLE_QUERY, +// ROLE_UPDATE, +// +// /** +// * 用户管理 +// */ +// USER_QUERY, +// USER_CREATE, +// USER_UPDATE, +// USER_DELETE, +// /** +// * 票据管理 +// * */ +// BILL_QUERY, +// BILL_CREATE, +// BILL_UPDATE, +// BILL_AUDIT, +// BILL_CHOOSE_AUDITOR, +// BILL_DELETE; +// +// +// @Override +// public String getAuthority() { +// return name(); +// } } diff --git a/src/main/java/com/zsc/edu/bill/modules/system/entity/RoleAuthority.java b/src/main/java/com/zsc/edu/bill/modules/system/entity/RoleAuthority.java index d511abf..2b75956 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/entity/RoleAuthority.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/entity/RoleAuthority.java @@ -1,16 +1,21 @@ package com.zsc.edu.bill.modules.system.entity; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; +import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesRepository; import lombok.AllArgsConstructor; import lombok.Data; import lombok.Getter; import lombok.Setter; import java.io.Serializable; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; /** * sys_role_authorities - * @author + * @author zhuang */ @AllArgsConstructor @Getter @@ -20,6 +25,8 @@ public class RoleAuthority implements Serializable { private Long roleId; - private String authority; + private Long authorityId; +// @TableField(exist = false) +// private Set authorities; } \ No newline at end of file diff --git a/src/main/java/com/zsc/edu/bill/modules/system/entity/UserRole.java b/src/main/java/com/zsc/edu/bill/modules/system/entity/UserRole.java index 25453ee..93dd8a5 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/entity/UserRole.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/entity/UserRole.java @@ -7,7 +7,7 @@ import java.io.Serializable; /** * sys_users_roles - * @author + * @author zhuang */ @Data @TableName("sys_users_roles") diff --git a/src/main/java/com/zsc/edu/bill/modules/system/query/UserQuery.java b/src/main/java/com/zsc/edu/bill/modules/system/query/UserQuery.java index e3f70b1..44fab3a 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/query/UserQuery.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/query/UserQuery.java @@ -2,10 +2,8 @@ package com.zsc.edu.bill.modules.system.query; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zsc.edu.bill.modules.system.entity.User; -import com.zsc.edu.bill.modules.system.vo.UserVo; import lombok.AllArgsConstructor; import lombok.Data; -import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import org.springframework.util.StringUtils; diff --git a/src/main/java/com/zsc/edu/bill/modules/system/repo/RoleAuthoritiesRepository.java b/src/main/java/com/zsc/edu/bill/modules/system/repo/RoleAuthoritiesRepository.java index 751731b..6be6eee 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/repo/RoleAuthoritiesRepository.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/repo/RoleAuthoritiesRepository.java @@ -2,7 +2,6 @@ package com.zsc.edu.bill.modules.system.repo; import com.zsc.edu.bill.modules.system.entity.RoleAuthority; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import org.apache.ibatis.annotations.Select; import java.util.List; @@ -11,6 +10,8 @@ import java.util.List; */ public interface RoleAuthoritiesRepository extends BaseMapper { - @Select("select * from sys_role_authorities where role_id=#{roleId}") - List selectByRoleId(Long roleId); +// @Select("select * from sys_role_authorities where role_id=#{roleId}") +// List selectByRoleId(Long roleId); + +// List selectAuthorityByRoleId(@Param("roleId") Long roleId); } \ No newline at end of file diff --git a/src/main/java/com/zsc/edu/bill/modules/system/repo/RoleRepository.java b/src/main/java/com/zsc/edu/bill/modules/system/repo/RoleRepository.java index b2371ac..73dfc27 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/repo/RoleRepository.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/repo/RoleRepository.java @@ -2,6 +2,9 @@ package com.zsc.edu.bill.modules.system.repo; import com.zsc.edu.bill.modules.system.entity.Role; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.zsc.edu.bill.modules.system.vo.RoleVo; +import org.apache.ibatis.annotations.Param; + /** *

@@ -12,4 +15,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; * @since 2023-04-06 */ public interface RoleRepository extends BaseMapper { + + RoleVo selectRoleById(@Param("roleId") Long roleId); } diff --git a/src/main/java/com/zsc/edu/bill/modules/system/repo/UserRepository.java b/src/main/java/com/zsc/edu/bill/modules/system/repo/UserRepository.java index 52733fc..41e0532 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/repo/UserRepository.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/repo/UserRepository.java @@ -1,14 +1,11 @@ package com.zsc.edu.bill.modules.system.repo; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO; import com.zsc.edu.bill.modules.system.entity.User; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.zsc.edu.bill.modules.system.vo.UserVo; import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Select; /** *

diff --git a/src/main/java/com/zsc/edu/bill/modules/system/repo/UserRolesReposity.java b/src/main/java/com/zsc/edu/bill/modules/system/repo/UserRolesRepository.java similarity index 63% rename from src/main/java/com/zsc/edu/bill/modules/system/repo/UserRolesReposity.java rename to src/main/java/com/zsc/edu/bill/modules/system/repo/UserRolesRepository.java index 445ce95..4e45288 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/repo/UserRolesReposity.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/repo/UserRolesRepository.java @@ -3,5 +3,8 @@ package com.zsc.edu.bill.modules.system.repo; import com.zsc.edu.bill.modules.system.entity.UserRole; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -public interface UserRolesReposity extends BaseMapper { +/** + * @author zhuang + */ +public interface UserRolesRepository extends BaseMapper { } \ No newline at end of file diff --git a/src/main/java/com/zsc/edu/bill/modules/system/service/DeptService.java b/src/main/java/com/zsc/edu/bill/modules/system/service/DeptService.java index 58ae1da..dffe405 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/service/DeptService.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/service/DeptService.java @@ -17,7 +17,6 @@ public interface DeptService extends IService { /** * 创建部门 * @param dto - * @return */ Dept create(DeptDto dto); @@ -25,7 +24,6 @@ public interface DeptService extends IService { * 更新部门 * @param dto * @param id - * @return */ Boolean edit(DeptDto dto, Long id); diff --git a/src/main/java/com/zsc/edu/bill/modules/system/service/RoleAuthService.java b/src/main/java/com/zsc/edu/bill/modules/system/service/RoleAuthService.java index ea48b90..ff3ecf4 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/service/RoleAuthService.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/service/RoleAuthService.java @@ -3,7 +3,6 @@ package com.zsc.edu.bill.modules.system.service; import com.zsc.edu.bill.modules.system.entity.RoleAuthority; import com.baomidou.mybatisplus.extension.service.IService; -import java.util.List; /** *

@@ -14,7 +13,8 @@ import java.util.List; * @since 2023-04-06 */ public interface RoleAuthService extends IService { + boolean removeByRoleId(Long id); - List getAuthorityByUserId(Long id); +// List getAuthorityByUserId(Long id); } diff --git a/src/main/java/com/zsc/edu/bill/modules/system/service/RoleService.java b/src/main/java/com/zsc/edu/bill/modules/system/service/RoleService.java index a0074be..3a32071 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/service/RoleService.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/service/RoleService.java @@ -4,6 +4,7 @@ package com.zsc.edu.bill.modules.system.service; import com.zsc.edu.bill.modules.system.dto.RoleDto; import com.zsc.edu.bill.modules.system.entity.Role; import com.baomidou.mybatisplus.extension.service.IService; +import com.zsc.edu.bill.modules.system.vo.RoleVo; /** *

@@ -19,7 +20,7 @@ public interface RoleService extends IService { Boolean edit(RoleDto roleDto, Long id); - Role detail(Long id); + RoleVo detail(Long id); Boolean toggle(Long id); diff --git a/src/main/java/com/zsc/edu/bill/modules/system/service/UserService.java b/src/main/java/com/zsc/edu/bill/modules/system/service/UserService.java index 3282174..051d769 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/service/UserService.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/service/UserService.java @@ -1,7 +1,6 @@ package com.zsc.edu.bill.modules.system.service; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zsc.edu.bill.framework.security.UserDetailsImpl; import com.zsc.edu.bill.modules.system.dto.*; import com.zsc.edu.bill.modules.system.entity.User; diff --git a/src/main/java/com/zsc/edu/bill/modules/system/service/impl/RoleAuthServiceImpl.java b/src/main/java/com/zsc/edu/bill/modules/system/service/impl/RoleAuthServiceImpl.java index d9a4524..54832c4 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/service/impl/RoleAuthServiceImpl.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/service/impl/RoleAuthServiceImpl.java @@ -7,9 +7,10 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; -import java.util.List; -import java.util.stream.Collectors; +/** + * @author zhuang + */ @Service public class RoleAuthServiceImpl extends ServiceImpl implements RoleAuthService { @Override @@ -17,11 +18,11 @@ public class RoleAuthServiceImpl extends ServiceImpl().eq(RoleAuthority::getRoleId, roleId)); } - @Override - public List getAuthorityByUserId(Long id) { - List list = list(new LambdaQueryWrapper().eq(RoleAuthority::getRoleId, id)); - return list(new LambdaQueryWrapper().eq(RoleAuthority::getRoleId, id)).stream().map(RoleAuthority::getAuthority).collect(Collectors.toList()); - - - } +// @Override +// public List getAuthorityByUserId(Long id) { +// List list = list(new LambdaQueryWrapper().eq(RoleAuthority::getRoleId, id)); +// return list(new LambdaQueryWrapper().eq(RoleAuthority::getRoleId, id)).stream().map(RoleAuthority::getAuthority).collect(Collectors.toList()); +// +// +// } } diff --git a/src/main/java/com/zsc/edu/bill/modules/system/service/impl/RoleServiceImpl.java b/src/main/java/com/zsc/edu/bill/modules/system/service/impl/RoleServiceImpl.java index e3af787..3330496 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/service/impl/RoleServiceImpl.java @@ -7,12 +7,14 @@ import com.zsc.edu.bill.modules.system.entity.Role; import com.zsc.edu.bill.modules.system.entity.RoleAuthority; import com.zsc.edu.bill.modules.system.entity.UserRole; import com.zsc.edu.bill.modules.system.mapper.RoleMapper; +import com.zsc.edu.bill.modules.system.repo.AuthorityRepository; import com.zsc.edu.bill.modules.system.repo.RoleRepository; -import com.zsc.edu.bill.modules.system.repo.UserRolesReposity; +import com.zsc.edu.bill.modules.system.repo.UserRolesRepository; import com.zsc.edu.bill.modules.system.service.RoleAuthService; import com.zsc.edu.bill.modules.system.service.RoleService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.zsc.edu.bill.modules.system.vo.RoleVo; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; @@ -34,9 +36,11 @@ import java.util.stream.Collectors; public class RoleServiceImpl extends ServiceImpl implements RoleService { private final RoleMapper mapper; - private final UserRolesReposity urRepo; + private final UserRolesRepository urRepo; private final RoleAuthService roleAuthService; + private final AuthorityRepository authorityRepository; + private final RoleRepository roleRepository; @Override public Role create(RoleDto dto) { @@ -76,7 +80,7 @@ public class RoleServiceImpl extends ServiceImpl implement if (dto.getAuthorities() != null && !dto.getAuthorities().isEmpty()) { roleAuthService.remove(new LambdaQueryWrapper().eq(RoleAuthority::getRoleId, id)); Set authorities = new HashSet<>(dto.getAuthorities()); - roleAuthService.saveBatch(authorities.stream().map(authority -> new RoleAuthority(id, authority.getAuthority())).collect(Collectors.toList())); + // roleAuthService.saveBatch(authorities.stream().map(authority -> new RoleAuthority(id, authority.getId())).collect(Collectors.toList())); } return updateById(role); @@ -95,18 +99,17 @@ public class RoleServiceImpl extends ServiceImpl implement } @Override - public Role detail(Long id) { - Role role = getById(id); - List roleAuthorities = roleAuthService.list(new LambdaQueryWrapper().eq(RoleAuthority::getRoleId, role.id)); - role.authorities = roleAuthorities.stream() - .map(roleAuthority -> Authority.valueOf(roleAuthority.getAuthority())) - .collect(Collectors.toSet()); - return role; + public RoleVo detail(Long id) { + // Role role = getById(id); +// // TODO 联表查询 +// // List roleAuthorities = roleAuthService.list(new LambdaQueryWrapper().eq(RoleAuthority::getRoleId, role.id)); +// role.authorities = authorityRepository.selectAuthoritiesByRoleId(role.id); + return roleRepository.selectRoleById(id); } private boolean saveRoleAuths(Long roleId, Set authorities) { // 保存角色关联权限 - List roleAuthorities = authorities.stream().map(authority -> new RoleAuthority(roleId, authority.getAuthority())).collect(Collectors.toList()); + List roleAuthorities = authorities.stream().map(authority -> new RoleAuthority(roleId, authority.getId())).collect(Collectors.toList()); return roleAuthService.saveBatch(roleAuthorities); } } diff --git a/src/main/java/com/zsc/edu/bill/modules/system/utils/sendMail.java b/src/main/java/com/zsc/edu/bill/modules/system/utils/sendMail.java index e375fb1..bb5b7e3 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/utils/sendMail.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/utils/sendMail.java @@ -3,8 +3,6 @@ package com.zsc.edu.bill.modules.system.utils; import jakarta.mail.MessagingException; import jakarta.mail.internet.MimeMessage; import lombok.Getter; -import org.apache.commons.lang3.RandomStringUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; diff --git a/src/main/java/com/zsc/edu/bill/modules/system/vo/UserDetail.java b/src/main/java/com/zsc/edu/bill/modules/system/vo/UserDetail.java index e5d0d6a..5a445e2 100644 --- a/src/main/java/com/zsc/edu/bill/modules/system/vo/UserDetail.java +++ b/src/main/java/com/zsc/edu/bill/modules/system/vo/UserDetail.java @@ -2,13 +2,10 @@ package com.zsc.edu.bill.modules.system.vo; import com.zsc.edu.bill.modules.system.entity.Authority; import com.zsc.edu.bill.modules.system.entity.User; -import lombok.Data; -import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import java.util.List; -import java.util.Set; /** * @author ftz @@ -19,6 +16,6 @@ import java.util.Set; @Setter public class UserDetail { private User user; - private List authorities; + private List authorities; private String permissions; } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 817ac54..8b3e946 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -10,9 +10,9 @@ mybatis-plus: spring: datasource: - url: jdbc:mysql://106.53.179.133:3306/study?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&characterEncoding=utf8&serverTimezone=Hongkong + url: jdbc:mysql://localhost:3306/study?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&characterEncoding=utf8&serverTimezone=Hongkong username: root - password: 123456 + password: tian14384, driver-class-name: com.mysql.cj.jdbc.Driver servlet: multipart: diff --git a/src/main/resources/db/study.sql b/src/main/resources/db/study.sql index d94f4dd..116dba8 100644 --- a/src/main/resources/db/study.sql +++ b/src/main/resources/db/study.sql @@ -466,6 +466,8 @@ CREATE TABLE `sys_role` ( INDEX `role_name_index`(`name` ASC) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 84 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '角色表' ROW_FORMAT = COMPACT; + + -- ---------------------------- -- Records of sys_role -- ---------------------------- @@ -473,13 +475,33 @@ INSERT INTO `sys_role` VALUES (50, 'admin', NULL, NULL, NULL, NULL, NULL, '2024- INSERT INTO `sys_role` VALUES (51, 'user', NULL, NULL, NULL, NULL, NULL, '2024-01-16 09:22:30', '2024-02-21 07:06:10', b'1', '客户'); INSERT INTO `sys_role` VALUES (54, 'auditor', NULL, NULL, NULL, NULL, NULL, '2024-01-19 16:15:45', '2024-01-24 14:48:55', b'1', '审核员'); +-- ---------------------------- +-- Table structure for sys_authorities +-- ---------------------------- + +DROP TABLE IF EXISTS `sys_authority`; +CREATE TABLE `sys_authority` ( + `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID', + `name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '名称', + `enabled` bit(1) NULL DEFAULT NULL COMMENT '状态', + `create_by` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '创建者', + `update_by` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '更新者', + `create_time` datetime NULL DEFAULT NULL COMMENT '创建日期', + `update_time` datetime NULL DEFAULT NULL COMMENT '更新时间', + `remark` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL, + PRIMARY KEY (`id`) USING BTREE, + UNIQUE INDEX `uniq_name`(`name` ASC) USING BTREE, + INDEX `authority_name_index`(`name` ASC) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '权限表' ROW_FORMAT = COMPACT; + -- ---------------------------- -- Table structure for sys_role_authorities -- ---------------------------- DROP TABLE IF EXISTS `sys_role_authorities`; CREATE TABLE `sys_role_authorities` ( `role_id` bigint NOT NULL, - `authority` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL, + `authority_id` bigint NOT NULL , + PRIMARY KEY (`role_id`, `authority_id`) USING BTREE, INDEX `FKbyfnfkpgrf4jmo3nf97arsphd`(`role_id` ASC) USING BTREE ) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = COMPACT; diff --git a/src/main/resources/mappers/RoleAuthoritiesReposity.xml b/src/main/resources/mappers/RoleAuthoritiesReposity.xml index 674f01f..9647e45 100644 --- a/src/main/resources/mappers/RoleAuthoritiesReposity.xml +++ b/src/main/resources/mappers/RoleAuthoritiesReposity.xml @@ -11,5 +11,4 @@ - \ No newline at end of file diff --git a/src/main/resources/mappers/RoleMapper.xml b/src/main/resources/mappers/RoleMapper.xml index 7e9c8d4..136259c 100644 --- a/src/main/resources/mappers/RoleMapper.xml +++ b/src/main/resources/mappers/RoleMapper.xml @@ -1,5 +1,37 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/com/zsc/edu/bill/MockMvcConfigBase.java b/src/test/java/com/zsc/edu/bill/MockMvcConfigBase.java index d1b900c..b29e622 100644 --- a/src/test/java/com/zsc/edu/bill/MockMvcConfigBase.java +++ b/src/test/java/com/zsc/edu/bill/MockMvcConfigBase.java @@ -48,15 +48,16 @@ abstract public class MockMvcConfigBase { protected CustomAuthenticationFailureHandler customAuthenticationFailureHandler; @MockBean protected CustomAccessDeniedHandler customAccessDeniedHandler; - @Autowired - protected MockMvc mockMvc; - @Autowired - protected ObjectMapper objectMapper; +// @Autowired +// protected MockMvc mockMvc; +// @Autowired +// protected ObjectMapper objectMapper; @BeforeAll public static void setup() { Dept dept = DeptBuilder.aDept().name("Platform").build(); - Role role = RoleBuilder.aRole().authorities(new HashSet<>(Arrays.asList(Authority.values()))).build(); + Role role = RoleBuilder.aRole().authorities(new HashSet<>()).build(); +// Role role = RoleBuilder.aRole().authorities(new HashSet<>(Arrays.asList(Authority))).build(); user = UserBuilder.anUser().username("admin").dept(dept).role(role).build(); userDetails = UserDetailsImpl.from(user); } diff --git a/src/test/java/com/zsc/edu/bill/rest/DeptControllerTest.java b/src/test/java/com/zsc/edu/bill/rest/DeptControllerTest.java index 77d841e..e94ed80 100644 --- a/src/test/java/com/zsc/edu/bill/rest/DeptControllerTest.java +++ b/src/test/java/com/zsc/edu/bill/rest/DeptControllerTest.java @@ -1,5 +1,7 @@ package com.zsc.edu.bill.rest; +//import com.zsc.edu.bill.MockMvcConfigBase; +import com.fasterxml.jackson.databind.ObjectMapper; import com.zsc.edu.bill.MockMvcConfigBase; import com.zsc.edu.bill.domain.DeptBuilder; import com.zsc.edu.bill.modules.system.controller.DeptController; @@ -14,6 +16,7 @@ import org.mockito.Spy; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; import java.util.List; @@ -34,6 +37,10 @@ public class DeptControllerTest extends MockMvcConfigBase { private static Dept dept2; + private MockMvc mockMvc; + + private ObjectMapper objectMapper; + @MockBean protected DeptMapper deptMapper; diff --git a/src/test/java/com/zsc/edu/bill/rest/RoleControllerTest.java b/src/test/java/com/zsc/edu/bill/rest/RoleControllerTest.java index 0083641..c59d2f6 100644 --- a/src/test/java/com/zsc/edu/bill/rest/RoleControllerTest.java +++ b/src/test/java/com/zsc/edu/bill/rest/RoleControllerTest.java @@ -1,5 +1,6 @@ package com.zsc.edu.bill.rest; +import com.fasterxml.jackson.databind.ObjectMapper; import com.zsc.edu.bill.MockMvcConfigBase; import com.zsc.edu.bill.domain.RoleBuilder; import com.zsc.edu.bill.modules.system.controller.RoleController; @@ -9,9 +10,11 @@ import com.zsc.edu.bill.modules.system.service.RoleService; import org.assertj.core.util.Lists; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; import java.util.List; @@ -33,6 +36,10 @@ public class RoleControllerTest extends MockMvcConfigBase { private static Role Role2; + private MockMvc mockMvc; + + private ObjectMapper objectMapper; + @MockBean private RoleService service; diff --git a/src/test/java/com/zsc/edu/bill/service/RoleServiceTest.java b/src/test/java/com/zsc/edu/bill/service/RoleServiceTest.java index 987e647..7132560 100644 --- a/src/test/java/com/zsc/edu/bill/service/RoleServiceTest.java +++ b/src/test/java/com/zsc/edu/bill/service/RoleServiceTest.java @@ -6,6 +6,7 @@ import com.zsc.edu.bill.domain.RoleBuilder; import com.zsc.edu.bill.modules.system.dto.RoleDto; import com.zsc.edu.bill.modules.system.entity.Authority; import com.zsc.edu.bill.modules.system.entity.Role; +import com.zsc.edu.bill.modules.system.repo.AuthorityRepository; import com.zsc.edu.bill.modules.system.repo.RoleRepository; import com.zsc.edu.bill.modules.system.service.RoleService; import org.junit.jupiter.api.AfterEach; @@ -40,6 +41,8 @@ class RoleServiceTest { private Role Role3; private Role Role4; + private AuthorityRepository authorityRepository; + @BeforeEach void setUp() { role1 = RoleBuilder.aRole().name("超级管理员").build(); @@ -69,7 +72,7 @@ class RoleServiceTest { RoleDto dto = new RoleDto(); dto.setName("东菱经销商5"); dto.setRemark("remark..."); - dto.setAuthorities(new HashSet<>(Arrays.asList(Authority.values()))); + dto.setAuthorities(new HashSet<>(Arrays.asList(authorityRepository.selectOne(null)))); Role Role = service.create(dto); assertNotNull(Role.getId()); assertEquals(3, service.list().size()); @@ -82,9 +85,12 @@ class RoleServiceTest { RoleDto dto = new RoleDto(); dto.setName("超级管理员2"); dto.setRemark("remark..."); - dto.setAuthorities(Set.of(Authority.ROLE_UPDATE, Authority.DEPT_UPDATE)); + dto.setAuthorities(authorityRepository.selectAuthoritiesByRoleId(1L)); assertTrue(service.edit(dto, role2.id)); } - + @Test + void roleVo(){ + repo.deleteById(role1.id); + } } diff --git a/src/test/java/com/zsc/edu/bill/service/UserServiceTest.java b/src/test/java/com/zsc/edu/bill/service/UserServiceTest.java index 6a19ad6..f1b2e3d 100644 --- a/src/test/java/com/zsc/edu/bill/service/UserServiceTest.java +++ b/src/test/java/com/zsc/edu/bill/service/UserServiceTest.java @@ -3,6 +3,7 @@ package com.zsc.edu.bill.service; import com.zsc.edu.bill.modules.system.entity.Authority; import com.zsc.edu.bill.modules.system.entity.RoleAuthority; import com.zsc.edu.bill.modules.system.entity.User; +import com.zsc.edu.bill.modules.system.repo.AuthorityRepository; import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesRepository; import com.zsc.edu.bill.modules.system.repo.UserRepository; import jakarta.annotation.Resource; @@ -24,13 +25,13 @@ public class UserServiceTest { private RoleAuthoritiesRepository roleAuthoritiesRepository; @Resource private UserRepository userRepository; + @Resource + private AuthorityRepository authorityRepository; @Test void test() { User user=userRepository.selectByUsername("admin"); - List authorities= roleAuthoritiesRepository.selectByRoleId(user.getRoleId()); - Set authorities1=new HashSet<>(); - authorities.stream().forEach(authority ->authorities1.add(Authority.valueOf(authority.getAuthority()))); - user.role.authorities=authorities1; + Set authorities= authorityRepository.selectAuthoritiesByRoleId(user.getRoleId()); + user.role.authorities=authorities; System.out.println(user); }