博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现自己的权限管理系统(十):角色模块
阅读量:3891 次
发布时间:2019-05-23

本文共 7780 字,大约阅读时间需要 25 分钟。

一、参数校验

public class RoleParam {    private Integer id;    @NotBlank(message = "角色名称不可以为空")    @Length(min = 2, max = 20, message = "角色名称长度需要在2-20个字之间")    private String name;    @Min(value = 1, message = "角色类型不合法")    @Max(value = 2, message = "角色类型不合法")    private Integer type = 1;    @NotNull(message = "角色状态不可以为空")    @Min(value = 0, message = "角色状态不合法")    @Max(value = 1, message = "角色状态不合法")    private Integer status;    @Length(min = 0, max = 200, message = "角色备注长度需要在200个字符以内")    private String remark;}

2、Service层

@Servicepublic class SysRoleService {    @Autowired    private SysRoleMapper sysRoleMapper;    @Autowired    private SysRoleAclMapper sysRoleAclMapper;    @Autowired    private SysRoleUserMapper sysRoleUserMapper;    @Autowired    private SysUserMapper sysUserMapper;    @Resource    private SysLogService sysLogService;    public void save(RoleParam param) {        BeanValidator.check(param);        if (checkExist(param.getName(), param.getId())) {            throw new ParamException("角色名称已经存在");        }        //类名.builder().属性(data)        SysRole role = SysRole.builder().name(param.getName()).status(param.getStatus()).type(param.getType())                .remark(param.getRemark()).build();        role.setOperator(RequestHolder.getCurrentUser().getUsername());        role.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));        role.setOperateTime(new Date());        sysRoleMapper.insertSelective(role);        sysLogService.saveRoleLog(null, role);    }    private boolean checkExist(String name, Integer id) {        return sysRoleMapper.countByName(name, id)>0;    }    public void update(RoleParam param) {        BeanValidator.check(param);        if (checkExist(param.getName(), param.getId())) {            throw new ParamException("角色名称已经存在");        }        SysRole before = sysRoleMapper.selectByPrimaryKey(param.getId());        Preconditions.checkNotNull(before, "待更新的角色不存在");        SysRole after = SysRole.builder().id(param.getId()).name(param.getName()).status(param.getStatus()).type(param.getType())                .remark(param.getRemark()).build();        after.setOperator(RequestHolder.getCurrentUser().getUsername());        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));        after.setOperateTime(new Date());        sysRoleMapper.updateByPrimaryKeySelective(after);        sysLogService.saveRoleLog(before, after);    }    public void delete(RoleParam param) {        SysRole role = sysRoleMapper.selectByPrimaryKey(param.getId());        Preconditions.checkNotNull(role, "待删除的角色不存在,无法删除");        sysRoleMapper.deleteByPrimaryKey(param.getId());    }    public List
getAll() { return sysRoleMapper.getAll(); } //通过权限ID获得角色列表 public List
getRoleListByAclId(int aclId) { List
roleIdList = sysRoleAclMapper.getRoleIdListByAclId(aclId); if (CollectionUtils.isEmpty(roleIdList)) { return Lists.newArrayList(); } return sysRoleMapper.getByIdList(roleIdList); } //通过角色列表获得用户列表 public List
getUserListByRoleList(List
roleList) { if (CollectionUtils.isEmpty(roleList)) { return Lists.newArrayList(); } List
roleIdList = roleList.stream().map(role -> role.getId()).collect(Collectors.toList()); List
userIdList = sysRoleUserMapper.getUserIdListByRoleIdList(roleIdList); if (CollectionUtils.isEmpty(userIdList)) { return Lists.newArrayList(); } return sysUserMapper.getByIdList(userIdList); } //通过用户ID获得角色列表 public List
getRoleListByUserId(int userId) { List
roleIdList = sysRoleUserMapper.getRoleIdListByUserId(userId); if (CollectionUtils.isEmpty(roleIdList)) { return Lists.newArrayList(); } return sysRoleMapper.getByIdList(roleIdList); }}

3、Controler

package com.mmall.controller;import com.google.common.collect.Lists;import com.google.common.collect.Maps;import com.mmall.common.JsonData;import com.mmall.model.SysUser;import com.mmall.param.RoleParam;import com.mmall.service.*;import com.mmall.util.StringUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import java.util.List;import java.util.Map;import java.util.Set;import java.util.stream.Collectors;@Controller@RequestMapping("/sys/role")public class SysRoleController {    @Autowired    private SysRoleService sysRoleService;    @Autowired    private SysTreeService sysTreeService;    @Autowired    private SysRoleUserService sysRoleUserService;    @Autowired    private SysRoleAclService sysRoleAclService;    @Autowired    private SysUserService sysUserService;    @RequestMapping("role.page")    public ModelAndView page() {        return new ModelAndView("role");    }    @RequestMapping("/save.json")    @ResponseBody    public JsonData saveRole(RoleParam param) {        sysRoleService.save(param);        return JsonData.success();    }    @RequestMapping("/update.json")    @ResponseBody    public JsonData updateRole(RoleParam param) {        sysRoleService.update(param);        return JsonData.success();    }    @RequestMapping("/delete.json")    @ResponseBody    public JsonData deleteRole(RoleParam param) {        sysRoleService.delete(param);        return JsonData.success();    }    @RequestMapping("/list.json")    @ResponseBody    public JsonData list() {        return JsonData.success(sysRoleService.getAll());    }    @RequestMapping("/roleTree.json")    @ResponseBody    public JsonData roleTree(@RequestParam("roleId") int roleId) {        return JsonData.success(sysTreeService.roleTree(roleId));    }    //保存角色与权限点的关系    @RequestMapping("/changeAcls.json")    @ResponseBody    public JsonData changeAcls(            @RequestParam("roleId") int roleId,            @RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds) {        List
aclIdList = StringUtil.splitToListInt(aclIds); sysRoleAclService.changeRoleAcls(roleId, aclIdList); return JsonData.success(); } //保存角色与用户的关系 @RequestMapping("/changeUsers.json") @ResponseBody public JsonData changeUsers( @RequestParam("roleId") int roleId, @RequestParam(value = "userIds", required = false, defaultValue = "") String userIds) { //把UserId转为list List
userIdList = StringUtil.splitToListInt(userIds); sysRoleUserService.changeRoleUsers(roleId, userIdList); return JsonData.success(); } //加载角色用户数据 @RequestMapping("/users.json") @ResponseBody public JsonData users(@RequestParam("roleId") int roleId) { //通过roleId获取Userlist List
selectedUserList = sysRoleUserService.getListByRoleId(roleId); //获取所有的user,然后减去前面获取的,就剩下没有获取的 List
allUserList = sysUserService.getAll(); List
unselectedUserList = Lists.newArrayList(); //优化for循环 Set
selectedUserIdSet = selectedUserList.stream().map(sysUser -> sysUser.getId()).collect(Collectors.toSet()); //如果是有效的并且在未选中的列表中就添加 for(SysUser sysUser : allUserList) { if (sysUser.getStatus() == 1 && !selectedUserIdSet.contains(sysUser.getId())) { unselectedUserList.add(sysUser); } } /*当选择的用户的状态已经无效就不展示,使用流式过滤 selectedUserList = selectedUserList.stream().filter(sysUser -> sysUser.getStatus() != 1).collect(Collectors.toList()); */ Map
> map = Maps.newHashMap(); map.put("selected", selectedUserList); map.put("unselected", unselectedUserList); return JsonData.success(map); }}

 

转载地址:http://ctphn.baihongyu.com/

你可能感兴趣的文章
5年开发经验的我被几条朋友圈打击到,点燃自己冲击阿里面经!
查看>>
5年工作经验的我放弃安逸,一份来自腾讯魔鬼面试的终极考验!
查看>>
学JAVA吗同学,这篇Sping boot 确定不了解下么?
查看>>
(3年+offer)华为技术岗面试初面+综合面试经验总结
查看>>
男默女泪,努力复习的我终于通过社招进入BAT工作了!(JAVA+JVM+框架+中间件+Spring干货分享)
查看>>
Python 导包
查看>>
dok_matrix
查看>>
theano 后端爆内存
查看>>
os.environ 和 keras.json
查看>>
后台面试经典问题-手写LRU算法
查看>>
Part-Guided Attention Learning for Vehicle Instance Retrieval
查看>>
Deep Residual Learning for Image Recognition
查看>>
Bag of Tricks and A Strong Baseline for Deep Person Re-identification
查看>>
vue+flask实现视频目标检测yolov5
查看>>
关于BigInteger
查看>>
UIScrollView不能响应UITouch事件
查看>>
iOS TextFiled 文本密码切换 光标偏移解决
查看>>
iOS 当前应用所占内存和设备可用内存
查看>>
iOS 文件属性
查看>>
UIView的layoutSubviews和drawRect方法何时调用
查看>>