From ea2ce3d1d406c73a0bae2668525f29076e5470e7 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Wed, 5 Feb 2020 17:02:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E5=AE=8C=E5=96=84=E6=9D=83?= =?UTF-8?q?=E9=99=90=E7=AE=A1=E7=90=86=E5=AD=90=E7=B3=BB=E7=BB=9F=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 35 +++++++++ .../ase/controller/LoginController.java | 1 - .../FunctionalPermissionContainer.java | 25 +++++- .../ScopePermissionContainer.java | 18 ++--- .../model/{pernission => permission}/Tag.java | 36 ++++++++- .../{pernission => permission}/User.java | 61 ++++++++++++++- .../ase/repository/TagRepository.java | 13 ++++ .../ase/repository/UserRepository.java | 8 +- .../ase/service/ASEUserDetailsService.java | 1 - .../codesdream/ase/service/IUserService.java | 4 +- .../codesdream/ase/service/UserService.java | 11 ++- .../com/codesdream/ase/test/UserTest.java | 76 +++++++++++++++++++ src/main/resources/templates/login.html | 1 - 13 files changed, 258 insertions(+), 32 deletions(-) rename src/main/java/com/codesdream/ase/model/{pernission => permission}/FunctionalPermissionContainer.java (65%) rename src/main/java/com/codesdream/ase/model/{pernission => permission}/ScopePermissionContainer.java (81%) rename src/main/java/com/codesdream/ase/model/{pernission => permission}/Tag.java (54%) rename src/main/java/com/codesdream/ase/model/{pernission => permission}/User.java (53%) create mode 100644 src/main/java/com/codesdream/ase/repository/TagRepository.java create mode 100644 src/main/java/com/codesdream/ase/test/UserTest.java diff --git a/pom.xml b/pom.xml index 0557167..e1cc379 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,41 @@ 1.11 + + org.springframework + spring-test + 5.2.3.RELEASE + compile + + + + org.springframework.boot + spring-boot-test + + + + org.junit.jupiter + junit-jupiter-api + 5.5.2 + compile + + + + junit + junit + + + + org.springframework.boot + spring-boot-test-autoconfigure + + + + org.hsqldb + hsqldb + runtime + + diff --git a/src/main/java/com/codesdream/ase/controller/LoginController.java b/src/main/java/com/codesdream/ase/controller/LoginController.java index d15cf08..d6a8d9b 100644 --- a/src/main/java/com/codesdream/ase/controller/LoginController.java +++ b/src/main/java/com/codesdream/ase/controller/LoginController.java @@ -1,6 +1,5 @@ package com.codesdream.ase.controller; -import com.codesdream.ase.model.pernission.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/src/main/java/com/codesdream/ase/model/pernission/FunctionalPermissionContainer.java b/src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java similarity index 65% rename from src/main/java/com/codesdream/ase/model/pernission/FunctionalPermissionContainer.java rename to src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java index 28bbb23..1044b76 100644 --- a/src/main/java/com/codesdream/ase/model/pernission/FunctionalPermissionContainer.java +++ b/src/main/java/com/codesdream/ase/model/permission/FunctionalPermissionContainer.java @@ -1,7 +1,9 @@ -package com.codesdream.ase.model.pernission; +package com.codesdream.ase.model.permission; import javax.persistence.*; +import java.util.HashSet; import java.util.List; +import java.util.Set; @Entity @Table(name = "functional_permit_container") @@ -11,22 +13,37 @@ public class FunctionalPermissionContainer { private int id; // 功能性权限容器名 - // @Column(nullable = false, unique = true) + @Column(nullable = false, unique = true) private String name; // 功能性权限容器解释 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; - // @Column(nullable = false) + @Column(nullable = false) private boolean enabled; - // @Column(nullable = false) + @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; } diff --git a/src/main/java/com/codesdream/ase/model/pernission/ScopePermissionContainer.java b/src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java similarity index 81% rename from src/main/java/com/codesdream/ase/model/pernission/ScopePermissionContainer.java rename to src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java index f4c63ee..2421e95 100644 --- a/src/main/java/com/codesdream/ase/model/pernission/ScopePermissionContainer.java +++ b/src/main/java/com/codesdream/ase/model/permission/ScopePermissionContainer.java @@ -1,7 +1,7 @@ -package com.codesdream.ase.model.pernission; +package com.codesdream.ase.model.permission; import javax.persistence.*; -import java.util.List; +import java.util.Set; @Entity @Table(name = "scope_permit_container") @@ -15,15 +15,15 @@ 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; - @ElementCollection - private List tags; - public int getId() { return id; } @@ -63,12 +63,4 @@ public class ScopePermissionContainer { public void setDeleted(boolean deleted) { this.deleted = deleted; } - - public List getTags() { - return tags; - } - - public void setTags(List tags) { - this.tags = tags; - } } diff --git a/src/main/java/com/codesdream/ase/model/pernission/Tag.java b/src/main/java/com/codesdream/ase/model/permission/Tag.java similarity index 54% rename from src/main/java/com/codesdream/ase/model/pernission/Tag.java rename to src/main/java/com/codesdream/ase/model/permission/Tag.java index fcfe749..67bbc7a 100644 --- a/src/main/java/com/codesdream/ase/model/pernission/Tag.java +++ b/src/main/java/com/codesdream/ase/model/permission/Tag.java @@ -1,7 +1,9 @@ -package com.codesdream.ase.model.pernission; +package com.codesdream.ase.model.permission; import javax.persistence.*; -import java.util.Objects; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; /** * 标签 @@ -20,6 +22,10 @@ public class Tag { // 标签解释 private String description; + // 标签关联用户 + @ManyToMany(mappedBy = "tags", cascade = CascadeType.MERGE, fetch = FetchType.LAZY) + private Set users; + // 启用标志 @Column(nullable = false) private boolean enabled; @@ -28,16 +34,26 @@ public class Tag { @Column(nullable = false) private boolean deleted; + // 标签对应权限容器 + @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "tags") + private Set functionalPermissionContainers; + 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() { @@ -79,4 +95,20 @@ public class Tag { 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/pernission/User.java b/src/main/java/com/codesdream/ase/model/permission/User.java similarity index 53% rename from src/main/java/com/codesdream/ase/model/pernission/User.java rename to src/main/java/com/codesdream/ase/model/permission/User.java index b3b217d..2866c16 100644 --- a/src/main/java/com/codesdream/ase/model/pernission/User.java +++ b/src/main/java/com/codesdream/ase/model/permission/User.java @@ -1,4 +1,4 @@ -package com.codesdream.ase.model.pernission; +package com.codesdream.ase.model.permission; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; @@ -7,7 +7,8 @@ import org.springframework.security.core.userdetails.UserDetails; import javax.persistence.*; import java.util.ArrayList; import java.util.Collection; -import java.util.Objects; +import java.util.HashSet; +import java.util.Set; @Entity @Table(name = "user") @@ -16,17 +17,49 @@ public class User implements UserDetails { @GeneratedValue(strategy = GenerationType.AUTO) private int id; // 用户名 + @Column(unique = true, nullable = false) private String username; + // 密码(必须以哈希值sha256储存) + @Column(nullable = false) private String password; + // 手机号 + @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; + + // 是否启用 + @Column(nullable = false) + private boolean enabled; + // 是否删除 + @Column(nullable = false) + private boolean deleted; + public User(String username, String password) { this.username = username; this.password = password; + this.enabled = true; + this.deleted = false; + this.tags = new HashSet(); } public User() { - + this.username = null; + this.password = null; + this.enabled = true; + this.deleted = false; + this.tags = new HashSet(); } @Override @@ -51,7 +84,7 @@ public class User implements UserDetails { @Override public boolean isEnabled() { - return false; + return this.enabled; } public void setUsername(String username) { @@ -73,4 +106,24 @@ public class User implements UserDetails { 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/repository/TagRepository.java b/src/main/java/com/codesdream/ase/repository/TagRepository.java new file mode 100644 index 0000000..0d7ea18 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/TagRepository.java @@ -0,0 +1,13 @@ +package com.codesdream.ase.repository; + +import com.codesdream.ase.model.permission.FunctionalPermissionContainer; +import com.codesdream.ase.model.permission.Tag; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface TagRepository extends CrudRepository { + Optional findByName(String name); +} diff --git a/src/main/java/com/codesdream/ase/repository/UserRepository.java b/src/main/java/com/codesdream/ase/repository/UserRepository.java index 9a09697..3056837 100644 --- a/src/main/java/com/codesdream/ase/repository/UserRepository.java +++ b/src/main/java/com/codesdream/ase/repository/UserRepository.java @@ -1,10 +1,14 @@ package com.codesdream.ase.repository; -import com.codesdream.ase.model.pernission.User; +import com.codesdream.ase.model.permission.Tag; +import com.codesdream.ase.model.permission.User; import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; import java.util.Optional; +import java.util.Set; -public interface UserRepository extends CrudRepository { +@Repository +public interface UserRepository extends CrudRepository { Optional findByUsername(String username); } diff --git a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java index 39840c5..3808771 100644 --- a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java +++ b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java @@ -1,6 +1,5 @@ package com.codesdream.ase.service; -import com.codesdream.ase.model.pernission.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; diff --git a/src/main/java/com/codesdream/ase/service/IUserService.java b/src/main/java/com/codesdream/ase/service/IUserService.java index c23de53..32919fd 100644 --- a/src/main/java/com/codesdream/ase/service/IUserService.java +++ b/src/main/java/com/codesdream/ase/service/IUserService.java @@ -1,6 +1,6 @@ package com.codesdream.ase.service; -import com.codesdream.ase.model.pernission.User; +import com.codesdream.ase.model.permission.User; import java.util.List; import java.util.Optional; @@ -9,7 +9,7 @@ import java.util.Optional; public interface IUserService { List findAll(); - Optional findUserById(long id); + Optional findUserById(int id); Optional findUserByUsername(String username); User save(User user); } diff --git a/src/main/java/com/codesdream/ase/service/UserService.java b/src/main/java/com/codesdream/ase/service/UserService.java index a6e6a79..8b62559 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -1,6 +1,7 @@ package com.codesdream.ase.service; -import com.codesdream.ase.model.pernission.User; +import com.codesdream.ase.component.ASEPasswordEncoder; +import com.codesdream.ase.model.permission.User; import com.codesdream.ase.repository.UserRepository; import org.springframework.stereotype.Service; @@ -13,13 +14,16 @@ public class UserService implements IUserService { @Resource UserRepository userRepository; + @Resource + ASEPasswordEncoder asePasswordEncoder; + @Override public List findAll() { return (List) userRepository.findAll(); } @Override - public Optional findUserById(long id) { + public Optional findUserById(int id) { return userRepository.findById(id); } @@ -30,6 +34,9 @@ public class UserService implements IUserService { @Override public User save(User user) { + user.setPassword(asePasswordEncoder.encode(user.getPassword())); + user.setEnabled(true); + user.setDeleted(false); return userRepository.save(user); } diff --git a/src/main/java/com/codesdream/ase/test/UserTest.java b/src/main/java/com/codesdream/ase/test/UserTest.java new file mode 100644 index 0000000..31670e5 --- /dev/null +++ b/src/main/java/com/codesdream/ase/test/UserTest.java @@ -0,0 +1,76 @@ +package com.codesdream.ase.test; + +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 static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.HashMap; +import java.util.HashSet; + +/** + * 用户基本表单元测试 + * 用于测试数据库与DAO层交互是否通畅 + */ +@DataJpaTest +@RunWith(SpringRunner.class) +public class UserTest { + + @Autowired + private TestEntityManager entityManager; + + @Autowired + private UserRepository userRepository; + + /** + * 基本存储与查询测试 + */ + @Test + public void UserBaseTest_1(){ + User user = new User("Tim", "123456"); + userRepository.save(user); + assertTrue(userRepository.findByUsername("Tim").isPresent()); + assertFalse(userRepository.findByUsername("Tom").isPresent()); + user = userRepository.findByUsername("Tim").get(); + + assertEquals(user.getUsername(), "Tim"); + assertEquals(user.getPassword(), "123456"); + assertTrue(user.isEnabled()); + assertFalse(user.isDeleted()); + } + @Test + public void UserBaseTest_2(){ + // 用户 + User user = new User("Pat", "123456"); + // 标签 + Tag tag = new Tag("学生","普通学生"); + // 功能性权限容器 + FunctionalPermissionContainer functionalPermissionContainer = + new FunctionalPermissionContainer("基本用户权限", "基本的用户权限"); + + // 添加为标签功能性权限容器 + HashSet functionalPermissionContainers = new HashSet<>(); + functionalPermissionContainers.add(functionalPermissionContainer); + tag.setFunctionalPermissionContainers(functionalPermissionContainers); + + // 为用户添加标签 + HashSet tags = new HashSet<>(); + tags.add(tag); + user.setTags(tags); + + userRepository.save(user); + + + + + } +} diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index 3c8184f..3769ba3 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -32,7 +32,6 @@ -