diff --git a/src/main/java/cn/lihongjie/coal/user/service/UserService.java b/src/main/java/cn/lihongjie/coal/user/service/UserService.java index 96febce7..71696586 100644 --- a/src/main/java/cn/lihongjie/coal/user/service/UserService.java +++ b/src/main/java/cn/lihongjie/coal/user/service/UserService.java @@ -81,8 +81,7 @@ public class UserService extends BaseService { @Autowired PasswordDictService passwordDictService; private Pbkdf2PasswordEncoder passwordEncoder; - @Autowired - UserService that; + @Autowired UserService that; @PostConstruct public void init() { @@ -193,14 +192,41 @@ public class UserService extends BaseService { UserEntity user = this.repository.get(request.getId()); this.mapper.updateEntity(user, request); this.repository.save(user); + String organizationId = user.getOrganizationId(); + + assertAdmin(organizationId); + clearUserCache(request.getId()); return getById(user.getId()); } + private void assertAdmin(String organizationId) { + long adminCnt = + this.repository.count( + new Specification() { + @Override + public Predicate toPredicate( + Root root, + CriteriaQuery query, + CriteriaBuilder criteriaBuilder) { + return criteriaBuilder.and( + criteriaBuilder.equal( + root.get("organizationId"), organizationId), + criteriaBuilder.equal(root.get("orgAdmin"), true)); + } + }); + + if (adminCnt == 0) { + throw new BizException("组织必须有一个管理员"); + } + } + public void delete(IdRequest request) { this.repository.deleteAllById(request.getIds()); request.getIds().forEach(this::clearUserCache); + + this.assertAdmin(Ctx.currentUser().getOrganizationId()); } public UserDto getById(String id) { @@ -541,7 +567,7 @@ public class UserService extends BaseService { stopWatch.start("contains"); boolean contains = ids.contains(resourceId); stopWatch.stop(); -// log.info(stopWatch.prettyPrint()); + // log.info(stopWatch.prettyPrint()); return contains; } @@ -549,7 +575,5 @@ public class UserService extends BaseService { Try.run(() -> cacheManager.getCache(Constants.CACHE_USER_RESOURCES).clear()); Try.run(() -> cacheManager.getCache(Constants.CACHE_USER_PERMISSIONS).clear()); Try.run(() -> cacheManager.getCache(Constants.CACHE_USER_RESOURCE_IDS).clear()); - - } }