添加权限管理子系统的服务层接口,修正组件层问题

This commit is contained in:
Saturneric 2020-02-15 17:42:37 +08:00
parent 18050e037f
commit c567d961c1
21 changed files with 342 additions and 39 deletions

View File

@ -1,23 +1,22 @@
package com.codesdream.ase.component;
import com.codesdream.ase.model.permission.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
/**
* 生成功能性权限容器列表
*/
@Component
public class UserFunctionalPermissionContainersListGenerator {
private UserFunctionalScopeRelationsListGenerator functionalScopeRelationsListGenerator;
public UserFunctionalPermissionContainersListGenerator(){
this.functionalScopeRelationsListGenerator =
new UserFunctionalScopeRelationsListGenerator();
public UserFunctionalPermissionContainersListGenerator(
UserFunctionalScopeRelationsListGenerator userFunctionalScopeRelationsListGenerator)
{
if(userFunctionalScopeRelationsListGenerator == null) throw new NullPointerException();
this.functionalScopeRelationsListGenerator = userFunctionalScopeRelationsListGenerator;
}
public Collection<FunctionalPermissionContainer> generateFunctionalContainers(

View File

@ -3,23 +3,23 @@ package com.codesdream.ase.component;
import com.codesdream.ase.model.permission.FunctionalScopeRelation;
import com.codesdream.ase.model.permission.PermissionContainersCollection;
import com.codesdream.ase.model.permission.User;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
/**
* 生成功能性权限容器与范围性权限容器关联对列表
*/
@Component
public class UserFunctionalScopeRelationsListGenerator {
private UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator;
public UserFunctionalScopeRelationsListGenerator(){
public UserFunctionalScopeRelationsListGenerator(
UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator)
{
if(userPermissionContainersCollectionsListGenerator == null) throw new NullPointerException();
this.userPermissionContainersCollectionsListGenerator =
new UserPermissionContainersCollectionsListGenerator();
userPermissionContainersCollectionsListGenerator;
}
public Collection<FunctionalScopeRelation> generateFunctionalScopeRelations(

View File

@ -3,25 +3,21 @@ package com.codesdream.ase.component;
import com.codesdream.ase.model.permission.PermissionContainersCollection;
import com.codesdream.ase.model.permission.Tag;
import com.codesdream.ase.model.permission.User;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
/**
* 生成权限容器集合列表
*/
@Component
public class UserPermissionContainersCollectionsListGenerator {
private UserTagsListGenerator userTagsListGenerator;
public UserPermissionContainersCollectionsListGenerator(){
this.userTagsListGenerator =
new UserTagsListGenerator();
public UserPermissionContainersCollectionsListGenerator(UserTagsListGenerator userTagsListGenerator)
{
if(userTagsListGenerator == null) throw new NullPointerException();
this.userTagsListGenerator = userTagsListGenerator;
}
public Collection<PermissionContainersCollection> generatePermissionContainerCollections(

View File

@ -2,27 +2,25 @@ package com.codesdream.ase.component;
import com.codesdream.ase.model.permission.FunctionalPermissionContainer;
import com.codesdream.ase.model.permission.User;
import org.springframework.beans.factory.annotation.Autowired;
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 {
private UserFunctionalPermissionContainersListGenerator functionalPermissionContainersListGenerator;
public UserRolesListGenerator(){
public UserRolesListGenerator(
UserFunctionalPermissionContainersListGenerator userFunctionalPermissionContainersListGenerator)
{
this.functionalPermissionContainersListGenerator =
new UserFunctionalPermissionContainersListGenerator();
userFunctionalPermissionContainersListGenerator;
}
public Collection<GrantedAuthority> generateRoles(

View File

@ -14,7 +14,6 @@ import java.util.Optional;
/**
* 生成用户标签列表
*/
@Component
public class UserTagsListGenerator {
@Resource
UserRepository userRepository;

View File

@ -0,0 +1,34 @@
package com.codesdream.ase.configure;
import com.codesdream.ase.component.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ComponentsConfigure {
@Bean
UserTagsListGenerator userTagsListGenerator(){
return new UserTagsListGenerator();
}
@Bean
UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator(){
return new UserPermissionContainersCollectionsListGenerator(userTagsListGenerator());
}
@Bean
UserFunctionalScopeRelationsListGenerator userFunctionalScopeRelationsListGenerator(){
return new UserFunctionalScopeRelationsListGenerator(userPermissionContainersCollectionsListGenerator());
}
@Bean
UserFunctionalPermissionContainersListGenerator userFunctionalPermissionContainersListGenerator(){
return new UserFunctionalPermissionContainersListGenerator(userFunctionalScopeRelationsListGenerator());
}
@Bean
UserRolesListGenerator userRolesListGenerator(){
return new UserRolesListGenerator(userFunctionalPermissionContainersListGenerator());
}
}

View File

@ -16,4 +16,5 @@ import java.util.Map;
public class GlobalConfigure {
}

View File

@ -0,0 +1,12 @@
package com.codesdream.ase.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping(value = "/home")
public String showHomeView(){
return "home";
}
}

View File

@ -49,8 +49,8 @@ public class RegisterController {
if (password.equals(retry_password)) {
user.setPassword(password);
userService.save(user);
return "index";
// 返回登录界面
return "login";
}
}

View File

@ -26,7 +26,7 @@ public class Tag {
private String description;
// 标签关联用户
@ManyToMany(mappedBy = "tags", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private Set<User> users;
// 启用标志

View File

@ -1,10 +1,13 @@
package com.codesdream.ase.model.permission;
import com.codesdream.ase.component.UserRolesListGenerator;
import com.codesdream.ase.service.UserService;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;
@ -14,6 +17,10 @@ import java.util.Set;
@Entity
@Table(name = "user")
public class User implements UserDetails {
@Resource
private transient UserService userService;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ -45,9 +52,14 @@ public class User implements UserDetails {
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Tag> tags;
// 用户详细信息
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private UserDetail userDetail;
// 用户认证表
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private UserAuth userAuth;
public User(String username, String password) {
this.username = username;
@ -73,12 +85,12 @@ public class User implements UserDetails {
this.enabled = true;
this.tags = new HashSet<>();
this.userDetail = new UserDetail();
this.userAuth = new UserAuth();
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
UserRolesListGenerator userRolesListGenerator = new UserRolesListGenerator();
return userRolesListGenerator.generateRoles(this);
return userService.getUserAuthorities(this);
}
}

View File

@ -0,0 +1,24 @@
package com.codesdream.ase.model.permission;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Table(name = "user_auth")
public class UserAuth {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
// 密保问题
private String user_question;
// 密保问题答案
private String user_answer;
// 用户邮箱
private String mail;
}

View File

@ -35,4 +35,7 @@ public class UserDetail {
// 学生ID
private String studentID;
// 在校状态
private boolean atSchool;
}

View File

@ -8,5 +8,5 @@ import java.util.Optional;
@Repository
public interface FunctionalPermissionRepository extends CrudRepository<FunctionalPermissionContainer, Integer> {
Optional<FunctionalPermissionRepository> findByName(String name);
Optional<FunctionalPermissionContainer> findByName(String name);
}

View File

@ -6,5 +6,5 @@ import org.springframework.data.repository.CrudRepository;
import java.util.Optional;
public interface ScopePermissionRepository extends CrudRepository<ScopePermissionContainer, Integer> {
Optional<ScopePermissionRepository> findByName(String name);
Optional<ScopePermissionContainer> findByName(String name);
}

View File

@ -1,4 +1,71 @@
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 {
// 查找用户标签
Optional<Tag> findTag(String name);
// 查找功能性权限容器
Optional<FunctionalPermissionContainer> findFunctionalPermissionContainer(String name);
// 查找范围性权限容器
Optional<ScopePermissionContainer> findScopePermissionContainer(String name);
// 标签下所有的获得权限容器集合列表
Collection<PermissionContainersCollection> getPermissionContainerCollections(Tag tag);
// 获得范围性权限容器下的所有标签列表
Collection<Tag> getTagsFromScopePermissionContainers(
ScopePermissionContainer scopePermissionContainer);
// 查找用户下的所有标签列表
Collection<Tag> getTagsFromUser(User user);
// 查找功能性权限容器下的所有范围性权限容器列表
Collection<FunctionalPermissionContainer> getFunctionPermissionContainers(
PermissionContainersCollection permissionContainersCollection);
// 查找标签下的所有用户
Collection<User> getUsersFromTag(Tag tag);
// 指定一对功能性权限容器与对应的范围性权限容器并添加到指定权限容器集合中
void addRelationItemToPermissionContainerCollectionPermissionContainerCollection(
PermissionContainersCollection permissionContainersCollection,
FunctionalPermissionContainer functionalPermissionContainer,
ScopePermissionContainer scopePermissionContainer);
// 指定多对功能性权限容器与对应的范围性权限容器并添加到指定权限容器集合中
void addRelationItemsToPermissionContainerCollectionPermissionContainerCollection(
PermissionContainersCollection permissionContainersCollection,
Collection<Pair<FunctionalPermissionContainer, ScopePermissionContainer>>
functionalScopePermissionContainerPairs);
// 添加一个用户到指定标签中
void addUserToTag(Tag tag, User user);
// 添加多个用户到指定标签中
void addUsersToTag(Tag tag, Collection<User> users);
// 为功能性权限容器添加一个访问控制角色
void addRoleToFunctionalPermissionContainer(
FunctionalPermissionContainer functionalPermissionContainer,
String role);
// 为功能性权限容器添加多个访问控制角色
void addRolesToFunctionalPermissionContainer(
FunctionalPermissionContainer functionalPermissionContainer,
Collection<String> roles);
void save(Tag tag);
void save(FunctionalPermissionContainer functionalPermissionContainer);
void save(ScopePermissionContainer scopePermissionContainer);
}

View File

@ -1,18 +1,32 @@
package com.codesdream.ase.service;
import com.codesdream.ase.model.permission.User;
import org.springframework.security.core.GrantedAuthority;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
public interface IUserService {
// 获得一个空的默认用户
User getDefaultUser();
List<User> findAll();
Optional<User> findUserById(int id);
Optional<User> findUserByUsername(String username);
// 获得用户所有的权限角色
Collection<? extends GrantedAuthority> getUserAuthorities(User user);
// 更新用户的密码
void updatePassword(User user, String password);
User save(User user);
// 获得默认用户
User getDefaultUser();
User update(User user);
}

View File

@ -1,8 +1,127 @@
package com.codesdream.ase.service;
import com.codesdream.ase.component.UserFunctionalPermissionContainersListGenerator;
import com.codesdream.ase.component.UserFunctionalScopeRelationsListGenerator;
import com.codesdream.ase.model.permission.*;
import com.codesdream.ase.repository.FunctionalPermissionRepository;
import com.codesdream.ase.repository.ScopePermissionRepository;
import com.codesdream.ase.repository.TagRepository;
import javafx.util.Pair;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
@Service
public class PermissionService implements IPermissionService {
@Resource
private TagRepository tagRepository;
@Resource
private FunctionalPermissionRepository functionalPermissionRepository;
@Resource
private ScopePermissionRepository scopePermissionRepository;
@Resource
private UserFunctionalPermissionContainersListGenerator userFunctionalPermissionContainersListGenerator;
@Resource
UserFunctionalScopeRelationsListGenerator userFunctionalScopeRelationsListGenerator;
@Override
public Optional<Tag> findTag(String name) {
return tagRepository.findByName(name);
}
@Override
public Optional<FunctionalPermissionContainer> findFunctionalPermissionContainer(String name) {
return functionalPermissionRepository.findByName(name);
}
@Override
public Optional<ScopePermissionContainer> findScopePermissionContainer(String name) {
return scopePermissionRepository.findByName(name);
}
@Override
public Collection<PermissionContainersCollection> getPermissionContainerCollections(Tag tag) {
return new ArrayList<>(tag.getPermissionContainersCollections());
}
@Override
public Collection<Tag> getTagsFromScopePermissionContainers(ScopePermissionContainer scopePermissionContainer) {
return new ArrayList<>(scopePermissionContainer.getTags());
}
@Override
public Collection<Tag> getTagsFromUser(User user) {
return new ArrayList<>(user.getTags());
}
@Override
public Collection<FunctionalPermissionContainer> getFunctionPermissionContainers(PermissionContainersCollection permissionContainersCollection) {
Collection<PermissionContainersCollection> permissionContainersCollections = new ArrayList<PermissionContainersCollection>(){{
add(permissionContainersCollection);
}};
// 生成功能性与范围性权限容器关联对
Collection<FunctionalScopeRelation> functionalScopeRelations =
userFunctionalScopeRelationsListGenerator.generateFunctionalScopeRelations(permissionContainersCollections);
return userFunctionalPermissionContainersListGenerator.generateFunctionalContainers(functionalScopeRelations);
}
@Override
public Collection<User> getUsersFromTag(Tag tag) {
return null;
}
@Override
public void addRelationItemToPermissionContainerCollectionPermissionContainerCollection(PermissionContainersCollection permissionContainersCollection, FunctionalPermissionContainer functionalPermissionContainer, ScopePermissionContainer scopePermissionContainer) {
}
@Override
public void addRelationItemsToPermissionContainerCollectionPermissionContainerCollection(PermissionContainersCollection permissionContainersCollection, Collection<Pair<FunctionalPermissionContainer, ScopePermissionContainer>> functionalScopePermissionContainerPairs) {
}
@Override
public void addUserToTag(Tag tag, User user) {
}
@Override
public void addUsersToTag(Tag tag, Collection<User> users) {
}
@Override
public void addRoleToFunctionalPermissionContainer(FunctionalPermissionContainer functionalPermissionContainer, String role) {
}
@Override
public void addRolesToFunctionalPermissionContainer(FunctionalPermissionContainer functionalPermissionContainer, Collection<String> roles) {
}
@Override
public void save(Tag tag) {
}
@Override
public void save(FunctionalPermissionContainer functionalPermissionContainer) {
}
@Override
public void save(ScopePermissionContainer scopePermissionContainer) {
}
}

View File

@ -1,16 +1,22 @@
package com.codesdream.ase.service;
import com.codesdream.ase.component.ASEPasswordEncoder;
import com.codesdream.ase.component.UserRolesListGenerator;
import com.codesdream.ase.model.permission.User;
import com.codesdream.ase.repository.UserRepository;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@Service
public class UserService implements IUserService {
@Resource
UserRolesListGenerator userRolesListGenerator;
@Resource
UserRepository userRepository;
@ -32,6 +38,17 @@ public class UserService implements IUserService {
return userRepository.findByUsername(username);
}
@Override
public Collection<? extends GrantedAuthority> getUserAuthorities(User user) {
return userRolesListGenerator.generateRoles(user);
}
@Override
public void updatePassword(User user, String password) {
user.setPassword(asePasswordEncoder.encode(password));
update(user);
}
@Override
public User save(User user) {
// 查找用户名是否已经被注册
@ -41,11 +58,18 @@ public class UserService implements IUserService {
return userRepository.save(user);
}
@Override
public User update(User user) {
if(!userRepository.findByUsername(user.getUsername()).isPresent())
throw new RuntimeException("Username Already Exists");
return userRepository.save(user);
}
// 获得一个默认初始化的用户对象
@Override
public User getDefaultUser() {
return new User();
}
}

View File

@ -47,6 +47,7 @@ public class UserTest {
assertTrue(user.isEnabled());
assertFalse(user.isDeleted());
}
@Test
public void UserBaseTest_2(){
// 用户

View File

@ -33,7 +33,7 @@
</div>
<div class="form-group">
<label for="userquestion">(密保问题)</label>
<label for="user-question">(密保问题)</label>
<input type="text" class="form-control" id="answer" name="answer" placeholder="这里输入答案">
</div>
<div class="form-group">