feat(权限的增删改查):
修改,角色的联表查询
This commit is contained in:
parent
f3ebc49a60
commit
168584f258
@ -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) {
|
||||
|
@ -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<RoleAuthority> 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<GrantedAuthority> authorities = new ArrayList<>();
|
||||
// authorities=authorityList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
|
||||
// return authorities;
|
||||
|
||||
|
||||
// .orElseThrow(() ->
|
||||
// new UsernameNotFoundException("用户 '" + username + "' 不存在!")
|
||||
|
@ -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)
|
||||
|
@ -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<Authority> authorities;
|
||||
public Set<SimpleGrantedAuthority> authorities;
|
||||
|
||||
public UserDetailsImpl(Long id, String username, String password, String nickName, Boolean enabled, Dept dept, Role role, Set<Authority> 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
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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')")
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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<Role>().eq("id",user.roleId));
|
||||
userDetail.setPermissions(role.getName());
|
||||
userDetail.setAuthorities(roleAuthService.getAuthorityByUserId(role.getId()));
|
||||
Set<Authority> 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<UserVo> query(UserQuery query, PageDTO<User> page) {
|
||||
return service.page(query, page);
|
||||
}
|
||||
|
@ -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();
|
||||
// }
|
||||
}
|
||||
|
@ -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<Authority> authorities;
|
||||
}
|
@ -7,7 +7,7 @@ import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* sys_users_roles
|
||||
* @author
|
||||
* @author zhuang
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_users_roles")
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<RoleAuthority> {
|
||||
|
||||
@Select("select * from sys_role_authorities where role_id=#{roleId}")
|
||||
List<RoleAuthority> selectByRoleId(Long roleId);
|
||||
// @Select("select * from sys_role_authorities where role_id=#{roleId}")
|
||||
// List<RoleAuthority> selectByRoleId(Long roleId);
|
||||
|
||||
// List<RoleAuthority> selectAuthorityByRoleId(@Param("roleId") Long roleId);
|
||||
}
|
@ -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;
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -12,4 +15,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
* @since 2023-04-06
|
||||
*/
|
||||
public interface RoleRepository extends BaseMapper<Role> {
|
||||
|
||||
RoleVo selectRoleById(@Param("roleId") Long roleId);
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -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<UserRole> {
|
||||
/**
|
||||
* @author zhuang
|
||||
*/
|
||||
public interface UserRolesRepository extends BaseMapper<UserRole> {
|
||||
}
|
@ -17,7 +17,6 @@ public interface DeptService extends IService<Dept> {
|
||||
/**
|
||||
* 创建部门
|
||||
* @param dto
|
||||
* @return
|
||||
*/
|
||||
Dept create(DeptDto dto);
|
||||
|
||||
@ -25,7 +24,6 @@ public interface DeptService extends IService<Dept> {
|
||||
* 更新部门
|
||||
* @param dto
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
Boolean edit(DeptDto dto, Long id);
|
||||
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -14,7 +13,8 @@ import java.util.List;
|
||||
* @since 2023-04-06
|
||||
*/
|
||||
public interface RoleAuthService extends IService<RoleAuthority> {
|
||||
|
||||
boolean removeByRoleId(Long id);
|
||||
|
||||
List<String> getAuthorityByUserId(Long id);
|
||||
// List<String> getAuthorityByUserId(Long id);
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -19,7 +20,7 @@ public interface RoleService extends IService<Role> {
|
||||
|
||||
Boolean edit(RoleDto roleDto, Long id);
|
||||
|
||||
Role detail(Long id);
|
||||
RoleVo detail(Long id);
|
||||
|
||||
Boolean toggle(Long id);
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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<RoleAuthoritiesRepository, RoleAuthority> implements RoleAuthService {
|
||||
@Override
|
||||
@ -17,11 +18,11 @@ public class RoleAuthServiceImpl extends ServiceImpl<RoleAuthoritiesRepository,
|
||||
return remove(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, roleId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAuthorityByUserId(Long id) {
|
||||
List<RoleAuthority> list = list(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, id));
|
||||
return list(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, id)).stream().map(RoleAuthority::getAuthority).collect(Collectors.toList());
|
||||
|
||||
|
||||
}
|
||||
// @Override
|
||||
// public List<String> getAuthorityByUserId(Long id) {
|
||||
// List<RoleAuthority> list = list(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, id));
|
||||
// return list(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, id)).stream().map(RoleAuthority::getAuthority).collect(Collectors.toList());
|
||||
//
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
@ -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<RoleRepository, Role> 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<RoleRepository, Role> implement
|
||||
if (dto.getAuthorities() != null && !dto.getAuthorities().isEmpty()) {
|
||||
roleAuthService.remove(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, id));
|
||||
Set<Authority> 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<RoleRepository, Role> implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role detail(Long id) {
|
||||
Role role = getById(id);
|
||||
List<RoleAuthority> roleAuthorities = roleAuthService.list(new LambdaQueryWrapper<RoleAuthority>().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<RoleAuthority> roleAuthorities = roleAuthService.list(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, role.id));
|
||||
// role.authorities = authorityRepository.selectAuthoritiesByRoleId(role.id);
|
||||
return roleRepository.selectRoleById(id);
|
||||
}
|
||||
|
||||
private boolean saveRoleAuths(Long roleId, Set<Authority> authorities) {
|
||||
// 保存角色关联权限
|
||||
List<RoleAuthority> roleAuthorities = authorities.stream().map(authority -> new RoleAuthority(roleId, authority.getAuthority())).collect(Collectors.toList());
|
||||
List<RoleAuthority> roleAuthorities = authorities.stream().map(authority -> new RoleAuthority(roleId, authority.getId())).collect(Collectors.toList());
|
||||
return roleAuthService.saveBatch(roleAuthorities);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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<String> authorities;
|
||||
private List<Authority> authorities;
|
||||
private String permissions;
|
||||
}
|
||||
|
@ -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:
|
||||
|
@ -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;
|
||||
|
||||
|
@ -11,5 +11,4 @@
|
||||
<!-- </select>-->
|
||||
|
||||
|
||||
|
||||
</mapper>
|
@ -1,5 +1,37 @@
|
||||
<?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.zsc.edu.bill.modules.system.mapper.RoleMapper">
|
||||
<mapper namespace="com.zsc.edu.bill.modules.system.repo.RoleRepository">
|
||||
<!--查角色包含权限-->
|
||||
|
||||
<resultMap id="roleMap" type="com.zsc.edu.bill.modules.system.vo.RoleVo" autoMapping="true">
|
||||
<id column="id" jdbcType="BIGINT" property="id"/>
|
||||
<result column="name" jdbcType="VARCHAR" property="name"/>
|
||||
<result column="create_by" jdbcType="VARCHAR" property="createBy"/>
|
||||
<result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
|
||||
<result column="create_time" jdbcType="DATE" property="createTime"/>
|
||||
<result column="update_time" jdbcType="DATE" property="updateTime"/>
|
||||
<result column="enabled" jdbcType="BIT" property="enabled"/>
|
||||
<result column="remark" jdbcType="VARCHAR" property="remark"/>
|
||||
<collection property="authorities" ofType="com.zsc.edu.bill.modules.system.entity.Authority" autoMapping="true" columnPrefix="authority_">
|
||||
<id column="id" jdbcType="BIT" property="id"/>
|
||||
<result column="name" jdbcType="VARCHAR" property="name"/>
|
||||
<result column="remark" jdbcType="VARCHAR" property="remark"/>
|
||||
</collection>
|
||||
</resultMap>
|
||||
<!-- <select id="selectRoleById" resultMap="roleMap">-->
|
||||
<!-- select * from-->
|
||||
<!-- sys_role sr, sys_role_authorities sra, sys_authority sa-->
|
||||
<!-- where sr.id=sra.role_id and sra.authority_id=sa.id-->
|
||||
<!-- and sr.id=#{roleId}-->
|
||||
<!-- </select>-->
|
||||
|
||||
<select id="selectRoleById" resultMap="roleMap">
|
||||
select sr.*,
|
||||
sa.id as authority_id, sa.name as authority_name, sa.enabled as authority_enabled,sa.remark as authority_remark,sa.create_by as authority_create_by,sa.create_time as authority_create_time,sa.update_by as authority_update_by,sa.update_time as authority_update_time
|
||||
from sys_role sr
|
||||
left join sys_role_authorities sra on sr.id=sra.role_id
|
||||
left join sys_authority sa on sra.authority_id=sa.id
|
||||
where sr.id=#{roleId}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<RoleAuthority> authorities= roleAuthoritiesRepository.selectByRoleId(user.getRoleId());
|
||||
Set<Authority> authorities1=new HashSet<>();
|
||||
authorities.stream().forEach(authority ->authorities1.add(Authority.valueOf(authority.getAuthority())));
|
||||
user.role.authorities=authorities1;
|
||||
Set<Authority> authorities= authorityRepository.selectAuthoritiesByRoleId(user.getRoleId());
|
||||
user.role.authorities=authorities;
|
||||
System.out.println(user);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user