Java Project Code
Controller
PermissionSchemeController
1.function: API of permission scheme
2.block: Post, Get, Put, Delete
1)Post
//post_1:新增权限方案
@PostMapping
@ApiOperation(value="Add",notes="Add")
//RepnseEntity是返回的自定义数据类型,PermissionVO也是自定义的实体类型
public ResponseEntity save(@PathVariable String company, @RequestBody PermissionVO vo){
//调用类函数,company, vo 为传递的参数
permissionSchemeService.savePermissionScheme(company, vo);
//返回的ResponseEntity中包括status, body,body中返回的也是当以的数据类型
return ResponseEntity.status(HttpStatus.CREATED)
.body(AdminResponse.result(GlobalConstant.SUCCESS,"Save Success!"));
对应permissionSchemeService.savePermissionScheme的具体实现
//savePermissionScheme的实现
@Override
public void savePermissionScheme(String company, PermissionSchemeVO vo) {
PermissionScheme permissionScheme = new PermissionScheme();
//BeanUtils:Static convenience methods for JavaBeans: for instantiating beans, checking bean property types, copying bean properties, etc.
BeanUtils.copyProperties(vo, permissionScheme);
permissionScheme.setCreateTime(new Date());
permissionScheme.setCompanyUuid(company);
//获取当前用户信息
BaseUserDetails currentLogin = AuthUtils.getCurrentLogin();
if (null != currentLogin) {
permissionScheme.setCreateUser(currentLogin.getUserId());
}
permissionSchemeMapper.insertSelective(permissionScheme);
}
//post_2:根据id复制权限方案
@PostMapping("/{id}")
@ApiOperation(value="Copy Scheme", notes="Copy Scheme")
public ResponseEntity copyScheme(@PathVariable String company, @PathVariadble("id") Long id){
ermissionSchemeService.copyPermissionScheme(company. id);
return ResponseEntity.status(HttpStatus.CREATED)
.body(AdminResponse.result(GlobalConstant.SUCCESS,"Save Success!"));
}
其对应的copyPermissionScheme代码如下
@Override
@Transactional
public void copyPermissionScheme(String company, Long id) {
//查询被复制的权限方案
PermissionScheme permissionScheme = permissionSchemeMapper.selectByPrimaryKey(id);
if (null != permissionScheme) {
//进行复制
permissionScheme.setId(null);
String newSchemeName = SchemeConstant.PERMISSION_SCHEME_COPY + permissionScheme.getName();
Example example = new ExamplePlus(PermissionScheme.class);
example.setOrderByClause("id desc");
example.createCriteria().andLike("name", newSchemeName);
List<PermissionScheme> permissionSchemes = permissionSchemeMapper.selectByExample(example);
if (!ArrayUtil.isEmpty(permissionSchemes)) {
String name = permissionSchemes.get(0).getName();
newSchemeName = SchemeConstant.PERMISSION_SCHEME_COPY + name;
//修改方案名称
permissionScheme.setName(newSchemeName);
permissionScheme.setCreateTime(new Date());
permissionSchemeMapper.insertSelective(permissionScheme);
Privilege privilege = new Privilege();
privilege.setSchemeId(id);
List<Privilege> privilegeList = privilegeMapper.select(privilege);
if (!ArrayUtil.isEmpty(privilegeList)) {
//进行权限复制
privilege.setId(null);
privilege.setSchemeId(permissionScheme.getId());
privilegeList.forEach(p -> p.setSchemeId(permissionScheme.getId()));
privilegeList.forEach(p -> p.setCreateTime(new Date()));
privilegeMapper.insertList(privilegeList);
}
}
}
2)Get
//Get_1:queryList
@GetMapping
@ApiOperation(value="queryList", notes="queryLies")
//使用自定义的PageBean数据实体类型(包括page, pageSize等属性)
public ResponseEntity<AdminResponse<PageBean<PermissionScheme>>> queryList
(@PathVariable String company, @RequestParam(value="page", defaultValue="1") Integer page,
@RequestParam(value="pageSize",defaultValue="10") Integer pageSize,
PermissionSchemeCondition condition){
return ResponseEntity.status(HttpStatus.OK)
.body(AdminResponse.result(GlobalConstant.SUCCESS, "query list get success!",
PermissionSchemeService.queryPermissionScheme(company,page,pageSize,condition)));
对应queryPermissionScheme的代码如下:
@Override
public PageBean<PermissionScheme> queryPermissionScheme(String company, Integer page, Integer pageSize,
PermissionSchemeCondition condition) {
Example example = new ExamplePlus(PermissionScheme.class);
String name = StringUtils.isEmpty(condition.getName()) ? null : condition.getName();
Example.Criteria criteria = example.createCriteria();
criteria.andLike("name", name);
criteria.andEqualTo("companyUuid", company);
example.setOrderByClause("id desc");
int totalCount = permissionSchemeMapper.selectCountByExample(example);
int offset = (page - 1) * pageSize;
PageHelper.offsetPage(offset, pageSize);
List<PermissionScheme> userList = permissionSchemeMapper.selectByExample(example);
return new PageBean<>(pageSize, totalCount, page, userList);
}
//Get_2:Get accoding to id
@GetMapping("/{id}")
@ApiOperation(value="Get by id ",notes="Get by id")
public ResponseEnity<AdminResponse<PermissionScheme>> queryById(
@PathVariable String company, @PathVariable("id") Long id){
return ResponseEntity.status(HttpStatus.OK)
.body(AdminResponse.result(GlobalConstant.SUCCESS, "query success!",
PermissionSchemeService.queryByPermissionSchemeId(company, id)));
}
queryByPermissiionSchemeId的具体实现如下
@Override
public PermissionScheme queryByPermissionSchemeId(String company, Long id) {
//两个大括号的方式初始化(本质上是匿名内部类 + 实例化代码块儿)
return permissionSchemeMapper.selectOne(new PermissionScheme() {
{
setCompanyUuid(company);
setId(id);
}
});
}
3)Put:
//Put
@PutMapping("/{id}")
@ApiOperation(value=""Update, notes="Update")
public ResponseEntity update(@PathVariable String company, @PathVariable Long id,
@RequestBody PermissionSchemeVO vo){
permissionSchemeService.updateByIdAndCompany(company,id,vo);
return ResponseEntity.status(HttpStatus.CREATED)
.body(AdminResponse.result(GlobalConstant.SUCCESSE,"update sucess!"));
}
对应的updateByIdAndCompany具体实现如下
@Override
public void updateByIdAndCompany(String company, Long id, PermissionSchemeVO vo){
Example example=new ExamplePlus(PermissionScheme.class);
Example.criteria criteria=example.createCriteria();
criteria.andEqualTo("id",id);
criteria.andEqualTo("companyuuid",company);
PermissionScheme entity=new PermissionScheme();
BeanUtils.copyProperties(vo, entity);
entity.setUpdateTime(new Date());
//获取当前用户信息
BaseUserDetails currentLogin=AuthUitls.getCurrentLogin();
if(null!=currentLogin){
entity.setUpdateUser(currentLogin.getUserId());
}
PermissionSchemeMapper.updateExampleSelective(entity,example);
}
4)Delete
@DeleteMapping("/{id}")
@ApiOperation(value="Delete", notes="Delete")
public ResponseEntity deletById(@PathVariable String company, @PathVariable("id") Long id){
PermissionSchemeService.deleteById(company, id);
return ResponseEntity.status(HttpStatus.OK)
.body(AdminResponse.result(GlobalConstant.SUCCESS,"delete success!"));
deleteById对应的具体实现如下
@Override
public void deleteById(String company, Long id){
permissionMapper.delete(new PermissionScheme(){
{
setId(id);
setCompannyuuid(company);
}
});
}
PrivilegeController
saveProjectUserRolePermission(company, vo)
updateSelective(entity)
queryByProjectUserRolePermissionId(company,id)
deleteById(company, id)
ProjectController
@Override
@Transactional
public void deleteProject(String company, String uuid) throws IOException {
List<XlyProjectUserBean> list = queryUsersByProject(company, uuid, null);
List<Privilege> privilegeList = queryProjectAllPrivilegeComponent(company, uuid);
//1. 把项目状态置为delete
Project project = queryByCompanyAndUuid(company, uuid);
// 提前获取objectUsers
String comDelProject = "com-delProject";
// 获取通知对象
NotifyObjectUser objectUsers = notificationService.getObjectUsers(company, uuid, comDelProject);
if (null == project) {
throw new CustomerException("项目不存在!");
}
project.setApplyStatus(ApplyStatus.DELETE);
updateSelective(project);
// 2. 把项目的tag 删除
projectTagService.deleteTagsByProjectUuid(company, uuid);
//3.把项目的收藏删除
projectCollectService.deleteByProjectUuid(company, uuid);
// 4.把项目的权限关联删除,(包含以下:)
//4.1 用户,组直接拥有的项目权限关联
privilegeService.deleteByProjectUuid(company, uuid);
//4.2 用户角色拥有的项目权限关联
deleteProjectUserRoleRelation(company, uuid);
//4.3 用户组角色拥有的项目权限关联
deleteProjectGroupRoleRelation(company, uuid);
// 5. 删除项目的安全级别卡片信息
securityLevelService.deleteByProject(company, uuid);
// 6. 删除项目的组件信息
for (Privilege p : privilegeList) {
productService.deleteProducts(project, p.getPermissionAppId());
}
delProjectComponents(company, uuid);
// 7. 刷新项目用户的权限
for (XlyProjectUserBean bean : list) {
permissionService.refreshPermission(company, uuid, PrivilegeTypeEnum.USER, Long.valueOf(bean.getUserId()));
}
log.info(">>>>>>发送 com-delProject 通知消息");
notificationService.sendDelProjectNotification(company, uuid, project, objectUsers);
}