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