超级管理员 角色-菜单-权限 逻辑调整

v1.4.1
Parker 4 years ago
parent 024b6bf1a2
commit e214c8caf3

@ -45,18 +45,22 @@ public class JwtRealm extends AuthorizingRealm implements FlagRealm {
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
UserModel user = (UserModel) principals.getPrimaryPrincipal();
String userId = user.getId();
//用户权限列表
List<String> permsSet = UserUtil.getUserAllPermsByUserId(userId);
if(CollUtil.isNotEmpty(permsSet)){
info.addStringPermissions(permsSet);
}
//用户角色列表
List<String> rolesSet = UserUtil.getUserRolesByUserId(userId);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addStringPermissions(permsSet);
info.addRoles(rolesSet);
if(CollUtil.isNotEmpty(rolesSet)){
info.addRoles(rolesSet);
}
return info;
}

@ -187,7 +187,7 @@ public class UserTokenUtil {
return;
}
try {
// 获得要出用户
// 获得要退出用户
String userId = getUserIdByToken(token);
UserModel user = UserUtil.getUser(userId);
if(user != null){

@ -98,23 +98,21 @@ public class SysAreaServiceImpl extends CrudServiceImpl<SysAreaMapper, SysArea,
@Override
@Transactional(rollbackFor = Exception.class)
public boolean delete(String id) {
boolean ret = super.delete(id);
// 删除子数据
// 先删除子数据
this.deleteByParentId(id);
return ret;
return super.delete(id);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean deleteAll(String[] ids) {
boolean ret = super.deleteAll(ids);
// 删除子数据
// 先删除子数据
for (String id : ids) {
this.deleteByParentId(id);
}
return ret;
return super.deleteAll(ids);
}
/**

@ -20,6 +20,7 @@ import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.enums.DictType;
import org.opsli.common.exception.ServiceException;
@ -34,6 +35,7 @@ import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.menu.entity.SysMenu;
import org.opsli.modulars.system.menu.mapper.MenuMapper;
import org.opsli.modulars.system.menu.service.IMenuService;
import org.opsli.modulars.system.role.service.IRoleMenuRefService;
import org.opsli.modulars.system.user.service.IUserRoleRefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -56,6 +58,8 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
private MenuMapper mapper;
@Autowired
private IUserRoleRefService iUserRoleRefService;
@Autowired
private IRoleMenuRefService iRoleMenuRefService;
@Override
public MenuModel getByCode(String menuCode) {
@ -88,6 +92,28 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
model.setParentId("0");
}
// 菜单有变动 直接刷新超级管理员 菜单缓存
UserModel adminUser = UserUtil.getUserByUserName(UserUtil.SUPER_ADMIN);
if(adminUser != null){
// 计数器
int cacheCount = 2;
boolean cacheRet;
cacheRet = UserUtil.refreshUserAllPerms(adminUser.getId());
if(cacheRet){
cacheCount--;
}
cacheRet = UserUtil.refreshUserMenus(adminUser.getId());
if(cacheRet){
cacheCount--;
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
return super.insert(model);
}
@ -118,15 +144,20 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
@Transactional(rollbackFor = Exception.class)
public boolean delete(String id) {
MenuModel menuModel = super.get(id);
boolean ret = super.delete(id);
if(menuModel == null){
return false;
}
// 清除缓存
this.clearCache(menuModel);
// 删除子数据
this.deleteByParentId(id);
if(ret){
// 清除缓存
this.clearCache(menuModel);
}
return ret;
// 移除权限数据
iRoleMenuRefService.delPermsByMenuIds(Convert.toList(String.class, id));
return super.delete(id);
}
@Override
@ -144,13 +175,15 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
this.clearCache(menuModel);
}
boolean ret = super.deleteAll(ids);
// 删除子数据
// 先删除子数据
for (String id : ids) {
this.deleteByParentId(id);
}
return ret;
// 移除权限数据
iRoleMenuRefService.delPermsByMenuIds(Convert.toList(String.class, ids));
return super.deleteAll(ids);
}
/**
@ -184,16 +217,21 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
* @return
*/
@Transactional(rollbackFor = Exception.class)
public void deleteByParentId(String parentId) {
public boolean deleteByParentId(String parentId) {
boolean ret = false;
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.eq(HumpUtil.humpToUnderline(MyBatisConstants.FIELD_PARENT_ID), parentId);
List<SysMenu> menuList = super.findList(queryWrapper);
for (SysMenu sysMenu : menuList) {
super.delete(sysMenu.getId());
for (SysMenu child : menuList) {
// 删除菜单数据
super.delete(child.getId());
// 移除权限数据
iRoleMenuRefService.delPermsByMenuIds(Convert.toList(String.class, child.getId()));
// 逐级删除子数据
this.deleteByParentId(sysMenu.getId());
ret = this.deleteByParentId(child.getId());
}
return ret;
}
// ============
@ -232,6 +270,21 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
}
}
}
// 菜单有变动 直接刷新超级管理员 菜单缓存
UserModel adminUser = UserUtil.getUserByUserName(UserUtil.SUPER_ADMIN);
if(adminUser != null){
cacheCount += 2;
cacheRet = UserUtil.refreshUserAllPerms(adminUser.getId());
if(cacheRet){
cacheCount--;
}
cacheRet = UserUtil.refreshUserMenus(adminUser.getId());
if(cacheRet){
cacheCount--;
}
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败

@ -15,6 +15,7 @@
*/
package org.opsli.modulars.system.menu.web;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
@ -34,6 +35,7 @@ import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.annotation.EnableLog;
import org.opsli.common.annotation.RequiresPermissionsCus;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.general.StartPrint;
@ -72,42 +74,31 @@ public class MenuRestController extends BaseRestController<SysMenu, MenuModel, I
/**
* -
*
*
* @return ResultVo
*/
@ApiOperation(value = "根据 获得用户 菜单 - 权限", notes = "根据 获得用户 菜单 - 权限")
@Override
public ResultVo<?> getMenuAndPermsTree() {
// 菜单集合
List<MenuModel> menuModelList = null;
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
// 判断是否是超级管理员,如果是 则显示全部菜单 否则显示有权限菜单
UserModel user = UserUtil.getUser();
// 获得全量数据
if(StringUtils.equals(UserUtil.SUPER_ADMIN, user.getUsername())){
List<SysMenu> menuList = IService.findList(queryBuilder.build());
menuModelList = WrapperUtil.transformInstance(menuList, modelClazz);
}else {
List<MenuModel> menuListByUserId = UserUtil.getMenuListByUserId(user.getId());
if(menuListByUserId != null){
// 这里有 ehcache的坑 需要深克隆再操作
menuModelList = WrapperUtil.cloneTransformInstance(menuListByUserId
,modelClazz);
}
List<String> perms = UserUtil.getUserAllPermsByUserId(user.getId());
if(perms != null){
QueryWrapper<SysMenu> wrapper = queryBuilder.build();
wrapper.in("menu_code", perms);
List<SysMenu> sysMenus = IService.findList(wrapper);
List<MenuModel> menuModels = WrapperUtil.transformInstance(sysMenus, modelClazz);
if(menuModelList != null){
menuModelList.addAll(menuModels);
}
}
// 获得当前用户菜单
List<MenuModel> menuModelList = UserUtil.getMenuListByUserId(user.getId());
if(CollUtil.isEmpty(menuModelList)){
// 用户暂无角色菜单,请设置后登录
throw new ServiceException(SystemMsg.EXCEPTION_USER_MENU_NOT_NULL);
}
if(menuModelList == null){
return ResultVo.error("菜单为空");
// 获得当前用户权限
List<String> perms = UserUtil.getUserAllPermsByUserId(user.getId());
if(CollUtil.isNotEmpty(perms)){
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> wrapper = queryBuilder.build();
wrapper.in("menu_code", perms);
List<SysMenu> sysMenus = IService.findList(wrapper);
List<MenuModel> menuModels = WrapperUtil.transformInstance(sysMenus, MenuModel.class);
menuModelList.addAll(menuModels);
}
//配置
@ -149,6 +140,9 @@ public class MenuRestController extends BaseRestController<SysMenu, MenuModel, I
/**
*
*
*
*
* @return ResultVo
*/
@ApiOperation(value = "当前登陆用户菜单", notes = "当前登陆用户菜单")
@ -158,11 +152,9 @@ public class MenuRestController extends BaseRestController<SysMenu, MenuModel, I
// 获得用户 对应菜单
List<MenuModel> menuList = UserUtil.getMenuListByUserId(user.getId());
if(menuList == null){
return ResultVo.error(
SystemMsg.EXCEPTION_USER_MENU_NOT_NULL.getCode(),
SystemMsg.EXCEPTION_USER_MENU_NOT_NULL.getMessage());
if(CollUtil.isEmpty(menuList)){
// 用户暂无角色菜单,请设置后登录
throw new ServiceException(SystemMsg.EXCEPTION_USER_MENU_NOT_NULL);
}
// 这里有坑 如果 为 菜单数据 且 组件(Component)地址为空 不会跳转到主页 也不报错

@ -136,7 +136,6 @@ public class SysOrgServiceImpl extends CrudServiceImpl<SysOrgMapper, SysOrg, Sys
@Override
@Transactional(rollbackFor = Exception.class)
public boolean delete(String id) {
boolean ret;
if(StringUtils.isEmpty(id)){
return false;
}
@ -144,10 +143,10 @@ public class SysOrgServiceImpl extends CrudServiceImpl<SysOrgMapper, SysOrg, Sys
// 如果有组织还在被引用 则不允许操作该组织
this.validationUsedByDel(Collections.singletonList(id));
ret = super.delete(id);
// 删除子数据
// 先删除子数据
this.deleteByParentId(id);
return ret;
return super.delete(id);
}
@Override
@ -161,13 +160,12 @@ public class SysOrgServiceImpl extends CrudServiceImpl<SysOrgMapper, SysOrg, Sys
// 如果有组织还在被引用 则不允许操作该组织
this.validationUsedByDel(Convert.toList(String.class, ids));
ret = super.deleteAll(ids);
// 删除子数据
// 先删除子数据
for (String id : ids) {
this.deleteByParentId(id);
}
return ret;
return super.deleteAll(ids);
}
/**

@ -43,4 +43,11 @@ public interface IRoleMenuRefService {
*/
boolean setPerms(String roleId,String[] permsIds);
/**
* ID
* @param menuIds ID
* @return boolean
*/
boolean delPermsByMenuIds(List<String> menuIds);
}

@ -88,6 +88,14 @@ public class RoleMenuRefServiceImpl extends ServiceImpl<RoleMenuRefMapper,SysRol
return true;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean delPermsByMenuIds(List<String> menuIds){
QueryWrapper<SysRoleMenuRef> queryWrapper = new QueryWrapper<>();
queryWrapper.in("menu_id", menuIds);
return this.remove(queryWrapper);
}
// =========================
/**

@ -41,6 +41,7 @@ import org.opsli.core.persistence.querybuilder.chain.TenantHandler;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.menu.entity.SysMenu;
import org.opsli.modulars.system.menu.service.IMenuService;
import org.opsli.modulars.system.user.entity.SysUser;
import org.opsli.modulars.system.user.entity.SysUserAndOrg;
import org.opsli.modulars.system.user.mapper.UserMapper;
@ -64,6 +65,8 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
@Autowired(required = false)
private UserMapper mapper;
@Autowired
private IMenuService iMenuService;
@Override
@Transactional(rollbackFor = Exception.class)
@ -297,14 +300,56 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
@Override
public List<String> getAllPerms(String userId) {
List<String> perms = mapper.queryAllPerms(userId);
UserModel userModel = this.get(userId);
if(userModel == null){
return new ArrayList<>();
}
List<String> perms;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
perms = Lists.newArrayList();
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.notIn("parent_id", -1);
queryWrapper.eq("type", '2');
queryWrapper.eq("hidden", '0');
List<SysMenu> menuList = iMenuService.findList(queryWrapper);
for (SysMenu sysMenu : menuList) {
perms.add(sysMenu.getMenuCode());
}
}else{
perms = mapper.queryAllPerms(userId);
}
// 去重
return new ArrayList<>(new LinkedHashSet<>(perms));
}
@Override
public List<MenuModel> getMenuListByUserId(String userId) {
List<SysMenu> menuList = mapper.findMenuListByUserId(userId);
UserModel userModel = this.get(userId);
if(userModel == null){
return new ArrayList<>();
}
List<SysMenu> menuList;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.notIn("parent_id", -1);
queryWrapper.in("type", '1', '3');
queryWrapper.eq("hidden", '0');
menuList = iMenuService.findList(queryWrapper);
}else{
menuList = mapper.findMenuListByUserId(userId);
}
return WrapperUtil.transformInstance(menuList, MenuModel.class);
}

Loading…
Cancel
Save