|
|
@@ -1,5 +1,6 @@
|
|
|
package com.uas.sso.sso.backend.service.impl;
|
|
|
|
|
|
+import com.uas.sso.core.Status;
|
|
|
import com.uas.sso.dao.AppDao;
|
|
|
import com.uas.sso.dao.UserDao;
|
|
|
import com.uas.sso.dao.UserspaceDao;
|
|
|
@@ -12,12 +13,18 @@ import com.uas.sso.sso.backend.service.UserSpaceService;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
/**
|
|
|
@@ -46,98 +53,111 @@ public class UserSpaceServiceImpl implements UserSpaceService {
|
|
|
public Page<Userspace> showSpaceByPagination(Pageable page, Short validCode, String fromApp,
|
|
|
String key, String keyword) {
|
|
|
|
|
|
- return userspaceDao.findAll((root, query, builder) -> {
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
+ return userspaceDao.findAll(new Specification<Userspace>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<Userspace> root, CriteriaQuery<?> query,
|
|
|
+ CriteriaBuilder builder) {
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
|
|
|
- if (!StringUtils.isEmpty(validCode)) {
|
|
|
- Predicate predicate = builder.equal(root.get("validCode"), validCode);
|
|
|
- predicates.add(predicate);
|
|
|
- }
|
|
|
+ if (!StringUtils.isEmpty(validCode)) {
|
|
|
+ Predicate predicate = builder.equal(root.get("validCode"), validCode);
|
|
|
+ predicates.add(predicate);
|
|
|
+ }
|
|
|
|
|
|
- if (!StringUtils.isEmpty(fromApp)) {
|
|
|
- Predicate predicate = builder.equal(root.get("fromApp"), fromApp);
|
|
|
- predicates.add(predicate);
|
|
|
- }
|
|
|
+ if (!StringUtils.isEmpty(fromApp)) {
|
|
|
+ Predicate predicate = builder.equal(root.get("fromApp"), fromApp);
|
|
|
+ predicates.add(predicate);
|
|
|
+ }
|
|
|
|
|
|
- // 搜索关键字过滤
|
|
|
- if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(keyword)) {
|
|
|
- Predicate predicate = builder.like(root.get(key), "%" + keyword + "%");
|
|
|
- predicates.add(predicate);
|
|
|
- }
|
|
|
+ // 搜索关键字过滤
|
|
|
+ if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(keyword)) {
|
|
|
+ Predicate predicate = builder.like(root.get(key), "%" + keyword + "%");
|
|
|
+ predicates.add(predicate);
|
|
|
+ }
|
|
|
|
|
|
- predicates.removeAll(Collections.singletonList(null));
|
|
|
+ predicates.removeAll(Collections.singletonList(null));
|
|
|
|
|
|
- Predicate[] array = new Predicate[predicates.size()];
|
|
|
- predicates.toArray(array);
|
|
|
- Predicate predicate = builder.and(array);
|
|
|
- query.where(predicate);
|
|
|
- return null;
|
|
|
+ Predicate[] array = new Predicate[predicates.size()];
|
|
|
+ predicates.toArray(array);
|
|
|
+ Predicate predicate = builder.and(array);
|
|
|
+ query.where(predicate);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}, page);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Boolean modifyUserInfo(UpdateSpaceInfo spaceInfo) {
|
|
|
+ public Userspace modifyUserSpaceInfo(UpdateSpaceInfo spaceInfo) {
|
|
|
Userspace userspace = userspaceDao.findOne(spaceInfo.getSpaceUU());
|
|
|
if (userspace == null) {
|
|
|
throw new ValidationFailedException(String.format("UU%d对应企业不存在", spaceInfo.getSpaceUU()));
|
|
|
}
|
|
|
|
|
|
- if (!userspace.getSpaceName().equals(spaceInfo.getSpaceName())) {
|
|
|
- Userspace existSpace = userspaceDao.findBySpaceName(spaceInfo.getSpaceName());
|
|
|
- if (existSpace != null) {
|
|
|
- throw new ValidationFailedException(String.format("企业%s已注册", spaceInfo.getSpaceName()));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 避免清空营业执照因唯一性约束导致报错
|
|
|
if (StringUtils.isEmpty(spaceInfo.getBusinessCode())) {
|
|
|
+ // 避免清空营业执照因唯一性约束导致报错
|
|
|
spaceInfo.setBusinessCode(null);
|
|
|
- } else {
|
|
|
- if (!spaceInfo.getBusinessCode().equals(userspace.getBusinessCode())) {
|
|
|
- Userspace existSpace = userspaceDao.findByBusinessCode(spaceInfo.getBusinessCode());
|
|
|
- if (existSpace != null) {
|
|
|
- throw new ValidationFailedException(String.format("营业执照%s已认证", spaceInfo.getBusinessCode()));
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
userspace = spaceInfo.fillSpaceInfo(userspace);
|
|
|
|
|
|
- userspaceDao.save(userspace);
|
|
|
- return true;
|
|
|
+ return userspaceDao.save(userspace);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Page<Userspace> showSpaceByPagination(Pageable page, String spaceName, String businessCode,
|
|
|
- Short validCode) {
|
|
|
- if (validCode == null) {
|
|
|
- throw new ValidationFailedException("企业信息认证状态必须指定");
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public Userspace unbindUser(Long spaceUu, Long userUu) {
|
|
|
+ User user = assertUserExist(userUu);
|
|
|
+ Userspace space = assertSpaceExist(spaceUu);
|
|
|
+
|
|
|
+ if (user.getUserUU().equals(space.getAdminUU())) {
|
|
|
+ throw new ValidationFailedException("企业管理员不能直接解绑");
|
|
|
}
|
|
|
|
|
|
- return userspaceDao.findAll((root, query, builder) -> {
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
+ if (!CollectionUtils.isEmpty(space.getUsers())) {
|
|
|
+ space.getUsers().remove(user);
|
|
|
+ }
|
|
|
+ return userspaceDao.save(space);
|
|
|
+ }
|
|
|
|
|
|
- // 根据企业认证状态进行过滤
|
|
|
- predicates.add(builder.equal(root.get("validCode"), validCode));
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public Userspace changeAdmin(Long spaceUu, Long userUU) {
|
|
|
+ User admin = assertUserExist(userUU);
|
|
|
+ Userspace space = assertSpaceExist(spaceUu);
|
|
|
+
|
|
|
+ // 检验选定管理员用户是否添加到当前企业
|
|
|
+ List<User> userList = space.getUsers();
|
|
|
+ if (CollectionUtils.isEmpty(userList) || !userList.contains(admin)) {
|
|
|
+ throw new ValidationFailedException("企业管理员必须是当前企业用户");
|
|
|
+ }
|
|
|
|
|
|
- if (!StringUtils.isEmpty(spaceName)) {
|
|
|
- Predicate predicate = builder.like(root.get("spaceName"), "%" + spaceName + "%");
|
|
|
- predicates.add(predicate);
|
|
|
- }
|
|
|
+ // 更新管理员信息
|
|
|
+ space.setAdminUU(userUU);
|
|
|
+ space.setAdmin(admin);
|
|
|
+ return userspaceDao.save(space);
|
|
|
+ }
|
|
|
|
|
|
- if (!StringUtils.isEmpty(businessCode)) {
|
|
|
- Predicate predicate = builder.like(root.get("businessCode"), businessCode + "%");
|
|
|
- predicates.add(predicate);
|
|
|
- }
|
|
|
+ private User assertUserExist(Long userUU) {
|
|
|
+ User user = userDao.findOne(userUU);
|
|
|
+ if (user == null) {
|
|
|
+ throw new ValidationFailedException(String.format("用户[%d]不存在", userUU));
|
|
|
+ }
|
|
|
+ return user;
|
|
|
+ }
|
|
|
|
|
|
- predicates.removeAll(Collections.singletonList(null));
|
|
|
+ @Override
|
|
|
+ public Userspace authEnterpriseInfo(@NotNull Long spaceUu, @NotNull Boolean isPass,
|
|
|
+ String reason) {
|
|
|
+ Userspace space = assertSpaceExist(spaceUu);
|
|
|
|
|
|
- Predicate[] array = new Predicate[predicates.size()];
|
|
|
- predicates.toArray(array);
|
|
|
- Predicate predicate = builder.and(array);
|
|
|
- query.where(predicate);
|
|
|
- return null;
|
|
|
- }, page);
|
|
|
+ if (isPass) {
|
|
|
+ space.setValidCode((short) Status.AUTHENTICATED.getCode());
|
|
|
+ } else {
|
|
|
+ space.setValidCode((short) Status.NOT_PASSED.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO 业务不清楚
|
|
|
+ return space;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -167,15 +187,12 @@ public class UserSpaceServiceImpl implements UserSpaceService {
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = RuntimeException.class)
|
|
|
- public void bindAppWithSpace(Long spaceUu, String appUid) {
|
|
|
+ public Userspace bindAppWithSpace(Long spaceUu, String appUid) {
|
|
|
if (spaceUu == null || StringUtils.isEmpty(appUid)) {
|
|
|
throw new ValidationFailedException("企业UU或应用Uid不能为空");
|
|
|
}
|
|
|
|
|
|
- Userspace space = userspaceDao.findOne(spaceUu);
|
|
|
- if (space == null) {
|
|
|
- throw new ValidationFailedException(String.format("UU %d 对应企业不存在", spaceUu));
|
|
|
- }
|
|
|
+ Userspace space = assertSpaceExist(spaceUu);
|
|
|
|
|
|
App app = appDao.findOne(appUid);
|
|
|
if (app == null) {
|
|
|
@@ -186,7 +203,21 @@ public class UserSpaceServiceImpl implements UserSpaceService {
|
|
|
if (!apps.contains(app)) {
|
|
|
apps.add(app);
|
|
|
|
|
|
- userspaceDao.save(space);
|
|
|
+ space = userspaceDao.save(space);
|
|
|
+ }
|
|
|
+ return space;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 业务逻辑校验-企业UU对应企业是否存在
|
|
|
+ *
|
|
|
+ * @param spaceUu 企业UU
|
|
|
+ */
|
|
|
+ private Userspace assertSpaceExist(@NotNull Long spaceUu) {
|
|
|
+ Userspace space = userspaceDao.findOne(spaceUu);
|
|
|
+ if (space == null) {
|
|
|
+ throw new ValidationFailedException(String.format("企业[%d]不存在", spaceUu));
|
|
|
}
|
|
|
+ return space;
|
|
|
}
|
|
|
}
|