Skip to content

Commit

Permalink
feat-系统初始化-支持命令行交互的方式输入管理员用户名和密码,同时支持环境变量的方式获取配置信息MUYUN_USERNAME、MUYU…
Browse files Browse the repository at this point in the history
…N_PASSWORD
  • Loading branch information
aruis committed Oct 23, 2024
1 parent 9eda682 commit b68276f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion muyun-boot/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,8 +101,25 @@ protected void afterInit() {

Map<String, ?> 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));
}
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit b68276f

Please sign in to comment.