diff --git a/src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java b/src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java index 19c2577..5d40330 100644 --- a/src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java +++ b/src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java @@ -19,36 +19,31 @@ public class FunctionalPermissionContainer { private int id; // 功能性权限容器名 - @Column(nullable = false, unique = true) - private String name; + @Column(unique = true) + private String name = ""; // 功能性权限容器解释 - private String description; + private String description = ""; // 对应访问控制角色列表 @ElementCollection @Column(nullable = false) - private List roles; + private List roles = new LinkedList<>(); // 是否启用 @Column(nullable = false) - private boolean enabled; + private boolean enabled = true; // 是否删除 @Column(nullable = false) - private boolean deleted; + private boolean deleted = false; + + public FunctionalPermissionContainer(String name) { + this.name = name; + } public FunctionalPermissionContainer(){ - initDefault(); + } - public FunctionalPermissionContainer(String name, String description) { - this.name = name; - this.description = description; - initDefault(); - } - - private void initDefault(){ - this.roles = new LinkedList<>(); - } } diff --git a/src/main/java/com/codesdream/ase/model/permission/PermissionContainersCollection.java b/src/main/java/com/codesdream/ase/model/permission/PermissionContainersCollection.java index 9574851..d1fc295 100644 --- a/src/main/java/com/codesdream/ase/model/permission/PermissionContainersCollection.java +++ b/src/main/java/com/codesdream/ase/model/permission/PermissionContainersCollection.java @@ -2,6 +2,7 @@ package com.codesdream.ase.model.permission; import lombok.Data; import javax.persistence.*; +import java.util.HashSet; import java.util.Set; /** @@ -16,13 +17,21 @@ public class PermissionContainersCollection { private int id; // 权限容器集合名称 - @Column(nullable = false) - private String name; + @Column(unique = true) + private String name = ""; // 权限容器集合概述 - private String description; + private String description = ""; // 对应功能性权限容器与范围性权限容器关联对 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private Set functionalScopeRelations; + private Set functionalScopeRelations = new HashSet<>(); + + public PermissionContainersCollection(String name){ + this.name = name; + } + + public PermissionContainersCollection(){ + + } } diff --git a/src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java b/src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java index 87e4485..5c60e36 100644 --- a/src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java +++ b/src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java @@ -3,6 +3,7 @@ package com.codesdream.ase.model.permission; import lombok.Data; import javax.persistence.*; +import java.util.LinkedList; import java.util.List; import java.util.Set; @@ -14,17 +15,25 @@ public class ScopePermissionContainer { @GeneratedValue(strategy = GenerationType.AUTO) private int id; - @Column(unique = true, nullable = false) - private String name; + @Column(unique = true) + private String name = ""; - private String description; + private String description = ""; - @OneToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) - private List tags; + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH}, fetch = FetchType.LAZY) + private List tags = new LinkedList<>(); @Column(nullable = false) - private boolean enabled; + private boolean enabled = true; @Column(nullable = false) - private boolean deleted; + private boolean deleted = false; + + public ScopePermissionContainer(String name){ + this.name = name; + } + + public ScopePermissionContainer(){ + + } } diff --git a/src/main/java/com/codesdream/ase/model/permission/Tag.java b/src/main/java/com/codesdream/ase/model/permission/Tag.java index 6b222aa..ef59aa5 100644 --- a/src/main/java/com/codesdream/ase/model/permission/Tag.java +++ b/src/main/java/com/codesdream/ase/model/permission/Tag.java @@ -19,40 +19,34 @@ public class Tag { private int id; // 标签名 - @Column(nullable = false, unique = true) - private String name; + @Column(unique = true) + private String name = ""; // 标签解释 - private String description; + private String description = ""; // 标签关联用户 - @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) - private Set users; + @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH}, fetch = FetchType.LAZY) + private Set users = new HashSet<>(); // 启用标志 @Column(nullable = false) - private boolean enabled; + private boolean enabled = true; // 删除标志 @Column(nullable = false) - private boolean deleted; + private boolean deleted = false; // 对应权限容器集合 - @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY) - private Set permissionContainersCollections; + @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH}, fetch = FetchType.LAZY) + private Set permissionContainersCollections = new HashSet<>(); - public Tag(String name, String description) { + public Tag(String name) { this.name = name; - this.description = description; - this.deleted = false; - this.enabled = true; - this.users = new HashSet(); } - public Tag() { - this.deleted = false; - this.enabled = true; - this.users = new HashSet(); + public Tag(){ + } } diff --git a/src/main/java/com/codesdream/ase/model/permission/UserAuth.java b/src/main/java/com/codesdream/ase/model/permission/UserAuth.java index 13e7071..f4dc777 100644 --- a/src/main/java/com/codesdream/ase/model/permission/UserAuth.java +++ b/src/main/java/com/codesdream/ase/model/permission/UserAuth.java @@ -14,14 +14,14 @@ public class UserAuth { private int id; // 密保问题 - private String userQuestion; + private String userQuestion = null; // 密保问题答案 - private String userAnswer; + private String userAnswer = null; // 用户邮箱 - private String mail; + private String mail = null; // 学生ID - private String studentID; + private String studentID = null; } diff --git a/src/main/java/com/codesdream/ase/model/permission/UserDetail.java b/src/main/java/com/codesdream/ase/model/permission/UserDetail.java index df7824c..7f2e448 100644 --- a/src/main/java/com/codesdream/ase/model/permission/UserDetail.java +++ b/src/main/java/com/codesdream/ase/model/permission/UserDetail.java @@ -17,7 +17,7 @@ public class UserDetail { private int id; // 学生班号 - private String classId = ""; + private String classId = null; // 所属地区 @OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) @@ -40,7 +40,7 @@ public class UserDetail { private BasePoliticalStatus basePoliticalStatus = null; // 真实姓名 - private String realName = ""; + private String realName = null; // 在校认证状态 private boolean atSchool = false; diff --git a/src/main/java/com/codesdream/ase/service/IPermissionService.java b/src/main/java/com/codesdream/ase/service/IPermissionService.java index 0ccb611..f4a8b1a 100644 --- a/src/main/java/com/codesdream/ase/service/IPermissionService.java +++ b/src/main/java/com/codesdream/ase/service/IPermissionService.java @@ -1,13 +1,21 @@ package com.codesdream.ase.service; import com.codesdream.ase.model.permission.*; -import com.sun.org.apache.xpath.internal.functions.FuncQname; import javafx.util.Pair; import java.util.Collection; import java.util.Optional; public interface IPermissionService { + + FunctionalPermissionContainer getDefaultFPC(String name); + + ScopePermissionContainer getDefaultSPC(String name); + + PermissionContainersCollection getDefaultPCC(String name); + + Tag getDefaultTag(String name); + // 查找用户标签 Optional findTag(String name); @@ -41,45 +49,59 @@ public interface IPermissionService { Collection getUsersFromTag(Tag tag); // 指定一对功能性权限容器与对应的范围性权限容器并添加到指定权限容器集合中 - void addRelationItemToPCCollection( + PermissionContainersCollection addRelationItemToPCC( PermissionContainersCollection pcc, FunctionalPermissionContainer fpc, ScopePermissionContainer spc); // 指定多对功能性权限容器与对应的范围性权限容器并添加到指定权限容器集合中 - void addRelationItemsToPCC( + PermissionContainersCollection addRelationItemsToPCC( PermissionContainersCollection pcc, Collection> fspcPairs); // 添加一个用户到指定标签中 - void addUserToTag(Tag tag, User user); + Tag addUserToTag(Tag tag, User user); // 添加多个用户到指定标签中 - void addUsersToTag(Tag tag, Collection users); + Tag addUsersToTag(Tag tag, Collection users); // 为功能性权限容器添加一个访问控制角色 - void addRoleToFPC( + FunctionalPermissionContainer addRoleToFPC( FunctionalPermissionContainer fpc, String role); // 为功能性权限容器添加多个访问控制角色 - void addRolesToFPC( + FunctionalPermissionContainer addRolesToFPC( FunctionalPermissionContainer fpc, Collection roles); - void save(Tag tag); + // 为范围性权限容器添加一个标签 + ScopePermissionContainer addTagToSPC(ScopePermissionContainer spc, Tag tag); - void save(FunctionalPermissionContainer fpc); + // 为范围性权限容器添加多个标签 + ScopePermissionContainer addTagsToSPC(ScopePermissionContainer spc, Collection tags); - void save(ScopePermissionContainer spc); + // 将一个权限容器集合添加到标签中 + Tag addPCCToTag(Tag tag, PermissionContainersCollection pcc); - void save(PermissionContainersCollection pcc); + // 将多个权限容器集合添加到标签中 + Tag addPCCsToTag(Tag tag, Collection pccs); - void update(FunctionalPermissionContainer fpc); + Tag save(Tag tag); - void update(ScopePermissionContainer spc); + FunctionalPermissionContainer save(FunctionalPermissionContainer fpc); - void update(PermissionContainersCollection pcc); + ScopePermissionContainer save(ScopePermissionContainer spc); + + PermissionContainersCollection save(PermissionContainersCollection pcc); + + Tag update(Tag tag); + + FunctionalPermissionContainer update(FunctionalPermissionContainer fpc); + + ScopePermissionContainer update(ScopePermissionContainer spc); + + PermissionContainersCollection update(PermissionContainersCollection pcc); } diff --git a/src/main/java/com/codesdream/ase/service/IUserService.java b/src/main/java/com/codesdream/ase/service/IUserService.java index 7b7c2b0..8bfe3fa 100644 --- a/src/main/java/com/codesdream/ase/service/IUserService.java +++ b/src/main/java/com/codesdream/ase/service/IUserService.java @@ -30,10 +30,10 @@ public interface IUserService { Collection getUserAuthorities(User user); // 更新用户的密码 - void updatePassword(User user, String password); + User updatePassword(User user, String password); // 封禁用户 - void disableUser(User user); + User disableUser(User user); // 根据学号生成随机用户名 void generateRandomUsernameByStudentID(User user, String id); diff --git a/src/main/java/com/codesdream/ase/service/PermissionService.java b/src/main/java/com/codesdream/ase/service/PermissionService.java index 04b30da..a810e5f 100644 --- a/src/main/java/com/codesdream/ase/service/PermissionService.java +++ b/src/main/java/com/codesdream/ase/service/PermissionService.java @@ -4,9 +4,11 @@ import com.codesdream.ase.component.permission.UserFPCListGenerator; import com.codesdream.ase.component.permission.UserFSRGenerator; import com.codesdream.ase.model.permission.*; import com.codesdream.ase.repository.permission.FunctionalPermissionContainerRepository; +import com.codesdream.ase.repository.permission.PermissionContainersCollectionRepository; 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; @@ -26,12 +28,38 @@ public class PermissionService implements IPermissionService { @Resource private ScopePermissionContainerRepository spcRepository; + @Resource + private PermissionContainersCollectionRepository pccRepository; + + @Resource + private IUserService userService; + @Resource private UserFPCListGenerator userFPCListGenerator; @Resource private UserFSRGenerator userFSRGenerator; + @Override + public FunctionalPermissionContainer getDefaultFPC(String name) { + return new FunctionalPermissionContainer(name); + } + + @Override + public ScopePermissionContainer getDefaultSPC(String name) { + return new ScopePermissionContainer(name); + } + + @Override + public PermissionContainersCollection getDefaultPCC(String name) { + return new PermissionContainersCollection(name); + } + + @Override + public Tag getDefaultTag(String name) { + return new Tag(name); + } + @Override public Optional findTag(String name) { return tagRepository.findByName(name); @@ -94,80 +122,150 @@ public class PermissionService implements IPermissionService { } @Override - public void addRelationItemToPCCollection(PermissionContainersCollection pcc, - FunctionalPermissionContainer fpc, - ScopePermissionContainer spc) + public PermissionContainersCollection addRelationItemToPCC(PermissionContainersCollection pcc, + FunctionalPermissionContainer fpc, + ScopePermissionContainer spc) { if(!findFPC(fpc.getId()).isPresent()){ - + throw new RuntimeException("FPC NOT In Database"); + } + if(!findSPC(spc.getId()).isPresent()){ + throw new RuntimeException("SPC NOT In Database"); } FunctionalScopeRelation relation = new FunctionalScopeRelation(); relation.setFunctionalPermissionContainer(fpc); relation.setScopePermissionContainer(spc); pcc.getFunctionalScopeRelations().add(relation); - update(pcc); + return update(pcc); } @Override - public void addRelationItemsToPCC(PermissionContainersCollection pcc, - Collection> fspcPairs) + public PermissionContainersCollection addRelationItemsToPCC(PermissionContainersCollection pcc, + Collection> fspcPairs) { - + for(Pair fspc :fspcPairs){ + pcc = addRelationItemToPCC(pcc, fspc.getKey(), fspc.getValue()); + } + return pcc; } @Override - public void addUserToTag(Tag tag, User user) { - + public Tag addUserToTag(Tag tag, User user) { + // 检查用户是否存在 + if(!userService.checkIfUserExists(user.getUsername()).getKey()) + throw new RuntimeException("User Not Exist"); + tag.getUsers().add(user); + return update(tag); } @Override - public void addUsersToTag(Tag tag, Collection users) { - + public Tag addUsersToTag(Tag tag, Collection users) { + for(User user :users){ + tag = addUserToTag(tag, user); + } + return tag; } @Override - public void addRoleToFPC(FunctionalPermissionContainer fpc, String role) { - + public FunctionalPermissionContainer addRoleToFPC(FunctionalPermissionContainer fpc, String role) { + fpc.getRoles().add(role); + return update(fpc); } @Override - public void addRolesToFPC(FunctionalPermissionContainer fpc, Collection roles) { - + public FunctionalPermissionContainer addRolesToFPC(FunctionalPermissionContainer fpc, Collection roles) { + for(String role : roles){ + fpc = addRoleToFPC(fpc, role); + } + return fpc; } @Override - public void save(Tag tag) { - + public ScopePermissionContainer addTagToSPC(ScopePermissionContainer spc, Tag tag) { + if(!tagRepository.findByName(tag.getName()).isPresent()) + throw new RuntimeException("Tag Not Exist"); + spc.getTags().add(tag); + return update(spc); } @Override - public void save(FunctionalPermissionContainer fpc) { - + public ScopePermissionContainer addTagsToSPC(ScopePermissionContainer spc, Collection tags) { + for(Tag tag :tags){ + spc = addTagToSPC(spc, tag); + } + return spc; } @Override - public void save(ScopePermissionContainer spc) { - + public Tag addPCCToTag(Tag tag, PermissionContainersCollection pcc) { + if(!pccRepository.findByName(pcc.getName()).isPresent()) + throw new RuntimeException("PCC Not Exist"); + tag.getPermissionContainersCollections().add(pcc); + return update(tag); } @Override - public void save(PermissionContainersCollection pcc) { - + public Tag addPCCsToTag(Tag tag, Collection pccs) { + for(PermissionContainersCollection pcc : pccs) { + tag = addPCCToTag(tag, pcc); + } + return tag; } @Override - public void update(FunctionalPermissionContainer fpc) { - + public Tag save(Tag tag) { + if(tagRepository.findByName(tag.getName()).isPresent()) + throw new RuntimeException("Tag Already Exist"); + return tagRepository.save(tag); } @Override - public void update(ScopePermissionContainer spc) { - + public FunctionalPermissionContainer save(FunctionalPermissionContainer fpc) { + if(fpcRepository.findByName(fpc.getName()).isPresent()) + throw new RuntimeException("FPC Already Exist"); + return fpcRepository.save(fpc); } @Override - public void update(PermissionContainersCollection pcc) { + public ScopePermissionContainer save(ScopePermissionContainer spc) { + if(spcRepository.findByName(spc.getName()).isPresent()) + throw new RuntimeException("SPC Already Exist"); + return spcRepository.save(spc); + } + @Override + public PermissionContainersCollection save(PermissionContainersCollection pcc) { + if(pccRepository.findByName(pcc.getName()).isPresent()) + throw new RuntimeException("PCC Already Exist"); + return pccRepository.save(pcc); + } + + @Override + public Tag update(Tag tag) { + if(!tagRepository.findByName(tag.getName()).isPresent()) + throw new RuntimeException(("Tag Not Exist")); + return tagRepository.save(tag); + } + + @Override + public FunctionalPermissionContainer update(FunctionalPermissionContainer fpc) { + if(!fpcRepository.findByName(fpc.getName()).isPresent()) + throw new RuntimeException("FPC Not Exist"); + return fpcRepository.save(fpc); + } + + @Override + public ScopePermissionContainer update(ScopePermissionContainer spc) { + if(!spcRepository.findByName(spc.getName()).isPresent()) + throw new RuntimeException("SPC Not Exist"); + return spcRepository.save(spc); + } + + @Override + public PermissionContainersCollection update(PermissionContainersCollection pcc) { + if(!pccRepository.findByName(pcc.getName()).isPresent()) + throw new RuntimeException("PCC Not Exist"); + return pccRepository.save(pcc); } } diff --git a/src/main/java/com/codesdream/ase/service/UserService.java b/src/main/java/com/codesdream/ase/service/UserService.java index 8af0386..394094d 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -63,16 +63,16 @@ public class UserService implements IUserService { } @Override - public void updatePassword(User user, String password) { + public User updatePassword(User user, String password) { user.setPassword(passwordEncoder.encode(password)); - update(user); + return update(user); } // 封禁用户 @Override - public void disableUser(User user){ + public User disableUser(User user){ user.setEnabled(false); - update(user); + return update(user); } @Override diff --git a/src/test/java/com/codesdream/ase/test/BaseInformationTest.java b/src/test/java/com/codesdream/ase/test/BaseInformationTest.java index be5aaa1..b52bffe 100644 --- a/src/test/java/com/codesdream/ase/test/BaseInformationTest.java +++ b/src/test/java/com/codesdream/ase/test/BaseInformationTest.java @@ -37,6 +37,5 @@ public class BaseInformationTest { Assert.assertTrue(informationService.checkEthnic("汉族")); Assert.assertTrue(informationService.checkMajor("软件工程")); Assert.assertTrue(informationService.checkPoliticalStatus("群众")); - } } diff --git a/src/test/java/com/codesdream/ase/test/PermissionServiceTest.java b/src/test/java/com/codesdream/ase/test/PermissionServiceTest.java index f6c14bd..f5fb5f1 100644 --- a/src/test/java/com/codesdream/ase/test/PermissionServiceTest.java +++ b/src/test/java/com/codesdream/ase/test/PermissionServiceTest.java @@ -1,12 +1,61 @@ package com.codesdream.ase.test; +import com.codesdream.ase.model.permission.FunctionalPermissionContainer; +import com.codesdream.ase.model.permission.PermissionContainersCollection; +import com.codesdream.ase.model.permission.ScopePermissionContainer; +import com.codesdream.ase.model.permission.Tag; +import com.codesdream.ase.service.IPermissionService; +import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import javax.annotation.Resource; + @SpringBootTest @RunWith(SpringRunner.class) public class PermissionServiceTest { + @Resource + private IPermissionService permissionService; + + @Test + public void PermissionServiceBaseTest(){ + FunctionalPermissionContainer fpc = permissionService.getDefaultFPC("活动管理权"); + fpc = permissionService.save(fpc); + + Tag tag1 = permissionService.getDefaultTag("九班班长"), + tag2 = permissionService.getDefaultTag("九班班委"), + tag3 = permissionService.getDefaultTag("九班普通学生"); + tag1 = permissionService.save(tag1); + tag2 = permissionService.save(tag2); + tag3 = permissionService.save(tag3); + + ScopePermissionContainer spc = permissionService.getDefaultSPC("九班全体学生"); + spc = permissionService.save(spc); + + PermissionContainersCollection pcc = permissionService.getDefaultPCC("九班班长权限容器集合"); + pcc = permissionService.save(pcc); + + // 给活动管理权赋予添加/参与/管理的权力 + fpc = permissionService.addRoleToFPC(fpc, "activity_create"); + fpc = permissionService.addRoleToFPC(fpc, "activity_participate"); + fpc = permissionService.addRoleToFPC(fpc, "activity_manage"); + + // 把九班班委加入到九班全体学生中 + spc = permissionService.addTagToSPC(spc, tag2); + // 将九班普通学生加入到九班全体学生中 + spc = permissionService.addTagToSPC(spc, tag3); + + // 把活动管理权赋予范围九班全体学生,加入到九班班长权限容器集合中 + pcc = permissionService.addRelationItemToPCC(pcc, fpc, spc); + // 将设置好的权限容器集合赋予九班班长 + tag1 = permissionService.addPCCToTag(tag1, pcc); + } + + @Test + public void PermissionServiceBaseTest2() { + + } }