|
@@ -1,24 +1,28 @@
|
|
package cn.com.qmth.examcloud.service.core.service;
|
|
package cn.com.qmth.examcloud.service.core.service;
|
|
|
|
|
|
|
|
+import cn.com.qmth.examcloud.common.uac.AccessCtrlUtil;
|
|
|
|
+import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
|
|
|
|
+import cn.com.qmth.examcloud.common.util.RedisUtil;
|
|
import cn.com.qmth.examcloud.service.core.dao.Org;
|
|
import cn.com.qmth.examcloud.service.core.dao.Org;
|
|
import cn.com.qmth.examcloud.service.core.dao.Student;
|
|
import cn.com.qmth.examcloud.service.core.dao.Student;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.dao.User;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.dao.UserRole;
|
|
import cn.com.qmth.examcloud.service.core.dto.UserInfo;
|
|
import cn.com.qmth.examcloud.service.core.dto.UserInfo;
|
|
import cn.com.qmth.examcloud.service.core.enums.UserType;
|
|
import cn.com.qmth.examcloud.service.core.enums.UserType;
|
|
import cn.com.qmth.examcloud.service.core.params.UserParam;
|
|
import cn.com.qmth.examcloud.service.core.params.UserParam;
|
|
import cn.com.qmth.examcloud.service.core.repo.OrgRepo;
|
|
import cn.com.qmth.examcloud.service.core.repo.OrgRepo;
|
|
import cn.com.qmth.examcloud.service.core.repo.StudentRepo;
|
|
import cn.com.qmth.examcloud.service.core.repo.StudentRepo;
|
|
import cn.com.qmth.examcloud.service.core.repo.UserRepo;
|
|
import cn.com.qmth.examcloud.service.core.repo.UserRepo;
|
|
-import cn.com.qmth.examcloud.service.core.dao.User;
|
|
|
|
|
|
+import cn.com.qmth.examcloud.service.core.repo.UserRoleRepo;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
-import org.springframework.data.domain.Example;
|
|
|
|
-import org.springframework.data.domain.ExampleMatcher;
|
|
|
|
-import org.springframework.data.domain.Pageable;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
|
|
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Set;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 用户服务类
|
|
* 用户服务类
|
|
@@ -36,6 +40,9 @@ public class UserService {
|
|
@Autowired
|
|
@Autowired
|
|
OrgRepo orgRepo;
|
|
OrgRepo orgRepo;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ UserRoleRepo userRoleRepo;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 初始化密码
|
|
* 初始化密码
|
|
* @param userId
|
|
* @param userId
|
|
@@ -62,7 +69,8 @@ public class UserService {
|
|
* @param password
|
|
* @param password
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public ResponseEntity login(String loginName,String password){
|
|
|
|
|
|
+ public ResponseEntity login(String loginName,
|
|
|
|
+ String password){
|
|
User user = userRepo.findByLoginName(loginName);
|
|
User user = userRepo.findByLoginName(loginName);
|
|
return loginProcess(user,password);
|
|
return loginProcess(user,password);
|
|
|
|
|
|
@@ -75,7 +83,9 @@ public class UserService {
|
|
* @param password
|
|
* @param password
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public ResponseEntity login(long orgId,String loginName,String password){
|
|
|
|
|
|
+ public ResponseEntity login(long orgId,
|
|
|
|
+ String loginName,
|
|
|
|
+ String password){
|
|
User user = userRepo.findByRootOrgIdAndLoginName(orgId,loginName);
|
|
User user = userRepo.findByRootOrgIdAndLoginName(orgId,loginName);
|
|
return loginProcess(user,password);
|
|
return loginProcess(user,password);
|
|
}
|
|
}
|
|
@@ -86,16 +96,35 @@ public class UserService {
|
|
* @param password
|
|
* @param password
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public ResponseEntity loginProcess(User user,String password){
|
|
|
|
|
|
+ public ResponseEntity loginProcess(User user,
|
|
|
|
+ String password){
|
|
if(user == null){
|
|
if(user == null){
|
|
return new ResponseEntity("该用户不存在",HttpStatus.NOT_FOUND);
|
|
return new ResponseEntity("该用户不存在",HttpStatus.NOT_FOUND);
|
|
}else if(!user.getPassword().equals(password)){
|
|
}else if(!user.getPassword().equals(password)){
|
|
return new ResponseEntity("密码错误",HttpStatus.EXPECTATION_FAILED);
|
|
return new ResponseEntity("密码错误",HttpStatus.EXPECTATION_FAILED);
|
|
}else{
|
|
}else{
|
|
- String token = "";
|
|
|
|
|
|
+ String token = AccessCtrlUtil.buildToken();
|
|
|
|
+ createAccessUser(token,user);
|
|
return new ResponseEntity(getUserInfo(user,token),HttpStatus.OK);
|
|
return new ResponseEntity(getUserInfo(user,token),HttpStatus.OK);
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 创建访问用户并加入K/V缓存
|
|
|
|
+ * @param token
|
|
|
|
+ * @param user
|
|
|
|
+ */
|
|
|
|
+ public void createAccessUser(String token,User user){
|
|
|
|
+ AccessUser accessUser = new AccessUser();
|
|
|
|
+ Set<UserRole> userRoleSet = userRoleRepo.findByUserId(user.getId());
|
|
|
|
+ Map<String,Set<String>> rolesMap = userRoleSet.stream()
|
|
|
|
+ .collect(Collectors.groupingBy(UserRole::getAppCode,
|
|
|
|
+ Collectors.mapping(UserRole::getRoleCode,Collectors.toSet())));
|
|
|
|
+ accessUser.setLoginName(user.getLoginName());
|
|
|
|
+ accessUser.setOrgId(user.getOrgId());
|
|
|
|
+ accessUser.setUserId(user.getId());
|
|
|
|
+ accessUser.setRoles(rolesMap);
|
|
|
|
+ RedisUtil.setByte(token,accessUser);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|