diff --git a/src/main/java/com/codesdream/ase/component/json/model/JsonableFPC.java b/src/main/java/com/codesdream/ase/component/json/model/JsonableFPC.java new file mode 100644 index 0000000..cd32c79 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/json/model/JsonableFPC.java @@ -0,0 +1,30 @@ +package com.codesdream.ase.component.json.model; + +import com.codesdream.ase.model.permission.FunctionalPermissionContainer; +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.NoArgsConstructor; +import springfox.documentation.spring.web.json.Json; + +@Data +@NoArgsConstructor +@ApiModel("功能性权限容器") +public class JsonableFPC { + private Integer id; + + private String name; + + private String description; + + private boolean enabled; + + private boolean deleted; + + public JsonableFPC(FunctionalPermissionContainer fpc){ + this.id = fpc.getId(); + this.name = fpc.getName(); + this.description = fpc.getDescription(); + this.enabled = fpc.isEnabled(); + this.deleted = fpc.isDeleted(); + } +} diff --git a/src/main/java/com/codesdream/ase/component/json/model/JsonableRoleList.java b/src/main/java/com/codesdream/ase/component/json/model/JsonableRoleList.java new file mode 100644 index 0000000..8046070 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/json/model/JsonableRoleList.java @@ -0,0 +1,24 @@ +package com.codesdream.ase.component.json.model; + +import com.codesdream.ase.model.permission.FunctionalPermissionContainer; +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.HashSet; +import java.util.Set; + +@Data +@NoArgsConstructor +@ApiModel("功能性权限容器所属角色集合") +public class JsonableRoleList { + private Integer id; + private Set roles = new HashSet<>(); + + public JsonableRoleList(FunctionalPermissionContainer fpc){ + this.id = fpc.getId(); + if(fpc.getRoles() != null) { + this.roles.addAll(fpc.getRoles()); + } + } +} diff --git a/src/main/java/com/codesdream/ase/component/json/model/JsonableSPC.java b/src/main/java/com/codesdream/ase/component/json/model/JsonableSPC.java new file mode 100644 index 0000000..124f9ae --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/json/model/JsonableSPC.java @@ -0,0 +1,30 @@ +package com.codesdream.ase.component.json.model; + +import com.codesdream.ase.model.permission.ScopePermissionContainer; +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@ApiModel("范围性权限容器") +public class JsonableSPC { + private Integer id; + + private String name; + + private String description; + + private boolean enabled; + + private boolean deleted; + + public JsonableSPC(ScopePermissionContainer spc){ + this.id = spc.getId(); + this.name = spc.getName(); + this.description = spc.getDescription(); + this.enabled = spc.isEnabled(); + this.deleted = spc.isDeleted(); + } + +} diff --git a/src/main/java/com/codesdream/ase/controller/permission/FPCController.java b/src/main/java/com/codesdream/ase/controller/permission/FPCController.java new file mode 100644 index 0000000..c4cfc71 --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/permission/FPCController.java @@ -0,0 +1,77 @@ +package com.codesdream.ase.controller.permission; + +import com.codesdream.ase.component.json.model.JsonableFPC; +import com.codesdream.ase.component.json.model.JsonableRoleList; +import com.codesdream.ase.exception.badrequest.AlreadyExistException; +import com.codesdream.ase.exception.notfound.NotFoundException; +import com.codesdream.ase.model.permission.FunctionalPermissionContainer; +import com.codesdream.ase.service.PermissionService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + +@RestController +@RequestMapping(value = "pmt") +@Api(tags = "功能性权限容器接口") +public class FPCController { + + @Resource + private PermissionService permissionService; + + @GetMapping("fpc") + @ResponseStatus(HttpStatus.OK) + @ApiOperation(value = "查找功能性权限容器") + public JsonableFPC getFPC(@RequestParam(value = "id") Integer id){ + Optional fpc = permissionService.findFPC(id); + if(!fpc.isPresent()) throw new NotFoundException(id.toString()); + return new JsonableFPC(fpc.get()); + } + + @PostMapping("fpc") + @ResponseStatus(HttpStatus.CREATED) + @ApiOperation(value = "创建新的功能性权限容器") + public JsonableFPC createFPC(@RequestBody JsonableFPC fpc){ + if(permissionService.findFPC(fpc.getName()).isPresent()) throw new AlreadyExistException(fpc.getName()); + FunctionalPermissionContainer functionalPermissionContainer = new FunctionalPermissionContainer(); + + functionalPermissionContainer.setName(fpc.getName()); + functionalPermissionContainer.setDescription(fpc.getDescription()); + functionalPermissionContainer.setEnabled(fpc.isEnabled()); + functionalPermissionContainer.setDeleted(fpc.isDeleted()); + + return new JsonableFPC(permissionService.save(functionalPermissionContainer)); + } + + @GetMapping("fpcs") + @ResponseStatus(HttpStatus.OK) + @ApiOperation(value = "获得所有功能性权限容器的信息") + public Set listFPC(){ + Iterable fpcs = permissionService.findAllFPC(); + Set jsonableFPCS = new HashSet<>(); + for(FunctionalPermissionContainer fpc : fpcs){ + jsonableFPCS.add(new JsonableFPC(fpc)); + } + return jsonableFPCS; + } + + @GetMapping("fpc/role") + @ResponseStatus(HttpStatus.OK) + @ApiOperation(value = "获得功能性权限容器所属角色") + public JsonableRoleList getFPCRoleList(@RequestParam(value = "id") Integer id){ + Optional functionalPermissionContainer = + permissionService.findFPC(id); + if(!functionalPermissionContainer.isPresent()) throw new NotFoundException(id.toString()); + + return new JsonableRoleList(functionalPermissionContainer.get()); + } + + + + +} diff --git a/src/main/java/com/codesdream/ase/controller/permission/SPCController.java b/src/main/java/com/codesdream/ase/controller/permission/SPCController.java new file mode 100644 index 0000000..1a4f8f6 --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/permission/SPCController.java @@ -0,0 +1,79 @@ +package com.codesdream.ase.controller.permission; + +import com.codesdream.ase.component.json.model.JsonableSPC; +import com.codesdream.ase.component.json.model.JsonableTag; +import com.codesdream.ase.exception.badrequest.AlreadyExistException; +import com.codesdream.ase.exception.notfound.NotFoundException; +import com.codesdream.ase.model.permission.ScopePermissionContainer; +import com.codesdream.ase.model.permission.Tag; +import com.codesdream.ase.service.PermissionService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + +@RestController +@RequestMapping(value = "pmt") +@Api(tags = "范围性权限容器接口") +public class SPCController { + + @Resource + private PermissionService permissionService; + + @GetMapping(value = "spc") + @ResponseStatus(HttpStatus.OK) + @ApiOperation(value = "获得范围性权限容器信息") + public JsonableSPC getSPC(@RequestParam(value = "id") Integer id){ + Optional spc = permissionService.findSPC(id); + if(!spc.isPresent()) throw new NotFoundException(id.toString()); + + return new JsonableSPC(spc.get()); + + } + + @GetMapping(value = "spcs") + @ResponseStatus(HttpStatus.OK) + @ApiOperation(value = "获得所有的范围性权限容器信息") + public Set listSPC(){ + Iterable spcs = permissionService.findALLSPC(); + Set jsonableSPCS = new HashSet<>(); + for(ScopePermissionContainer spc : spcs){ + jsonableSPCS.add(new JsonableSPC(spc)); + } + return jsonableSPCS; + } + + @GetMapping(value = "spc/tag") + @ResponseStatus(HttpStatus.OK) + @ApiOperation(value = "查询范围性权限容器下的所有标签集合") + public Set listSPCTag(@RequestParam(value = "id") Integer id){ + Optional spc = permissionService.findSPC(id); + if(!spc.isPresent()) throw new NotFoundException(id.toString()); + + Set tags = new HashSet<>(); + for(Tag tag : spc.get().getTags()){ + tags.add(new JsonableTag(tag)); + } + return tags; + } + + @PostMapping(value = "spc") + @ResponseStatus(HttpStatus.CREATED) + @ApiOperation(value = "创建范围性权限容器") + public JsonableSPC createSPC(@RequestBody JsonableSPC spc) { + if(permissionService.findSPC(spc.getName()).isPresent()) throw new AlreadyExistException(spc.getName()); + + ScopePermissionContainer defaultSPC = permissionService.getDefaultSPC(spc.getName()); + defaultSPC.setDeleted(spc.isDeleted()); + defaultSPC.setEnabled(spc.isEnabled()); + defaultSPC.setDescription(spc.getDescription()); + + return new JsonableSPC(permissionService.save(defaultSPC)); + } + +} diff --git a/src/main/java/com/codesdream/ase/controller/PermissionController.java b/src/main/java/com/codesdream/ase/controller/permission/TagController.java similarity index 66% rename from src/main/java/com/codesdream/ase/controller/PermissionController.java rename to src/main/java/com/codesdream/ase/controller/permission/TagController.java index 400bbfa..b45a258 100644 --- a/src/main/java/com/codesdream/ase/controller/PermissionController.java +++ b/src/main/java/com/codesdream/ase/controller/permission/TagController.java @@ -1,4 +1,4 @@ -package com.codesdream.ase.controller; +package com.codesdream.ase.controller.permission; import com.codesdream.ase.component.datamanager.JsonPathParameter; import com.codesdream.ase.component.json.model.JsonablePCCList; @@ -14,18 +14,17 @@ import com.codesdream.ase.model.permission.User; import com.codesdream.ase.service.IUserService; import com.codesdream.ase.service.PermissionService; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.github.fge.jsonpatch.JsonPatch; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; -import org.apache.poi.ss.formula.functions.T; +import io.swagger.models.auth.In; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import springfox.documentation.spring.web.json.Json; import javax.annotation.Resource; +import javax.jws.soap.SOAPBinding; import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -33,8 +32,8 @@ import java.util.Set; @RestController @RequestMapping("pmt") -@Api(tags = "权限管理接口") -public class PermissionController { +@Api(tags = "标签管理接口") +public class TagController { @Resource private PermissionService permissionService; @@ -64,20 +63,18 @@ public class PermissionController { @GetMapping("tag") @ResponseStatus(HttpStatus.OK) @ApiOperation("搜索标签信息") - @ApiImplicitParam(name = "name", value = "标签名") - public JsonableTag checkTag(@RequestParam(value = "name") String name){ - Optional tagOptional = permissionService.findTag(name); + public JsonableTag checkTag(@RequestParam(value = "id") Integer id){ + Optional tagOptional = permissionService.findTag(id); if(tagOptional.isPresent()){ return new JsonableTag(tagOptional.get()); } - else throw new NotFoundException(name); + else throw new NotFoundException(id.toString()); } // 根据名字搜索标签的简要信息 @GetMapping("tags") @ResponseStatus(HttpStatus.OK) @ApiOperation("列出所有的标签信息") - @ApiImplicitParam(name = "name", value = "标签名") public Set listTag(){ Iterable tagIterable = permissionService.findAllTag(); Set jsonableTagSet = new HashSet<>(); @@ -91,10 +88,9 @@ public class PermissionController { @DeleteMapping("tag") @ResponseStatus(HttpStatus.NO_CONTENT) @ApiOperation("删除标签") - @ApiImplicitParam(name = "name", value = "标签名") - public void deleteTag(@RequestParam(value = "name") String name){ - Optional tag = permissionService.findTag(name); - if(!tag.isPresent()) throw new NotFoundException(name); + public void deleteTag(@RequestParam(value = "id") Integer id){ + Optional tag = permissionService.findTag(id); + if(!tag.isPresent()) throw new NotFoundException(id.toString()); // 检查外键关联 if(tag.get().getUsers().size() > 0) throw new RelatedObjectsExistException(); @@ -104,13 +100,12 @@ public class PermissionController { } // 根据名字搜索标签的简要信息 - @PatchMapping(path = "tag", consumes = "application/json-patch+json") + @PatchMapping(path = "tag") @ResponseStatus(HttpStatus.CREATED) @ApiOperation("修改标签属性") - @ApiImplicitParam(name = "name", value = "标签名") - public JsonableTag updateTag(@RequestParam(value = "name") String name, @RequestBody JsonPatch patch){ - Optional tag = permissionService.findTag(name); - if(!tag.isPresent()) throw new NotFoundException(name); + public JsonableTag updateTag(@RequestParam(value = "id") Integer id, @RequestBody JsonPatch patch){ + Optional tag = permissionService.findTag(id); + if(!tag.isPresent()) throw new NotFoundException(id.toString()); JsonableTag jsonableTag = new JsonableTag(tag.get()); jsonableTag = pathParameter.parsePathToObject(patch, jsonableTag); @@ -122,34 +117,42 @@ public class PermissionController { } - - @GetMapping("tag/users") @ResponseStatus(HttpStatus.OK) @ApiOperation("搜索单个标签所属用户集合信息") - public JsonableUserList getUserTag(@RequestParam(value = "name") String name){ - Optional tag = permissionService.findTag(name); - if(!tag.isPresent()) throw new NotFoundException(name); - return new JsonableUserList(tag.get()); + public Set getUserTag(@RequestParam(value = "id") Integer id){ + Optional tag = permissionService.findTag(id); + if(!tag.isPresent()) throw new NotFoundException(id.toString()); + Set jsonableUsers = new HashSet<>(); + for(User user : tag.get().getUsers()){ + jsonableUsers.add(new JsonableUser(user)); + } + return jsonableUsers; } @PutMapping("tag/users") @ApiOperation("更新索单个标签所属用户集合信息") - public JsonableUserList setUserTag(@RequestParam String name, @RequestBody JsonableUserList userList){ - Optional tag = permissionService.findTag(name); - if(!tag.isPresent()) throw new NotFoundException(name); + public Set setUserTag(@RequestParam(value = "id") Integer id, + @RequestBody JsonableUserList userList){ + Optional tag = permissionService.findTag(id); + if(!tag.isPresent()) throw new NotFoundException(id.toString()); Set userSet = new HashSet<>(userList.getUsers()); tag.get().setUsers(userService.findUsersById(userSet)); - return new JsonableUserList(permissionService.save(tag.get())); + Set jsonableUsers = new HashSet<>(); + for(User user : tag.get().getUsers()){ + jsonableUsers.add(new JsonableUser(user)); + } + return jsonableUsers; } @PostMapping("tag/users") @ApiOperation("更新单个标签所属用户集合中添加一个或多个用户") - public JsonableUserList addUserTag(@RequestParam String name, @RequestBody JsonableUserList userList){ - Optional tag = permissionService.findTag(name); - if(!tag.isPresent()) throw new NotFoundException(name); + public Set addUserTag(@RequestParam(value = "id") Integer id, + @RequestBody JsonableUserList userList){ + Optional tag = permissionService.findTag(id); + if(!tag.isPresent()) throw new NotFoundException(id.toString()); Set newUserSet = userService.findUsersById(new HashSet<>(userList.getUsers())); Set userSet = tag.get().getUsers(); @@ -157,30 +160,38 @@ public class PermissionController { userSet.addAll(newUserSet); tag.get().setUsers(userSet); - return new JsonableUserList(permissionService.save(tag.get())); + Set jsonableUsers = new HashSet<>(); + for(User user : tag.get().getUsers()){ + jsonableUsers.add(new JsonableUser(user)); + } + return jsonableUsers; } @DeleteMapping("tag/users") @ResponseStatus(HttpStatus.OK) @ApiOperation("从单个标签所属用户集合中删除一个或多个用户") - @ApiImplicitParam(name = "name", value = "标签名") - public JsonableUserList deleteUserTag(@RequestParam String name, @RequestBody JsonableUserList userList){ - Optional tag = permissionService.findTag(name); - if(!tag.isPresent()) throw new NotFoundException(name); + public Set deleteUserTag(@RequestParam Integer id, + @RequestBody JsonableUserList userList){ + Optional tag = permissionService.findTag(id); + if(!tag.isPresent()) throw new NotFoundException(id.toString()); Set userSet = tag.get().getUsers(); Set deleteUserSet = userService.findUsersById(new HashSet<>(userList.getUsers())); userSet.removeAll(deleteUserSet); tag.get().setUsers(userSet); - return new JsonableUserList(permissionService.save(tag.get())); + Set jsonableUsers = new HashSet<>(); + for(User user : tag.get().getUsers()){ + jsonableUsers.add(new JsonableUser(user)); + } + return jsonableUsers; } @GetMapping("tags/users") @ResponseStatus(HttpStatus.OK) @ApiOperation("搜索多个标签所属用户集合信息") - public Set getUserTags(@RequestParam(value = "name") List names){ - Set tagSet = permissionService.findTags(names); + public Set getUserTags(@RequestParam(value = "id") List ids){ + Set tagSet = permissionService.findTags(ids); Set userSet = new HashSet<>(); Set jsonableUsers = new HashSet<>(); for(Tag tag : tagSet){ @@ -195,9 +206,9 @@ public class PermissionController { @GetMapping("tag/pcc") @ResponseStatus(HttpStatus.OK) @ApiOperation("获取标签所含权限容器集合列表") - public JsonablePCCList getPCCTag(@RequestParam(value = "name") String name){ - Optional tagOptional = permissionService.findTag(name); - if(!tagOptional.isPresent()) throw new NotFoundException(name); + public JsonablePCCList getPCCTag(@RequestParam(value = "id") Integer id){ + Optional tagOptional = permissionService.findTag(id); + if(!tagOptional.isPresent()) throw new NotFoundException(id.toString()); return new JsonablePCCList(tagOptional.get()); } @@ -205,9 +216,9 @@ public class PermissionController { @PostMapping("tag/pcc") @ResponseStatus(HttpStatus.CREATED) @ApiOperation("在指定标签的权限列表中添加一个或多个权限容器") - public JsonablePCCList addPCCTag(@RequestParam(value = "name") String name, JsonablePCCList jsonablePCCList){ - Optional tagOptional = permissionService.findTag(name); - if(!tagOptional.isPresent()) throw new NotFoundException(name); + public JsonablePCCList addPCCTag(@RequestParam(value = "id") Integer id, JsonablePCCList jsonablePCCList){ + Optional tagOptional = permissionService.findTag(id); + if(!tagOptional.isPresent()) throw new NotFoundException(id.toString()); Set pccs = tagOptional.get().getPermissionContainersCollections(); pccs.addAll(permissionService.findPCCs(new HashSet(jsonablePCCList.getPccIdList()))); diff --git a/src/main/java/com/codesdream/ase/service/IPermissionService.java b/src/main/java/com/codesdream/ase/service/IPermissionService.java index fb875ab..5c82bc8 100644 --- a/src/main/java/com/codesdream/ase/service/IPermissionService.java +++ b/src/main/java/com/codesdream/ase/service/IPermissionService.java @@ -3,10 +3,7 @@ package com.codesdream.ase.service; import com.codesdream.ase.model.permission.*; import javafx.util.Pair; -import java.util.Collection; -import java.util.List; -import java.util.Optional; -import java.util.Set; +import java.util.*; public interface IPermissionService { @@ -27,17 +24,21 @@ public interface IPermissionService { // 列出所有的标签 Iterable findAllTag(); - Set findTags(List names); + Set findTags(List ids); // 查找功能性权限容器 Optional findFPC(String name); + Iterable findAllFPC(); + // 查找范围性权限容器 Optional findSPC(String name); // 根据id查找功能性权限容器 Optional findFPC(int id); + Iterable findALLSPC(); + // 根据id查找范围性权限容器 Optional findSPC(int id); diff --git a/src/main/java/com/codesdream/ase/service/PermissionService.java b/src/main/java/com/codesdream/ase/service/PermissionService.java index 6ddea4f..9c4c520 100644 --- a/src/main/java/com/codesdream/ase/service/PermissionService.java +++ b/src/main/java/com/codesdream/ase/service/PermissionService.java @@ -9,7 +9,6 @@ import com.codesdream.ase.repository.permission.PermissionContainersCollectionRe import com.codesdream.ase.repository.permission.ScopePermissionContainerRepository; import com.codesdream.ase.repository.permission.TagRepository; import javafx.util.Pair; -import org.apache.poi.ss.formula.functions.T; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -75,11 +74,11 @@ public class PermissionService implements IPermissionService { } @Override - public Set findTags(List names) { + public Set findTags(List ids) { Set tagSet = new HashSet<>(); - for(String name : names){ - Optional tag = findTag(name); - if(!tag.isPresent()) throw new NotFoundException(name); + for(Integer id : ids){ + Optional tag = findTag(id); + if(!tag.isPresent()) throw new NotFoundException(id.toString()); tagSet.add(tag.get()); } return tagSet; @@ -90,6 +89,10 @@ public class PermissionService implements IPermissionService { return fpcRepository.findByName(name); } + public Iterable findAllFPC() { + return fpcRepository.findAll(); + } + @Override public Optional findSPC(String name) { return spcRepository.findByName(name); @@ -100,6 +103,11 @@ public class PermissionService implements IPermissionService { return fpcRepository.findById(id); } + @Override + public Iterable findALLSPC() { + return spcRepository.findAll(); + } + @Override public Optional findSPC(int id) { return spcRepository.findById(id);