|
@@ -1,474 +1,474 @@
|
|
-package cn.com.qmth.examcloud.service.core.service;
|
|
|
|
-
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.HashSet;
|
|
|
|
-import java.util.LinkedList;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Set;
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
-
|
|
|
|
-import javax.persistence.criteria.*;
|
|
|
|
-
|
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.data.domain.*;
|
|
|
|
-import org.springframework.data.jpa.domain.Specification;
|
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
-
|
|
|
|
-import ch.qos.logback.core.net.LoginAuthenticator;
|
|
|
|
-import cn.com.qmth.examcloud.common.uac.AccessCtrlUtil;
|
|
|
|
-import cn.com.qmth.examcloud.common.uac.AccessUserOps;
|
|
|
|
-import cn.com.qmth.examcloud.common.uac.AccessUserOpsForRedis;
|
|
|
|
-import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
|
|
|
|
-import cn.com.qmth.examcloud.common.uac.enums.RoleMeta;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.dto.UserInfo;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.entity.Org;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.entity.Student;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.entity.User;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.entity.UserLogin;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.entity.UserOpsLog;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.entity.UserRole;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.enums.OrgType;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.enums.UserScope;
|
|
|
|
-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.repo.OrgRepo;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.repo.StudentRepo;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.repo.UserLoginRepo;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.repo.UserOpsLogRepo;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.repo.UserRepo;
|
|
|
|
-import cn.com.qmth.examcloud.service.core.repo.UserRoleRepo;
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- * 用户服务类
|
|
|
|
- * Created by songyue on 17/1/13.
|
|
|
|
- */
|
|
|
|
-@Service
|
|
|
|
-public class UserService {
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- UserRepo userRepo;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- StudentRepo studentRepo;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- OrgRepo orgRepo;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- OrgService orgService;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- UserRoleRepo userRoleRepo;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- UserLoginRepo userLoginRepo;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- UserOpsLogRepo userOpsLogRepo;
|
|
|
|
-
|
|
|
|
- RedisTemplate redisTemplate;
|
|
|
|
-
|
|
|
|
- AccessUserOps accessUserOps;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- public UserService(RedisTemplate redisTemplate){
|
|
|
|
- this.redisTemplate = redisTemplate;
|
|
|
|
- this.accessUserOps = new AccessUserOpsForRedis(redisTemplate);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 查询所有用户
|
|
|
|
- * @param userCriteria
|
|
|
|
- * @param pageable
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public Page<User> findAll(User userCriteria, Pageable pageable){
|
|
|
|
- Specification<User> userSpecification = (root, query, cb) -> {
|
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
|
- predicates.add(cb.equal(root.get("type"),userCriteria.getType()));
|
|
|
|
- predicates.add(cb.equal(root.get("rootOrgId"),userCriteria.getRootOrgId()));
|
|
|
|
- if(StringUtils.isNotEmpty(userCriteria.getLoginName())){
|
|
|
|
- predicates.add(cb.like(root.get("loginName"),"%"+userCriteria.getLoginName()+"%"));
|
|
|
|
- }
|
|
|
|
- if(StringUtils.isNotEmpty(userCriteria.getName())){
|
|
|
|
- predicates.add(cb.like(root.get("name"),"%"+userCriteria.getName()+"%"));
|
|
|
|
- }
|
|
|
|
- if(userCriteria.getOrgId() != null){
|
|
|
|
- predicates.add(cb.equal(root.get("orgId"),userCriteria.getOrgId()));
|
|
|
|
- }
|
|
|
|
- if(userCriteria.getEnable() != null){
|
|
|
|
- predicates.add(cb.equal(root.get("enable"),userCriteria.getEnable()));
|
|
|
|
- }
|
|
|
|
- return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
- };
|
|
|
|
- List<User> users = userRepo.findAll(userSpecification);
|
|
|
|
- int pageNumber = pageable.getPageNumber();
|
|
|
|
- int pageSize = pageable.getPageSize();
|
|
|
|
- users = users.stream()
|
|
|
|
- .filter(user -> filterNonMarkerUser(user))
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- long total = users.size();
|
|
|
|
- users = users.stream()
|
|
|
|
- .skip(pageNumber * pageSize)
|
|
|
|
- .limit(pageSize)
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- Page<User> userPage = new PageImpl<User>(users,pageable,total);
|
|
|
|
- return userPage;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 过滤非评卷员
|
|
|
|
- * @param user
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- private boolean filterNonMarkerUser(User user){
|
|
|
|
- List<UserRole> userRoles = user.getUserRoles();
|
|
|
|
- if(userRoles != null){
|
|
|
|
- if(userRoles.size() == 1 && userRoles.get(0).getRoleCode().equals(RoleMeta.MARKER.name())){
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- return true;
|
|
|
|
- }else{
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 过滤管理角色
|
|
|
|
- * @param user
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- private boolean filterAdminUser(User user){
|
|
|
|
- List<UserRole> userRoles = user.getUserRoles();
|
|
|
|
- if(userRoles != null){
|
|
|
|
- for(UserRole userRole:userRoles){
|
|
|
|
- if(userRole.getRoleCode().equals(RoleMeta.MARKER.name())
|
|
|
|
- || userRole.getRoleCode().equals(RoleMeta.STUDENT.name())
|
|
|
|
- || userRole.getRoleCode().equals(RoleMeta.LC_USER.name())){
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return true;
|
|
|
|
- }else{
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 查询所有机构用户
|
|
|
|
- * @param userCriteria
|
|
|
|
- * @param pageable
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public Page<User> findOrgUser(User userCriteria,Pageable pageable){
|
|
|
|
- Specification<User> userSpecification = (root, query, cb) -> {
|
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
|
- predicates.add(cb.equal(root.get("rootOrgId"),root.get("orgId")));
|
|
|
|
- predicates.add(cb.equal(root.get("type"),userCriteria.getType()));
|
|
|
|
- if(userCriteria.getOrgId() != null){
|
|
|
|
- predicates.add(cb.equal(root.get("rootOrgId"),userCriteria.getOrgId()));
|
|
|
|
- }
|
|
|
|
- if(StringUtils.isNotEmpty(userCriteria.getLoginName())){
|
|
|
|
- predicates.add(cb.like(root.get("loginName"),"%"+userCriteria.getLoginName()+"%"));
|
|
|
|
- }
|
|
|
|
- if(StringUtils.isNotEmpty(userCriteria.getName())){
|
|
|
|
- predicates.add(cb.like(root.get("name"),"%"+userCriteria.getName()+"%"));
|
|
|
|
- }
|
|
|
|
- if(userCriteria.getEnable() != null){
|
|
|
|
- predicates.add(cb.equal(root.get("enable"),userCriteria.getEnable()));
|
|
|
|
- }
|
|
|
|
- return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
- };
|
|
|
|
- List<User> users = userRepo.findAll(userSpecification);
|
|
|
|
- int pageNumber = pageable.getPageNumber();
|
|
|
|
- int pageSize = pageable.getPageSize();
|
|
|
|
- users = users.stream()
|
|
|
|
- .filter(user -> filterAdminUser(user))
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- long total = users.size();
|
|
|
|
- users = users.stream()
|
|
|
|
- .skip(pageNumber * pageSize)
|
|
|
|
- .limit(pageSize)
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- Page<User> userPage = new PageImpl<User>(users,pageable,total);
|
|
|
|
- return userPage;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 初始化密码
|
|
|
|
- * @param userId
|
|
|
|
- */
|
|
|
|
- public void initPassword(long userId){
|
|
|
|
- User user = userRepo.findOne(userId);
|
|
|
|
- //根据用户类型采用不同的初始化策略
|
|
|
|
- if(user.getType() == UserType.NOT_STUDENT){
|
|
|
|
- //初始化为默认密码
|
|
|
|
- user.setPassword(UserParam.DEFAULT_PASSWORD);
|
|
|
|
-
|
|
|
|
- }else if(user.getType() == UserType.STUDENT){
|
|
|
|
- //截取身份证后6位为学生登录密码
|
|
|
|
- Student student = studentRepo.findByUserId(userId);
|
|
|
|
- String identityNumber = student.getIdentityNumber();
|
|
|
|
- if(StringUtils.isNotEmpty(identityNumber)){
|
|
|
|
- user.setPassword(StringUtils.substring(identityNumber,-6,identityNumber.length()));
|
|
|
|
- }else{
|
|
|
|
- user.setPassword(UserParam.DEFAULT_PASSWORD);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- user.setUpdateTime(new Date());
|
|
|
|
- userRepo.save(user);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 一般登录
|
|
|
|
- * @param loginName
|
|
|
|
- * @param password
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public UserInfo login(String loginName,
|
|
|
|
- String password)throws Exception{
|
|
|
|
- User user = userRepo.findByLoginName(loginName);
|
|
|
|
- return loginProcess(user,password);
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 二级登录
|
|
|
|
- * @param orgId
|
|
|
|
- * @param loginName
|
|
|
|
- * @param password
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public UserInfo login(long orgId,
|
|
|
|
- String loginName,
|
|
|
|
- String password)throws Exception{
|
|
|
|
- User user = userRepo.findByRootOrgIdAndLoginName(orgId,loginName);
|
|
|
|
- return loginProcess(user,password);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 登录处理
|
|
|
|
- * @param user
|
|
|
|
- * @param password
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- 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();
|
|
|
|
- initUserLogin(user);
|
|
|
|
- createAccessUser(token,user,null);
|
|
|
|
- createUserLogin(token,user);
|
|
|
|
- return getUserInfo(user,token);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 初始化用户登录
|
|
|
|
- * @param user
|
|
|
|
- */
|
|
|
|
- public void initUserLogin(User user){
|
|
|
|
- //判断是否已登录,若已登录则强制已登录用户退出
|
|
|
|
- UserLogin userLogin= userLoginRepo.findFirstByUserId(user.getId());
|
|
|
|
- if(userLogin != null){
|
|
|
|
- accessUserOps.delete(userLogin.getToken());
|
|
|
|
- userLoginRepo.deleteByUserId(userLogin.getUserId());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 创建用户操作日志
|
|
|
|
- * @param user
|
|
|
|
- * @param action
|
|
|
|
- */
|
|
|
|
- public void createUserOpsLog(User user,String action){
|
|
|
|
- UserOpsLog userOpsLog = new UserOpsLog(action,user.getId(),user.getName(),new Date());
|
|
|
|
- userOpsLogRepo.save(userOpsLog);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 创建用户登录记录
|
|
|
|
- * @param token
|
|
|
|
- * @param user
|
|
|
|
- */
|
|
|
|
- public void createUserLogin(String token,User user){
|
|
|
|
- UserLogin userLogin = new UserLogin();
|
|
|
|
- userLogin.setLoginTime(new Date());
|
|
|
|
- userLogin.setUserId(user.getId());
|
|
|
|
- userLogin.setToken(token);
|
|
|
|
- userLoginRepo.save(userLogin);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 创建访问用户并加入K/V缓存
|
|
|
|
- * @param token
|
|
|
|
- * @param user
|
|
|
|
- */
|
|
|
|
- public void createAccessUser(String token,User user,Long studentId)throws Exception{
|
|
|
|
- AccessUser accessUser = new AccessUser();
|
|
|
|
- Set<UserRole> userRoleSet = new HashSet<UserRole>(user.getUserRoles());
|
|
|
|
- Org org = orgService.findOne(user.getRootOrgId());
|
|
|
|
- Map<String,Set<String>> rolesMap = userRoleSet.stream()
|
|
|
|
- .collect(Collectors.groupingBy(UserRole::getAppCode,
|
|
|
|
- Collectors.mapping(UserRole::getRoleCode,Collectors.toSet())));
|
|
|
|
- accessUser.setLoginName(user.getLoginName());
|
|
|
|
- accessUser.setName(user.getName());
|
|
|
|
- accessUser.setOrgId(user.getOrgId());
|
|
|
|
- accessUser.setRootOrgId(user.getRootOrgId());
|
|
|
|
- if(org != null){
|
|
|
|
- accessUser.setRootOrgName(org.getName());
|
|
|
|
- }
|
|
|
|
- accessUser.setUserId(user.getId());
|
|
|
|
- accessUser.setRoles(rolesMap);
|
|
|
|
- accessUser.setToken(token);
|
|
|
|
- accessUser.setStudentId(studentId);
|
|
|
|
- accessUserOps.set(token,accessUser);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取用户信息DTO
|
|
|
|
- * @param user
|
|
|
|
- * @param token
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public UserInfo getUserInfo(User user,String token)throws Exception{
|
|
|
|
- UserInfo userInfo = new UserInfo();
|
|
|
|
- Org org = orgService.findOne(user.getOrgId());
|
|
|
|
- Org rootOrg = orgService.findOne(user.getRootOrgId());
|
|
|
|
- userInfo.setUserId(user.getId());
|
|
|
|
- userInfo.setOrgId(user.getOrgId());
|
|
|
|
- userInfo.setRootOrgId(user.getRootOrgId());
|
|
|
|
- userInfo.setName(user.getName());
|
|
|
|
- userInfo.setAvatar(user.getAvatar());
|
|
|
|
- userInfo.setLoginName(user.getLoginName());
|
|
|
|
- if(org != null){
|
|
|
|
- userInfo.setOrgName(org.getName());
|
|
|
|
- }
|
|
|
|
- if(rootOrg != null){
|
|
|
|
- userInfo.setRootOrgLogo(rootOrg.getLogo());
|
|
|
|
- userInfo.setRootOrgName(rootOrg.getName());
|
|
|
|
- }
|
|
|
|
- userInfo.setToken(token);
|
|
|
|
- userInfo.setType(user.getType().toString());
|
|
|
|
- userInfo.setUserRoles(user.getUserRoles());
|
|
|
|
- userInfo.setRoleNames(getRoleNames(user.getUserRoles()));
|
|
|
|
- return userInfo;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取角色名称
|
|
|
|
- * @param userRoles
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- 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
|
|
|
|
- */
|
|
|
|
- public void logout(AccessUser accessUser) {
|
|
|
|
- accessUserOps.delete(accessUser.getToken());
|
|
|
|
- userLoginRepo.deleteByUserId(accessUser.getUserId());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public List<User> getMarker(Long rootOrgId) {
|
|
|
|
- return userRepo.findMarkerByRootOrgId(rootOrgId);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 查询评卷员带分页
|
|
|
|
- * @param user
|
|
|
|
- * @param pageable
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public Page<User> getAllMaker(User user,Pageable pageable){
|
|
|
|
- Specification<User> specification = getSpecification(user);
|
|
|
|
- return userRepo.findAll(specification,pageable);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public Specification<User> getSpecification(User user){
|
|
|
|
- Specification<User> specification = (root, query, cb) -> {
|
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
|
- predicates.add(cb.equal(root.get("type"),UserType.NOT_STUDENT));
|
|
|
|
- predicates.add(cb.equal(root.get("rootOrgId"),user.getRootOrgId()));
|
|
|
|
- Join join = root.join("userRoles");
|
|
|
|
- predicates.add(cb.equal(join.get("roleCode"), RoleMeta.MARKER.name()));
|
|
|
|
- if(StringUtils.isNotEmpty(user.getLoginName())){
|
|
|
|
- predicates.add(cb.like(root.get("loginName"),"%"+user.getLoginName()+"%"));
|
|
|
|
- }
|
|
|
|
- if(StringUtils.isNotEmpty(user.getName())){
|
|
|
|
- predicates.add(cb.like(root.get("name"),"%"+user.getName()+"%"));
|
|
|
|
- }
|
|
|
|
- if(user.getEnable() != null){
|
|
|
|
- predicates.add(cb.equal(root.get("enable"),user.getEnable()));
|
|
|
|
- }
|
|
|
|
- return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
- };
|
|
|
|
- return specification;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public User save(User user) throws Exception{
|
|
|
|
- user.setScope(UserScope.ORG);
|
|
|
|
- user.setCreateTime(new Date());
|
|
|
|
- checkLoginName(user.getRootOrgId(), user.getLoginName());
|
|
|
|
- return userRepo.save(user);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void checkLoginName(Long rootOrgId,String loginName) {
|
|
|
|
- User old = userRepo.findByRootOrgIdAndLoginName(rootOrgId,loginName);
|
|
|
|
- if(old!=null){
|
|
|
|
- throw new RuntimeException("用户名已存在");
|
|
|
|
- }else if("admin".equalsIgnoreCase(loginName)){
|
|
|
|
- throw new RuntimeException("不能创建admin用户");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public User update(Long id, User user) {
|
|
|
|
- User old = userRepo.findOne(id);
|
|
|
|
- if(!old.getLoginName().equals(user.getLoginName())){
|
|
|
|
- checkLoginName(user.getRootOrgId(), user.getLoginName());
|
|
|
|
- }
|
|
|
|
- user.setUpdateTime(new Date());
|
|
|
|
- return userRepo.save(user);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public User saveMarker(Long rootOrgId, String loginName,String name) {
|
|
|
|
- User old = userRepo.findByRootOrgIdAndLoginName(rootOrgId,loginName);
|
|
|
|
- if(old!=null){
|
|
|
|
- return old;
|
|
|
|
- }
|
|
|
|
- User user = new User();
|
|
|
|
- user.setName(name);
|
|
|
|
- user.setLoginName(loginName);
|
|
|
|
- user.setEnable(true);
|
|
|
|
- user.setOrgId(rootOrgId);
|
|
|
|
- user.setRootOrgId(rootOrgId);
|
|
|
|
- user.setPassword(loginName);
|
|
|
|
- UserRole userRole = new UserRole(RoleMeta.MARKER.getAppCode(), RoleMeta.MARKER.toString());
|
|
|
|
- List<UserRole> userRoles = new LinkedList<UserRole>();
|
|
|
|
- userRoles.add(userRole);
|
|
|
|
- user.setUserRoles(userRoles);
|
|
|
|
- user.setScope(UserScope.ORG);
|
|
|
|
- user.setType(UserType.NOT_STUDENT);
|
|
|
|
- user.setCreateTime(new Date());
|
|
|
|
- return userRepo.save(user);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
|
|
+package cn.com.qmth.examcloud.service.core.service;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.LinkedList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Set;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+import javax.persistence.criteria.*;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.data.domain.*;
|
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import ch.qos.logback.core.net.LoginAuthenticator;
|
|
|
|
+import cn.com.qmth.examcloud.common.uac.AccessCtrlUtil;
|
|
|
|
+import cn.com.qmth.examcloud.common.uac.AccessUserOps;
|
|
|
|
+import cn.com.qmth.examcloud.common.uac.AccessUserOpsForRedis;
|
|
|
|
+import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
|
|
|
|
+import cn.com.qmth.examcloud.common.uac.enums.RoleMeta;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.dto.UserInfo;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.entity.Org;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.entity.Student;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.entity.User;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.entity.UserLogin;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.entity.UserOpsLog;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.entity.UserRole;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.enums.OrgType;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.enums.UserScope;
|
|
|
|
+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.repo.OrgRepo;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.repo.StudentRepo;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.repo.UserLoginRepo;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.repo.UserOpsLogRepo;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.repo.UserRepo;
|
|
|
|
+import cn.com.qmth.examcloud.service.core.repo.UserRoleRepo;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 用户服务类
|
|
|
|
+ * Created by songyue on 17/1/13.
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class UserService {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ UserRepo userRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ StudentRepo studentRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ OrgRepo orgRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ OrgService orgService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ UserRoleRepo userRoleRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ UserLoginRepo userLoginRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ UserOpsLogRepo userOpsLogRepo;
|
|
|
|
+
|
|
|
|
+ RedisTemplate redisTemplate;
|
|
|
|
+
|
|
|
|
+ AccessUserOps accessUserOps;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ public UserService(RedisTemplate redisTemplate){
|
|
|
|
+ this.redisTemplate = redisTemplate;
|
|
|
|
+ this.accessUserOps = new AccessUserOpsForRedis(redisTemplate);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询所有用户
|
|
|
|
+ * @param userCriteria
|
|
|
|
+ * @param pageable
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public Page<User> findAll(User userCriteria, Pageable pageable){
|
|
|
|
+ Specification<User> userSpecification = (root, query, cb) -> {
|
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
|
+ predicates.add(cb.equal(root.get("type"),userCriteria.getType()));
|
|
|
|
+ predicates.add(cb.equal(root.get("rootOrgId"),userCriteria.getRootOrgId()));
|
|
|
|
+ if(StringUtils.isNotEmpty(userCriteria.getLoginName())){
|
|
|
|
+ predicates.add(cb.like(root.get("loginName"),"%"+userCriteria.getLoginName()+"%"));
|
|
|
|
+ }
|
|
|
|
+ if(StringUtils.isNotEmpty(userCriteria.getName())){
|
|
|
|
+ predicates.add(cb.like(root.get("name"),"%"+userCriteria.getName()+"%"));
|
|
|
|
+ }
|
|
|
|
+ if(userCriteria.getOrgId() != null){
|
|
|
|
+ predicates.add(cb.equal(root.get("orgId"),userCriteria.getOrgId()));
|
|
|
|
+ }
|
|
|
|
+ if(userCriteria.getEnable() != null){
|
|
|
|
+ predicates.add(cb.equal(root.get("enable"),userCriteria.getEnable()));
|
|
|
|
+ }
|
|
|
|
+ return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
+ };
|
|
|
|
+ List<User> users = userRepo.findAll(userSpecification);
|
|
|
|
+ int pageNumber = pageable.getPageNumber();
|
|
|
|
+ int pageSize = pageable.getPageSize();
|
|
|
|
+ users = users.stream()
|
|
|
|
+ .filter(user -> filterNonMarkerUser(user))
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+ long total = users.size();
|
|
|
|
+ users = users.stream()
|
|
|
|
+ .skip(pageNumber * pageSize)
|
|
|
|
+ .limit(pageSize)
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+ Page<User> userPage = new PageImpl<User>(users,pageable,total);
|
|
|
|
+ return userPage;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 过滤非评卷员
|
|
|
|
+ * @param user
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private boolean filterNonMarkerUser(User user){
|
|
|
|
+ List<UserRole> userRoles = user.getUserRoles();
|
|
|
|
+ if(userRoles != null){
|
|
|
|
+ if(userRoles.size() == 1 && userRoles.get(0).getRoleCode().equals(RoleMeta.MARKER.name())){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 过滤管理角色
|
|
|
|
+ * @param user
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private boolean filterAdminUser(User user){
|
|
|
|
+ List<UserRole> userRoles = user.getUserRoles();
|
|
|
|
+ if(userRoles != null){
|
|
|
|
+ for(UserRole userRole:userRoles){
|
|
|
|
+ if(userRole.getRoleCode().equals(RoleMeta.MARKER.name())
|
|
|
|
+ || userRole.getRoleCode().equals(RoleMeta.STUDENT.name())
|
|
|
|
+ || userRole.getRoleCode().equals(RoleMeta.LC_USER.name())){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询所有机构用户
|
|
|
|
+ * @param userCriteria
|
|
|
|
+ * @param pageable
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public Page<User> findOrgUser(User userCriteria,Pageable pageable){
|
|
|
|
+ Specification<User> userSpecification = (root, query, cb) -> {
|
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
|
+ predicates.add(cb.equal(root.get("rootOrgId"),root.get("orgId")));
|
|
|
|
+ predicates.add(cb.equal(root.get("type"),userCriteria.getType()));
|
|
|
|
+ if(userCriteria.getOrgId() != null){
|
|
|
|
+ predicates.add(cb.equal(root.get("rootOrgId"),userCriteria.getOrgId()));
|
|
|
|
+ }
|
|
|
|
+ if(StringUtils.isNotEmpty(userCriteria.getLoginName())){
|
|
|
|
+ predicates.add(cb.like(root.get("loginName"),"%"+userCriteria.getLoginName()+"%"));
|
|
|
|
+ }
|
|
|
|
+ if(StringUtils.isNotEmpty(userCriteria.getName())){
|
|
|
|
+ predicates.add(cb.like(root.get("name"),"%"+userCriteria.getName()+"%"));
|
|
|
|
+ }
|
|
|
|
+ if(userCriteria.getEnable() != null){
|
|
|
|
+ predicates.add(cb.equal(root.get("enable"),userCriteria.getEnable()));
|
|
|
|
+ }
|
|
|
|
+ return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
+ };
|
|
|
|
+ List<User> users = userRepo.findAll(userSpecification);
|
|
|
|
+ int pageNumber = pageable.getPageNumber();
|
|
|
|
+ int pageSize = pageable.getPageSize();
|
|
|
|
+ users = users.stream()
|
|
|
|
+ .filter(user -> filterAdminUser(user))
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+ long total = users.size();
|
|
|
|
+ users = users.stream()
|
|
|
|
+ .skip(pageNumber * pageSize)
|
|
|
|
+ .limit(pageSize)
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+ Page<User> userPage = new PageImpl<User>(users,pageable,total);
|
|
|
|
+ return userPage;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 初始化密码
|
|
|
|
+ * @param userId
|
|
|
|
+ */
|
|
|
|
+ public void initPassword(long userId){
|
|
|
|
+ User user = userRepo.findOne(userId);
|
|
|
|
+ //根据用户类型采用不同的初始化策略
|
|
|
|
+ if(user.getType() == UserType.NOT_STUDENT){
|
|
|
|
+ //初始化为默认密码
|
|
|
|
+ user.setPassword(UserParam.DEFAULT_PASSWORD);
|
|
|
|
+
|
|
|
|
+ }else if(user.getType() == UserType.STUDENT){
|
|
|
|
+ //截取身份证后6位为学生登录密码
|
|
|
|
+ Student student = studentRepo.findByUserId(userId);
|
|
|
|
+ String identityNumber = student.getIdentityNumber();
|
|
|
|
+ if(StringUtils.isNotEmpty(identityNumber)){
|
|
|
|
+ user.setPassword(StringUtils.substring(identityNumber,-6,identityNumber.length()));
|
|
|
|
+ }else{
|
|
|
|
+ user.setPassword(UserParam.DEFAULT_PASSWORD);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ user.setUpdateTime(new Date());
|
|
|
|
+ userRepo.save(user);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 一般登录
|
|
|
|
+ * @param loginName
|
|
|
|
+ * @param password
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public UserInfo login(String loginName,
|
|
|
|
+ String password)throws Exception{
|
|
|
|
+ User user = userRepo.findByLoginName(loginName);
|
|
|
|
+ return loginProcess(user,password);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 二级登录
|
|
|
|
+ * @param orgId
|
|
|
|
+ * @param loginName
|
|
|
|
+ * @param password
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public UserInfo login(long orgId,
|
|
|
|
+ String loginName,
|
|
|
|
+ String password)throws Exception{
|
|
|
|
+ User user = userRepo.findByRootOrgIdAndLoginName(orgId,loginName);
|
|
|
|
+ return loginProcess(user,password);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 登录处理
|
|
|
|
+ * @param user
|
|
|
|
+ * @param password
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ 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();
|
|
|
|
+ initUserLogin(user);
|
|
|
|
+ createAccessUser(token,user,null);
|
|
|
|
+ createUserLogin(token,user);
|
|
|
|
+ return getUserInfo(user,token);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 初始化用户登录
|
|
|
|
+ * @param user
|
|
|
|
+ */
|
|
|
|
+ public void initUserLogin(User user){
|
|
|
|
+ //判断是否已登录,若已登录则强制已登录用户退出
|
|
|
|
+ UserLogin userLogin= userLoginRepo.findFirstByUserId(user.getId());
|
|
|
|
+ if(userLogin != null){
|
|
|
|
+ accessUserOps.delete(userLogin.getToken());
|
|
|
|
+ userLoginRepo.deleteByUserId(userLogin.getUserId());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建用户操作日志
|
|
|
|
+ * @param user
|
|
|
|
+ * @param action
|
|
|
|
+ */
|
|
|
|
+ public void createUserOpsLog(User user,String action){
|
|
|
|
+ UserOpsLog userOpsLog = new UserOpsLog(action,user.getId(),user.getName(),new Date());
|
|
|
|
+ userOpsLogRepo.save(userOpsLog);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建用户登录记录
|
|
|
|
+ * @param token
|
|
|
|
+ * @param user
|
|
|
|
+ */
|
|
|
|
+ public void createUserLogin(String token,User user){
|
|
|
|
+ UserLogin userLogin = new UserLogin();
|
|
|
|
+ userLogin.setLoginTime(new Date());
|
|
|
|
+ userLogin.setUserId(user.getId());
|
|
|
|
+ userLogin.setToken(token);
|
|
|
|
+ userLoginRepo.save(userLogin);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建访问用户并加入K/V缓存
|
|
|
|
+ * @param token
|
|
|
|
+ * @param user
|
|
|
|
+ */
|
|
|
|
+ public void createAccessUser(String token,User user,Long studentId)throws Exception{
|
|
|
|
+ AccessUser accessUser = new AccessUser();
|
|
|
|
+ Set<UserRole> userRoleSet = new HashSet<UserRole>(user.getUserRoles());
|
|
|
|
+ Org org = orgService.findOne(user.getRootOrgId());
|
|
|
|
+ Map<String,Set<String>> rolesMap = userRoleSet.stream()
|
|
|
|
+ .collect(Collectors.groupingBy(UserRole::getAppCode,
|
|
|
|
+ Collectors.mapping(UserRole::getRoleCode,Collectors.toSet())));
|
|
|
|
+ accessUser.setLoginName(user.getLoginName());
|
|
|
|
+ accessUser.setName(user.getName());
|
|
|
|
+ accessUser.setOrgId(user.getOrgId());
|
|
|
|
+ accessUser.setRootOrgId(user.getRootOrgId());
|
|
|
|
+ if(org != null){
|
|
|
|
+ accessUser.setRootOrgName(org.getName());
|
|
|
|
+ }
|
|
|
|
+ accessUser.setUserId(user.getId());
|
|
|
|
+ accessUser.setRoles(rolesMap);
|
|
|
|
+ accessUser.setToken(token);
|
|
|
|
+ accessUser.setStudentId(studentId);
|
|
|
|
+ accessUserOps.set(token,accessUser);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取用户信息DTO
|
|
|
|
+ * @param user
|
|
|
|
+ * @param token
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public UserInfo getUserInfo(User user,String token)throws Exception{
|
|
|
|
+ UserInfo userInfo = new UserInfo();
|
|
|
|
+ Org org = orgService.findOne(user.getOrgId());
|
|
|
|
+ Org rootOrg = orgService.findOne(user.getRootOrgId());
|
|
|
|
+ userInfo.setUserId(user.getId());
|
|
|
|
+ userInfo.setOrgId(user.getOrgId());
|
|
|
|
+ userInfo.setRootOrgId(user.getRootOrgId());
|
|
|
|
+ userInfo.setName(user.getName());
|
|
|
|
+ userInfo.setAvatar(user.getAvatar());
|
|
|
|
+ userInfo.setLoginName(user.getLoginName());
|
|
|
|
+ if(org != null){
|
|
|
|
+ userInfo.setOrgName(org.getName());
|
|
|
|
+ }
|
|
|
|
+ if(rootOrg != null){
|
|
|
|
+ userInfo.setRootOrgLogo(rootOrg.getLogo());
|
|
|
|
+ userInfo.setRootOrgName(rootOrg.getName());
|
|
|
|
+ }
|
|
|
|
+ userInfo.setToken(token);
|
|
|
|
+ userInfo.setType(user.getType().toString());
|
|
|
|
+ userInfo.setUserRoles(user.getUserRoles());
|
|
|
|
+ userInfo.setRoleNames(getRoleNames(user.getUserRoles()));
|
|
|
|
+ return userInfo;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取角色名称
|
|
|
|
+ * @param userRoles
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ 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
|
|
|
|
+ */
|
|
|
|
+ public void logout(AccessUser accessUser) {
|
|
|
|
+ accessUserOps.delete(accessUser.getToken());
|
|
|
|
+ userLoginRepo.deleteByUserId(accessUser.getUserId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<User> getMarker(Long rootOrgId) {
|
|
|
|
+ return userRepo.findMarkerByRootOrgId(rootOrgId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询评卷员带分页
|
|
|
|
+ * @param user
|
|
|
|
+ * @param pageable
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public Page<User> getAllMaker(User user,Pageable pageable){
|
|
|
|
+ Specification<User> specification = getSpecification(user);
|
|
|
|
+ return userRepo.findAll(specification,pageable);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Specification<User> getSpecification(User user){
|
|
|
|
+ Specification<User> specification = (root, query, cb) -> {
|
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
|
+ predicates.add(cb.equal(root.get("type"),UserType.NOT_STUDENT));
|
|
|
|
+ predicates.add(cb.equal(root.get("rootOrgId"),user.getRootOrgId()));
|
|
|
|
+ Join join = root.join("userRoles");
|
|
|
|
+ predicates.add(cb.equal(join.get("roleCode"), RoleMeta.MARKER.name()));
|
|
|
|
+ if(StringUtils.isNotEmpty(user.getLoginName())){
|
|
|
|
+ predicates.add(cb.like(root.get("loginName"),"%"+user.getLoginName()+"%"));
|
|
|
|
+ }
|
|
|
|
+ if(StringUtils.isNotEmpty(user.getName())){
|
|
|
|
+ predicates.add(cb.like(root.get("name"),"%"+user.getName()+"%"));
|
|
|
|
+ }
|
|
|
|
+ if(user.getEnable() != null){
|
|
|
|
+ predicates.add(cb.equal(root.get("enable"),user.getEnable()));
|
|
|
|
+ }
|
|
|
|
+ return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
+ };
|
|
|
|
+ return specification;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public User save(User user) throws Exception{
|
|
|
|
+ user.setScope(UserScope.ORG);
|
|
|
|
+ user.setCreateTime(new Date());
|
|
|
|
+ checkLoginName(user.getRootOrgId(), user.getLoginName());
|
|
|
|
+ return userRepo.save(user);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void checkLoginName(Long rootOrgId,String loginName) {
|
|
|
|
+ User old = userRepo.findByRootOrgIdAndLoginName(rootOrgId,loginName);
|
|
|
|
+ if(old!=null){
|
|
|
|
+ throw new RuntimeException("用户名已存在");
|
|
|
|
+ }else if("admin".equalsIgnoreCase(loginName)){
|
|
|
|
+ throw new RuntimeException("不能创建admin用户");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public User update(Long id, User user) {
|
|
|
|
+ User old = userRepo.findOne(id);
|
|
|
|
+ if(!old.getLoginName().equals(user.getLoginName())){
|
|
|
|
+ checkLoginName(user.getRootOrgId(), user.getLoginName());
|
|
|
|
+ }
|
|
|
|
+ user.setUpdateTime(new Date());
|
|
|
|
+ return userRepo.save(user);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public User saveMarker(Long rootOrgId, String loginName,String name) {
|
|
|
|
+ User old = userRepo.findByRootOrgIdAndLoginName(rootOrgId,loginName);
|
|
|
|
+ if(old!=null){
|
|
|
|
+ return old;
|
|
|
|
+ }
|
|
|
|
+ User user = new User();
|
|
|
|
+ user.setName(name);
|
|
|
|
+ user.setLoginName(loginName);
|
|
|
|
+ user.setEnable(true);
|
|
|
|
+ user.setOrgId(rootOrgId);
|
|
|
|
+ user.setRootOrgId(rootOrgId);
|
|
|
|
+ user.setPassword(loginName);
|
|
|
|
+ UserRole userRole = new UserRole(RoleMeta.MARKER.getAppCode(), RoleMeta.MARKER.toString());
|
|
|
|
+ List<UserRole> userRoles = new LinkedList<UserRole>();
|
|
|
|
+ userRoles.add(userRole);
|
|
|
|
+ user.setUserRoles(userRoles);
|
|
|
|
+ user.setScope(UserScope.ORG);
|
|
|
|
+ user.setType(UserType.NOT_STUDENT);
|
|
|
|
+ user.setCreateTime(new Date());
|
|
|
|
+ return userRepo.save(user);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|