Explorar el Código

更新 UserKey model 和相关页面接口

reghao hace 2 años
padre
commit
749b8448d1

+ 2 - 4
oss-console/src/main/java/cn/reghao/oss/console/app/controller/page/UserKeyController.java

@@ -2,12 +2,10 @@ package cn.reghao.oss.console.app.controller.page;
 
 import cn.reghao.jutil.jdk.result.Result;
 import cn.reghao.jutil.jdk.result.WebResult;
-import cn.reghao.oss.console.app.model.dto.UserKeyDto;
 import cn.reghao.oss.console.app.service.UserKeyService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.http.MediaType;
-import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 /**
@@ -26,8 +24,8 @@ public class UserKeyController {
 
     @ApiOperation(value = "创建帐号")
     @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String create(@Validated UserKeyDto userKeyDto) {
-        Result result = userKeyService.create(userKeyDto);
+    public String create() {
+        Result result = userKeyService.create();
         return WebResult.result(result);
     }
 

+ 0 - 2
oss-console/src/main/java/cn/reghao/oss/console/app/controller/page/UserKeyPageController.java

@@ -1,6 +1,5 @@
 package cn.reghao.oss.console.app.controller.page;
 
-import cn.reghao.oss.console.app.model.po.UserKey;
 import cn.reghao.oss.console.app.model.vo.KeyValue;
 import cn.reghao.oss.console.app.model.vo.UserKeyVo;
 import cn.reghao.oss.console.app.service.StoreNodeService;
@@ -13,7 +12,6 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 
 import java.util.List;
-import java.util.stream.Collectors;
 
 /**
  * @author reghao

+ 1 - 5
oss-console/src/main/java/cn/reghao/oss/console/app/db/repository/UserKeyRepository.java

@@ -3,15 +3,11 @@ package cn.reghao.oss.console.app.db.repository;
 import cn.reghao.oss.console.app.model.po.UserKey;
 import org.springframework.data.jpa.repository.JpaRepository;
 
-import java.util.List;
-
 /**
  * @author reghao
  * @date 2024-02-27 10:35:53
  */
 public interface UserKeyRepository extends JpaRepository<UserKey, Integer> {
-    int countByNodeIdAndUserId(int nodeId, int userId);
-    List<UserKey> findByUserId(int userId);
+    UserKey findByCreateBy(int createBy);
     UserKey findByAccessKeyId(String accessKeyId);
-    UserKey findByNodeIdAndUserId(int nodeId, int userId);
 }

+ 0 - 17
oss-console/src/main/java/cn/reghao/oss/console/app/model/dto/UserKeyDto.java

@@ -1,17 +0,0 @@
-package cn.reghao.oss.console.app.model.dto;
-
-import lombok.Getter;
-import lombok.Setter;
-
-import javax.validation.constraints.NotNull;
-
-/**
- * @author reghao
- * @date 2024-02-27 11:01:40
- */
-@Setter
-@Getter
-public class UserKeyDto {
-    @NotNull
-    private Integer nodeId;
-}

+ 1 - 2
oss-console/src/main/java/cn/reghao/oss/console/app/model/po/UserKey.java

@@ -22,6 +22,5 @@ public class UserKey extends BaseEntity {
     @Column(unique = true)
     private String accessKeyId;
     private String accessKeySecret;
-    private Integer nodeId;
-    private Integer userId;
+    private Integer createBy;
 }

+ 0 - 1
oss-console/src/main/java/cn/reghao/oss/console/app/model/vo/UserKeyVo.java

@@ -10,7 +10,6 @@ import lombok.Getter;
 @AllArgsConstructor
 @Getter
 public class UserKeyVo {
-    private String domain;
     private String accessKeyId;
     private String accessKeySecret;
 }

+ 22 - 30
oss-console/src/main/java/cn/reghao/oss/console/app/service/UserKeyService.java

@@ -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));
     }
 }

+ 2 - 10
oss-console/src/main/java/cn/reghao/oss/console/app/service/UserNodeService.java

@@ -6,7 +6,6 @@ import cn.reghao.oss.console.app.db.repository.UserNodeRepository;
 import cn.reghao.oss.console.app.model.dto.UserNodeDto;
 import cn.reghao.oss.console.app.model.po.StoreNode;
 import cn.reghao.oss.console.app.model.po.UploadChannel;
-import cn.reghao.oss.console.app.model.po.UserKey;
 import cn.reghao.oss.console.app.model.po.UserNode;
 import org.springframework.stereotype.Service;
 
@@ -22,14 +21,12 @@ import java.util.stream.Collectors;
 public class UserNodeService {
     private final UserNodeRepository userNodeRepository;
     private final StoreNodeService storeNodeService;
-    private UserKeyService userKeyService;
-    private UploadChannelService uploadChannelService;
+    private final UploadChannelService uploadChannelService;
 
     public UserNodeService(UserNodeRepository userNodeRepository, StoreNodeService storeNodeService,
-                           UserKeyService userKeyService, UploadChannelService uploadChannelService) {
+                           UploadChannelService uploadChannelService) {
         this.userNodeRepository = userNodeRepository;
         this.storeNodeService = storeNodeService;
-        this.userKeyService = userKeyService;
         this.uploadChannelService = uploadChannelService;
     }
 
@@ -55,11 +52,6 @@ public class UserNodeService {
             int nodeId = storeNode.getId();
             int userId = UserContext.getUser().getId();
 
-            UserKey userKey = userKeyService.getByNodeIdAndUserId(nodeId, userId);
-            if (userKey != null) {
-                return Result.fail("UserKey exists");
-            }
-
             UploadChannel uploadChannel = uploadChannelService.getByNodeIdAndUserId(nodeId, userId);
             if (uploadChannel != null) {
                 return Result.fail("UploadChannel exists");

+ 5 - 14
oss-console/src/main/resources/templates/userkey/index1.html

@@ -14,20 +14,13 @@
     <div class="layui-card-body">
         <div class="layui-row timo-card-screen put-row">
             <div class="layui-row timo-card-screen put-row">
-                <div class="pull-left layui-form-pane">
-                    <div class="layui-inline">
-                        <label class="layui-form-label">节点</label>
-                        <div class="layui-input-block timo-search-status">
-                            <select id="getPageByEnv" class="timo-search-select" name="env" onchange="getPageByCriteria()"
-                                    mo:dict="ENVIRONMENT" mo-selected="${env}"></select>
-                        </div>
-                    </div>
-                </div>
                 <div class="pull-right">
                     <div class="btn-group-right">
-                        <button class="layui-btn open-popup" data-title="创建帐号" th:attr="data-url=@{/store/my/key/add}"
-                                data-size="640,480">
-                            <i class="fa fa-plus"></i> 创建
+                        <button class="layui-btn">
+                            <a class="ajax-post" th:attr="data-msg='确定要创建 key'"
+                               th:href="@{'/api/store/my/key/create'}">
+                                <i class="fa fa-plus"></i> 创建
+                            </a>
                         </button>
                     </div>
                 </div>
@@ -37,7 +30,6 @@
             <table class="layui-table timo-table">
                 <thead>
                 <tr>
-                    <th class="sortable" data-field="appName">domain</th>
                     <th class="sortable" data-field="appName">AccessKeyId</th>
                     <th class="sortable" data-field="appName">AccessKeySecret</th>
                     <th>操作</th>
@@ -45,7 +37,6 @@
                 </thead>
                 <tbody>
                 <tr th:each="item:${list}">
-                    <td th:text="${item.domain}">节点</td>
                     <td th:text="${item.accessKeyId}">应用名</td>
                     <td th:text="${item.accessKeySecret}">应用 ID</td>
                     <td>