master
parent
af96001858
commit
893e3a9767
|
|
@ -168,7 +168,7 @@ public class PermissionController {
|
|||
@ApiOperation(value = "修改菜单")
|
||||
@PreAuthorize("hasAuthority('sys:menu:add')")
|
||||
public void update(@RequestBody Permission permission) {
|
||||
permissionDao.update(permission);
|
||||
permissionService.update(permission);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.boot.security.server.dao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
|
|
@ -36,10 +37,13 @@ public interface PermissionDao {
|
|||
|
||||
@Delete("delete from sys_permission where id = #{id}")
|
||||
int delete(Long id);
|
||||
|
||||
|
||||
@Delete("delete from sys_permission where parentId = #{id}")
|
||||
int deleteByParentId(Long id);
|
||||
|
||||
@Delete("delete from sys_role_permission where permissionId = #{permissionId}")
|
||||
int deleteRolePermission(Long permissionId);
|
||||
|
||||
@Select("select ru.userId from sys_role_permission rp inner join sys_role_user ru on ru.roleId = rp.roleId where rp.permissionId = #{permissionId}")
|
||||
Set<Long> listUserIds(Long permissionId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.boot.security.server.dao;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
|
|
@ -47,4 +48,7 @@ public interface RoleDao {
|
|||
|
||||
@Delete("delete from sys_role_user where roleId = #{roleId}")
|
||||
int deleteRoleUser(Long roleId);
|
||||
|
||||
@Select("select ru.userId from sys_role r inner join sys_role_user ru on r.id = ru.roleId where ru.roleId = #{roleId}")
|
||||
Set<Long> listUserIds(Long roleId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.boot.security.server.service;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import com.boot.security.server.dto.UserDto;
|
||||
import com.boot.security.server.model.SysUser;
|
||||
|
||||
|
|
@ -13,5 +15,5 @@ public interface UserService {
|
|||
|
||||
void changePassword(String username, String oldPassword, String newPassword);
|
||||
|
||||
void updateLoginUserCache(Long userId);
|
||||
void updateLoginUserCache(Set<Long> userIds);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.boot.security.server.service.impl;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
|
@ -7,6 +9,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
import com.boot.security.server.dao.PermissionDao;
|
||||
import com.boot.security.server.model.Permission;
|
||||
import com.boot.security.server.service.PermissionService;
|
||||
import com.boot.security.server.service.UserService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
|
@ -16,6 +19,8 @@ public class PermissionServiceImpl implements PermissionService {
|
|||
|
||||
@Autowired
|
||||
private PermissionDao permissionDao;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
public void save(Permission permission) {
|
||||
|
|
@ -26,17 +31,25 @@ public class PermissionServiceImpl implements PermissionService {
|
|||
|
||||
@Override
|
||||
public void update(Permission permission) {
|
||||
Set<Long> userIds = listUserIds(permission.getId());
|
||||
permissionDao.update(permission);
|
||||
userService.updateLoginUserCache(userIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void delete(Long id) {
|
||||
Set<Long> userIds = listUserIds(id);
|
||||
permissionDao.deleteRolePermission(id);
|
||||
permissionDao.delete(id);
|
||||
permissionDao.deleteByParentId(id);
|
||||
|
||||
log.debug("删除菜单id:{}", id);
|
||||
userService.updateLoginUserCache(userIds);
|
||||
}
|
||||
|
||||
private Set<Long> listUserIds(Long permissionId) {
|
||||
return permissionDao.listUserIds(permissionId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.boot.security.server.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
|
@ -11,6 +12,7 @@ import com.boot.security.server.dao.RoleDao;
|
|||
import com.boot.security.server.dto.RoleDto;
|
||||
import com.boot.security.server.model.Role;
|
||||
import com.boot.security.server.service.RoleService;
|
||||
import com.boot.security.server.service.UserService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
|
@ -20,49 +22,68 @@ public class RoleServiceImpl implements RoleService {
|
|||
|
||||
@Autowired
|
||||
private RoleDao roleDao;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveRole(RoleDto roleDto) {
|
||||
Role role = roleDto;
|
||||
List<Long> permissionIds = roleDto.getPermissionIds();
|
||||
permissionIds.remove(0L);
|
||||
|
||||
if (role.getId() != null) {// 修改
|
||||
Role r = roleDao.getRole(role.getName());
|
||||
if (r != null && r.getId() != role.getId()) {
|
||||
throw new IllegalArgumentException(role.getName() + "已存在");
|
||||
}
|
||||
|
||||
roleDao.update(role);
|
||||
updateRole(role, permissionIds);
|
||||
} else {// 新增
|
||||
Role r = roleDao.getRole(role.getName());
|
||||
if (r != null) {
|
||||
throw new IllegalArgumentException(role.getName() + "已存在");
|
||||
}
|
||||
|
||||
roleDao.save(role);
|
||||
|
||||
log.debug("新增角色{}", role.getName());
|
||||
saveRole(role, permissionIds);
|
||||
}
|
||||
|
||||
saveRolePermission(role.getId(), roleDto.getPermissionIds());
|
||||
}
|
||||
|
||||
private void saveRolePermission(Long roleId, List<Long> permissionIds) {
|
||||
roleDao.deleteRolePermission(roleId);
|
||||
permissionIds.remove(0L);
|
||||
if (!CollectionUtils.isEmpty(permissionIds)) {
|
||||
roleDao.saveRolePermission(roleId, permissionIds);
|
||||
private void saveRole(Role role, List<Long> permissionIds) {
|
||||
Role r = roleDao.getRole(role.getName());
|
||||
if (r != null) {
|
||||
throw new IllegalArgumentException(role.getName() + "已存在");
|
||||
}
|
||||
|
||||
roleDao.save(role);
|
||||
if (!CollectionUtils.isEmpty(permissionIds)) {
|
||||
roleDao.saveRolePermission(role.getId(), permissionIds);
|
||||
}
|
||||
log.debug("新增角色{}", role.getName());
|
||||
}
|
||||
|
||||
private void updateRole(Role role, List<Long> permissionIds) {
|
||||
Role r = roleDao.getRole(role.getName());
|
||||
if (r != null && r.getId() != role.getId()) {
|
||||
throw new IllegalArgumentException(role.getName() + "已存在");
|
||||
}
|
||||
|
||||
roleDao.update(role);
|
||||
Set<Long> userIds = listUserIds(role.getId());
|
||||
|
||||
roleDao.deleteRolePermission(role.getId());
|
||||
if (!CollectionUtils.isEmpty(permissionIds)) {
|
||||
roleDao.saveRolePermission(role.getId(), permissionIds);
|
||||
}
|
||||
log.debug("修改角色{}", role.getName());
|
||||
|
||||
userService.updateLoginUserCache(userIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteRole(Long id) {
|
||||
Set<Long> userIds = listUserIds(id);
|
||||
roleDao.deleteRolePermission(id);
|
||||
roleDao.deleteRoleUser(id);
|
||||
roleDao.delete(id);
|
||||
|
||||
log.debug("删除角色id:{}", id);
|
||||
userService.updateLoginUserCache(userIds);
|
||||
}
|
||||
|
||||
private Set<Long> listUserIds(Long roleId) {
|
||||
return roleDao.listUserIds(roleId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
package com.boot.security.server.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
|
@ -20,6 +20,7 @@ import com.boot.security.server.model.SysUser;
|
|||
import com.boot.security.server.model.SysUser.Status;
|
||||
import com.boot.security.server.service.TokenService;
|
||||
import com.boot.security.server.service.UserService;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
|
@ -84,7 +85,7 @@ public class UserServiceImpl implements UserService {
|
|||
public SysUser updateUser(UserDto userDto) {
|
||||
userDao.update(userDto);
|
||||
saveUserRoles(userDto.getId(), userDto.getRoleIds());
|
||||
updateLoginUserCache(userDto.getId());
|
||||
updateLoginUserCache(Sets.newHashSet(userDto.getId()));
|
||||
|
||||
return userDto;
|
||||
}
|
||||
|
|
@ -92,22 +93,26 @@ public class UserServiceImpl implements UserService {
|
|||
/**
|
||||
* 修改登陆用户的缓存
|
||||
*/
|
||||
@Async
|
||||
@Override
|
||||
public void updateLoginUserCache(Long userId) {
|
||||
String token = tokenService.getTokenByUserId(userId);
|
||||
if (!StringUtils.isEmpty(token)) {
|
||||
SysUser sysUser = userDao.getById(userId);
|
||||
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setToken(token);
|
||||
BeanUtils.copyProperties(sysUser, loginUser);
|
||||
|
||||
List<Permission> permissions = permissionDao.listByUserId(sysUser.getId());
|
||||
loginUser.setPermissions(permissions);
|
||||
|
||||
tokenService.updateLoginUser(loginUser);
|
||||
public void updateLoginUserCache(Set<Long> userIds) {
|
||||
if (CollectionUtils.isEmpty(userIds)) {
|
||||
return;
|
||||
}
|
||||
|
||||
userIds.parallelStream().forEach(userId -> {
|
||||
String token = tokenService.getTokenByUserId(userId);
|
||||
if (!StringUtils.isEmpty(token)) {
|
||||
SysUser sysUser = userDao.getById(userId);
|
||||
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setToken(token);
|
||||
BeanUtils.copyProperties(sysUser, loginUser);
|
||||
|
||||
List<Permission> permissions = permissionDao.listByUserId(sysUser.getId());
|
||||
loginUser.setPermissions(permissions);
|
||||
|
||||
tokenService.updateLoginUser(loginUser);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue