mirror of
https://gitee.com/republicline/rax-remote-v2.git
synced 2025-08-24 04:04:57 +08:00
角色管理
This commit is contained in:
parent
4c33b1a1cf
commit
b923213fb2
|
@ -68,8 +68,8 @@ public class SysRoleController {
|
|||
@PostMapping
|
||||
@PreAuthorize("@pms.hasPermission('sys_role_add')")
|
||||
@CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
|
||||
public R save(@Valid @RequestBody SysRole sysRole) {
|
||||
return R.ok(sysRoleService.save(sysRole));
|
||||
public R save(@Valid SysRole sysRole, String menuIds) {
|
||||
return sysRoleService.saveRole(sysRole, menuIds);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,6 +119,11 @@ public class SysRoleController {
|
|||
.like(StrUtil.isNotBlank(role.getRoleName()), SysRole::getRoleName, role.getRoleName())));
|
||||
}
|
||||
|
||||
@GetMapping("/tree")
|
||||
public R getRoleTree(Page page, String roleName) {
|
||||
return sysRoleService.getRoleTree(page, roleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新角色菜单
|
||||
* @param roleVo 角色对象
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
package com.rax.admin.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.rax.admin.api.entity.SysRole;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -24,4 +27,6 @@ public interface SysRoleMapper extends BaseMapper<SysRole> {
|
|||
*/
|
||||
List<SysRole> listRolesByUserId(Long userId);
|
||||
|
||||
List<SysRole> getRoleTree(@Param("page") Page page, @Param("roleName") String roleName);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.rax.admin.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.rax.admin.api.entity.SysRole;
|
||||
import com.rax.admin.api.vo.RoleExcelVO;
|
||||
|
@ -19,6 +20,8 @@ import java.util.List;
|
|||
*/
|
||||
public interface SysRoleService extends IService<SysRole> {
|
||||
|
||||
R saveRole(SysRole sysRole, String menuIds);
|
||||
|
||||
/**
|
||||
* 通过用户ID,查询角色信息
|
||||
* @param userId
|
||||
|
@ -48,6 +51,8 @@ public interface SysRoleService extends IService<SysRole> {
|
|||
*/
|
||||
Boolean updateRoleMenus(RoleVO roleVo);
|
||||
|
||||
R getRoleTree(Page page, String roleName);
|
||||
|
||||
/**
|
||||
* 导入角色
|
||||
* @param excelVOList 角色列表
|
||||
|
|
|
@ -2,7 +2,10 @@ package com.rax.admin.service.impl;
|
|||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.rax.admin.api.entity.SysRole;
|
||||
import com.rax.admin.api.entity.SysRoleMenu;
|
||||
|
@ -20,8 +23,10 @@ import lombok.AllArgsConstructor;
|
|||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
@ -41,8 +46,28 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
private SysRoleMenuService roleMenuService;
|
||||
|
||||
@Override
|
||||
public R saveRole(SysRole sysRole, String menuIds) {
|
||||
boolean saveStatus = save(sysRole);
|
||||
if (saveStatus) {
|
||||
if (StringUtils.hasText(menuIds)) {
|
||||
List<SysRoleMenu> roleMenuList = Arrays.stream(menuIds.split(StrUtil.COMMA)).map(menuId -> {
|
||||
SysRoleMenu roleMenu = new SysRoleMenu();
|
||||
roleMenu.setRoleId(sysRole.getRoleId());
|
||||
roleMenu.setMenuId(Long.valueOf(menuId));
|
||||
return roleMenu;
|
||||
}).toList();
|
||||
roleMenuService.saveBatch(roleMenuList);
|
||||
}
|
||||
return R.ok("角色保存成功");
|
||||
} else {
|
||||
return R.failed("角色保存失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过用户ID,查询角色信息
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
|
@ -53,8 +78,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
/**
|
||||
* 根据角色ID 查询角色列表,注意缓存删除
|
||||
*
|
||||
* @param roleIdList 角色ID列表
|
||||
* @param key 缓存key
|
||||
* @param key 缓存key
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
|
@ -65,6 +91,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
/**
|
||||
* 通过角色ID,删除角色,并清空角色菜单缓存
|
||||
*
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
|
@ -72,12 +99,13 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean removeRoleByIds(Long[] ids) {
|
||||
roleMenuService
|
||||
.remove(Wrappers.<SysRoleMenu>update().lambda().in(SysRoleMenu::getRoleId, CollUtil.toList(ids)));
|
||||
.remove(Wrappers.<SysRoleMenu>update().lambda().in(SysRoleMenu::getRoleId, CollUtil.toList(ids)));
|
||||
return this.removeBatchByIds(CollUtil.toList(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角色菜单列表
|
||||
*
|
||||
* @param roleVo 角色&菜单列表
|
||||
* @return
|
||||
*/
|
||||
|
@ -86,9 +114,15 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
return roleMenuService.saveRoleMenus(roleVo.getRoleId(), roleVo.getMenuIds());
|
||||
}
|
||||
|
||||
public R getRoleTree(Page page, String roleName) {
|
||||
baseMapper.getRoleTree(page, roleName);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入角色
|
||||
* @param excelVOList 角色列表
|
||||
*
|
||||
* @param excelVOList 角色列表
|
||||
* @param bindingResult 错误信息列表
|
||||
* @return ok fail
|
||||
*/
|
||||
|
@ -105,8 +139,8 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
Set<String> errorMsg = new HashSet<>();
|
||||
// 检验角色名称或者角色编码是否存在
|
||||
boolean existRole = roleList.stream()
|
||||
.anyMatch(sysRole -> excel.getRoleName().equals(sysRole.getRoleName())
|
||||
|| excel.getRoleCode().equals(sysRole.getRoleCode()));
|
||||
.anyMatch(sysRole -> excel.getRoleName().equals(sysRole.getRoleName())
|
||||
|| excel.getRoleCode().equals(sysRole.getRoleCode()));
|
||||
|
||||
if (existRole) {
|
||||
errorMsg.add(MsgUtils.getMessage(ErrorCodes.SYS_ROLE_NAMEORCODE_EXISTING, excel.getRoleName(),
|
||||
|
@ -116,8 +150,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
// 数据合法情况
|
||||
if (CollUtil.isEmpty(errorMsg)) {
|
||||
insertExcelRole(excel);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// 数据不合法情况
|
||||
errorMessageList.add(new ErrorMessage(excel.getLineNum(), errorMsg));
|
||||
}
|
||||
|
@ -130,6 +163,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
/**
|
||||
* 查询全部的角色
|
||||
*
|
||||
* @return list
|
||||
*/
|
||||
@Override
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
server:
|
||||
# port: 6379
|
||||
port: 9999
|
||||
servlet:
|
||||
context-path: /admin
|
||||
|
@ -12,12 +13,17 @@ spring:
|
|||
data:
|
||||
redis:
|
||||
host: 192.168.244.129
|
||||
# host: localhost
|
||||
# port: 6378
|
||||
# 数据库相关配置
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
username: root
|
||||
password: root
|
||||
url: jdbc:mysql://192.168.244.129:3306/rax_backend?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&nullCatalogMeansCurrent=true
|
||||
# username: root
|
||||
# password: Xg137839
|
||||
# url: jdbc:mysql://localhost:3306/rax_backend?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&nullCatalogMeansCurrent=true
|
||||
# 定时任务属性配置
|
||||
quartz:
|
||||
properties:
|
||||
|
@ -104,7 +110,14 @@ vital-sign:
|
|||
host: 192.168.244.129:27017
|
||||
password: root
|
||||
username: root
|
||||
# host: localhost:27017
|
||||
# password: Xg137839mg
|
||||
# username: useradmin
|
||||
|
||||
mysql:
|
||||
host: 192.168.244.129:3306
|
||||
password: root
|
||||
username: root
|
||||
# host: localhost:3306
|
||||
# password: Xg137839
|
||||
# username: root
|
||||
|
|
|
@ -27,4 +27,26 @@
|
|||
AND sys_role.del_flag = '0'
|
||||
and sys_user_role.user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<select id="getRoleTree" resultMap="BaseResultMap">
|
||||
select r.role_id,
|
||||
r.role_name,
|
||||
r.role_code,
|
||||
r.role.desc,
|
||||
r.create_time,
|
||||
r.update_time,
|
||||
GROUP_CONCAT(m.`name`) as menu
|
||||
from sys_role r
|
||||
left join
|
||||
sys_role_menu rm on r.role_id = rm.role_id
|
||||
left join
|
||||
sys_menu m on m.menu_id = rm.menu_id
|
||||
where r.del_flag = 0
|
||||
<if test="roleName != null and roleName != ''">
|
||||
<bind name="bindName" value="'%' + roleName + '%'"/>
|
||||
and r.role_name like '#{bindName}'
|
||||
</if>
|
||||
GROUP BY r.role_id
|
||||
ORDER BY r.create_time desc limit #{page.current}, #{page.size}
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue
Block a user