diff --git a/pom.xml b/pom.xml index e1cc379..024f723 100644 --- a/pom.xml +++ b/pom.xml @@ -107,6 +107,12 @@ runtime + + org.projectlombok + lombok + 1.16.10 + + diff --git a/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java b/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java new file mode 100644 index 0000000..19d32d9 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java @@ -0,0 +1,34 @@ +package com.codesdream.ase.component; + +import com.codesdream.ase.model.permission.FunctionalPermissionContainer; +import com.codesdream.ase.model.permission.PermissionContainersCollection; +import com.codesdream.ase.model.permission.Tag; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.repository.UserRepository; + +import javax.annotation.Resource; +import javax.swing.text.html.Option; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Optional; + +public class UserFunctionalPermissionContainersListGenerator { + @Resource + UserTagsListGenerator userTagsListGenerator; + + @Resource + UserRepository userRepository; + + public Collection generateFunctionalContainers(User user){ + Collection tags = userTagsListGenerator.generateTags(user); + Collection functionalPermissionContainers = new ArrayList<>(); + // 等待添加 + return functionalPermissionContainers; + } + + public Collection generateFunctionalContainers(String username){ + Optional user = userRepository.findByUsername(username); + if(!user.isPresent()) throw new RuntimeException("User Not Found"); + return generateFunctionalContainers(user.get()); + } +} diff --git a/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java b/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java index 9b6795d..a424154 100644 --- a/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java @@ -3,21 +3,35 @@ package com.codesdream.ase.component; import com.codesdream.ase.model.permission.FunctionalPermissionContainer; import com.codesdream.ase.model.permission.Tag; import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.repository.UserRepository; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.stereotype.Component; +import javax.annotation.Resource; import java.util.ArrayList; import java.util.Collection; +import java.util.Optional; @Component public class UserRolesListGenerator { - public Collection GenerateRoles(User user){ + @Resource + UserRepository userRepository; + + public Collection generateRoles(String username){ + Optional user = userRepository.findByUsername(username); + // 如果没有找到用户 + if(!user.isPresent()) throw new RuntimeException("User Not Found"); + + return generateRoles(user.get()); + } + + public Collection generateRoles(User user){ Collection authorities = new ArrayList<>(); + // 列举用户对应的所有标签 for(Tag tag : user.getTags()){ - for(FunctionalPermissionContainer functionalPermissionContainer : tag.getFunctionalPermissionContainers()){ - authorities.add(new SimpleGrantedAuthority(functionalPermissionContainer.getName())); - } + // 列举标签对应的所有功能性权限容器 + // 等待添加 } return authorities; } diff --git a/src/main/java/com/codesdream/ase/component/UserTagsListGenerator.java b/src/main/java/com/codesdream/ase/component/UserTagsListGenerator.java new file mode 100644 index 0000000..500533e --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/UserTagsListGenerator.java @@ -0,0 +1,46 @@ +package com.codesdream.ase.component; + +import com.codesdream.ase.model.permission.Tag; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.repository.UserRepository; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Optional; + +@Component +public class UserTagsListGenerator { + @Resource + UserRepository userRepository; + + public Collection generateTags(User user){ + return new ArrayList(user.getTags()); + } + + public Collection generateTags(String username){ + Optional user = userRepository.findByUsername(username); + // 检查用户是否存在 + if(!user.isPresent()) throw new RuntimeException("User Not Found"); + + return generateTags(user.get()); + } + + public Collection generateTagsName(User user){ + Collection tagsName = new ArrayList<>(); + Collection tags = generateTags(user); + for(Tag tag : tags){ + tagsName.add(tag.getName()); + } + return tagsName; + } + + public Collection generateTagsName(String username){ + Optional user = userRepository.findByUsername(username); + + if(!user.isPresent()) throw new RuntimeException("User Not Found"); + + return generateTagsName(user.get()); + } +} diff --git a/src/main/java/com/codesdream/ase/controller/RegisterController.java b/src/main/java/com/codesdream/ase/controller/RegisterController.java new file mode 100644 index 0000000..259cf93 --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/RegisterController.java @@ -0,0 +1,4 @@ +package com.codesdream.ase.controller; + +public class RegisterController { +} 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 1044b76..486f148 100644 --- a/src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java +++ b/src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java @@ -1,10 +1,16 @@ package com.codesdream.ase.model.permission; +import lombok.Data; + import javax.persistence.*; import java.util.HashSet; import java.util.List; import java.util.Set; +/** + * 功能性权限容器 + */ +@Data @Entity @Table(name = "functional_permit_container") public class FunctionalPermissionContainer { @@ -19,76 +25,21 @@ public class FunctionalPermissionContainer { // 功能性权限容器解释 private String description; - // 功能性容器对应范围性容器 - @ManyToOne(cascade=CascadeType.PERSIST,fetch=FetchType.LAZY) - private ScopePermissionContainer scopePermissionContainer; - - @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) - private Set tags; - + // 对应访问控制角色列表 @ElementCollection @Column(nullable = false) - private List urls; + private List roles; + // 是否启用 @Column(nullable = false) private boolean enabled; + // 是否删除 @Column(nullable = false) private boolean deleted; public FunctionalPermissionContainer(String name, String description) { this.name = name; this.description = description; - - this.tags = new HashSet(); - this.scopePermissionContainer = null; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public boolean isDeleted() { - return deleted; - } - - public void setDeleted(boolean deleted) { - this.deleted = deleted; - } - - public List getUrls() { - return urls; - } - - public void setUrls(List urls) { - this.urls = urls; } } diff --git a/src/main/java/com/codesdream/ase/model/permission/FunctionalScopeRelation.java b/src/main/java/com/codesdream/ase/model/permission/FunctionalScopeRelation.java new file mode 100644 index 0000000..2f111ad --- /dev/null +++ b/src/main/java/com/codesdream/ase/model/permission/FunctionalScopeRelation.java @@ -0,0 +1,27 @@ +package com.codesdream.ase.model.permission; + + +import lombok.Data; + +import javax.persistence.*; + +/** + * 功能性权限容器与范围性权限容器关联对 + */ +@Data +@Entity +@Table(name = "functional_scope_relation") +public class FunctionalScopeRelation { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + // 对应功能性权限容器 + @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private FunctionalPermissionContainer functionalPermissionContainer; + + // 对应范围性权限容器 + @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private ScopePermissionContainer scopePermissionContainer; +} diff --git a/src/main/java/com/codesdream/ase/model/permission/PermissionContainersCollection.java b/src/main/java/com/codesdream/ase/model/permission/PermissionContainersCollection.java new file mode 100644 index 0000000..b32279b --- /dev/null +++ b/src/main/java/com/codesdream/ase/model/permission/PermissionContainersCollection.java @@ -0,0 +1,21 @@ +package com.codesdream.ase.model.permission; + +import lombok.Data; +import javax.persistence.*; +import java.util.Set; + +/** + * 权限容器集合 + */ +@Data +@Entity +@Table(name = "permission_container_collection") +public class PermissionContainersCollection { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + // 对应功能性权限容器与范围性权限容器关联对 + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private Set functionalScopeRelations; +} 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 2421e95..6d3d358 100644 --- a/src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java +++ b/src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java @@ -1,8 +1,11 @@ package com.codesdream.ase.model.permission; +import lombok.Data; + import javax.persistence.*; import java.util.Set; +@Data @Entity @Table(name = "scope_permit_container") public class ScopePermissionContainer { @@ -15,52 +18,9 @@ public class ScopePermissionContainer { private String description; - @OneToMany(cascade=CascadeType.MERGE,fetch=FetchType.LAZY,mappedBy="scopePermissionContainer") - private Set functionalPermissionContainers; - @Column(nullable = false) private boolean enabled; @Column(nullable = false) private boolean deleted; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public boolean isDeleted() { - return deleted; - } - - public void setDeleted(boolean deleted) { - this.deleted = deleted; - } } 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 67bbc7a..8fc4609 100644 --- a/src/main/java/com/codesdream/ase/model/permission/Tag.java +++ b/src/main/java/com/codesdream/ase/model/permission/Tag.java @@ -1,5 +1,8 @@ package com.codesdream.ase.model.permission; +import lombok.Data; +import org.hibernate.annotations.ManyToAny; + import javax.persistence.*; import java.util.HashMap; import java.util.HashSet; @@ -8,6 +11,7 @@ import java.util.Set; /** * 标签 */ +@Data @Entity @Table(name = "tag") public class Tag { @@ -34,81 +38,22 @@ public class Tag { @Column(nullable = false) private boolean deleted; - // 标签对应权限容器 - @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "tags") - private Set functionalPermissionContainers; + // 对应权限容器集合 + @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private Set permissionContainersCollections; public Tag(String name, String description) { this.name = name; this.description = description; this.deleted = false; this.enabled = true; - this.users = new HashSet(); - this.functionalPermissionContainers = new HashSet(); } public Tag() { this.deleted = false; this.enabled = true; - this.users = new HashSet(); - this.functionalPermissionContainers = new HashSet(); } - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public boolean isDeleted() { - return deleted; - } - - public void setDeleted(boolean deleted) { - this.deleted = deleted; - } - - public Set getUsers() { - return users; - } - - public void setUsers(Set users) { - this.users = users; - } - - public Set getFunctionalPermissionContainers() { - return functionalPermissionContainers; - } - - public void setFunctionalPermissionContainers(Set functionalPermissionContainers) { - this.functionalPermissionContainers = functionalPermissionContainers; - } } diff --git a/src/main/java/com/codesdream/ase/model/permission/User.java b/src/main/java/com/codesdream/ase/model/permission/User.java index c8b6c5a..4e78871 100644 --- a/src/main/java/com/codesdream/ase/model/permission/User.java +++ b/src/main/java/com/codesdream/ase/model/permission/User.java @@ -1,6 +1,7 @@ package com.codesdream.ase.model.permission; import com.codesdream.ase.component.UserRolesListGenerator; +import lombok.Data; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; @@ -9,6 +10,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.Set; +@Data @Entity @Table(name = "user") public class User implements UserDetails { @@ -27,101 +29,56 @@ public class User implements UserDetails { @Column(nullable = true) private String phone_number; - // 用户关联标签 - @ManyToMany(cascade = CascadeType.PERSIST) - @JoinTable(name = "user_tag", - joinColumns = { - @JoinColumn(name = "user_id", referencedColumnName = "id") - }, - inverseJoinColumns = { - @JoinColumn(name = "tag_id", referencedColumnName = "id") - }) - private Set tags; + // 账号是否过期 + private boolean accountNonExpired; + + // 账号是否被封禁 + private boolean accountNonLocked; + + // 证书是否过期 + private boolean credentialsNonExpired; + + // 账号是否激活 + private boolean enabled; - // 是否启用 - @Column(nullable = false) - private boolean enabled; // 是否删除 @Column(nullable = false) private boolean deleted; + // 用户关联标签 + @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private Set tags; + + + public User(String username, String password) { this.username = username; this.password = password; - this.enabled = true; - this.deleted = false; - this.tags = new HashSet(); + + initUserDefault(); } public User() { this.username = null; this.password = null; - this.enabled = true; this.deleted = false; - this.tags = new HashSet(); + + initUserDefault(); } - @Override - public String getUsername() { - return username; - } - - @Override - public boolean isAccountNonExpired() { - return isEnabled(); - } - - @Override - public boolean isAccountNonLocked() { - return isEnabled(); - } - - @Override - public boolean isCredentialsNonExpired() { - return isEnabled(); - } - - @Override - public boolean isEnabled() { - return this.enabled; - } - - public void setUsername(String username) { - this.username = username; + // 用默认的方式初始化User对象的值 + private void initUserDefault(){ + this.accountNonExpired = true; + this.accountNonLocked = true; + this.credentialsNonExpired = true; + this.deleted = false; + this.tags = new HashSet<>(); } @Override public Collection getAuthorities() { UserRolesListGenerator userRolesListGenerator = new UserRolesListGenerator(); - return userRolesListGenerator.GenerateRoles(this); + return userRolesListGenerator.generateRoles(this); } - @Override - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public boolean isDeleted() { - return deleted; - } - - public void setDeleted(boolean deleted) { - this.deleted = deleted; - } - - public Set getTags() { - return tags; - } - - public void setTags(Set tags) { - this.tags = tags; - } } diff --git a/src/main/java/com/codesdream/ase/test/UserTest.java b/src/main/java/com/codesdream/ase/test/UserTest.java index 31670e5..35539e4 100644 --- a/src/main/java/com/codesdream/ase/test/UserTest.java +++ b/src/main/java/com/codesdream/ase/test/UserTest.java @@ -60,7 +60,7 @@ public class UserTest { // 添加为标签功能性权限容器 HashSet functionalPermissionContainers = new HashSet<>(); functionalPermissionContainers.add(functionalPermissionContainer); - tag.setFunctionalPermissionContainers(functionalPermissionContainers); + // 等待添加 // 为用户添加标签 HashSet tags = new HashSet<>(); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f02a177..85a7e74 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,6 +8,7 @@ spring.thymeleaf.encoding=UTF-8 spring.jpa.generate-ddl=false spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create +spring.jooq.sql-dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.datasource.url=jdbc:mysql://${MYSQL_HOST:119.23.9.34}:3306/ase?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=codedream