Browse Source

[feat]

1、增加loginUser中设备权限字段①
pull/4/head
杨威 2 months ago
parent
commit
9a3ffebe7a
  1. 6
      dk-api/api-system/src/main/java/org/dromara/system/api/model/LoginUser.java
  2. 2
      dk-auth/src/main/java/org/dromara/auth/DKAuthApplication.java
  3. 24
      dk-auth/src/main/java/org/dromara/auth/feign/FeignDeviceGroup.java
  4. 22
      dk-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java
  5. 144
      dk-modules/sample/src/main/java/org/dromara/sample/manage/service/impl/DeviceProServiceImpl.java

6
dk-api/api-system/src/main/java/org/dromara/system/api/model/LoginUser.java

@ -135,6 +135,12 @@ public class LoginUser implements Serializable {
*/
private List<PostDTO> posts;
/**
* 设备sn权限集合
*/
private List<String> deviceList;
/**
* 获取登录id
*/

2
dk-auth/src/main/java/org/dromara/auth/DKAuthApplication.java

@ -5,6 +5,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 认证授权中心
@ -12,6 +13,7 @@ import org.springframework.boot.context.metrics.buffering.BufferingApplicationSt
* @author ruoyi
*/
@EnableDubbo
@EnableFeignClients
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DKAuthApplication {
public static void main(String[] args) {

24
dk-auth/src/main/java/org/dromara/auth/feign/FeignDeviceGroup.java

@ -0,0 +1,24 @@
package org.dromara.auth.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
@FeignClient(name = "gateway",path = "sample")
public interface FeignDeviceGroup {
@GetMapping("/manage/api/v1/device/group/feign/user")
public List<Integer> listDeviceGroup(@RequestParam("userId") Long userId);
@GetMapping("/manage/api/v1/device/group/feign/device")
public List<String> listDevice(@RequestParam("userId") Long userId);
@GetMapping("/device/flight/count")
public Map<String,Integer> getDevices();
}

22
dk-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java

@ -3,10 +3,12 @@ package org.dromara.auth.service.impl;
import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.ListUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.auth.domain.vo.LoginVo;
import org.dromara.auth.feign.FeignDeviceGroup;
import org.dromara.auth.form.PasswordLoginBody;
import org.dromara.auth.properties.CaptchaProperties;
import org.dromara.auth.service.IAuthStrategy;
@ -26,8 +28,12 @@ import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.api.RemoteUserService;
import org.dromara.system.api.domain.vo.RemoteClientVo;
import org.dromara.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* 密码认证策略
*
@ -45,6 +51,9 @@ public class PasswordAuthStrategy implements IAuthStrategy {
@DubboReference
private RemoteUserService remoteUserService;
@Autowired
FeignDeviceGroup feignDeviceGroup;
@Override
public LoginVo login(String body, RemoteClientVo client) {
PasswordLoginBody loginBody = JsonUtils.parseObject(body, PasswordLoginBody.class);
@ -73,6 +82,19 @@ public class PasswordAuthStrategy implements IAuthStrategy {
model.setTimeout(client.getTimeout());
model.setActiveTimeout(client.getActiveTimeout());
model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId());
//获取设备权限
List<String> deviceList = new ArrayList<>();
try {
deviceList = feignDeviceGroup.listDevice(loginUser.getUserId());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
loginUser.setDeviceList(deviceList);
// 生成token
LoginHelper.login(loginUser, model);

144
dk-modules/sample/src/main/java/org/dromara/sample/manage/service/impl/DeviceProServiceImpl.java

@ -88,7 +88,7 @@ public class DeviceProServiceImpl implements IDeviceProService {
@Autowired
private IDeviceProUserMapper deviceProUserMapper;
private final ObjectMapper objectMapper = new ObjectMapper();
// private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public PaginationData<DeviceProEntity> listDeviceProEntityMap(Page page, DeviceProDTO deviceProDTO) {
@ -131,52 +131,52 @@ public class DeviceProServiceImpl implements IDeviceProService {
public Boolean updateDeviceProUser(List<DeviceProUserEntity> userEntity,Integer proId) {
try {
//获取项目组信息
DeviceProEntity deviceProEntity = deviceProMapper.selectOne(new LambdaQueryWrapper<DeviceProEntity>().eq(DeviceProEntity::getId, proId));
// DeviceProEntity deviceProEntity = deviceProMapper.selectOne(new LambdaQueryWrapper<DeviceProEntity>().eq(DeviceProEntity::getId, proId));
//查询之前项目所属的人员信息
List<DeviceProUserEntity> proUserEntityList = deviceProUserMapper.selectList(new LambdaQueryWrapper<DeviceProUserEntity>().eq(DeviceProUserEntity::getDeviceProId, proId));
List<DeviceProUserEntity> difference = new ArrayList<>();
// List<DeviceProUserEntity> proUserEntityList = deviceProUserMapper.selectList(new LambdaQueryWrapper<DeviceProUserEntity>().eq(DeviceProUserEntity::getDeviceProId, proId));
//
// List<DeviceProUserEntity> difference = new ArrayList<>();
// 判断两个集合的差值(基于 userId)
difference = proUserEntityList.stream()
.filter(proUser -> userEntity.stream()
.noneMatch(user -> user.getUserId().equals(proUser.getUserId())))
.toList();
// difference = proUserEntityList.stream()
// .filter(proUser -> userEntity.stream()
// .noneMatch(user -> user.getUserId().equals(proUser.getUserId())))
// .toList();
//--------------------------------------------缓存相关操作----------------------------------------------------------------
//人员如果解绑了项目删除缓存数据
difference.forEach(proUserEntity -> {
//删除人员项目组信息
List<Object> projects = RedisUtils.getCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()));
List<Map> projectList = projects.stream()
.filter(Map.class::isInstance)
.map(Map.class::cast)
.toList();
//删除人员中所拥有的当前的项目组,然后构建新的存入redis
List<Map> deviceProEntities = projectList.stream().filter(p -> !Integer.valueOf(p.get("id") + "").equals(proId)).toList();
//删除
RedisUtils.deleteObject(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()));
//重新插入
RedisUtils.setCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()),deviceProEntities);
//删除人员所拥有的设备信息
List<Object> cacheList = RedisUtils.getCacheList(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId()));
List<Map> deviceList = cacheList.stream()
.filter(Map.class::isInstance)
.map(Map.class::cast)
.toList();
//过滤除了这个删除的项目组之外的设备
deviceList = deviceList.stream().filter(p-> !Integer.valueOf(p.get("proId") + "").equals(proId)).toList();
//删除
RedisUtils.deleteObject(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId()));
RedisUtils.setCacheList(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId()),deviceList);
});
// difference.forEach(proUserEntity -> {
// //删除人员项目组信息
// List<Object> projects = RedisUtils.getCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()));
// List<Map> projectList = projects.stream()
// .filter(Map.class::isInstance)
// .map(Map.class::cast)
// .toList();
//
// //删除人员中所拥有的当前的项目组,然后构建新的存入redis
// List<Map> deviceProEntities = projectList.stream().filter(p -> !Integer.valueOf(p.get("id") + "").equals(proId)).toList();
// //删除
// RedisUtils.deleteObject(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()));
// //重新插入
// RedisUtils.setCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()),deviceProEntities);
//
//
// //删除人员所拥有的设备信息
// List<Object> cacheList = RedisUtils.getCacheList(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId()));
// List<Map> deviceList = cacheList.stream()
// .filter(Map.class::isInstance)
// .map(Map.class::cast)
// .toList();
//
// //过滤除了这个删除的项目组之外的设备
// deviceList = deviceList.stream().filter(p-> !Integer.valueOf(p.get("proId") + "").equals(proId)).toList();
// //删除
// RedisUtils.deleteObject(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId()));
//
// RedisUtils.setCacheList(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId()),deviceList);
// });
//--------------------------------------------缓存相关操作----------------------------------------------------------------
@ -188,18 +188,18 @@ public class DeviceProServiceImpl implements IDeviceProService {
deviceProUserMapper.insert(userEntity);
//更新缓存记录
userEntity.forEach(proUserEntity -> {
List<Object> projects = RedisUtils.getCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()));
List<Map> projectList = projects.stream()
.filter(Map.class::isInstance)
.map(Map.class::cast)
.toList();
if (!projectList.stream().anyMatch(p-> Integer.valueOf(p.get("id") + "").equals(proUserEntity.getDeviceProId()))){
Map proMap = objectMapper.convertValue(deviceProEntity, Map.class);
RedisUtils.addCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()), proMap);
}
});
// userEntity.forEach(proUserEntity -> {
// List<Object> projects = RedisUtils.getCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()));
// List<Map> projectList = projects.stream()
// .filter(Map.class::isInstance)
// .map(Map.class::cast)
// .toList();
//
// if (!projectList.stream().anyMatch(p-> Integer.valueOf(p.get("id") + "").equals(proUserEntity.getDeviceProId()))){
// Map proMap = objectMapper.convertValue(deviceProEntity, Map.class);
// RedisUtils.addCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()), proMap);
// }
// });
}
return true;
@ -229,29 +229,31 @@ public class DeviceProServiceImpl implements IDeviceProService {
wrapper.set(DeviceEntity::getProId, proId);
wrapper.eq(DeviceEntity::getId, deviceId);
if (deviceMapper.update(wrapper) > 0) {
DeviceEntity deviceEntity = deviceMapper.selectOne(new LambdaQueryWrapper<DeviceEntity>().eq(DeviceEntity::getId, deviceId));
//根据proid获取人员用于存储人员的设备权限
List<DeviceProUserEntity> proUserEntityList = deviceProUserMapper.selectList(new LambdaQueryWrapper<DeviceProUserEntity>().eq(DeviceProUserEntity::getDeviceProId, proId));
proUserEntityList.forEach(deviceProUserEntity -> {
List<Object> devices = RedisUtils.getCacheList(String.format(Constants.PRO_DEVICE, deviceProUserEntity.getUserId()));
List<Map> deviceList = devices.stream()
.filter(Map.class::isInstance)
.map(Map.class::cast)
.toList();
if (!deviceList.stream().anyMatch(p-> Integer.valueOf(p.get("id") + "").equals(deviceEntity.getId()))){
Map deviceMap = objectMapper.convertValue(deviceEntity, Map.class);
RedisUtils.addCacheList(String.format(Constants.PRO_DEVICE, deviceProUserEntity.getUserId()), deviceMap);
}
});
}
return deviceMapper.update(wrapper) > 0;
// if (deviceMapper.update(wrapper) > 0) {
// DeviceEntity deviceEntity = deviceMapper.selectOne(new LambdaQueryWrapper<DeviceEntity>().eq(DeviceEntity::getId, deviceId));
// //根据proid获取人员用于存储人员的设备权限
// List<DeviceProUserEntity> proUserEntityList = deviceProUserMapper.selectList(new LambdaQueryWrapper<DeviceProUserEntity>().eq(DeviceProUserEntity::getDeviceProId, proId));
// proUserEntityList.forEach(deviceProUserEntity -> {
// List<Object> devices = RedisUtils.getCacheList(String.format(Constants.PRO_DEVICE, deviceProUserEntity.getUserId()));
// List<Map> deviceList = devices.stream()
// .filter(Map.class::isInstance)
// .map(Map.class::cast)
// .toList();
//
// if (!deviceList.stream().anyMatch(p-> Integer.valueOf(p.get("id") + "").equals(deviceEntity.getId()))){
// Map deviceMap = objectMapper.convertValue(deviceEntity, Map.class);
//
// RedisUtils.addCacheList(String.format(Constants.PRO_DEVICE, deviceProUserEntity.getUserId()), deviceMap);
// }
// });
// }
} catch (Exception e) {
return false;
}
return true;
}
/**

Loading…
Cancel
Save