|
@@ -15,13 +15,20 @@ import cn.com.qmth.examcloud.commons.base.util.UUID;
|
|
|
import cn.com.qmth.examcloud.commons.web.redis.RedisClient;
|
|
|
import cn.com.qmth.examcloud.commons.web.security.bean.Role;
|
|
|
import cn.com.qmth.examcloud.commons.web.security.bean.User;
|
|
|
-import cn.com.qmth.examcloud.commons.web.support.ResponseStatus;
|
|
|
+import cn.com.qmth.examcloud.commons.web.security.enums.RoleMeta;
|
|
|
import cn.com.qmth.examcloud.core.basic.api.UserCloudService;
|
|
|
-import cn.com.qmth.examcloud.core.basic.api.bean.RoleBean;
|
|
|
-import cn.com.qmth.examcloud.core.basic.api.bean.UserBean;
|
|
|
-import cn.com.qmth.examcloud.core.basic.api.request.LoginReq;
|
|
|
-import cn.com.qmth.examcloud.core.basic.api.response.LoginResp;
|
|
|
+import cn.com.qmth.examcloud.core.basic.base.enums.AccountType;
|
|
|
+import cn.com.qmth.examcloud.core.basic.base.enums.UserType;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.RoleRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.StudentRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.UserRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.Org;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.Student;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserRole;
|
|
|
import cn.com.qmth.examcloud.core.basic.service.AuthService;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.UserService;
|
|
|
import cn.com.qmth.examcloud.core.basic.service.bean.LoginInfo;
|
|
|
|
|
|
/**
|
|
@@ -40,48 +47,115 @@ public class AuthServiceImpl implements AuthService {
|
|
|
@Autowired
|
|
|
UserCloudService userCloudService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserRepo userRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OrgRepo orgRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RoleRepo roleRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ StudentRepo studentRepo;
|
|
|
+
|
|
|
@Override
|
|
|
public User login(LoginInfo loginInfo) {
|
|
|
- LoginReq loginReq = new LoginReq();
|
|
|
- loginReq.setAccountType(loginInfo.getAccountType());
|
|
|
- loginReq.setAccountValue(loginInfo.getAccountValue());
|
|
|
- loginReq.setRootOrgId(loginInfo.getRootOrgId());
|
|
|
- loginReq.setDomain(loginInfo.getDomain());
|
|
|
- loginReq.setPassword(loginInfo.getPassword());
|
|
|
- LoginResp loginResp = null;
|
|
|
- try {
|
|
|
- loginResp = userCloudService.login(loginReq);
|
|
|
- } catch (StatusException e) {
|
|
|
- if (e.getCode().equals(ResponseStatus.SERVER_ERROR.getCode())) {
|
|
|
- throw e;
|
|
|
- } else {
|
|
|
- throw new StatusException("P-001001", "用户名或密码错误", e);
|
|
|
+
|
|
|
+ String rootOrgId = loginInfo.getRootOrgId();
|
|
|
+ Org rootOrg = null;
|
|
|
+ if (StringUtils.isBlank(rootOrgId)) {
|
|
|
+ if (StringUtils.isBlank(loginInfo.getDomain())) {
|
|
|
+ throw new StatusException("B-001001", "domain,rootOrgId 必须有一个不为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ rootOrg = orgRepo.findFirstByParentIdAndCode(0L, loginInfo.getDomain());
|
|
|
+
|
|
|
+ if (null == rootOrg) {
|
|
|
+ throw new StatusException("B-001002", "机构不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ rootOrgId = String.valueOf(rootOrg.getId());
|
|
|
+
|
|
|
+ } else {
|
|
|
+ rootOrg = orgRepo.findOne(Long.valueOf(rootOrgId));
|
|
|
+ if (null == rootOrg) {
|
|
|
+ throw new StatusException("B-001002", "机构不存在");
|
|
|
}
|
|
|
}
|
|
|
- UserBean userBean = loginResp.getUserBean();
|
|
|
|
|
|
+ String accountType = loginInfo.getAccountType();
|
|
|
+ String accountValue = loginInfo.getAccountValue();
|
|
|
+ String password = loginInfo.getPassword();
|
|
|
+
|
|
|
+ UserEntity userEntity = null;
|
|
|
+ Student student = null;
|
|
|
User user = new User();
|
|
|
+
|
|
|
+ // 常规账户登录
|
|
|
+ if (AccountType.COMMON_LOGIN_NAME.getCode().equals(accountType)) {
|
|
|
+ user.setUserType(UserType.COMMON.getCode());
|
|
|
+ userEntity = userService.getUser(Long.parseLong(rootOrgId), accountValue);
|
|
|
+ } else {
|
|
|
+ // 学生学号登录
|
|
|
+ if (AccountType.STUDENT_CODE.getCode().equals(accountType)) {
|
|
|
+ student = studentRepo.findByStudentCodeAndRootOrgId(accountValue, rootOrg.getId());
|
|
|
+ }
|
|
|
+ // 学生身份证号登录
|
|
|
+ else if (AccountType.STUDENT_IDENTITY_NUMBER.getCode().equals(accountType)) {
|
|
|
+ student = studentRepo.findByIdentityNumberAndRootOrgId(accountValue,
|
|
|
+ rootOrg.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ user.setUserType(UserType.STUDENT.getCode());
|
|
|
+ user.setStudentCode(student.getStudentCode());
|
|
|
+ user.setIdentityNumber(student.getIdentityNumber());
|
|
|
+
|
|
|
+ userEntity = userRepo.findOne(student.getUser().getId());
|
|
|
+ List<UserRole> roleList = Lists.newArrayList();
|
|
|
+ UserRole userRole = new UserRole();
|
|
|
+ userRole.setRoleCode(RoleMeta.STUDENT.name());
|
|
|
+ roleList.add(userRole);
|
|
|
+ userEntity.setUserRoles(roleList);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null == userEntity) {
|
|
|
+ throw new StatusException("B-001004", "用户不存在");
|
|
|
+ }
|
|
|
+ String rightPassword = userEntity.getPassword();
|
|
|
+ if (!rightPassword.equals(password)) {
|
|
|
+ throw new StatusException("B-001003", "密码错误");
|
|
|
+ }
|
|
|
+
|
|
|
List<Role> roleList = Lists.newArrayList();
|
|
|
user.setRoleList(roleList);
|
|
|
|
|
|
- user.setUserType(userBean.getUserType());
|
|
|
- user.setUserId(userBean.getUserId());
|
|
|
- user.setStudentId(userBean.getStudentId());
|
|
|
- user.setDisplayName(userBean.getDisplayName());
|
|
|
- user.setRootOrgId(userBean.getRootOrgId());
|
|
|
- user.setRootOrgName(userBean.getRootOrgName());
|
|
|
- user.setOrgId(userBean.getOrgId());
|
|
|
- user.setOrgName(userBean.getOrgName());
|
|
|
- user.setIdentityNumber(userBean.getIdentityNumber());
|
|
|
- user.setStudentCode(userBean.getStudentCode());
|
|
|
-
|
|
|
- List<RoleBean> roleBeanList = userBean.getRoleList();
|
|
|
-
|
|
|
- if (CollectionUtils.isNotEmpty(roleBeanList)) {
|
|
|
- for (RoleBean cur : roleBeanList) {
|
|
|
- Role role = new Role();
|
|
|
- role.setRoleCode(cur.getRoleCode());
|
|
|
- role.setRoleName(cur.getRoleName());
|
|
|
+ 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();
|
|
|
+
|
|
|
+ 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);
|
|
|
}
|
|
|
}
|