permission

This commit is contained in:
xzb 2022-10-13 17:48:52 +08:00
parent e54db545cf
commit de0bb265d8
3 changed files with 135 additions and 4 deletions

View File

@ -1,8 +1,10 @@
package club.joylink.rtss.controller.permission2;
import club.joylink.rtss.services.permission.AcPermissionService;
import club.joylink.rtss.vo.AccountVO;
import club.joylink.rtss.vo.client.PageVO;
import club.joylink.rtss.vo.permission.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -13,6 +15,8 @@ import java.util.List;
@RestController
@RequestMapping("/api/v2/permission")
public class AcPermissionController {
@Autowired
private AcPermissionService permissionService;
/**
* 创建权限
@ -21,7 +25,7 @@ public class AcPermissionController {
*/
@PostMapping("/create")
public String createPermission(@RequestBody CreatePermissionReqVo req, @RequestAttribute AccountVO user) {
return null;
return this.permissionService.createPermission(req, user);
}
/**
@ -29,7 +33,7 @@ public class AcPermissionController {
*/
@PutMapping("/basic")
public void updatePermissionBasic(@RequestBody UpdatePermissionBasicReqVo req) {
this.permissionService.updatePermissionBasic(req);
}
/**
@ -40,7 +44,7 @@ public class AcPermissionController {
*/
@PostMapping("/{permissionId}/accept/{abilityId}")
public void addAbilityToPermission(@PathVariable("permissionId") Long permissionId, @PathVariable("abilityId") Long abilityId) {
this.permissionService.addAbilityToPermission(permissionId, abilityId);
}
/**
@ -68,6 +72,7 @@ public class AcPermissionController {
public WholePermissionRspVo findWholePermission() {
return null;
}
/**
* 分页获取权限
*/

View File

@ -0,0 +1,123 @@
package club.joylink.rtss.services.permission;
import club.joylink.rtss.dao.permission.AcPermissionDAO;
import club.joylink.rtss.dao.permission.PermissionSystemAbilityDAO;
import club.joylink.rtss.dao.permission.SystemAbilityDAO;
import club.joylink.rtss.entity.permission.*;
import club.joylink.rtss.exception.BusinessExceptionAssertEnum;
import club.joylink.rtss.vo.AccountVO;
import club.joylink.rtss.vo.permission.CreatePermissionReqVo;
import club.joylink.rtss.vo.permission.UpdatePermissionBasicReqVo;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class AcPermissionService {
@Autowired
private AcPermissionDAO permissionDAO;
@Autowired
private SystemAbilityDAO abilityDAO;
@Autowired
private PermissionSystemAbilityDAO permissionSystemAbilityDAO;
/**
* 创建权限
*
* @return 如果创建成功则返回权限id
*/
@Transactional(rollbackFor = Exception.class)
public String createPermission(CreatePermissionReqVo req, AccountVO user) {
final String name = req.getName();
final String des = req.getDes();
//参数校验
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(null != name && name.trim().length() > 0, "权限名称不能为空");
//不存在同名
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(null == findByName(name), String.format("权限[name = %s]已经存在", name));
//
Permission permission = new Permission();
permission.setName(name);
permission.setName(des);
permission.setCreatorId(user.getId());
permission.setCreateTime(LocalDateTime.now());
permission.setUpdateTime(LocalDateTime.now());
permissionDAO.insertSelective(permission);
//
Permission find = findByName(name);
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(null != find, String.format("创建权限[name = %s]失败", name));
return String.valueOf(find.getId());
}
/**
* 修改权限基础信息(名称和描述)
*/
@Transactional(rollbackFor = Exception.class)
public void updatePermissionBasic(UpdatePermissionBasicReqVo req) {
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(null != req.getName() || null != req.getDes(), "权限名称和描述不能同时为空");
//
final Permission idFind = permissionDAO.selectByPrimaryKey(req.getId());
//
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(null != idFind, String.format("权限[id = %s]不存在", req.getId()));
//
Permission update = new Permission();
update.setId(idFind.getId());
if (null != req.getName()) {
final Permission nameFind = findByName(req.getName());
if (null != nameFind) {
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(nameFind.getId().compareTo(idFind.getId()) == 0, String.format("权限[name = %s]已经存在", req.getName()));
}
update.setName(req.getName());
}
if (null != req.getDes()) {
update.setDes(req.getDes());
}
update.setUpdateTime(LocalDateTime.now());
//
permissionDAO.updateByPrimaryKeySelective(update);
}
/**
* 给权限添加功能
*
* @param permissionId 权限id
* @param abilityId 权限功能id,即system_ability.id
*/
@Transactional(rollbackFor = Exception.class)
public void addAbilityToPermission(Long permissionId, Long abilityId) {
final Permission idFindPermission = permissionDAO.selectByPrimaryKey(permissionId);
final SystemAbility idFindAbility = abilityDAO.selectByPrimaryKey(abilityId);
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(null != idFindPermission, String.format("权限[id = %s]不存在", permissionId));
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(null != idFindAbility, String.format("权限系统功能[id = %s]不存在", abilityId));
//
PermissionSystemAbilityExample psaExample = new PermissionSystemAbilityExample();
psaExample.createCriteria().andPermissionIdEqualTo(permissionId).andSystemAbilityIdEqualTo(abilityId);
List<PermissionSystemAbility> psaFind = permissionSystemAbilityDAO.selectByExample(psaExample);
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(CollectionUtils.isEmpty(psaFind),String.format("权限[id = %s]已经包含功能[id = %s]",permissionId,abilityId));
//
PermissionSystemAbility permissionAddAbility = new PermissionSystemAbility();
permissionAddAbility.setPermissionId(permissionId);
permissionAddAbility.setSystemAbilityId(abilityId);
//
permissionSystemAbilityDAO.insertSelective(permissionAddAbility);
}
private Permission findByName(String name) {
PermissionExample example = new PermissionExample();
example.createCriteria().andNameEqualTo(name);
List<Permission> ps = permissionDAO.selectByExample(example);
if (!CollectionUtils.isEmpty(ps)) {
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(ps.size() == 1, String.format("权限[name = %s]不唯一", name));
return ps.get(0);
}
return null;
}
}

View File

@ -2,6 +2,8 @@ package club.joylink.rtss.vo.permission;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* 创建权限请求
*/
@ -10,6 +12,7 @@ public class CreatePermissionReqVo {
/**
* 权限名称
*/
@NotNull
private String name;
/**
* 权限描述