From 1b8098b7f79b66a80e5ca49d8765606cb5fa0408 Mon Sep 17 00:00:00 2001 From: ludc Date: 星期四, 30 十一月 2023 19:11:00 +0800 Subject: [PATCH] 操作日志完善 --- Source/UBCS/ubcs-service/ubcs-user/src/main/java/com/vci/ubcs/system/user/service/impl/UserServiceImpl.java | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 150 insertions(+), 16 deletions(-) diff --git a/Source/UBCS/ubcs-service/ubcs-user/src/main/java/com/vci/ubcs/system/user/service/impl/UserServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-user/src/main/java/com/vci/ubcs/system/user/service/impl/UserServiceImpl.java index 6e31f40..ff717d9 100644 --- a/Source/UBCS/ubcs-service/ubcs-user/src/main/java/com/vci/ubcs/system/user/service/impl/UserServiceImpl.java +++ b/Source/UBCS/ubcs-service/ubcs-user/src/main/java/com/vci/ubcs/system/user/service/impl/UserServiceImpl.java @@ -19,9 +19,13 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.vci.ubcs.starter.web.util.VciBaseUtil; import com.vci.ubcs.system.cache.DictCache; +import com.vci.ubcs.system.cache.NacosConfigCache; import com.vci.ubcs.system.cache.ParamCache; import com.vci.ubcs.system.cache.SysCache; import com.vci.ubcs.system.entity.Strategy; @@ -32,6 +36,7 @@ import com.vci.ubcs.system.user.cache.UserCache; import com.vci.ubcs.system.user.entity.*; import com.vci.ubcs.system.user.enums.UserEnum; +import com.vci.ubcs.system.user.enums.UserStatus; import com.vci.ubcs.system.user.excel.UserExcel; import com.vci.ubcs.system.user.mapper.UserMapper; import com.vci.ubcs.system.user.service.IUserDeptService; @@ -40,22 +45,24 @@ import com.vci.ubcs.system.user.vo.UserVO; import com.vci.ubcs.system.user.wrapper.UserWrapper; import lombok.RequiredArgsConstructor; +import org.springblade.core.log.annotation.GrantLog; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.support.Condition; import org.springblade.core.mp.support.Query; +import org.springblade.core.secure.BladeUser; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tenant.BladeTenantProperties; import org.springblade.core.tool.api.R; import org.springblade.core.tool.constant.BladeConstant; import org.springblade.core.tool.support.Kv; import org.springblade.core.tool.utils.*; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.DigestUtils; import java.util.*; +import java.util.stream.Collectors; import static com.vci.ubcs.common.constant.CommonConstant.DEFAULT_PARAM_PASSWORD; @@ -67,27 +74,28 @@ @Service @RequiredArgsConstructor public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService { + private static final String GUEST_NAME = "guest"; private final IUserDeptService userDeptService; private final IUserOauthService userOauthService; private final ISysClient sysClient; private final BladeTenantProperties tenantProperties; - //鎷垮埌閰嶇疆鐨勮秴绠d - @Value("${user-info.id}") - private String adminUserId; - @Override @Transactional(rollbackFor = Exception.class) public boolean submit(User user) { if (StringUtil.isBlank(user.getTenantId())) { - user.setTenantId(BladeConstant.ADMIN_TENANT_ID); + // 榛樿璁剧疆涓虹鐞嗙粍涓嬬殑鐢ㄦ埛 + user.setTenantId(NacosConfigCache.getAdminUserInfo().getTenantId()); } String tenantId = user.getTenantId(); //Tenant tenant = SysCache.getTenant(tenantId); if (Func.isNotEmpty(user.getPassword())) { user.setPassword(DigestUtil.encrypt(user.getPassword())); + } + if(Func.isEmpty(user.getUserStatus())){ + user.setUserStatus(UserStatus.Enable.getValue()); } Long userCount = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getTenantId, tenantId).eq(User::getAccount, user.getAccount())); if (userCount > 0L && Func.isEmpty(user.getId())) { @@ -102,7 +110,7 @@ Boolean flag = true; for (User user : users){ if (StringUtil.isBlank(user.getTenantId())) { - user.setTenantId(BladeConstant.ADMIN_TENANT_ID); + user.setTenantId(NacosConfigCache.getAdminUserInfo().getTenantId()); } String tenantId = user.getTenantId(); if (Func.isNotEmpty(user.getPassword())) { @@ -120,6 +128,7 @@ @Override @Transactional(rollbackFor = Exception.class) + @GrantLog("grantUser") public boolean updateUser(User user) { String tenantId = user.getTenantId(); Long userCount = baseMapper.selectCount( @@ -128,6 +137,7 @@ .eq(User::getAccount, user.getAccount()) .notIn(User::getId, user.getId()) ); + // 鍒ゆ柇鏄惁琚慨鏀逛负宸插瓨鍦ㄧ殑鐢ㄦ埛鍚� if (userCount > 0L) { throw new ServiceException(StringUtil.format("褰撳墠鐢ㄦ埛 [{}] 宸插瓨鍦�!", user.getAccount())); } @@ -140,8 +150,41 @@ return updateById(user); } + /** + * 鏍规嵁鏃ц处鍙凤紝淇敼涓烘柊璐﹀彿鍚� + * @param oldAccount + * @param newAccount + * @return + */ + @Override + public boolean updateByAccount(String oldAccount,String newAccount) { + User user = this.userByAccount(AuthUtil.getTenantId(), oldAccount); + if(Func.isEmpty(user)){ + return true; + } + user.setAccount(newAccount); + return this.updateUser(user); + } + + /** + * 鎹处鍙凤紝淇敼涓虹敤鎴风姸鎬� + * @param accounts + * @param status + * @return + */ + @Override + public boolean updateStatusByAccount(String accounts, String status) { + LambdaUpdateWrapper<User> updateWrapper = Wrappers.<User>update() + .lambda().in(User::getAccount, accounts) + .set(User::getUserStatus, status); + return this.update(updateWrapper); + } + private boolean submitUserDept(User user) { List<Long> deptIdList = Func.toLongList(user.getDeptId()); + if(deptIdList.isEmpty()){ + return true; + } List<UserDept> userDeptList = new ArrayList<>(); deptIdList.forEach(deptId -> { UserDept userDept = new UserDept(); @@ -157,6 +200,21 @@ public IPage<User> selectUserPage(IPage<User> page, User user, Long deptId, String tenantId) { List<Long> deptIdList = SysCache.getDeptChildIds(deptId); return page.setRecords(baseMapper.selectUserPage(page, user, deptIdList, tenantId)); + } + + @Override + public List<User> selectAllUser(User user, Long deptId){ + List<Long> deptIdList = SysCache.getDeptChildIds(deptId); + List<User> users = baseMapper.selectUserPage(user, deptIdList, (VciBaseUtil.checkAdminTenant() ? StringPool.EMPTY : AuthUtil.getTenantId())); + return users; + } + + @Override + public List<User> selectAllUser(){ + LambdaQueryWrapper<User> wrapper= Wrappers.lambdaQuery(); + wrapper.eq(User::getIsDeleted,0); + List<User> users = baseMapper.selectList(wrapper); + return users; } @Override @@ -204,13 +262,20 @@ @Override public UserInfo userInfo(String tenantId, String account) { - User user = baseMapper.getUser(tenantId, account); + User user = baseMapper.getUser(tenantId, account,null); return buildUserInfo(user); } @Override + public UserInfo userInfo(String tenantId, String account,String name) { + User user = baseMapper.getUser(tenantId, account,name); + UserInfo userInfo = buildUserInfo(user); + return null; + } + + @Override public UserInfo userInfo(String tenantId, String account, UserEnum userEnum) { - User user = baseMapper.getUser(tenantId, account); + User user = baseMapper.getUser(tenantId, account,null); return buildUserInfo(user, userEnum); } @@ -222,6 +287,7 @@ if (ObjectUtil.isEmpty(user)) { return null; } + user.setDeptName(Func.join(SysCache.getDeptNames(user.getDeptId()))); UserInfo userInfo = new UserInfo(); userInfo.setUser(user); if (Func.isNotEmpty(user)) { @@ -288,6 +354,16 @@ return this.update(user, Wrappers.<User>update().lambda().in(User::getId, Func.toLongList(userIds))); } + /** + * 鎺堟潈鏃ュ織鎻掑叆鎿嶄綔 + * @param res + */ + @Override + @GrantLog("grantUser") + public boolean grantLog(String res, boolean isException){ + return true; + } + @Override public boolean resetPassword(String userIds) { User user = new User(); @@ -307,6 +383,10 @@ } //鑾峰彇鐢ㄦ埛閲囩敤鐨勫瘑鐮佺瓥鐣� Strategy strategy = sysClient.getByUserId(userId).getData(); + // 鍑犱箮涓嶄細鍑虹幇杩欑鎯呭喌 + if(ObjectUtil.isEmpty(strategy)) { + throw new ServiceException("褰撳墠鐢ㄦ埛鏈簲鐢ㄥ瘑鐮佺瓥鐣ワ紒"); + } //瀵嗙爜闀垮害鏍¢獙 if(newPassword1.length() < strategy.getMinPwdLen() || newPassword1.length() > strategy.getMaxPwdLen()){ throw new ServiceException("瀵嗙爜涓繀椤诲惈鏈夈��"+strategy.getCombinationNames()+"銆戜腑鐨勩��"+strategy.getRequiredType()+"銆戠瀵嗙爜缁勫悎鏂瑰紡锛屼笖瀵嗙爜闀垮害蹇呴』鍦ㄣ��"+strategy.getMinPwdLen()+"-"+strategy.getMaxPwdLen()+"銆戣寖鍥村唴"); @@ -318,7 +398,7 @@ if(reqType>=strategy.getRequiredType()){ break; } - if(!Func.isEmpty(RegexUtil.findResult(regexs.get(i),newPassword1))){ + if(RegexUtil.find(regexs.get(i),newPassword1)){ reqType++; } } @@ -326,12 +406,13 @@ if(reqType<strategy.getRequiredType()){ throw new ServiceException(resException); } - // 鏄惁灞炰簬缁勫悎鏂瑰紡涓殑绫诲瀷 + // 鏄惁灞炰簬缁勫悎鏂瑰紡涓殑绫诲瀷,浠ュ墠鏄瘑鐮佸繀椤绘槸鍖呭惈鍦ㄧ粍鍚堟柟寮忎腑鐨勭被鍨� String regex = sysClient.getRegex(Arrays.asList(strategy.getCombinationIds().split(","))).getData(); regex = "^"+regex+"{"+strategy.getRequiredType()+",}$"; boolean result = RegexUtil.find(regex, newPassword1); if(!result){ - throw new ServiceException(resException); + throw new ServiceException("瀵嗙爜涓彧鑳藉瓨鍦ㄣ��"+strategy.getCombinationNames()+"銆戜腑鍖呭惈鐨勫瓧绗︼紒"); + //throw new ServiceException(resException); } //淇敼瀵嗙爜鍚屾椂锛屾敼鍙樼敤鎴蜂俊鎭腑鐨勫瘑鐮佷慨鏀圭姸鎬佸瓧娈�,瀵嗙爜淇敼鏃堕棿 return this.update(Wrappers.<User>update().lambda() @@ -352,6 +433,7 @@ @Override @Transactional(rollbackFor = Exception.class) public void importUser(List<UserExcel> data, Boolean isCovered) { + ArrayList<User> addUsers = new ArrayList<>(); data.forEach(userExcel -> { User user = Objects.requireNonNull(BeanUtil.copy(userExcel, User.class)); // 璁剧疆鐢ㄦ埛骞冲彴 @@ -379,8 +461,10 @@ // 鑾峰彇榛樿瀵嗙爜閰嶇疆 String initPassword = ParamCache.getValue(DEFAULT_PARAM_PASSWORD); user.setPassword(initPassword); - this.submit(user); + addUsers.add(user); + //this.submit(user); }); + this.submitList(addUsers); } @Override @@ -417,6 +501,7 @@ boolean oauthTemp = userOauthService.updateById(userOauth); return (userTemp && oauthTemp); } + @Override public boolean updatePlatform(Long userId, Integer userType, String userExt) { if (userType.equals(UserEnum.WEB.getCategory())) { @@ -478,16 +563,21 @@ @Override public Long checkRenAndExpr(Long userId) { //瓒呯骇绠$悊鍛樼洿鎺ヨ繑鍥炰笉闇�瑕佹彁閱掑瘑鐮佷慨鏀� - if(adminUserId.equals(userId)){ + if(NacosConfigCache.getAdminUserInfo().getUserId().equals(userId)){ return 0L; } + QueryWrapper<User> wrapper = Wrappers.<User>query().eq("ID", userId); + User dbUser = this.getOne(wrapper); //鑾峰彇鍒板瘑鐮佷慨鏀规椂闂� - Date pwdUpdateTime = this.getOne(Wrappers.<User>query().eq("ID", userId)).getPwdUpdateTime(); + Date pwdUpdateTime = Func.isNotEmpty(dbUser) ? dbUser.getPwdUpdateTime():new Date(); Long pwdupdateday = 0L; if(!Func.isEmpty(pwdUpdateTime)){ pwdupdateday = dateToDay(pwdUpdateTime); } Strategy strategy = sysClient.getByUserId(userId).getData(); + if(Func.isEmpty(strategy)){ + throw new ServiceException("瀵嗙爜绛栫暐鏌ヨ涓虹┖锛岃妫�鏌ュ綋鍓嶇鎴蜂笅鏄惁瀛樺湪榛樿瀵嗙爜绛栫暐锛�"); + } //鏄惁鎻愰啋閫氳繃鏈�鍚庝竴娆′慨鏀瑰瘑鐮佺殑鏃堕棿鍔犱笂杩囨湡鏃堕棿鍑忓幓褰撳墠鏃堕棿锛屽鏋滃皬浜庤繃鏈熸彁閱掓椂闂村氨杩涜鎻愰啋锛屽鏋�<=0灏辨彁閱掑繀椤讳慨鏀瑰瘑鐮� long reminder = pwdupdateday+strategy.getExpirationTime()-dateToDay(new Date()); //鎻愰啋鐢ㄦ埛蹇呴』淇敼瀵嗙爜 @@ -503,8 +593,52 @@ return 0L; } + @Override + public boolean updateByUseStrategyId(List<Long> userIds) { + return this.update(Wrappers.<User>lambdaUpdate().in(User::getId, userIds).set(User::getStrategyUpdateStatus,CommonConstant.IS_DEFAULT)); + } + /** - * 鏃堕棿鏍煎紡杞ぉ + * 鑾峰彇鍒版寚瀹氳韩浠芥潈闄愮殑鐢ㄦ埛鍒楄〃 + * @param user 鐢ㄦ埛鏌ヨ鐨勭敤鎴蜂俊鎭紝濡傜鎴蜂俊鎭紝閫氬父涓鸿嚜鍔ㄦ敞鍏ワ紝鍓嶇鍙�夋嫨涓嶄紶 + * @param roleName 瑕佹煡璇㈢殑瑙掕壊韬唤 + * @return + */ + @Override + public List<Map<String,String>> getByRoleUserList(BladeUser user, String roleName) { + // 鑰冭檻鍒颁竴涓敤鎴峰彲浠ユ嫢鏈夊绉嶈鑹叉潈闄愶紝鑰岀敤鎴峰叧鑱旇鑹叉潈闄愭槸鐢╮ole_id瀛楁鐢ㄩ�楀彿鍒嗛殧瑙掕壊id鐨勶紝鐩存帴閲囩敤瀛愭煡璇㈡潵in鏌ヨ涓嶈兘瀹炵幇锛屾墍浠ュ厛鏌ヨ瑙掕壊id + R<String> roleIds = sysClient.getRoleIds(user.getTenantId(), roleName); + if(!roleIds.isSuccess()){ + throw new ServiceException("绯荤粺鏈嶅姟feign鎺ュ彛璋冪敤閿欒锛�"); + } + if(Func.isBlank(roleIds.getData())){ + return new ArrayList<>(); + } + List<Map<String,String>> list = new ArrayList<>(); + Arrays.stream(roleIds.getData().split(",")).forEach(item->{ + list.addAll(this.baseMapper.getUserMap(item,user.getUserId().toString())); + }); + // 鍘婚櫎閲嶅 + return list.stream().distinct().collect(Collectors.toList()); + } + + /*** + * 鏇存柊鐢ㄦ埛鍚敤鍋滅敤鐘舵�� + * @param userIds + * @param status + * @return + */ + @Override + public boolean updateUserStatus(String userIds, boolean status) { + Integer userStatus = 0; + if(!status){ + userStatus = 1; + } + return this.update(Wrappers.<User>lambdaUpdate().in(User::getId, Func.toLongList(userIds)).set(User::getUserStatus,userStatus)); + } + + /** + * 鏃ユ湡鏃堕棿鏍煎紡杞ぉ * @param date * @return */ -- Gitblit v1.9.3