|
@@ -2,6 +2,7 @@ package cn.reghao.oss.console.app.service;
|
|
|
|
|
|
|
|
import cn.reghao.jutil.jdk.result.Result;
|
|
import cn.reghao.jutil.jdk.result.Result;
|
|
|
import cn.reghao.jutil.jdk.security.RandomString;
|
|
import cn.reghao.jutil.jdk.security.RandomString;
|
|
|
|
|
+import cn.reghao.oss.console.app.model.vo.UserKeyVo;
|
|
|
import cn.reghao.oss.store.api.constant.ChannelAction;
|
|
import cn.reghao.oss.store.api.constant.ChannelAction;
|
|
|
import cn.reghao.oss.store.api.dto.OssPayload;
|
|
import cn.reghao.oss.store.api.dto.OssPayload;
|
|
|
import cn.reghao.oss.console.account.service.UserContext;
|
|
import cn.reghao.oss.console.account.service.UserContext;
|
|
@@ -13,6 +14,7 @@ import cn.reghao.oss.console.util.JwtUtil;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author reghao
|
|
* @author reghao
|
|
@@ -21,15 +23,17 @@ import java.util.List;
|
|
|
@Service
|
|
@Service
|
|
|
public class UserKeyService {
|
|
public class UserKeyService {
|
|
|
private final UserKeyRepository userKeyRepository;
|
|
private final UserKeyRepository userKeyRepository;
|
|
|
|
|
+ private StoreNodeService storeNodeService;
|
|
|
|
|
|
|
|
- public UserKeyService(UserKeyRepository userKeyRepository) {
|
|
|
|
|
|
|
+ public UserKeyService(UserKeyRepository userKeyRepository, StoreNodeService storeNodeService) {
|
|
|
this.userKeyRepository = userKeyRepository;
|
|
this.userKeyRepository = userKeyRepository;
|
|
|
|
|
+ this.storeNodeService = storeNodeService;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Result create(UserKeyDto userKeyDto) {
|
|
public Result create(UserKeyDto userKeyDto) {
|
|
|
- String domain = userKeyDto.getDomain();
|
|
|
|
|
|
|
+ int nodeId = userKeyDto.getNodeId();
|
|
|
int userId = UserContext.getUser().getId();
|
|
int userId = UserContext.getUser().getId();
|
|
|
- int total = userKeyRepository.countByDomainAndUserId(domain, userId);
|
|
|
|
|
|
|
+ int total = userKeyRepository.countByNodeIdAndUserId(nodeId, userId);
|
|
|
if (total > 2) {
|
|
if (total > 2) {
|
|
|
String errMsg = String.format("you already have %s keys, key exceed limit", total);
|
|
String errMsg = String.format("you already have %s keys, key exceed limit", total);
|
|
|
return Result.fail(errMsg);
|
|
return Result.fail(errMsg);
|
|
@@ -37,7 +41,7 @@ public class UserKeyService {
|
|
|
|
|
|
|
|
String accessKeyId = RandomString.getString(8);
|
|
String accessKeyId = RandomString.getString(8);
|
|
|
String accessKeySecret = RandomString.getString(18);
|
|
String accessKeySecret = RandomString.getString(18);
|
|
|
- UserKey userKey = new UserKey(accessKeyId, accessKeySecret, domain, userId);
|
|
|
|
|
|
|
+ UserKey userKey = new UserKey(accessKeyId, accessKeySecret, nodeId, userId);
|
|
|
userKeyRepository.save(userKey);
|
|
userKeyRepository.save(userKey);
|
|
|
return Result.success("key has created");
|
|
return Result.success("key has created");
|
|
|
}
|
|
}
|
|
@@ -75,11 +79,21 @@ public class UserKeyService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void delete(String accessKeyId) {
|
|
public void delete(String accessKeyId) {
|
|
|
|
|
+ UserKey userKey = userKeyRepository.findByAccessKeyId(accessKeyId);
|
|
|
|
|
+ if (userKey != null) {
|
|
|
|
|
+ userKeyRepository.delete(userKey);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public List<UserKey> getUserKeys() {
|
|
|
|
|
|
|
+ public List<UserKeyVo> getUserKeys() {
|
|
|
int userId = UserContext.getUser().getId();
|
|
int userId = UserContext.getUser().getId();
|
|
|
- List<UserKey> list = userKeyRepository.findByUserId(userId);
|
|
|
|
|
- return list;
|
|
|
|
|
|
|
+ return userKeyRepository.findByUserId(userId).stream()
|
|
|
|
|
+ .map(userKey -> {
|
|
|
|
|
+ String domain = storeNodeService.getById(userKey.getNodeId()).getDomain();
|
|
|
|
|
+ String accessKeyId = userKey.getAccessKeyId();
|
|
|
|
|
+ String accessKeySecret = userKey.getAccessKeySecret();
|
|
|
|
|
+ return new UserKeyVo(domain, accessKeyId, accessKeySecret);
|
|
|
|
|
+ })
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|