diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java b/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java index 00d9da7..c3b9c63 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java @@ -34,8 +34,11 @@ public class DataModelRepositorySearcher { } public T getDataModelRepositoryInstance() { + // 确保可以引用 if(isPresent()) { - return (T) springUtil.getBean(repositoryClass); + @SuppressWarnings("unchecked") + T repository = (T) springUtil.getBean(repositoryClass); + return repository; } return null; } diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java b/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java index 113d963..d4c7925 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java @@ -10,7 +10,7 @@ import java.util.ArrayList; import java.util.Collection; /** - * 查找特定的Model以创建相应的操作表单(多例模式) + * 根据子系统及名称查找特定的Data Model */ @Data @Component diff --git a/src/main/java/com/codesdream/ase/controller/ASEErrorController.java b/src/main/java/com/codesdream/ase/controller/ASEErrorController.java index 2ceedcc..0fa6b72 100644 --- a/src/main/java/com/codesdream/ase/controller/ASEErrorController.java +++ b/src/main/java/com/codesdream/ase/controller/ASEErrorController.java @@ -13,6 +13,7 @@ import org.springframework.web.context.request.WebRequest; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; +import java.util.Date; import java.util.List; @Controller @@ -30,13 +31,12 @@ public class ASEErrorController implements ErrorController { model.addAttribute("exception_name", exception.getClass().getName()); model.addAttribute("exception_message", exception.getMessage()); List stack_infos = new ArrayList<>(); - for(StackTraceElement element : exception.getStackTrace()){ String s = element.toString(); stack_infos.add(s); } model.addAttribute("error_stack", stack_infos); - + model.addAttribute("exception_date",new Date()); return "error"; } diff --git a/src/main/java/com/codesdream/ase/controller/DataManagerController.java b/src/main/java/com/codesdream/ase/controller/DataManagerController.java index 92de3f8..05c7693 100644 --- a/src/main/java/com/codesdream/ase/controller/DataManagerController.java +++ b/src/main/java/com/codesdream/ase/controller/DataManagerController.java @@ -35,6 +35,8 @@ public class DataManagerController { if(!dataModelRepositorySearcher.isPresent()){ throw new RuntimeException("Data Model Repository Not Found"); } + + Map data; return "query"; } } diff --git a/src/main/java/com/codesdream/ase/exception/UserInformationIllegalException.java b/src/main/java/com/codesdream/ase/exception/UserInformationIllegalException.java new file mode 100644 index 0000000..ce1331e --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/UserInformationIllegalException.java @@ -0,0 +1,17 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class UserInformationIllegalException extends RuntimeException { + + String username; + + public UserInformationIllegalException(String username){ + super(); + this.username = username; + } + +} diff --git a/src/main/java/com/codesdream/ase/exception/UserNotFoundException.java b/src/main/java/com/codesdream/ase/exception/UserNotFoundException.java new file mode 100644 index 0000000..f0d38e3 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/UserNotFoundException.java @@ -0,0 +1,17 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class UserNotFoundException extends RuntimeException { + Integer id; + String username; + + public UserNotFoundException(Integer id, String username){ + super(); + this.id = id; + this.username = username; + } +} diff --git a/src/main/java/com/codesdream/ase/exception/UsernameAlreadyExistException.java b/src/main/java/com/codesdream/ase/exception/UsernameAlreadyExistException.java new file mode 100644 index 0000000..634d2e8 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/UsernameAlreadyExistException.java @@ -0,0 +1,16 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class UsernameAlreadyExistException extends RuntimeException { + + String username; + + public UsernameAlreadyExistException(String username){ + super(); + this.username = username; + } +} diff --git a/src/main/java/com/codesdream/ase/model/information/BaseCandidateCategory.java b/src/main/java/com/codesdream/ase/model/information/BaseCandidateCategory.java index e2e8d37..25535dd 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseCandidateCategory.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseCandidateCategory.java @@ -5,7 +5,7 @@ import lombok.Data; import javax.persistence.*; /** - * 考生类别 + * 考生类别基本信息 */ @Data @Entity diff --git a/src/main/java/com/codesdream/ase/model/information/BaseEthnic.java b/src/main/java/com/codesdream/ase/model/information/BaseEthnic.java index 189926a..9b6b7ab 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseEthnic.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseEthnic.java @@ -4,6 +4,9 @@ import lombok.Data; import javax.persistence.*; +/** + * 民族基本信息 + */ @Data @Entity @Table(name = "base_ethnic") diff --git a/src/main/java/com/codesdream/ase/model/information/BaseMajor.java b/src/main/java/com/codesdream/ase/model/information/BaseMajor.java index f744510..e7076fd 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseMajor.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseMajor.java @@ -4,6 +4,9 @@ import lombok.Data; import javax.persistence.*; +/** + * 专业基本信息 + */ @Data @Entity @Table(name = "base_major") diff --git a/src/main/java/com/codesdream/ase/model/information/BasePoliticalStatus.java b/src/main/java/com/codesdream/ase/model/information/BasePoliticalStatus.java index 1d5ef3c..fd89069 100644 --- a/src/main/java/com/codesdream/ase/model/information/BasePoliticalStatus.java +++ b/src/main/java/com/codesdream/ase/model/information/BasePoliticalStatus.java @@ -5,7 +5,7 @@ import lombok.Data; import javax.persistence.*; /** - * 政治面貌 + * 政治面貌基本信息 */ @Data @Entity 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 148a185..ef8d1ee 100644 --- a/src/main/java/com/codesdream/ase/model/permission/UserDetail.java +++ b/src/main/java/com/codesdream/ase/model/permission/UserDetail.java @@ -1,8 +1,6 @@ package com.codesdream.ase.model.permission; -import com.codesdream.ase.model.information.BaseAdministrativeDivision; -import com.codesdream.ase.model.information.BaseCollege; -import com.codesdream.ase.model.information.BaseMajor; +import com.codesdream.ase.model.information.*; import lombok.Data; import javax.persistence.*; @@ -19,20 +17,28 @@ public class UserDetail { private int id; // 所属地区 - @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) private BaseAdministrativeDivision baseAdministrativeDivision; // 所属学院 - @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) private BaseCollege baseCollege; // 所属专业 - @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) private BaseMajor baseMajor; + // 民族 + @OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) + private BaseEthnic baseEthnic; + + // 政治面貌 + @OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) + private BasePoliticalStatus basePoliticalStatus; + // 真实姓名 - private String realName; + private String realName = ""; // 在校状态 - private boolean atSchool; + private boolean atSchool = false; } diff --git a/src/main/java/com/codesdream/ase/repository/information/BaseAdministrativeDivisionRepository.java b/src/main/java/com/codesdream/ase/repository/information/BaseAdministrativeDivisionRepository.java new file mode 100644 index 0000000..c96f610 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/information/BaseAdministrativeDivisionRepository.java @@ -0,0 +1,12 @@ +package com.codesdream.ase.repository.information; + +import com.codesdream.ase.model.information.BaseAdministrativeDivision; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface BaseAdministrativeDivisionRepository extends CrudRepository { + Optional findByName(String name); +} diff --git a/src/main/java/com/codesdream/ase/repository/information/BaseCandidateCategoryRepository.java b/src/main/java/com/codesdream/ase/repository/information/BaseCandidateCategoryRepository.java new file mode 100644 index 0000000..1536e41 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/information/BaseCandidateCategoryRepository.java @@ -0,0 +1,12 @@ +package com.codesdream.ase.repository.information; + +import com.codesdream.ase.model.information.BaseCandidateCategory; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface BaseCandidateCategoryRepository extends CrudRepository { + Optional findByName(String name); +} diff --git a/src/main/java/com/codesdream/ase/repository/information/BaseCollegeRepository.java b/src/main/java/com/codesdream/ase/repository/information/BaseCollegeRepository.java new file mode 100644 index 0000000..d149b4d --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/information/BaseCollegeRepository.java @@ -0,0 +1,13 @@ +package com.codesdream.ase.repository.information; + +import com.codesdream.ase.model.information.BaseCollege; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface BaseCollegeRepository extends CrudRepository { + Optional findByName(String name); + Optional findByNumber(Integer number); +} diff --git a/src/main/java/com/codesdream/ase/repository/information/BaseEthnicRepository.java b/src/main/java/com/codesdream/ase/repository/information/BaseEthnicRepository.java new file mode 100644 index 0000000..b007952 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/information/BaseEthnicRepository.java @@ -0,0 +1,12 @@ +package com.codesdream.ase.repository.information; + +import com.codesdream.ase.model.information.BaseEthnic; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface BaseEthnicRepository extends CrudRepository { + Optional findByName(String name); +} diff --git a/src/main/java/com/codesdream/ase/repository/information/BaseMajorRepository.java b/src/main/java/com/codesdream/ase/repository/information/BaseMajorRepository.java new file mode 100644 index 0000000..a5d1f34 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/information/BaseMajorRepository.java @@ -0,0 +1,12 @@ +package com.codesdream.ase.repository.information; + +import com.codesdream.ase.model.information.BaseMajor; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface BaseMajorRepository extends CrudRepository { + Optional findByName(String name); +} diff --git a/src/main/java/com/codesdream/ase/repository/information/BasePoliticalStatusRepository.java b/src/main/java/com/codesdream/ase/repository/information/BasePoliticalStatusRepository.java new file mode 100644 index 0000000..1a42b33 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/information/BasePoliticalStatusRepository.java @@ -0,0 +1,10 @@ +package com.codesdream.ase.repository.information; + +import com.codesdream.ase.model.information.BasePoliticalStatus; +import org.springframework.data.repository.CrudRepository; + +import java.util.Optional; + +public interface BasePoliticalStatusRepository extends CrudRepository { + Optional findByName(String name); +} diff --git a/src/main/java/com/codesdream/ase/service/IBaseInformationService.java b/src/main/java/com/codesdream/ase/service/IBaseInformationService.java new file mode 100644 index 0000000..8c55369 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/IBaseInformationService.java @@ -0,0 +1,5 @@ +package com.codesdream.ase.service; + +public interface IBaseInformationService { + boolean checkAdministrativeDivision(String name); +} diff --git a/src/main/java/com/codesdream/ase/service/IUserService.java b/src/main/java/com/codesdream/ase/service/IUserService.java index 5de817d..277d413 100644 --- a/src/main/java/com/codesdream/ase/service/IUserService.java +++ b/src/main/java/com/codesdream/ase/service/IUserService.java @@ -1,6 +1,7 @@ package com.codesdream.ase.service; import com.codesdream.ase.model.permission.User; +import javafx.util.Pair; import org.springframework.security.core.GrantedAuthority; import java.util.Collection; @@ -16,20 +17,28 @@ public interface IUserService { List findAll(); Optional findUserById(int id); + User findUserByUsername(String username); + public Pair checkIfUserExists(String username); + // 获得用户所有的权限角色 Collection getUserAuthorities(User user); // 更新用户的密码 void updatePassword(User user, String password); - // 生成随机用户名 + // 根据学号生成随机用户名 void generateRandomUsernameByStudentID(User user, String id); + // 注册用户 User save(User user); + // 更新用户信息 User update(User user); + // 删除用户 + void delete(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 8c89180..10372be 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -3,8 +3,12 @@ package com.codesdream.ase.service; import com.codesdream.ase.component.permission.ASEPasswordEncoder; import com.codesdream.ase.component.permission.ASEUsernameEncoder; import com.codesdream.ase.component.permission.UserRolesListGenerator; +import com.codesdream.ase.exception.UserInformationIllegalException; +import com.codesdream.ase.exception.UserNotFoundException; +import com.codesdream.ase.exception.UsernameAlreadyExistException; import com.codesdream.ase.model.permission.User; import com.codesdream.ase.repository.permission.UserRepository; +import javafx.util.Pair; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; @@ -41,10 +45,16 @@ public class UserService implements IUserService { @Override public User findUserByUsername(String username) { Optional user = userRepository.findByUsername(username); - if(!user.isPresent()) throw new UsernameNotFoundException("User Not Found"); + if(!user.isPresent()) throw new UsernameNotFoundException(username); return user.get(); } + @Override + public Pair checkIfUserExists(String username){ + Optional user = userRepository.findByUsername(username); + return user.map(value -> new Pair<>(true, value)).orElseGet(() -> new Pair<>(false, null)); + } + @Override public Collection getUserAuthorities(User user) { return userRolesListGenerator.generateRoles(user); @@ -66,23 +76,43 @@ public class UserService implements IUserService { public User save(User user) { // 查找用户名是否已经被注册 if(userRepository.findByUsername(user.getUsername()).isPresent()) - throw new RuntimeException("Username Already Exists"); + throw new UsernameAlreadyExistException(user.getUsername()); + + // 用户信息一般性规范检查 + if(user.getUserAuth().getUserAnswer().length() > 255 + || user.getUserAuth().getUserQuestion().length() > 255 + || user.getUserAuth().getStudentID().length() > 24 + || user.getUserAuth().getMail().length() > 64 + || user.getUserDetail().getRealName().length() > 12) + throw new UserInformationIllegalException(user.getUsername()); + + // 强制以哈希值(sha256)保存密码 user.setPassword(passwordEncoder.encode(user.getPassword())); return userRepository.save(user); } @Override public User update(User user) { - if(!userRepository.findByUsername(user.getUsername()).isPresent()) - throw new RuntimeException("Username Already Exists"); + // 执行前检查 + if(!userRepository.findById(user.getId()).isPresent()) + throw new UserNotFoundException(user.getId(), user.getUsername()); return userRepository.save(user); } + @Override + public void delete(User user) { + // 执行前检查 + if(!userRepository.findById(user.getId()).isPresent()) + throw new UserNotFoundException(user.getId(), user.getUsername()); + userRepository.delete(user); + } + // 获得一个默认初始化的用户对象 @Override public User getDefaultUser() { return new User(); } + } diff --git a/src/main/java/com/codesdream/ase/test/UserTest.java b/src/main/java/com/codesdream/ase/test/UserTest.java index 11f270b..1b0c25e 100644 --- a/src/main/java/com/codesdream/ase/test/UserTest.java +++ b/src/main/java/com/codesdream/ase/test/UserTest.java @@ -1,74 +1,71 @@ 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.permission.UserRepository; import static org.junit.Assert.*; +import com.codesdream.ase.service.UserService; +import com.sun.org.apache.xpath.internal.operations.Bool; +import javafx.util.Pair; 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.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import java.util.HashSet; +import javax.annotation.Resource; /** * 用户基本表单元测试 * 用于测试数据库与DAO层交互是否通畅 */ -@DataJpaTest @RunWith(SpringRunner.class) +@SpringBootTest public class UserTest { - @Autowired - private TestEntityManager entityManager; - - @Autowired - private UserRepository userRepository; + @Resource + private UserService userService; /** * 基本存储与查询测试 */ @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(); + // 查找数据库中是否有重复项 + Pair checker = userService.checkIfUserExists("Tim"); + if(checker.getKey()){ + userService.delete(checker.getValue()); + } + + User user = userService.getDefaultUser(); + user.setUsername("Tim"); + user.setPassword("123456"); + user.getUserAuth().setStudentID("2018303026"); + user.getUserAuth().setMail("937447984@qq.com"); + user.getUserAuth().setUserQuestion("Your favourite animal?"); + user.getUserAuth().setUserAnswer("Cat"); + user.getUserDetail().setAtSchool(true); + userService.save(user); + + user = userService.findUserByUsername("Tim"); assertEquals(user.getUsername(), "Tim"); - assertEquals(user.getPassword(), "123456"); + assertEquals(user.getPassword(), + "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"); + // 检查账号状态 assertTrue(user.isEnabled()); assertFalse(user.isDeleted()); + assertTrue(user.isAccountNonExpired()); + assertTrue(user.isAccountNonLocked()); + assertTrue(user.isCredentialsNonExpired()); + assertEquals(user.getUserAuth().getStudentID(), "2018303026"); + assertEquals(user.getUserAuth().getMail(), "937447984@qq.com"); + assertEquals(user.getUserAuth().getUserQuestion(), "Your favourite animal?"); + assertEquals(user.getUserAuth().getUserAnswer(), "Cat"); } @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); - // 等待添加 - - // 为用户添加标签 - HashSet tags = new HashSet<>(); - tags.add(tag); - user.setTags(tags); - - userRepository.save(user); - + User user = userService.findUserByUsername("Tim"); diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index 9795c21..55690a0 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -8,83 +8,89 @@ -
-

Error

-
-
-
-
-

- 检测到异常发生 -

-

- 当服务器端程序内部某个模块抛出异常时,该界面就会被显示。此次异常的简要信息已被异常管理子系统收集并概括如下表: -

- - - - - - - - - - - - - - - - - - - +

  概要信息

+ +
+
+ 此次异常的简要信息已被异常管理子系统收集并概括如下表 +
+
- 编号 - - 信息项 - - 值 -
- 0 - - 服务器返回HTTP状态 - +
+
+
- 1 - - 抛出的异常类型 - +
+

Error Occurred

+
+ + +
+
+
+ +
+
+
+
-
+ + + + + + + + + + + + + + + + + + - - - - + + + + - - + + + + + + +
#信息项
0 HTTP状态
1异常类型
- 2 - - 异常附带消息 - +
2附带消息
2附带消息
+
- - -

- 运行栈相关信息补充 -

-

- -

+
+
+
+

  运行栈相关信息

+
+
+
    +
  1. + +
  2. +
+ +
+
+
+ + + - - - \ No newline at end of file diff --git a/src/main/resources/templates/not_found.html b/src/main/resources/templates/not_found.html index 1890621..6a4a8e1 100644 --- a/src/main/resources/templates/not_found.html +++ b/src/main/resources/templates/not_found.html @@ -8,10 +8,23 @@ -
-

404 Not Found

+
+
+
- +
+
+
+
+
+ +
+
+