wangwei 7 лет назад
Родитель
Сommit
91345581bc

+ 1 - 1
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/controller/AuthController.java

@@ -62,7 +62,7 @@ public class AuthController extends ControllerSupport {
 
 	@ApiOperation(value = "第三方机构接入", notes = "")
 	@PostMapping("/thirdPartyAccess")
-	public User thirdPartyAccess(@RequestParam long orgId, @RequestParam String userId,
+	public User thirdPartyAccess(@RequestParam Long orgId, @RequestParam String userId,
 			@RequestParam String appId, @RequestParam String timestamp,
 			@RequestParam String token) {
 		return authService.thirdPartyAccess(orgId, userId, appId, timestamp, token);

+ 2 - 2
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/AuthService.java

@@ -44,7 +44,7 @@ public interface AuthService {
 	 * 第三方登陆名接入
 	 *
 	 * @author WANGWEI
-	 * @param orgId
+	 * @param rootOrgId
 	 * @param loginName
 	 * @param appId
 	 * @param timestamp
@@ -52,7 +52,7 @@ public interface AuthService {
 	 * @return
 	 * @throws StatusException
 	 */
-	User thirdPartyAccess(long orgId, String loginName, String appId, String timestamp,
+	User thirdPartyAccess(Long rootOrgId, String loginName, String appId, String timestamp,
 			String token) throws StatusException;
 
 }

+ 77 - 33
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/AuthServiceImpl.java

@@ -141,9 +141,6 @@ public class AuthServiceImpl implements AuthService {
 			throw new StatusException("B-001003", "密码错误");
 		}
 
-		List<Role> roleList = Lists.newArrayList();
-		user.setRoleList(roleList);
-
 		user.setUserId(userEntity.getId());
 		user.setDisplayName(userEntity.getLoginName());
 		user.setRootOrgId(userEntity.getRootOrgId());
@@ -155,31 +152,10 @@ public class AuthServiceImpl implements AuthService {
 		}
 
 		List<UserRole> userRoles = userEntity.getUserRoles();
+		List<Role> roleList = getRoles(userRoles);
+		user.setRoleList(roleList);
 
-		if (CollectionUtils.isNotEmpty(userRoles)) {
-			for (UserRole cur : userRoles) {
-				String roleCode = cur.getRoleCode();
-				cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity roleEntity = roleRepo
-						.findByCode(roleCode);
-				if (null == roleEntity) {
-					throw new StatusException("B-002002",
-							"role code is wrong. roleCode=" + roleCode);
-				}
-				Role role = new Role(roleEntity.getId(), roleEntity.getCode(),
-						roleEntity.getName());
-				roleList.add(role);
-			}
-		}
-
-		user.setTokenCreationTime(new Date());
-		user.setToken(UUID.randomUUID());
-
-		String key = buildUserKey(user.getUserType(), user.getRootOrgId(), user.getUserId());
-		user.setKey(key);
-		user.setUserToken(key + ":" + user.getToken());
-
-		int sessionTimeout = PropertiesUtil.getInt(CommonPropKeys.SESSION_TIMEOUT, 3600);
-		redisClient.set(key, user, sessionTimeout);
+		setSession(user);
 
 		return user;
 	}
@@ -220,10 +196,16 @@ public class AuthServiceImpl implements AuthService {
 	}
 
 	@Override
-	public User thirdPartyAccess(long orgId, String loginName, String appId, String timestamp,
+	public User thirdPartyAccess(Long rootOrgId, String loginName, String appId, String timestamp,
 			String token) throws StatusException {
+
+		Org rootOrg = orgRepo.findOne(rootOrgId);
+		if (null == rootOrg) {
+			throw new StatusException("B-001002", "机构不存在");
+		}
+
 		ThirdPartyAccess thirdPartyAccess = thirdPartyAccessDao
-				.findOne(new ThirdPartyAccessPK(orgId, appId));
+				.findOne(new ThirdPartyAccessPK(rootOrgId, appId));
 
 		if (null == thirdPartyAccess) {
 			throw new StatusException("B-001201", "第三方系统接入信息未配置");
@@ -242,7 +224,7 @@ public class AuthServiceImpl implements AuthService {
 		}
 
 		String secretKey = thirdPartyAccess.getSecretKey();
-		String joinStr = StringUtil.join(loginName, orgId, appId, timestamp, secretKey);
+		String joinStr = StringUtil.join(loginName, rootOrgId, appId, timestamp, secretKey);
 		byte[] bytes = SHA256.encode(joinStr);
 		String hexAscii = ByteUtil.toHexAscii(bytes);
 
@@ -250,12 +232,74 @@ public class AuthServiceImpl implements AuthService {
 			throw new StatusException("B-001204", "token校验失败");
 		}
 
-		UserEntity user = userRepo.findByRootOrgIdAndLoginName(orgId, loginName);
-		if (user == null) {
+		UserEntity userEntity = userRepo.findByRootOrgIdAndLoginName(rootOrgId, loginName);
+		if (userEntity == null) {
 			throw new StatusException("B-001205", "用户不存在");
 		}
 
-		return null;
+		User user = new User();
+
+		user.setUserId(userEntity.getId());
+		user.setDisplayName(userEntity.getLoginName());
+		user.setRootOrgId(userEntity.getRootOrgId());
+		user.setRootOrgName(rootOrg.getName());
+		user.setOrgId(userEntity.getOrgId());
+		if (null != user.getOrgId()) {
+			Org org = orgRepo.findOne(user.getOrgId());
+			user.setOrgName(org.getName());
+		}
+
+		List<UserRole> userRoles = userEntity.getUserRoles();
+		List<Role> roleList = getRoles(userRoles);
+		user.setRoleList(roleList);
+
+		setSession(user);
+
+		return user;
+	}
+
+	/**
+	 * 设置session
+	 *
+	 * @author WANGWEI
+	 * @param user
+	 */
+	private void setSession(User user) {
+		user.setTokenCreationTime(new Date());
+		user.setToken(UUID.randomUUID());
+
+		String key = buildUserKey(user.getUserType(), user.getRootOrgId(), user.getUserId());
+		user.setKey(key);
+		user.setUserToken(key + ":" + user.getToken());
+
+		int sessionTimeout = PropertiesUtil.getInt(CommonPropKeys.SESSION_TIMEOUT, 3600);
+		redisClient.set(key, user, sessionTimeout);
+	}
+
+	/**
+	 * 获取角色集合
+	 *
+	 * @author WANGWEI
+	 * @param userRoles
+	 * @return
+	 */
+	private List<Role> getRoles(List<UserRole> userRoles) {
+		List<Role> roleList = Lists.newArrayList();
+		if (CollectionUtils.isNotEmpty(userRoles)) {
+			for (UserRole cur : userRoles) {
+				String roleCode = cur.getRoleCode();
+				cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity roleEntity = roleRepo
+						.findByCode(roleCode);
+				if (null == roleEntity) {
+					throw new StatusException("B-002002",
+							"role code is wrong. roleCode=" + roleCode);
+				}
+				Role role = new Role(roleEntity.getId(), roleEntity.getCode(),
+						roleEntity.getName());
+				roleList.add(role);
+			}
+		}
+		return roleList;
 	}
 
 }