调整权限管理子系统并添加相关组件

This commit is contained in:
Saturneric 2020-02-12 17:37:29 +08:00
parent 1a59705c1b
commit 870209c6c0
13 changed files with 209 additions and 243 deletions

View File

@ -107,6 +107,12 @@
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
<build>

View File

@ -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<FunctionalPermissionContainer> generateFunctionalContainers(User user){
Collection<Tag> tags = userTagsListGenerator.generateTags(user);
Collection<FunctionalPermissionContainer> functionalPermissionContainers = new ArrayList<>();
// 等待添加
return functionalPermissionContainers;
}
public Collection<FunctionalPermissionContainer> generateFunctionalContainers(String username){
Optional<User> user = userRepository.findByUsername(username);
if(!user.isPresent()) throw new RuntimeException("User Not Found");
return generateFunctionalContainers(user.get());
}
}

View File

@ -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<GrantedAuthority> GenerateRoles(User user){
@Resource
UserRepository userRepository;
public Collection<GrantedAuthority> generateRoles(String username){
Optional<User> user = userRepository.findByUsername(username);
// 如果没有找到用户
if(!user.isPresent()) throw new RuntimeException("User Not Found");
return generateRoles(user.get());
}
public Collection<GrantedAuthority> generateRoles(User user){
Collection<GrantedAuthority> authorities = new ArrayList<>();
// 列举用户对应的所有标签
for(Tag tag : user.getTags()){
for(FunctionalPermissionContainer functionalPermissionContainer : tag.getFunctionalPermissionContainers()){
authorities.add(new SimpleGrantedAuthority(functionalPermissionContainer.getName()));
}
// 列举标签对应的所有功能性权限容器
// 等待添加
}
return authorities;
}

View File

@ -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<Tag> generateTags(User user){
return new ArrayList<Tag>(user.getTags());
}
public Collection<Tag> generateTags(String username){
Optional<User> user = userRepository.findByUsername(username);
// 检查用户是否存在
if(!user.isPresent()) throw new RuntimeException("User Not Found");
return generateTags(user.get());
}
public Collection<String> generateTagsName(User user){
Collection<String> tagsName = new ArrayList<>();
Collection<Tag> tags = generateTags(user);
for(Tag tag : tags){
tagsName.add(tag.getName());
}
return tagsName;
}
public Collection<String> generateTagsName(String username){
Optional<User> user = userRepository.findByUsername(username);
if(!user.isPresent()) throw new RuntimeException("User Not Found");
return generateTagsName(user.get());
}
}

View File

@ -0,0 +1,4 @@
package com.codesdream.ase.controller;
public class RegisterController {
}

View File

@ -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<Tag> tags;
// 对应访问控制角色列表
@ElementCollection
@Column(nullable = false)
private List<String> urls;
private List<String> 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<Tag>();
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<String> getUrls() {
return urls;
}
public void setUrls(List<String> urls) {
this.urls = urls;
}
}

View File

@ -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;
}

View File

@ -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<FunctionalScopeRelation> functionalScopeRelations;
}

View File

@ -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<FunctionalPermissionContainer> 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;
}
}

View File

@ -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<FunctionalPermissionContainer> functionalPermissionContainers;
// 对应权限容器集合
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<PermissionContainersCollection> permissionContainersCollections;
public Tag(String name, String description) {
this.name = name;
this.description = description;
this.deleted = false;
this.enabled = true;
this.users = new HashSet<User>();
this.functionalPermissionContainers = new HashSet<FunctionalPermissionContainer>();
}
public Tag() {
this.deleted = false;
this.enabled = true;
this.users = new HashSet<User>();
this.functionalPermissionContainers = new HashSet<FunctionalPermissionContainer>();
}
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<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Set<FunctionalPermissionContainer> getFunctionalPermissionContainers() {
return functionalPermissionContainers;
}
public void setFunctionalPermissionContainers(Set<FunctionalPermissionContainer> functionalPermissionContainers) {
this.functionalPermissionContainers = functionalPermissionContainers;
}
}

View File

@ -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<Tag> 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<Tag> tags;
public User(String username, String password) {
this.username = username;
this.password = password;
this.enabled = true;
this.deleted = false;
this.tags = new HashSet<Tag>();
initUserDefault();
}
public User() {
this.username = null;
this.password = null;
this.enabled = true;
this.deleted = false;
this.tags = new HashSet<Tag>();
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<? extends GrantedAuthority> 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<Tag> getTags() {
return tags;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
}

View File

@ -60,7 +60,7 @@ public class UserTest {
// 添加为标签功能性权限容器
HashSet<FunctionalPermissionContainer> functionalPermissionContainers = new HashSet<>();
functionalPermissionContainers.add(functionalPermissionContainer);
tag.setFunctionalPermissionContainers(functionalPermissionContainers);
// 等待添加
// 为用户添加标签
HashSet<Tag> tags = new HashSet<>();

View File

@ -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