有了权限模块,接下来就是开发权限模块下的权限点。
一、参数校验
@Getter
@Setter
@ToString
public class AclParam {
private Integer id;
@NotBlank(message = "权限点名称不可以为空")
@Length(min = 2, max = 20, message = "权限点名称长度需要在2-20个字之间")
private String name;
@NotNull(message = "必须指定权限模块")
private Integer aclModuleId;
@Length(min = 6, max = 100, message = "权限点URL长度需要在6-100个字符之间")
private String url;
@NotNull(message = "必须指定权限点的类型")
@Min(value = 1, message = "权限点类型不合法")
@Max(value = 3, message = "权限点类型不合法")
private Integer type;
@NotNull(message = "必须指定权限点的状态")
@Min(value = 0, message = "权限点状态不合法")
@Max(value = 1, message = "权限点状态不合法")
private Integer status;
@NotNull(message = "必须指定权限点的展示顺序")
private Integer seq;
@Length(min = 0, max = 200, message = "权限点备注长度需要在200个字符以内")
private String remark;
}
二、Service层
1、增加和更新权限点
2、分页展示
package com.mmall.service;
import com.google.common.base.Preconditions;
import com.mmall.beans.PageQuery;
import com.mmall.beans.PageResult;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysAclMapper;
import com.mmall.exception.ParamException;
import com.mmall.model.SysAcl;
import com.mmall.param.AclParam;
import com.mmall.util.BeanValidator;
import com.mmall.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class SysAclService {
@Autowired
SysAclMapper sysAclMapper;
@Resource
private SysLogService sysLogService;
public void save(AclParam param) {
//参数检查
BeanValidator.check(param);
if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
throw new ParamException("当前权限模块下面存在相同名称的权限点");
}
//组装
SysAcl acl = SysAcl.builder().name(param.getName()).aclModuleId(param.getAclModuleId()).url(param.getUrl())
.type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark()).build();
acl.setCode(generateCode());
acl.setOperator(RequestHolder.getCurrentUser().getUsername());
acl.setOperateTime(new Date());
acl.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
sysAclMapper.insertSelective(acl);
sysLogService.saveAclLog(null, acl);
}
public void update(AclParam param) {
//参数检查
BeanValidator.check(param);
if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
throw new ParamException("当前权限模块下面存在相同名称的权限点");
}
//取出之前的值
SysAcl before = sysAclMapper.selectByPrimaryKey(param.getId());
Preconditions.checkNotNull(before, "待更新的权限点不存在");
SysAcl after = SysAcl.builder().id(param.getId()).name(param.getName()).aclModuleId(param.getAclModuleId()).url(param.getUrl())
.type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark()).build();
after.setOperator(RequestHolder.getCurrentUser().getUsername());
after.setOperateTime(new Date());
after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
sysAclMapper.updateByPrimaryKeySelective(after);
sysLogService.saveAclLog(before, after);
}
public boolean checkExist(int aclModuleId, String name, Integer id) {
return sysAclMapper.countByNameAndAclModuleId(aclModuleId, name, id) > 0;
}
//生成唯一编码
public String generateCode() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return dateFormat.format(new Date()) + "_" + (int)(Math.random() * 100);
}
public PageResult<SysAcl> getPageByAclModuleId(int aclModuleId, PageQuery page) {
BeanValidator.check(page);
//通过id获取列表信息
int count = sysAclMapper.countByAclModuleId(aclModuleId);
if (count > 0) {
List<SysAcl> aclList = sysAclMapper.getPageByAclModuleId(aclModuleId, page);
return PageResult.<SysAcl>builder().data(aclList).total(count).build();
}
return PageResult.<SysAcl>builder().build();
}
}
3、Controller
@Controller
@RequestMapping("/sys/acl")
public class SysAclController {
private final static Logger log = LoggerFactory.getLogger(SysAclController.class);
@Autowired
SysAclService sysAclService;
@Autowired
private SysRoleService sysRoleService;
@RequestMapping("/save.json")
@ResponseBody
public JsonData saveAcl(AclParam param) {
sysAclService.save(param);
return JsonData.success();
}
@RequestMapping("/update.json")
@ResponseBody
public JsonData updateAclModule(AclParam param) {
sysAclService.update(param);
return JsonData.success();
}
@RequestMapping("/page.json")
@ResponseBody
public JsonData list(@RequestParam("aclModuleId") Integer aclModuleId, PageQuery pageQuery) {
return JsonData.success(sysAclService.getPageByAclModuleId(aclModuleId, pageQuery));
}
//通过权限ID取用户列表和角色列表
@RequestMapping("acls.json")
@ResponseBody
public JsonData acls(@RequestParam("aclId") int aclId) {
Map<String, Object> map = Maps.newHashMap();
//根据权限ID取角色列表
List<SysRole> roleList = sysRoleService.getRoleListByAclId(aclId);
map.put("roles", roleList);
//根据角色列表取用户列表
map.put("users", sysRoleService.getUserListByRoleList(roleList));
return JsonData.success(map);
}
}