From 2d4afbbcfda71f826ed532d01de860395c4da2b6 Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期四, 26 五月 2022 14:40:36 +0800
Subject: [PATCH] 角色等级FIX
---
ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 230 insertions(+), 0 deletions(-)
diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
index 8bc07bd..62c74b9 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
@@ -1,18 +1,248 @@
package com.sandu.ximon.admin.service;
+import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.github.pagehelper.PageHelper;
+import com.sandu.common.enums.RoleLevelStatus;
+import com.sandu.common.execption.BusinessException;
+import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
+import com.sandu.common.util.SpringContextHolder;
+import com.sandu.ximon.admin.param.AdminParam;
+import com.sandu.ximon.admin.param.PwdParam;
+import com.sandu.ximon.admin.param.UserPwsParm;
+import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
+import com.sandu.ximon.dao.bo.AdminBo;
+import com.sandu.ximon.dao.bo.MenuNode;
import com.sandu.ximon.dao.domain.Admin;
+import com.sandu.ximon.dao.domain.AdminRoleRelation;
+import com.sandu.ximon.dao.domain.Role;
+import com.sandu.ximon.dao.domain.RoleMenuRelation;
+import com.sandu.ximon.dao.enums.AdministratorEnums;
import com.sandu.ximon.dao.mapper.AdminMapper;
+import lombok.AllArgsConstructor;
+import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
/**
* @author chenjiantian
* @date 2021/11/24 11:02
*/
@Service
+@AllArgsConstructor
public class AdminService extends BaseServiceImpl<AdminMapper, Admin> {
+
+ private final PasswordEncoder passwordEncoder;
+ private final AdminRoleRelationService adminRoleRelationService;
+ private final RoleService roleService;
+ private AdminMapper adminMapper;
+
public Admin findByUserName(String username) {
return getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getUsername, username).last("limit 1"));
}
+
+ @Transactional(rollbackFor = Exception.class)
+ public boolean register(AdminParam param) {
+ Admin admin = findByUserName(param.getUsername());
+ if (admin != null) {
+ throw new BusinessException("褰撳墠璐﹀彿" + param.getUsername() + "宸茬粡瀛樺湪");
+ }
+ Admin save = new Admin();
+ save.setUsername(param.getUsername());
+ save.setPassword(passwordEncoder.encode(param.getPassword()));
+ save.setNickName(param.getNickName());
+ save.setMobile(param.getMobile());
+ save.setNote(param.getNote());
+ if (!save(save)) {
+ throw new BusinessException("娉ㄥ唽绠$悊鍛樺け璐�");
+ }
+
+ Role role = roleService.getById(param.getRoleId());
+ if (role == null) {
+ throw new BusinessException("瑙掕壊涓嶅瓨鍦�");
+ }
+ if (RoleLevelStatus.SUPER.getCode().equals(role.getLevel())) {
+ throw new BusinessException("鏃犳硶娣诲姞瓒呯骇绠$悊鍛�");
+ }
+
+ AdminRoleRelation adminRoleRelation = new AdminRoleRelation();
+ adminRoleRelation.setAdminId(save.getId());
+ adminRoleRelation.setRoleId(param.getRoleId());
+ if (!adminRoleRelationService.save(adminRoleRelation)) {
+ throw new BusinessException("娣诲姞绠$悊鍛樿鑹插け璐�");
+ }
+
+ /**
+ * 娣诲姞绠$悊鍛樻棩蹇楄褰曞紑濮�
+ */
+ String content = "鏂版敞鍐岀鐞嗗憳锛�" + param.getUsername();
+ StoreOperationRecordsUtils.storeOperationData(null, null, "娣诲姞绠$悊鍛�", content);
+ /**
+ * 娣诲姞绠$悊鍛樻棩蹇楄褰曠粨鏉�
+ */
+ return true;
+ }
+
+ @Transactional(rollbackFor = Exception.class)
+ public boolean updateAdmin(Long adminId, AdminParam param) {
+ Admin admin = getById(adminId);
+ if (admin == null) {
+ throw new BusinessException("鎵句笉鍒扮鐞嗗憳");
+ }
+ List<Role> roles = roleService.listByAdminId(admin.getId());
+ if (CollectionUtil.isEmpty(roles)) {
+ throw new BusinessException("褰撳墠鐢ㄦ埛娌℃湁瑙掕壊");
+ }
+ List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList());
+ int min = Collections.min(levels);
+ roleService.assertLevels(min);
+
+ Admin update = new Admin();
+ update.setId(adminId);
+ update.setPassword(passwordEncoder.encode(param.getPassword()));
+ update.setNickName(param.getNickName());
+ update.setMobile(param.getMobile());
+ update.setNote(param.getNote());
+ if (!updateById(update)) {
+ throw new BusinessException("缂栬緫绠$悊鍛樺け璐�");
+ }
+
+ adminRoleRelationService.remove(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, admin.getId()));
+ AdminRoleRelation adminRoleRelation = new AdminRoleRelation();
+ adminRoleRelation.setAdminId(adminId);
+ adminRoleRelation.setRoleId(param.getRoleId());
+ if (!adminRoleRelationService.save(adminRoleRelation)) {
+ throw new BusinessException("娣诲姞绠$悊鍛樿鑹插け璐�");
+ }
+
+ return true;
+ }
+
+ /**
+ * 淇敼褰撳墠鐧诲綍鐢ㄦ埛瀵嗙爜
+ *
+ * @param param
+ * @return
+ */
+ public boolean updateMyPassword(PwdParam param) {
+ Long userId = SecurityUtils.getUserId();
+ Admin admin = getById(userId);
+ if (admin == null) {
+ throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦�");
+ }
+ //鍒ゆ柇鏃у瘑鐮佷笌鏁版嵁搴撴槸鍚︿竴鑷�
+ if (passwordEncoder.matches(param.getOldPass(), admin.getPassword())) {
+ //鍔犲瘑鏂板瘑鐮�
+ admin.setId(userId);
+ admin.setPassword(passwordEncoder.encode(param.getNewPass()));
+ return updateById(admin);
+ } else {
+ throw new BusinessException("鏃у瘑鐮佷笉姝g‘锛岃閲嶆柊纭瀵嗙爜锛�");
+ }
+ }
+
+ /**
+ * 淇敼绠$悊鍛樼殑瀵嗙爜
+ *
+ * @param param
+ * @return
+ */
+ public boolean updateAdminPassword(UserPwsParm param) {
+ Admin admin = getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getId, param.getUserid()));
+ if (admin == null) {
+ throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦�");
+ }
+
+ //鍔犲瘑鏂板瘑鐮�
+ admin.setPassword(passwordEncoder.encode(param.getNewPass()));
+ return updateById(admin);
+ }
+
+ public boolean deleteAdmin(Long adminId) {
+ Admin admin = getById(adminId);
+ if (admin == null) {
+ throw new BusinessException("鎵句笉鍒扮鐞嗗憳");
+ }
+ List<Role> roles = roleService.listByAdminId(admin.getId());
+ if (CollectionUtil.isEmpty(roles)) {
+ throw new BusinessException("褰撳墠鐢ㄦ埛娌℃湁瑙掕壊");
+ }
+ List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList());
+ int min = Collections.min(levels);
+ int maxLevel = roleService.assertLevels(min);
+ if (!RoleLevelStatus.SUPER.getCode().equals(maxLevel)) {
+ throw new BusinessException("鍙湁瓒呯骇绠$悊鍛樻墠鑳藉垹闄ょ敤鎴�");
+ }
+
+ return removeById(adminId);
+ }
+
+ /**
+ * 淇敼瓒呯骇绠$悊鍛樸�佺鐞嗗憳鐢ㄦ埛澶村儚
+ *
+ * @param userId
+ * @param IconUrl
+ * @return
+ */
+ public boolean updateIcon(Long userId, String IconUrl) {
+ Admin one = getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getId, userId));
+ if (one == null) {
+ throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦�");
+ }
+ one.setIcon(IconUrl);
+ return updateById(one);
+ }
+
+ public List<AdminBo> listAdmin(BaseConditionVO baseConditionVO, String keyword) {
+ if (baseConditionVO != null) {
+ PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
+ }
+
+ return adminMapper.listAdmin(keyword);
+ }
+
+ /**
+ * 鑾峰彇鐢ㄦ埛鏉冮檺鍒楄〃鍦�
+ */
+ public List<MenuNode> getUserPermissionList() {
+ List<MenuNode> resultList;
+ //鍒ゆ柇瓒呯涓庢櫘閫氱鐞嗗憳
+ if (SecurityUtils.getAdministratorIdentity().equals(AdministratorEnums.ADMIN.getCode())) {
+ //鍙傛暟涓簄ull鏃讹紝鑾峰彇鎵�鏈夌殑鑿滃崟鏉冮檺
+ resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(null);
+ } else {
+ //閫氳繃鐢ㄦ埛UserID鑾峰彇鐢ㄦ埛瑙掕壊
+ AdminRoleRelation one = SpringContextHolder.getBean(AdminRoleRelationService.class).
+ getOne(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, SecurityUtils.getUserId()));
+ //鍒ょ┖
+ if (one == null) {
+ throw new BusinessException("璇ョ敤鎴锋湭缁戝畾瑙掕壊");
+ }
+
+ //閫氳繃RoleID鑾峰彇MeunId鍒楄〃
+ List<RoleMenuRelation> menuIdList = SpringContextHolder.getBean(RoleMenuRelationService.class)
+ .list(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, one.getRoleId()));
+
+ //鍒ょ┖
+ if (menuIdList.isEmpty()) {
+ return new ArrayList<>();
+ }
+ List<Long> menuIds = new ArrayList<>(menuIdList.size());
+ for (RoleMenuRelation bean : menuIdList) {
+ menuIds.add(bean.getMenuId());
+ }
+
+ resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(menuIds);
+
+ }
+
+ return resultList;
+ }
}
--
Gitblit v1.9.3