|
|
@@ -8,13 +8,12 @@ import cn.reghao.oss.store.api.dto.OssPayload;
|
|
|
import cn.reghao.oss.console.account.service.UserContext;
|
|
|
import cn.reghao.oss.console.app.db.repository.UserKeyRepository;
|
|
|
import cn.reghao.oss.console.app.model.dto.KeyAuthDto;
|
|
|
-import cn.reghao.oss.console.app.model.dto.UserKeyDto;
|
|
|
import cn.reghao.oss.console.app.model.po.UserKey;
|
|
|
import cn.reghao.oss.console.util.JwtUtil;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -23,26 +22,24 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
public class UserKeyService {
|
|
|
private final UserKeyRepository userKeyRepository;
|
|
|
- private StoreNodeService storeNodeService;
|
|
|
|
|
|
- public UserKeyService(UserKeyRepository userKeyRepository, StoreNodeService storeNodeService) {
|
|
|
+ public UserKeyService(UserKeyRepository userKeyRepository) {
|
|
|
this.userKeyRepository = userKeyRepository;
|
|
|
- this.storeNodeService = storeNodeService;
|
|
|
}
|
|
|
|
|
|
- public Result create(UserKeyDto userKeyDto) {
|
|
|
- int nodeId = userKeyDto.getNodeId();
|
|
|
- int userId = UserContext.getUser().getId();
|
|
|
- int total = userKeyRepository.countByNodeIdAndUserId(nodeId, userId);
|
|
|
- if (total > 2) {
|
|
|
- String errMsg = String.format("you already have %s keys, key exceed limit", total);
|
|
|
+ public Result create() {
|
|
|
+ int loginUser = UserContext.getUser().getId();
|
|
|
+ UserKey userKey = userKeyRepository.findByCreateBy(loginUser);
|
|
|
+ if (userKey != null) {
|
|
|
+ String errMsg = String.format("you already have key");
|
|
|
return Result.fail(errMsg);
|
|
|
+ } else {
|
|
|
+ String accessKeyId = RandomString.getString(8);
|
|
|
+ String accessKeySecret = RandomString.getString(18);
|
|
|
+ userKey = new UserKey(accessKeyId, accessKeySecret, loginUser);
|
|
|
+ userKeyRepository.save(userKey);
|
|
|
}
|
|
|
|
|
|
- String accessKeyId = RandomString.getString(8);
|
|
|
- String accessKeySecret = RandomString.getString(18);
|
|
|
- UserKey userKey = new UserKey(accessKeyId, accessKeySecret, nodeId, userId);
|
|
|
- userKeyRepository.save(userKey);
|
|
|
return Result.success("key has created");
|
|
|
}
|
|
|
|
|
|
@@ -57,16 +54,15 @@ public class UserKeyService {
|
|
|
return Result.fail("secret not matched");
|
|
|
}
|
|
|
|
|
|
- int loginUser = userKey.getUserId();
|
|
|
+ int loginUser = userKey.getCreateBy();
|
|
|
String token = getToken(loginUser);
|
|
|
-
|
|
|
return Result.success(token);
|
|
|
}
|
|
|
|
|
|
public String getToken(int loginUser) {
|
|
|
String secretKey = "ossconsole.reghao.cn";
|
|
|
long timestamp = System.currentTimeMillis() + 3600*24*365;
|
|
|
- String action = ChannelAction.download.getName();
|
|
|
+ String action = ChannelAction.all.getName();
|
|
|
int channelId = 1;
|
|
|
OssPayload ossPayload = new OssPayload(action, channelId, loginUser);
|
|
|
return JwtUtil.createToken(ossPayload, timestamp, secretKey);
|
|
|
@@ -86,18 +82,14 @@ public class UserKeyService {
|
|
|
}
|
|
|
|
|
|
public List<UserKeyVo> getUserKeys() {
|
|
|
- int userId = UserContext.getUser().getId();
|
|
|
- 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());
|
|
|
- }
|
|
|
+ int loginUser = UserContext.getUser().getId();
|
|
|
+ UserKey userKey = userKeyRepository.findByCreateBy(loginUser);
|
|
|
+ if (userKey == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
|
|
|
- public UserKey getByNodeIdAndUserId(int nodeId, int userId) {
|
|
|
- return userKeyRepository.findByNodeIdAndUserId(nodeId, userId);
|
|
|
+ String accessKeyId = userKey.getAccessKeyId();
|
|
|
+ String accessKeySecret = userKey.getAccessKeySecret();
|
|
|
+ return List.of(new UserKeyVo(accessKeyId, accessKeySecret));
|
|
|
}
|
|
|
}
|