From b68276fc108cb63ea44827e88a6a8d99b7d30fa1 Mon Sep 17 00:00:00 2001 From: Liu Rui Date: Wed, 23 Oct 2024 09:10:49 +0800 Subject: [PATCH] =?UTF-8?q?feat-=E7=B3=BB=E7=BB=9F=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96-=E6=94=AF=E6=8C=81=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E4=BA=A4=E4=BA=92=E7=9A=84=E6=96=B9=E5=BC=8F=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E7=94=A8=E6=88=B7=E5=90=8D=E5=92=8C?= =?UTF-8?q?=E5=AF=86=E7=A0=81=EF=BC=8C=E5=90=8C=E6=97=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E7=9A=84=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=85=8D=E7=BD=AE=E4=BF=A1=E6=81=AFMUYUN=5FU?= =?UTF-8?q?SERNAME=E3=80=81MUYUN=5FPASSWORD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 4 +++ muyun-boot/src/main/resources/application.yml | 2 +- .../controller/UserInfoController.java | 32 ++++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 807f13fe..605ad914 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,6 +12,10 @@ jobs: check: runs-on: 'ubuntu-latest' steps: + - name: Set environment variable + run: | + echo "MUYUN_USERNAME=admin" >> $GITHUB_ENV + echo "MUYUN_PASSWORD=admin@muyun" >> $GITHUB_ENV - uses: actions/checkout@v3 - name: Set up JDK 21 uses: actions/setup-java@v3 diff --git a/muyun-boot/src/main/resources/application.yml b/muyun-boot/src/main/resources/application.yml index 846df522..18d5ff7b 100644 --- a/muyun-boot/src/main/resources/application.yml +++ b/muyun-boot/src/main/resources/application.yml @@ -1,6 +1,6 @@ muyun: debug: true - super-user-id: 1 + super-user-id: 1 # 可以在环境变量里预先设置 MUYUN_USERNAME、MUYUN_PASSWORD 来确定初始化的用户名和密码 file-server: upload-path: /Users/liurui/Downloads/uploads page-path: fileServer diff --git a/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/UserInfoController.java b/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/UserInfoController.java index 91af9e0a..cf5edd08 100644 --- a/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/UserInfoController.java +++ b/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/UserInfoController.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Scanner; import java.util.Set; import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH; @@ -100,8 +101,25 @@ protected void afterInit() { Map superUser = this.view(superUserId); if (superUser == null) { + String adminUsername = System.getenv("MUYUN_USERNAME"); + String adminPassword = System.getenv("MUYUN_PASSWORD"); + + if (adminUsername == null || adminPassword == null) { + System.out.println("SuperUser not found in the database."); + System.out.println("Please input the initial admin account details:"); + + Scanner scanner = new Scanner(System.in); + + adminUsername = promptForInput(scanner, "Admin Username: "); + adminPassword = promptForInput(scanner, "Admin Password: "); + } else { + logger.info("USE ENVIRONMENT INFORMATION"); + logger.info("ADMIN USERNAME: {}", adminUsername); + logger.info("ADMIN PASSWORD: {}", adminPassword); + } + this.create(Map.of("id", superUserId, "v_name", "超级管理员")); - this.setUser(superUserId, Map.of("v_username", "admin", "v_password", "admin@bsy", "v_password2", "admin@bsy")); + this.setUser(superUserId, Map.of("v_username", adminUsername, "v_password", adminPassword, "v_password2", adminPassword)); } } @@ -261,4 +279,16 @@ public ModuleConfig getModuleConfig() { .addAction(new ModuleAction("roles", "获取角色", ModuleAction.TypeLike.VIEW)) .addAction(new ModuleAction("setRoles", "设置角色", ModuleAction.TypeLike.UPDATE)); } + + private static String promptForInput(Scanner scanner, String promptMessage) { + String input; + do { + System.out.print(promptMessage); + input = scanner.nextLine().trim(); // 去掉输入前后的空格 + if (input.isEmpty()) { + System.out.println("Input cannot be empty. Please try again."); + } + } while (input.isEmpty()); + return input; + } }