bug--权限相关的关联表添加

This commit is contained in:
taochengbo
2020-05-16 21:03:07 +08:00
parent ac7d3f4648
commit d6a9ed47d5
19 changed files with 442 additions and 9 deletions

View File

@ -42,7 +42,7 @@ public class Dict implements Serializable {
/** 创建日期 */
//@TableField(fill= FieldFill.INSERT)
@TableField(fill= FieldFill.INSERT)
private Timestamp createTime;

View File

@ -51,7 +51,7 @@ public class DictDetail implements Serializable {
/** 创建日期 */
//@TableField(fill= FieldFill.INSERT)
@TableField(fill= FieldFill.INSERT)
private Timestamp createTime;

View File

@ -0,0 +1,37 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableName;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import javax.validation.constraints.*;
import java.io.Serializable;
/**
* @author hupeng
* @date 2020-05-16
*/
@Data
@TableName("roles_depts")
public class RolesDepts implements Serializable {
@TableId
private Long roleId;
@TableId
private Long deptId;
public void copy(RolesDepts source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableName;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import java.io.Serializable;
/**
* @author hupeng
* @date 2020-05-16
*/
@Data
@TableName("roles_menus")
public class RolesMenus implements Serializable {
/** 菜单ID */
@TableId
private Long menuId;
/** 角色ID */
@TableId
private Long roleId;
public void copy(RolesMenus source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}

View File

@ -57,6 +57,7 @@ public class User implements Serializable {
private Job job;
@TableField(exist = false)
private Dept dept;
/** 密码 */
private String password;
@ -78,7 +79,7 @@ public class User implements Serializable {
/** 创建日期 */
//@TableField(fill= FieldFill.INSERT)
@TableField(fill= FieldFill.INSERT)
private Timestamp createTime;

View File

@ -0,0 +1,35 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import java.io.Serializable;
/**
* @author hupeng
* @date 2020-05-16
*/
@Data
@TableName("users_roles")
public class UsersRoles implements Serializable {
/** 用户ID */
private Long userId;
/** 角色ID */
private Long roleId;
public void copy(UsersRoles source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}

View File

@ -128,7 +128,7 @@ public class SysUserController {
checkLevel(resources);
// 默认密码 123456
resources.setPassword(passwordEncoder.encode("123456"));
return new ResponseEntity<>(userService.save(resources),HttpStatus.CREATED);
return new ResponseEntity<>(userService.create(resources),HttpStatus.CREATED);
}
@Log("修改用户")
@ -138,7 +138,7 @@ public class SysUserController {
public ResponseEntity<Object> update(@Validated @RequestBody User resources){
//if(StrUtil.isNotEmpty("22")) throw new BadRequestException("演示环境禁止操作");
checkLevel(resources);
userService.saveOrUpdate(resources);
userService.update(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

View File

@ -0,0 +1,19 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service;
import co.yixiang.common.service.BaseService;
import co.yixiang.modules.system.domain.RolesDepts;
/**
* @author hupeng
* @date 2020-05-16
*/
public interface RolesDeptsService extends BaseService<RolesDepts>{
}

View File

@ -0,0 +1,19 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service;
import co.yixiang.common.service.BaseService;
import co.yixiang.modules.system.domain.RolesMenus;
/**
* @author hupeng
* @date 2020-05-16
*/
public interface RolesMenusService extends BaseService<RolesMenus>{
}

View File

@ -12,10 +12,8 @@ import co.yixiang.modules.system.domain.User;
import co.yixiang.modules.system.service.dto.UserDto;
import co.yixiang.modules.system.service.dto.UserQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.multipart.MultipartFile;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import java.io.IOException;
@ -77,4 +75,17 @@ public interface UserService extends BaseService<User>{
*/
void updateEmail(String username, String email);
/**
* 新增用户
* @param resources /
* @return /
*/
boolean create(User resources);
/**
* 编辑用户
* @param resources /
*/
void update(User resources);
}

View File

@ -0,0 +1,19 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service;
import co.yixiang.common.service.BaseService;
import co.yixiang.modules.system.domain.UsersRoles;
/**
* @author hupeng
* @date 2020-05-16
*/
public interface UsersRolesService extends BaseService<UsersRoles>{
}

View File

@ -0,0 +1,31 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service.impl;
import co.yixiang.modules.system.domain.RolesDepts;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import co.yixiang.modules.system.service.RolesDeptsService;
import co.yixiang.modules.system.service.mapper.RolesDeptsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @author hupeng
* @date 2020-05-16
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "rolesDepts")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RolesDeptsServiceImpl extends BaseServiceImpl<RolesDeptsMapper, RolesDepts> implements RolesDeptsService {
}

View File

@ -0,0 +1,31 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service.impl;
import co.yixiang.modules.system.domain.RolesMenus;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import co.yixiang.modules.system.service.RolesMenusService;
import co.yixiang.modules.system.service.mapper.RolesMenusMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @author hupeng
* @date 2020-05-16
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "rolesMenus")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RolesMenusServiceImpl extends BaseServiceImpl<RolesMenusMapper, RolesMenus> implements RolesMenusService {
}

View File

@ -8,13 +8,18 @@
*/
package co.yixiang.modules.system.service.impl;
import co.yixiang.exception.EntityExistException;
import co.yixiang.modules.system.domain.Role;
import co.yixiang.modules.system.domain.User;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.modules.system.domain.UserAvatar;
import co.yixiang.modules.system.domain.UsersRoles;
import co.yixiang.modules.system.service.*;
import co.yixiang.modules.system.service.mapper.RoleMapper;
import co.yixiang.utils.RedisUtils;
import co.yixiang.utils.SecurityUtils;
import co.yixiang.utils.StringUtils;
import co.yixiang.utils.ValidationUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageInfo;
@ -24,6 +29,7 @@ import co.yixiang.modules.system.service.dto.UserDto;
import co.yixiang.modules.system.service.dto.UserQueryCriteria;
import co.yixiang.modules.system.service.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@ -42,6 +48,7 @@ import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Set;
/**
* @author hupeng
@ -62,14 +69,18 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, User> imp
private final JobService jobService;
private final DeptService deptService;
private final RoleMapper roleMapper;
private final RedisUtils redisUtils;
private final UsersRolesService usersRolesService;
public SysUserServiceImpl(IGenerator generator, SysUserMapper userMapper, UserAvatarService userAvatarService, JobService jobService, DeptService deptService, RoleService roleService, RoleMapper roleMapper) {
public SysUserServiceImpl(IGenerator generator, SysUserMapper userMapper, UserAvatarService userAvatarService, JobService jobService, DeptService deptService, RoleService roleService, RoleMapper roleMapper, RedisUtils redisUtils, UsersRolesService usersRolesService) {
this.generator = generator;
this.userMapper = userMapper;
this.userAvatarService = userAvatarService;
this.jobService = jobService;
this.deptService = deptService;
this.roleMapper = roleMapper;
this.redisUtils = redisUtils;
this.usersRolesService = usersRolesService;
}
@Override
@ -180,4 +191,84 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, User> imp
userMapper.updateEmail(email, username);
}
/**
* 新增用户
*
* @param resources /
* @return /
*/
@Override
//@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public boolean create(User resources) {
User userName = this.getOne(new QueryWrapper<User>().lambda()
.eq(User::getUsername,resources.getUsername()));
if(userName != null){
throw new EntityExistException(User.class,"username",resources.getUsername());
}
User userEmail = this.getOne(new QueryWrapper<User>().lambda()
.eq(User::getEmail,resources.getEmail()));
if(userEmail != null){
throw new EntityExistException(User.class,"email",resources.getEmail());
}
resources.setDeptId(resources.getDept().getId());
resources.setJobId(resources.getJob().getId());
boolean result = this.save(resources);
UsersRoles usersRoles = new UsersRoles();
usersRoles.setUserId(resources.getId());
Set<Role> set = resources.getRoles();
for (Role roleIds : set ) {
usersRoles.setRoleId(roleIds.getId());
}
if (result) {
usersRolesService.save(usersRoles);
}
return result;
}
/**
* 编辑用户
*
* @param resources /
*/
@Override
//@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public void update(User resources) {
User user = this.getOne(new QueryWrapper<User>().lambda()
.eq(User::getId,resources.getId()));
ValidationUtil.isNull(user.getId(),"User","id",resources.getId());
User user1 = this.getOne(new QueryWrapper<User>().lambda()
.eq(User::getUsername,resources.getUsername()));
User user2 = this.getOne(new QueryWrapper<User>().lambda()
.eq(User::getEmail,resources.getEmail()));
if(user1 !=null&&!user.getId().equals(user1.getId())){
throw new EntityExistException(User.class,"username",resources.getUsername());
}
if(user2!=null&&!user.getId().equals(user2.getId())){
throw new EntityExistException(User.class,"email",resources.getEmail());
}
// 如果用户的角色改变了,需要手动清理下缓存
if (!resources.getRoles().equals(user.getRoles())) {
String key = "role::loadPermissionByUser:" + user.getUsername();
redisUtils.del(key);
key = "role::findByUsers_Id:" + user.getId();
redisUtils.del(key);
}
user.setUsername(resources.getUsername());
user.setEmail(resources.getEmail());
user.setEnabled(resources.getEnabled());
user.setRoles(resources.getRoles());
user.setDept(resources.getDept());
user.setJob(resources.getJob());
user.setPhone(resources.getPhone());
user.setNickName(resources.getNickName());
user.setSex(resources.getSex());
this.saveOrUpdate(user);
}
}

View File

@ -0,0 +1,30 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service.impl;
import co.yixiang.modules.system.domain.UsersRoles;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import co.yixiang.modules.system.service.UsersRolesService;
import co.yixiang.modules.system.service.mapper.UsersRolesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @author hupeng
* @date 2020-05-16
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "usersRoles")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UsersRolesServiceImpl extends BaseServiceImpl<UsersRolesMapper, UsersRoles> implements UsersRolesService {
}

View File

@ -30,5 +30,5 @@ public interface DeptMapper extends CoreMapper<Dept> {
Set<Dept> findDeptByRoleId(@Param("roleId") Long roleId);
@Select("select * from dept m LEFT JOIN roles_depts t on m.id= t.dept_id LEFT JOIN role r on r.id = t.role_id where r.id = #{roleId}")
Set<Dept> findDeptByRoleId(@Param("roleIds") Set<Long> roleId);
Set<Dept> findDeptByRoleIds(@Param("roleIds") Set<Long> roleId);
}

View File

@ -0,0 +1,24 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service.mapper;
import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.system.domain.RolesDepts;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
/**
* @author hupeng
* @date 2020-05-16
*/
@Repository
@Mapper
public interface RolesDeptsMapper extends CoreMapper<RolesDepts> {
}

View File

@ -0,0 +1,24 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service.mapper;
import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.system.domain.RolesMenus;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
/**
* @author hupeng
* @date 2020-05-16
*/
@Repository
@Mapper
public interface RolesMenusMapper extends CoreMapper<RolesMenus> {
}

View File

@ -0,0 +1,24 @@
/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制未经购买不得使用
* 购买后可获得全部源代码禁止转卖、分享、上传到码云、github等开源平台
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
*/
package co.yixiang.modules.system.service.mapper;
import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.system.domain.UsersRoles;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
/**
* @author hupeng
* @date 2020-05-16
*/
@Repository
@Mapper
public interface UsersRolesMapper extends CoreMapper<UsersRoles> {
}