Commit f47b0efd by fengshuonan

整理角色管理

parent 87d40e80
......@@ -82,9 +82,9 @@ public class ConstantFactory implements IConstantFactory {
if (ToolUtil.isEmpty(roleIds)) {
return "";
}
Integer[] roles = Convert.toIntArray(roleIds);
Long[] roles = Convert.toLongArray(roleIds);
StringBuilder sb = new StringBuilder();
for (int role : roles) {
for (Long role : roles) {
Role roleObj = roleMapper.selectById(role);
if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
sb.append(roleObj.getName()).append(",");
......@@ -137,9 +137,9 @@ public class ConstantFactory implements IConstantFactory {
@Override
public String getMenuNames(String menuIds) {
Integer[] menus = Convert.toIntArray(menuIds);
Long[] menus = Convert.toLongArray(menuIds);
StringBuilder sb = new StringBuilder();
for (int menu : menus) {
for (Long menu : menus) {
Menu menuObj = menuMapper.selectById(menu);
if (ToolUtil.isNotEmpty(menuObj) && ToolUtil.isNotEmpty(menuObj.getName())) {
sb.append(menuObj.getName()).append(",");
......
......@@ -19,13 +19,11 @@ import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.constant.cache.Cache;
import cn.stylefeng.guns.core.common.constant.dictmap.RoleDict;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.util.CacheUtil;
import cn.stylefeng.guns.modular.system.entity.Role;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.model.RoleDto;
......@@ -67,6 +65,9 @@ public class RoleController extends BaseController {
/**
* 跳转到角色列表页面
*
* @author fengshuonan
* @Date 2018/12/23 6:30 PM
*/
@RequestMapping("")
public String index() {
......@@ -75,6 +76,9 @@ public class RoleController extends BaseController {
/**
* 跳转到添加角色
*
* @author fengshuonan
* @Date 2018/12/23 6:30 PM
*/
@RequestMapping(value = "/role_add")
public String roleAdd() {
......@@ -83,22 +87,26 @@ public class RoleController extends BaseController {
/**
* 跳转到修改角色
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@Permission
@RequestMapping(value = "/role_edit")
public String roleEdit(@RequestParam Integer roleId, Model model) {
public String roleEdit(@RequestParam Long roleId) {
if (ToolUtil.isEmpty(roleId)) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
}
Role role = this.roleService.selectById(roleId);
model.addAttribute(role);
model.addAttribute("pName", ConstantFactory.me().getSingleRoleName(role.getPid()));
LogObjectHolder.me().set(role);
return PREFIX + "/role_edit.html";
}
/**
* 跳转到权限分配
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@Permission
@RequestMapping(value = "/role_assign/{roleId}")
......@@ -107,84 +115,73 @@ public class RoleController extends BaseController {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
}
model.addAttribute("roleId", roleId);
model.addAttribute("roleName", ConstantFactory.me().getSingleRoleName(roleId));
return PREFIX + "/role_assign.html";
}
/**
* 获取角色列表
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@Permission
@RequestMapping(value = "/list")
@ResponseBody
public Object list(@RequestParam(required = false) String roleName) {
List<Map<String, Object>> roles = this.roleService.selectRoles(super.getPara("roleName"));
public Object list(@RequestParam(value = "roleName", required = false) String roleName) {
List<Map<String, Object>> roles = this.roleService.selectRoles(roleName);
return super.warpObject(new RoleWarpper(roles));
}
/**
* 角色新增
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@RequestMapping(value = "/add")
@BussinessLog(value = "添加角色", key = "name", dict = RoleDict.class)
@Permission(Const.ADMIN_NAME)
@ResponseBody
public ResponseData add(Role role) {
role.setRoleId(null);
this.roleService.insert(role);
this.roleService.addRole(role);
return SUCCESS_TIP;
}
/**
* 角色修改
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@RequestMapping(value = "/edit")
@BussinessLog(value = "修改角色", key = "name", dict = RoleDict.class)
@Permission(Const.ADMIN_NAME)
@ResponseBody
public ResponseData edit(RoleDto roleDto) {
Role old = this.roleService.selectById(roleDto.getRoleId());
BeanUtil.copyProperties(roleDto, old);
this.roleService.updateById(old);
//删除缓存
CacheUtil.removeAll(Cache.CONSTANT);
this.roleService.editRole(roleDto);
return SUCCESS_TIP;
}
/**
* 删除角色
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@RequestMapping(value = "/remove")
@BussinessLog(value = "删除角色", key = "roleId", dict = RoleDict.class)
@Permission(Const.ADMIN_NAME)
@ResponseBody
public ResponseData remove(@RequestParam Long roleId) {
if (ToolUtil.isEmpty(roleId)) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
}
//不能删除超级管理员角色
if (roleId.equals(Const.ADMIN_ROLE_ID)) {
throw new ServiceException(BizExceptionEnum.CANT_DELETE_ADMIN);
}
//缓存被删除的角色名称
LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(roleId));
this.roleService.delRoleById(roleId);
//删除缓存
CacheUtil.removeAll(Cache.CONSTANT);
return SUCCESS_TIP;
}
/**
* 查看角色
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@RequestMapping(value = "/view/{roleId}")
@ResponseBody
......@@ -204,6 +201,9 @@ public class RoleController extends BaseController {
/**
* 配置权限
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@RequestMapping("/setAuthority")
@BussinessLog(value = "配置权限", key = "roleId,ids", dict = RoleDict.class)
......@@ -219,6 +219,9 @@ public class RoleController extends BaseController {
/**
* 获取角色列表
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@RequestMapping(value = "/roleTreeList")
@ResponseBody
......@@ -229,17 +232,20 @@ public class RoleController extends BaseController {
}
/**
* 获取角色列表
* 获取角色列表,通过用户id
*
* @author fengshuonan
* @Date 2018/12/23 6:31 PM
*/
@RequestMapping(value = "/roleTreeListByUserId/{userId}")
@ResponseBody
public List<ZTreeNode> roleTreeListByUserId(@PathVariable Long userId) {
User theUser = this.userService.selectById(userId);
String roleid = theUser.getRoleId();
if (ToolUtil.isEmpty(roleid)) {
String roleId = theUser.getRoleId();
if (ToolUtil.isEmpty(roleId)) {
return this.roleService.roleTreeList();
} else {
String[] strArray = roleid.split(",");
String[] strArray = roleId.split(",");
return this.roleService.roleTreeListByRoleId(strArray);
}
}
......
......@@ -28,6 +28,7 @@
<if test="condition != null">
where NAME like CONCAT('%',#{condition},'%')
</if>
order by SORT asc
</select>
<delete id="deleteRolesById">
......
package cn.stylefeng.guns.modular.system.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.constant.cache.Cache;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.util.CacheUtil;
import cn.stylefeng.guns.modular.system.entity.Relation;
import cn.stylefeng.guns.modular.system.entity.Role;
import cn.stylefeng.guns.modular.system.mapper.RelationMapper;
import cn.stylefeng.guns.modular.system.mapper.RoleMapper;
import cn.stylefeng.guns.modular.system.model.RoleDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -32,13 +43,52 @@ public class RoleService extends ServiceImpl<RoleMapper, Role> {
private RelationMapper relationMapper;
/**
* 添加角色
*
* @author fengshuonan
* @Date 2018/12/23 6:40 PM
*/
@Transactional(rollbackFor = Exception.class)
public void addRole(Role role) {
if (ToolUtil.isOneEmpty(role, role.getName(), role.getPid(), role.getDescription())) {
throw new RequestEmptyException();
}
role.setRoleId(null);
this.insert(role);
}
/**
* 编辑角色
*
* @author fengshuonan
* @Date 2018/12/23 6:40 PM
*/
@Transactional(rollbackFor = Exception.class)
public void editRole(RoleDto roleDto) {
if (ToolUtil.isOneEmpty(roleDto, roleDto.getName(), roleDto.getPid(), roleDto.getDescription())) {
throw new RequestEmptyException();
}
Role old = this.selectById(roleDto.getRoleId());
BeanUtil.copyProperties(roleDto, old);
this.updateById(old);
//删除缓存
CacheUtil.removeAll(Cache.CONSTANT);
}
/**
* 设置某个角色的权限
*
* @param roleId 角色id
* @param ids 权限的id
* @date 2017年2月13日 下午8:26:53
*/
@Transactional
@Transactional(rollbackFor = Exception.class)
public void setAuthority(Long roleId, String ids) {
// 删除该角色所有的权限
......@@ -59,13 +109,29 @@ public class RoleService extends ServiceImpl<RoleMapper, Role> {
* @author stylefeng
* @Date 2017/5/5 22:24
*/
@Transactional
@Transactional(rollbackFor = Exception.class)
public void delRoleById(Long roleId) {
if (ToolUtil.isEmpty(roleId)) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
}
//不能删除超级管理员角色
if (roleId.equals(Const.ADMIN_ROLE_ID)) {
throw new ServiceException(BizExceptionEnum.CANT_DELETE_ADMIN);
}
//缓存被删除的角色名称
LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(roleId));
//删除角色
this.roleMapper.deleteById(roleId);
// 删除该角色所有的权限
//删除该角色所有的权限
this.roleMapper.deleteRolesById(roleId);
//删除缓存
CacheUtil.removeAll(Cache.CONSTANT);
}
/**
......
Markdown is supported
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