瀏覽代碼

重构登录部分方法

宋悦 8 年之前
父節點
當前提交
ee4d441fe1

+ 8 - 1
core-api/src/main/java/cn/com/qmth/examcloud/service/core/api/StudentApi.java

@@ -1,5 +1,7 @@
 package cn.com.qmth.examcloud.service.core.api;
 
+import cn.com.qmth.examcloud.common.util.error.CommonError;
+import cn.com.qmth.examcloud.service.core.dto.UserInfo;
 import io.swagger.annotations.ApiOperation;
 
 import java.io.File;
@@ -100,6 +102,11 @@ public class StudentApi {
                                 @RequestParam String loginName,
                                 @RequestParam String password,
                                 @RequestParam LoginType loginType){
-        return new ResponseEntity(studentService.login(orgCode,loginName,password,loginType), HttpStatus.OK);
+        try{
+            UserInfo userInfo = studentService.login(orgCode,loginName,password,loginType);
+            return new ResponseEntity(userInfo,HttpStatus.OK);
+        }catch (Exception e){
+            return new ResponseEntity(new CommonError(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
+        }
     }
 }

+ 14 - 2
core-api/src/main/java/cn/com/qmth/examcloud/service/core/api/UserApi.java

@@ -1,5 +1,7 @@
 package cn.com.qmth.examcloud.service.core.api;
 
+import cn.com.qmth.examcloud.common.util.error.CommonError;
+import cn.com.qmth.examcloud.service.core.dto.UserInfo;
 import io.swagger.annotations.ApiOperation;
 
 import java.util.Date;
@@ -165,7 +167,12 @@ public class UserApi {
     @PostMapping("/login")
     public ResponseEntity login(@RequestParam String loginName,
                                 @RequestParam String password){
-        return new ResponseEntity(userService.login(loginName,password),HttpStatus.OK);
+        try{
+            UserInfo userInfo = userService.login(loginName,password);
+            return new ResponseEntity(userInfo,HttpStatus.OK);
+        }catch (Exception e){
+            return new ResponseEntity(new CommonError(e.getMessage()),HttpStatus.INTERNAL_SERVER_ERROR);
+        }
     }
 
     @ApiOperation(value="二级登录",notes="二级登录")
@@ -173,7 +180,12 @@ public class UserApi {
     public ResponseEntity login(@PathVariable long orgId,
                                 @RequestParam String loginName,
                                 @RequestParam String password){
-        return new ResponseEntity(userService.login(orgId,loginName,password),HttpStatus.OK);
+        try{
+            UserInfo userInfo = userService.login(orgId,loginName,password);
+            return new ResponseEntity(userInfo,HttpStatus.OK);
+        }catch (Exception e){
+            return new ResponseEntity(new CommonError(e.getMessage()),HttpStatus.INTERNAL_SERVER_ERROR);
+        }
     }
     
     @ApiOperation(value="登出",notes="登出")

+ 8 - 4
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/StudentService.java

@@ -150,8 +150,10 @@ public class StudentService {
      * @param password
      * @return
      */
-    public UserInfo login(String orgId, String loginName,
-                                   String password, LoginType loginType) {
+    public UserInfo login(String orgId,
+                          String loginName,
+                          String password,
+                          LoginType loginType) throws Exception{
         Org org = orgRepo.findByParentIdAndCode((long) 0, orgId);
         if (org == null) {
         	throw new RuntimeException("学校不存在");
@@ -169,12 +171,14 @@ public class StudentService {
         return this.loginProcess(student, password);
     }
 
-    private UserInfo loginProcess(Student student, String password) {
+    private UserInfo loginProcess(Student student, String password) throws Exception{
         if (student.getUser() == null) {
         	throw new RuntimeException("该用户不存在");
         } else if (!student.getUser().getPassword().equals(password)) {
         	throw new RuntimeException("密码错误");
-        } else {
+        } else if(!student.getUser().getEnable()){
+            throw new RuntimeException("该用户被禁用");
+        }else {
             String token = AccessCtrlUtil.buildToken();
             userService.createAccessUser(token, student.getUser(), student.getId());
             UserInfo userInfo = userService.getUserInfo(student.getUser(), token);

+ 19 - 5
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/UserService.java

@@ -9,6 +9,8 @@ import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
+import cn.com.qmth.examcloud.common.uac.enums.RoleMeta;
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Example;
@@ -31,6 +33,7 @@ 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.UserRepo;
 import cn.com.qmth.examcloud.service.core.repo.UserRoleRepo;
+import org.springframework.util.CollectionUtils;
 
 /**
  * 用户服务类
@@ -88,7 +91,7 @@ public class UserService {
      * @return
      */
     public UserInfo login(String loginName,
-                                String password){
+                          String password)throws Exception{
         User user = userRepo.findByLoginName(loginName);
         return loginProcess(user,password);
 
@@ -102,8 +105,8 @@ public class UserService {
      * @return
      */
     public UserInfo login(long orgId,
-                                String loginName,
-                                String password){
+                          String loginName,
+                          String password)throws Exception{
         User user = userRepo.findByRootOrgIdAndLoginName(orgId,loginName);
         return loginProcess(user,password);
     }
@@ -114,12 +117,13 @@ public class UserService {
      * @param password
      * @return
      */
-    public UserInfo loginProcess(User user,
-                                       String password){
+    public UserInfo loginProcess(User user, String password)throws Exception{
         if(user == null){
         	throw new RuntimeException("该用户不存在");
         }else if(!user.getPassword().equals(password)){
         	throw new RuntimeException("密码错误");
+        }else if(!user.getEnable()){
+            throw new RuntimeException("该用户被禁用");
         }else{
             String token = AccessCtrlUtil.buildToken();
             createAccessUser(token,user,null);
@@ -171,9 +175,19 @@ public class UserService {
         userInfo.setToken(token);
         userInfo.setType(user.getType().toString());
         userInfo.setUserRoles(user.getUserRoles());
+        userInfo.setRoleNames(getRoleNames(user.getUserRoles()));
         return userInfo;
     }
 
+    public List<String> getRoleNames(List<UserRole> userRoles){
+
+        List<String> roleNameList = userRoles.stream()
+                .map(userRole -> RoleMeta.valueOf(userRole.getRoleCode()).getCnName())
+                .collect(Collectors.toList());
+        return roleNameList;
+
+    }
+
     /**
      * 登出
      * @param accessUser

+ 10 - 0
core-domain/src/main/java/cn/com/qmth/examcloud/service/core/dto/UserInfo.java

@@ -39,6 +39,8 @@ public class UserInfo implements Serializable{
 
     private List<UserRole> userRoles;
 
+    private List<String> roleNames;
+
     public static long getSerialVersionUID() {
         return serialVersionUID;
     }
@@ -155,6 +157,14 @@ public class UserInfo implements Serializable{
         this.userRoles = userRoles;
     }
 
+    public List<String> getRoleNames() {
+        return roleNames;
+    }
+
+    public void setRoleNames(List<String> roleNames) {
+        this.roleNames = roleNames;
+    }
+
     public UserInfo() {
     }
 }

+ 1 - 1
core-domain/src/main/java/cn/com/qmth/examcloud/service/core/repo/UserRepo.java

@@ -33,7 +33,7 @@ public interface UserRepo extends JpaRepository<User,Long>,QueryByExampleExecuto
 
 	List<User> findByOrgId(Long orgId);
 	
-	@Query(nativeQuery = true,value = "SELECT DISTINCT * FROM ecs_core_user us, ecs_core_user_role ur WHERE us.id = ur.user_id And us.root_org_id = ? And ur.role_code = 'MARKER' ")
+	@Query(nativeQuery = true,value = "SELECT DISTINCT * FROM ecs_core_user us, ecs_core_user_role ur WHERE us.id = ur.user_id And us.root_org_id = ? And ur.role_code = 'MARKER' And us.enable = 1")
 	List<User> findMarkerByRootOrgId(Long rootOrgId);
 
 }