瀏覽代碼

完善用户操作

guochao 6 年之前
父節點
當前提交
51724a1eb6

文件差異過大導致無法顯示
+ 0 - 0
core/src/main/java/com/mooctest/crowd/domain/controller/UserController.java


+ 6 - 1
core/src/main/java/com/mooctest/crowd/domain/dao/RoleToPermissionDao.java

@@ -5,13 +5,18 @@ import com.mooctest.crowd.domain.model.RoleToPermission;
 import org.springframework.data.repository.CrudRepository;
 
 import javax.transaction.Transactional;
+import java.util.List;
 import java.util.Optional;
 
 @Transactional
 public interface RoleToPermissionDao extends CrudRepository<RoleToPermission,Long> {
 
     @Override
-    Optional<RoleToPermission> findById(Long aLong);
+    Optional<RoleToPermission> findById(Long id);
+
+    List<RoleToPermission> findAllByRoleId(Long roleId);
+
+    List<RoleToPermission> findAllByPermissionId(Long permissionId);
 
     @Override
     <S extends RoleToPermission> S save(S s);

+ 9 - 0
core/src/main/java/com/mooctest/crowd/domain/dao/UserDao.java

@@ -17,5 +17,14 @@ public interface UserDao extends PagingAndSortingRepository<UserPO, Long>, JpaSp
     @Override
     Iterable<UserPO> findAllById(Iterable<Long> iterable);
 
+    @Override
+    Iterable<UserPO> findAll();
+
+    @Override
+    void delete(UserPO userPO);
+
+    void deleteAll(Iterable<? extends UserPO> iterable);
+
     UserPO save(UserPO userPo);
+
 }

+ 9 - 39
core/src/main/java/com/mooctest/crowd/domain/domainservice/RegisterDService.java

@@ -1,19 +1,13 @@
 package com.mooctest.crowd.domain.domainservice;
 
-import com.mooctest.crowd.domain.configuration.DefaultFeatureConfiguration;
-import com.mooctest.crowd.domain.domainobject.Permission;
 import com.mooctest.crowd.domain.domainobject.Role;
 import com.mooctest.crowd.domain.domainobject.User;
 import com.mooctest.crowd.domain.factory.UserFactory;
-import com.mooctest.crowd.domain.model.UserToRole;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import com.mooctest.crowd.domain.util.EncryptionUtil;
 import org.springframework.stereotype.Service;
 
 import java.sql.Timestamp;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.List;
 
 /**
@@ -23,30 +17,14 @@ import java.util.List;
 @Service
 public class RegisterDService {
 
-//    @Autowired
-//    private UserRepo userRepo;
-
-//    @Autowired
-//    private User user = UserFactory.createUser();
-
-//    private Role role = new Role();
-
-//    private List<Role> roleList = new ArrayList<Role>();
-
-    private Permission permission = new Permission();
-
-    private List<Permission> permissionList = new ArrayList<>();
-
-    private UserToRole userToRole = new UserToRole();
-
-    private DefaultFeatureConfiguration defaultFeatureConfiguration = new DefaultFeatureConfiguration();
-
-    private Logger LOG = LoggerFactory.getLogger(getClass());
+//    private DefaultFeatureConfiguration defaultFeatureConfiguration = new DefaultFeatureConfiguration();
+//
+//    private Logger LOG = LoggerFactory.getLogger(getClass());
 
     public User register(String mobile, String password) {
         User user = UserFactory.createUser();
         user.setMobile(mobile);
-        user.setPassword(password);
+        user.setPassword(EncryptionUtil.encryptMD5(password));
         user.setIsAvailable(1);
         user.setIsDeleted(0);
         user.setCreateTime(new Timestamp(System.currentTimeMillis()));
@@ -60,18 +38,10 @@ public class RegisterDService {
         return user;
     }
 
-    private void createLogForRegister(String mobileNum, String reason) {
-        Date date = new Date();
-        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        LOG.error("用户{}注册失败,失败时间:{},失败原因:{}",new Object[]{mobileNum,df.format(date), reason});
-    }
-
-//    private User registerUser(User user) {
-//        user.setIsAvailable(1);
-//        user.setIsDeleted(0);
-//        user.setPassword(EncryptionUtil.encryptMD5(user.getPassword()));
-//        user.setCreateTime(new Timestamp(System.currentTimeMillis()));
-//        return user;
+//    private void createLogForRegister(String mobileNum, String reason) {
+//        Date date = new Date();
+//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+//        LOG.error("用户{}注册失败,失败时间:{},失败原因:{}",new Object[]{mobileNum,df.format(date), reason});
 //    }
 
 }

+ 4 - 0
core/src/main/java/com/mooctest/crowd/domain/exception/PermissionNotFoundException.java

@@ -0,0 +1,4 @@
+package com.mooctest.crowd.domain.exception;
+
+public class PermissionNotFoundException extends BaseException {
+}

+ 4 - 0
core/src/main/java/com/mooctest/crowd/domain/exception/RoleNotFoundException.java

@@ -0,0 +1,4 @@
+package com.mooctest.crowd.domain.exception;
+
+public class RoleNotFoundException extends BaseException {
+}

+ 6 - 3
core/src/main/java/com/mooctest/crowd/domain/repository/IUserRepo.java

@@ -2,7 +2,6 @@ package com.mooctest.crowd.domain.repository;
 
 import com.mooctest.crowd.domain.domainobject.User;
 import com.mooctest.crowd.domain.exception.UserNotExistException;
-import com.mooctest.crowd.domain.model.UserPO;
 
 import javax.management.relation.RoleNotFoundException;
 import java.util.List;
@@ -17,11 +16,15 @@ public interface IUserRepo {
 
     User getByID(Long id) throws UserNotExistException, RoleNotFoundException;
 
-    List<UserPO> getByIDList(List<Long> ids);
+    List<User> getByIdList(List<Long> ids) throws RoleNotFoundException;
+
+    List<User> getAllUser() throws RoleNotFoundException;
 
     void removeUser(User user);
 
-    User saveUser(User user);
+    void removeUserList(List<User> userList);
+
+    User saveUser(User user) throws RoleNotFoundException;
 
     User saveUserAndRole(User user);
 }

+ 94 - 49
core/src/main/java/com/mooctest/crowd/domain/repository/UserRepo.java

@@ -2,17 +2,17 @@ package com.mooctest.crowd.domain.repository;
 
 import com.google.common.collect.Lists;
 import com.mooctest.crowd.domain.dao.*;
+import com.mooctest.crowd.domain.domainobject.Permission;
 import com.mooctest.crowd.domain.domainobject.Role;
 import com.mooctest.crowd.domain.domainobject.User;
+import com.mooctest.crowd.domain.exception.PermissionNotFoundException;
+import com.mooctest.crowd.domain.exception.RoleNotFoundException;
 import com.mooctest.crowd.domain.exception.UserNotExistException;
-import com.mooctest.crowd.domain.model.RolePO;
-import com.mooctest.crowd.domain.model.UserPO;
-import com.mooctest.crowd.domain.model.UserToRole;
+import com.mooctest.crowd.domain.model.*;
 import com.mooctest.crowd.domain.util.Converter;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import javax.management.relation.RoleNotFoundException;
 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.List;
@@ -41,10 +41,6 @@ public class UserRepo implements IUserRepo {
     @Autowired
     private RoleToPermissionDao roleToPermissionDao;
 
-//    private UserToRole userToRole = new UserToRole();
-
-//    private RoleToPermission roleToPermission = new RoleToPermission();
-
     private Timestamp currentTime = new Timestamp(System.currentTimeMillis());
 
     @Override
@@ -53,7 +49,7 @@ public class UserRepo implements IUserRepo {
         if (!userPOOptional.isPresent()) {
             throw new UserNotExistException();
         }else{
-            User user = getUserAndRoleByUser(userPOOptional.get());
+            User user = getUserAndRoleAndPermissionByUserPO(userPOOptional.get());
             return user;
         }
     }
@@ -64,64 +60,60 @@ public class UserRepo implements IUserRepo {
         if (userPO == null) {
             throw new UserNotExistException();
         }else {
-            User user = getUserAndRoleByUser(userPO);
+            User user = getUserAndRoleAndPermissionByUserPO(userPO);
             return user;
         }
     }
 
-    private User getUserAndRoleByUser(UserPO userPO) throws RoleNotFoundException {
-        User user = Converter.convert(User.class, userPO);
-        List<UserToRole> userToRoleList = userToRoleDao.findByUserId(user.getId());
-        List<Role> roleList = new ArrayList<>();
-        for (UserToRole userToRole : userToRoleList) {
-            Optional<RolePO> rolePOOptional = roleDao.findById(userToRole.getRoleId());
-            if(rolePOOptional.isPresent()){
-                RolePO rolePO = rolePOOptional.get();
-                roleList.add(Converter.convert(Role.class, rolePO));
-            }else{
-                throw new RoleNotFoundException();
-            }
-        }
-        user.setRoleList(roleList);
-        return user;
-    }
-
     @Override
     public User saveUserAndRole(User user) {
         UserPO userPO = userDao.save(Converter.convert(UserPO.class, user));
+        User saveResultUser = Converter.convert(User.class, userPO);
         List<Role> roleList = user.getRoleList();
 
+        // 存储从数据库中取出的User的Role数据
+        List<Role> roleListResult = new ArrayList<>();
+
+        System.out.println(roleList);
         for(Role role : roleList){
             RolePO rolePO = roleDao.findByName(role.getName());
+            Role roleConvert = Converter.convert(Role.class, rolePO);
+
             UserToRole userToRole = new UserToRole();
-            userToRole.setRoleId(rolePO.getId());
-            userToRole.setUserId(userPO.getId());
+            userToRole.setRoleId(roleConvert.getId());
+            userToRole.setUserId(saveResultUser.getId());
             userToRole.setCreateTime(currentTime);
             userToRoleDao.save(userToRole);
-//            List<Permission> permissionList = role.getPermissionList();
-//            for(Permission permission : permissionList){
-//                PermissionPO permissionPO = permissionDao.findByName(permission.getName());
-//                roleToPermission.setRoleId(rolePO.getId());
-//                roleToPermission.setPermissionId(permissionPO.getId());
-//                roleToPermission.setCreateTime(currentTime);
-//                roleToPermissionDao.save(roleToPermission);
-//            }
-        }
 
-        return Converter.convert(User.class,userPO);
+            Role roleResult = getPermissionByRole(roleConvert);
+            roleListResult.add(roleResult);
+        }
+        saveResultUser.setRoleList(roleListResult);
+        return saveResultUser;
     }
 
     @Override
-    public User saveUser(User user) {
+    public User saveUser(User user) throws RoleNotFoundException {
         UserPO userPO = userDao.save(Converter.convert(UserPO.class, user));
-        return Converter.convert(User.class,userPO);
+        User userAndRoleByUserPO = getUserAndRoleAndPermissionByUserPO(userPO);
+        System.out.println(userAndRoleByUserPO);
+        return userAndRoleByUserPO;
     }
 
     @Override
-    public List<UserPO> getByIDList(List<Long> ids) {
+    public List<User> getByIdList(List<Long> ids) throws RoleNotFoundException {
         Iterable<UserPO> allUserPOById = userDao.findAllById(ids);
-        List<UserPO> userPOList = Lists.newArrayList(allUserPOById);
-        return userPOList;
+        ArrayList<UserPO> userPOArrayList = Lists.newArrayList(allUserPOById);
+        List<User> userListByIds = getUserAndRoleAndPermissionListByUserPOList(userPOArrayList);
+        return userListByIds;
+    }
+
+    @Override
+    public List<User> getAllUser() throws RoleNotFoundException {
+        Iterable<UserPO> allUserPO = userDao.findAll();
+        ArrayList<UserPO> userPOArrayList = Lists.newArrayList(allUserPO);
+        List<User> userListByIds = getUserAndRoleAndPermissionListByUserPOList(userPOArrayList);
+        return userListByIds;
     }
 
     @Override
@@ -130,10 +122,63 @@ public class UserRepo implements IUserRepo {
         userDao.delete(userPO);
     }
 
-//    @Override
-//    public User updateUser(User user) {
-//        UserPO userPO = userDao.save(Converter.convert(UserPO.class, user));
-//        return Converter.convert(User.class,userPO);
-//    }
+    @Override
+    public void removeUserList(List<User> userList) {
+        List<UserPO> userPOList = new ArrayList<>();
+        for (User user : userList) {
+            UserPO userPO = Converter.convert(UserPO.class, user);
+            userPOList.add(userPO);
+        }
+        System.out.println(userPOList.toString());
+        userDao.deleteAll(userPOList);
+    }
+
 
+    /*通过用户信息获取用户-角色-权限信息*/
+    private User getUserAndRoleAndPermissionByUserPO(UserPO userPO) throws RoleNotFoundException {
+        User userResult = Converter.convert(User.class, userPO);
+        List<UserToRole> userToRoleList = userToRoleDao.findByUserId(userResult.getId());
+        List<Role> roleResultList = new ArrayList<>();
+        for (UserToRole userToRole : userToRoleList) {
+            Optional<RolePO> rolePOOptional = roleDao.findById(userToRole.getRoleId());
+            if(rolePOOptional.isPresent()){
+                RolePO rolePO = rolePOOptional.get();
+                // 权限
+                Role roleResult = getPermissionByRole(Converter.convert(Role.class, rolePO));
+                roleResultList.add(roleResult);
+            }else{
+                throw new RoleNotFoundException();
+            }
+        }
+        userResult.setRoleList(roleResultList);
+        return userResult;
+    }
+
+    /*通过角色获取角色和权限信息*/
+    private Role getPermissionByRole(Role roleResult) {
+        List<Permission> permissionResultList = new ArrayList<>();
+        List<RoleToPermission> roleToPermissionList = roleToPermissionDao.findAllByRoleId(roleResult.getId());
+        for(RoleToPermission roleToPermission : roleToPermissionList){
+            Long permissionId = roleToPermission.getPermissionId();
+            Optional<PermissionPO> permissionPOOptional = permissionDao.findById(permissionId);
+            if(!permissionPOOptional.isPresent()){
+                throw new PermissionNotFoundException();
+            }else{
+                PermissionPO permissionPO = permissionPOOptional.get();
+                permissionResultList.add(Converter.convert(Permission.class,permissionPO));
+            }
+        }
+        roleResult.setPermissionList(permissionResultList);
+        return roleResult;
+    }
+    
+    /*通过UsePO获取用户-角色-权限信息*/
+    private List<User> getUserAndRoleAndPermissionListByUserPOList(List<UserPO> userPOList) throws RoleNotFoundException {
+        List<User> userList = new ArrayList<>();
+        for(UserPO userPO : userPOList){
+            User userAndRole = getUserAndRoleAndPermissionByUserPO(userPO);
+            userList.add(userAndRole);
+        }
+        return userList;
+    }
 }

部分文件因文件數量過多而無法顯示