Commit 784d670c authored by zhengjie's avatar zhengjie
Browse files

2.0 抢先版,主要更新了#71 | #IWYE2

parent 90c2bf90
package me.zhengjie.modules.security.service;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.RoleRepository;
import me.zhengjie.modules.system.service.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
......@@ -26,7 +26,7 @@ public class JwtPermissionService {
* @return
*/
@Cacheable(key = "'loadPermissionByUser:' + #p0.username")
public Collection<GrantedAuthority> mapToGrantedAuthorities(User user) {
public Collection<GrantedAuthority> mapToGrantedAuthorities(UserDTO user) {
System.out.println("--------------------loadPermissionByUser:" + user.getUsername() + "---------------------");
......
......@@ -2,25 +2,18 @@ package me.zhengjie.modules.security.service;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.exception.EntityNotFoundException;
import me.zhengjie.modules.system.repository.PermissionRepository;
import me.zhengjie.modules.system.repository.RoleRepository;
import me.zhengjie.modules.security.security.JwtUser;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.DeptDTO;
import me.zhengjie.modules.system.service.dto.JobDTO;
import me.zhengjie.modules.system.service.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
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.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author jie
......@@ -39,7 +32,7 @@ public class JwtUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username){
User user = userService.findByName(username);
UserDTO user = userService.findByName(username);
if (user == null) {
throw new BadRequestException("账号不存在");
} else {
......@@ -47,7 +40,7 @@ public class JwtUserDetailsService implements UserDetailsService {
}
}
public UserDetails createJwtUser(User user) {
public UserDetails createJwtUser(UserDTO user) {
return new JwtUser(
user.getId(),
user.getUsername(),
......@@ -55,8 +48,8 @@ public class JwtUserDetailsService implements UserDetailsService {
user.getAvatar(),
user.getEmail(),
user.getPhone(),
Optional.ofNullable(user.getDept()).map(Dept::getName).orElse(null),
Optional.ofNullable(user.getJob()).map(Job::getName).orElse(null),
Optional.ofNullable(user.getDept()).map(DeptDTO::getName).orElse(null),
Optional.ofNullable(user.getJob()).map(JobDTO::getName).orElse(null),
permissionService.mapToGrantedAuthorities(user),
user.getEnabled(),
user.getCreateTime(),
......
......@@ -45,6 +45,10 @@ public class Dept implements Serializable {
@NotNull
private Long pid;
@JsonIgnore
@ManyToMany(mappedBy = "depts")
private Set<Role> roles;
@Column(name = "create_time")
@CreationTimestamp
private Timestamp createTime;
......
package me.zhengjie.modules.system.domain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
......
......@@ -3,6 +3,7 @@ package me.zhengjie.modules.system.repository;
import me.zhengjie.modules.system.domain.Dept;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Set;
......@@ -19,4 +20,9 @@ public interface DeptRepository extends JpaRepository<Dept, Long>, JpaSpecificat
* @return
*/
List<Dept> findByPid(Long id);
@Query(value = "select name from dept where id = ?1",nativeQuery = true)
String findNameById(Long id);
Set<Dept> findByRoles_Id(Long id);
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ import me.zhengjie.modules.system.service.MenuService;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.MenuDTO;
import me.zhengjie.modules.system.service.dto.UserDTO;
import me.zhengjie.modules.system.service.mapper.MenuMapper;
import me.zhengjie.modules.system.service.query.MenuQueryService;
import me.zhengjie.utils.SecurityUtils;
......@@ -50,7 +51,7 @@ public class MenuController {
*/
@GetMapping(value = "/menus/build")
public ResponseEntity buildMenus(){
User user = userService.findByName(SecurityUtils.getUsername());
UserDTO user = userService.findByName(SecurityUtils.getUsername());
List<MenuDTO> menuDTOList = menuService.findByRoles(roleService.findByUsers_Id(user.getId()));
List<MenuDTO> menuDTOTree = (List<MenuDTO>)menuService.buildTree(menuDTOList).get("content");
return new ResponseEntity(menuService.buildMenus(menuDTOTree),HttpStatus.OK);
......
......@@ -7,6 +7,7 @@ import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.dto.RoleDTO;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import me.zhengjie.modules.system.service.query.RoleQueryService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -71,7 +72,7 @@ public class RoleController {
@GetMapping(value = "/roles/level")
public ResponseEntity getLevel(){
List<Integer> levels = roleService.findByUsers_Id(SecurityUtils.getUserId()).stream().map(Role::getLevel).collect(Collectors.toList());
List<Integer> levels = roleService.findByUsers_Id(SecurityUtils.getUserId()).stream().map(RoleSmallDTO::getLevel).collect(Collectors.toList());
return new ResponseEntity(Dict.create().set("level", Collections.min(levels)),HttpStatus.OK);
}
......
......@@ -9,6 +9,7 @@ import me.zhengjie.modules.system.domain.User;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import me.zhengjie.service.PictureService;
import me.zhengjie.service.VerificationCodeService;
import me.zhengjie.utils.*;
......@@ -119,8 +120,8 @@ public class UserController {
@DeleteMapping(value = "/users/{id}")
@PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_DELETE')")
public ResponseEntity delete(@PathVariable Long id){
Integer currentLevel = Collections.min(roleService.findByUsers_Id(SecurityUtils.getUserId()).stream().map(Role::getLevel).collect(Collectors.toList()));
Integer optLevel = Collections.min(roleService.findByUsers_Id(id).stream().map(Role::getLevel).collect(Collectors.toList()));
Integer currentLevel = Collections.min(roleService.findByUsers_Id(SecurityUtils.getUserId()).stream().map(RoleSmallDTO::getLevel).collect(Collectors.toList()));
Integer optLevel = Collections.min(roleService.findByUsers_Id(id).stream().map(RoleSmallDTO::getLevel).collect(Collectors.toList()));
if (currentLevel > optLevel) {
throw new BadRequestException("角色权限不足");
......@@ -196,7 +197,7 @@ public class UserController {
* @param resources
*/
private void checkLevel(User resources) {
Integer currentLevel = Collections.min(roleService.findByUsers_Id(SecurityUtils.getUserId()).stream().map(Role::getLevel).collect(Collectors.toList()));
Integer currentLevel = Collections.min(roleService.findByUsers_Id(SecurityUtils.getUserId()).stream().map(RoleSmallDTO::getLevel).collect(Collectors.toList()));
Integer optLevel = roleService.findByRoles(resources.getRoles());
if (currentLevel > optLevel) {
throw new BadRequestException("角色权限不足");
......
......@@ -7,6 +7,7 @@ import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import java.util.List;
import java.util.Set;
/**
* @author jie
......@@ -60,4 +61,6 @@ public interface DeptService {
*/
@Cacheable(keyGenerator = "keyGenerator")
List<Dept> findByPid(long pid);
Set<Dept> findByRoleIds(Long id);
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package me.zhengjie.modules.system.service;
import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.service.dto.MenuDTO;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
......@@ -74,7 +75,7 @@ public interface MenuService {
* @param roles
* @return
*/
List<MenuDTO> findByRoles(List<Role> roles);
List<MenuDTO> findByRoles(List<RoleSmallDTO> roles);
/**
* buildMenus
......
......@@ -3,6 +3,7 @@ package me.zhengjie.modules.system.service;
import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.service.dto.RoleDTO;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
......@@ -54,7 +55,7 @@ public interface RoleService {
* @return
*/
@Cacheable(key = "'findByUsers_Id:' + #p0")
List<Role> findByUsers_Id(Long id);
List<RoleSmallDTO> findByUsers_Id(Long id);
@Cacheable(keyGenerator = "keyGenerator")
Integer findByRoles(Set<Role> roles);
......
......@@ -50,7 +50,7 @@ public interface UserService {
* @return
*/
@Cacheable(key = "'loadUserByUsername:'+#p0")
User findByName(String userName);
UserDTO findByName(String userName);
/**
* 修改密码
......
......@@ -35,6 +35,11 @@ public class JobDTO implements Serializable {
private DeptDTO dept;
/**
* 如果分公司存在相同部门,则显示上级部门名称
*/
private String deptSuperiorName;
/**
* 创建日期
*/
......
package me.zhengjie.modules.system.service.dto;
import lombok.Data;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Set;
/**
* @author jie
* @date 2018-11-23
*/
@Data
public class RoleSmallDTO implements Serializable {
private Long id;
private String name;
private Integer level;
private String dataScope;
}
......@@ -36,7 +36,7 @@ public class UserDTO implements Serializable {
private Date lastPasswordResetTime;
@ApiModelProperty(hidden = true)
private Set<RoleDTO> roles;
private Set<RoleSmallDTO> roles;
@ApiModelProperty(hidden = true)
private JobDTO job;
......
......@@ -42,10 +42,16 @@ public class DeptServiceImpl implements DeptService {
return deptRepository.findByPid(pid);
}
@Override
public Set<Dept> findByRoleIds(Long id) {
return deptRepository.findByRoles_Id(id);
}
@Override
public Object buildTree(List<DeptDTO> deptDTOS) {
Set<DeptDTO> trees = new LinkedHashSet<>();
Set<DeptDTO> depts= new LinkedHashSet<>();
List<String> deptNames = deptDTOS.stream().map(DeptDTO::getName).collect(Collectors.toList());
Boolean isChild;
for (DeptDTO deptDTO : deptDTOS) {
isChild = false;
......@@ -61,9 +67,10 @@ public class DeptServiceImpl implements DeptService {
deptDTO.getChildren().add(it);
}
}
if(isChild) {
if(isChild)
depts.add(deptDTO);
else if(!deptNames.contains(deptRepository.findNameById(deptDTO.getPid())))
depts.add(deptDTO);
}
}
if (CollectionUtils.isEmpty(trees)) {
......
......@@ -2,7 +2,6 @@ package me.zhengjie.modules.system.service.impl;
import cn.hutool.core.util.StrUtil;
import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.domain.vo.MenuMetaVo;
import me.zhengjie.modules.system.domain.vo.MenuVo;
import me.zhengjie.exception.BadRequestException;
......@@ -10,6 +9,7 @@ import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.repository.MenuRepository;
import me.zhengjie.modules.system.service.MenuService;
import me.zhengjie.modules.system.service.dto.MenuDTO;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import me.zhengjie.modules.system.service.mapper.MenuMapper;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -37,9 +37,9 @@ public class MenuServiceImpl implements MenuService {
}
@Override
public List<MenuDTO> findByRoles(List<Role> roles) {
public List<MenuDTO> findByRoles(List<RoleSmallDTO> roles) {
Set<Menu> menus = new LinkedHashSet<>();
for (Role role : roles) {
for (RoleSmallDTO role : roles) {
List<Menu> menus1 = menuRepository.findByRoles_IdOrderBySortAsc(role.getId()).stream().collect(Collectors.toList());
menus.addAll(menus1);
}
......
......@@ -7,7 +7,9 @@ import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.repository.RoleRepository;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.dto.RoleDTO;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import me.zhengjie.modules.system.service.mapper.RoleMapper;
import me.zhengjie.modules.system.service.mapper.RoleSmallMapper;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -30,6 +32,9 @@ public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper roleMapper;
@Autowired
private RoleSmallMapper roleSmallMapper;
@Override
public RoleDTO findById(long id) {
Optional<Role> role = roleRepository.findById(id);
......@@ -100,8 +105,8 @@ public class RoleServiceImpl implements RoleService {
}
@Override
public List<Role> findByUsers_Id(Long id) {
return roleRepository.findByUsers_Id(id).stream().collect(Collectors.toList());
public List<RoleSmallDTO> findByUsers_Id(Long id) {
return roleSmallMapper.toDto(roleRepository.findByUsers_Id(id).stream().collect(Collectors.toList()));
}
@Override
......
......@@ -102,18 +102,17 @@ public class UserServiceImpl implements UserService {
}
@Override
public User findByName(String userName) {
public UserDTO findByName(String userName) {
User user = null;
if(ValidationUtil.isEmail(userName)){
user = userRepository.findByEmail(userName);
} else {
user = userRepository.findByUsername(userName);
}
if (user == null) {
throw new EntityNotFoundException(User.class, "name", userName);
} else {
return user;
return userMapper.toDto(user);
}
}
......
......@@ -4,6 +4,7 @@ import me.zhengjie.mapper.EntityMapper;
import me.zhengjie.modules.system.domain.Job;
import me.zhengjie.modules.system.service.dto.JobDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
/**
......@@ -13,4 +14,6 @@ import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring",uses = {DeptMapper.class},unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface JobMapper extends EntityMapper<JobDTO, Job> {
@Mapping(source = "deptSuperiorName", target = "deptSuperiorName")
JobDTO toDto(Job job, String deptSuperiorName);
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment