Skip to content

Commit ef0ce70

Browse files
author
agile.zhou
committed
1. 环境变量新增 saPassword ,用来配置 sa 的密码,启动的时候初始化
2. 环境变量新增 defaultApp,用来配置默认 app,启动的时候会默认创建这个app,密码为空
2 parents ee6b443 + 72e9612 commit ef0ce70

File tree

15 files changed

+279
-91
lines changed

15 files changed

+279
-91
lines changed

src/AgileConfig.Server.Apisite/AgileConfig.Server.Apisite.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6-
<AssemblyVersion>1.9.10</AssemblyVersion>
7-
<Version>1.9.10</Version>
8-
<PackageVersion>1.9.10</PackageVersion>
6+
<AssemblyVersion>1.9.11</AssemblyVersion>
7+
<Version>1.9.11</Version>
8+
<PackageVersion>1.9.11</PackageVersion>
99
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
10-
<FileVersion>1.9.10</FileVersion>
10+
<FileVersion>1.9.11</FileVersion>
1111
<Authors>kklldog</Authors>
1212
<Company>kklldog</Company>
1313
</PropertyGroup>

src/AgileConfig.Server.Apisite/Controllers/AdminController.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,25 @@ public class AdminController : Controller
2424
private readonly IJwtService _jwtService;
2525
private readonly IOidcClient _oidcClient;
2626
private readonly ITinyEventBus _tinyEventBus;
27+
private readonly ISystemInitializationService _systemInitializationService;
2728

2829
public AdminController(
2930
ISettingService settingService,
3031
IUserService userService,
3132
IPermissionService permissionService,
3233
IJwtService jwtService,
3334
IOidcClient oidcClient,
34-
ITinyEventBus tinyEventBus)
35+
ITinyEventBus tinyEventBus,
36+
ISystemInitializationService systemInitializationService
37+
)
3538
{
3639
_settingService = settingService;
3740
_userService = userService;
3841
_permissionService = permissionService;
3942
_jwtService = jwtService;
4043
_oidcClient = oidcClient;
4144
_tinyEventBus = tinyEventBus;
45+
_systemInitializationService = systemInitializationService;
4246
}
4347

4448

@@ -155,9 +159,9 @@ public async Task<IActionResult> OidcLoginByCode(string code)
155159
/// </summary>
156160
/// <returns></returns>
157161
[HttpGet]
158-
public async Task<IActionResult> PasswordInited()
162+
public IActionResult PasswordInited()
159163
{
160-
var has = await _settingService.HasSuperAdmin();
164+
var has = _systemInitializationService.HasSa();
161165
return Json(new
162166
{
163167
success = true,
@@ -170,7 +174,7 @@ public async Task<IActionResult> PasswordInited()
170174
/// </summary>
171175
/// <returns></returns>
172176
[HttpPost]
173-
public async Task<IActionResult> InitPassword([FromBody] InitPasswordVM model)
177+
public IActionResult InitPassword([FromBody] InitPasswordVM model)
174178
{
175179
var password = model.password;
176180
var confirmPassword = model.confirmPassword;
@@ -202,7 +206,7 @@ public async Task<IActionResult> InitPassword([FromBody] InitPasswordVM model)
202206
});
203207
}
204208

205-
if (await _settingService.HasSuperAdmin())
209+
if ( _systemInitializationService.HasSa())
206210
{
207211
return Json(new
208212
{
@@ -211,7 +215,7 @@ public async Task<IActionResult> InitPassword([FromBody] InitPasswordVM model)
211215
});
212216
}
213217

214-
var result = await _settingService.SetSuperAdminPassword(password);
218+
var result = _systemInitializationService.TryInitSaPassword(password);
215219

216220
if (result)
217221
{

src/AgileConfig.Server.Apisite/Controllers/HomeController.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Linq;
77
using System.Reflection;
88
using AgileConfig.Server.Apisite.Utilites;
9-
using AgileConfig.Server.OIDC;
109

1110
namespace AgileConfig.Server.Apisite.Controllers
1211
{
@@ -16,27 +15,30 @@ public class HomeController : Controller
1615
private readonly ISettingService _settingService;
1716
private readonly IUserService _userService;
1817
private readonly IPermissionService _permissionService;
18+
private readonly ISystemInitializationService _systemInitializationService;
1919

2020
public HomeController(
2121
ISettingService settingService,
2222
IUserService userService,
23-
IPermissionService permissionService
23+
IPermissionService permissionService,
24+
ISystemInitializationService systemInitializationService
2425
)
2526
{
2627
_settingService = settingService;
2728
_userService = userService;
2829
_permissionService = permissionService;
30+
_systemInitializationService = systemInitializationService;
2931
}
3032

3133
[AllowAnonymous]
32-
public async Task<IActionResult> IndexAsync()
34+
public IActionResult IndexAsync()
3335
{
3436
if (!Appsettings.IsAdminConsoleMode)
3537
{
3638
return Content($"AgileConfig Node is running now , {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} .");
3739
}
3840

39-
if (!await _settingService.HasSuperAdmin())
41+
if (!_systemInitializationService.HasSa())
4042
{
4143
return Redirect(Request.PathBase + "/ui#/user/initpassword");
4244
}
@@ -83,7 +85,7 @@ public async Task<IActionResult> Sys()
8385
return Json(new
8486
{
8587
appVer,
86-
passwordInited = await _settingService.HasSuperAdmin(),
88+
passwordInited = _systemInitializationService.HasSa(),
8789
ssoEnabled = Appsettings.SsoEnabled,
8890
ssoButtonText = Appsettings.SsoButtonText
8991
});
@@ -93,7 +95,7 @@ public async Task<IActionResult> Sys()
9395
return Json(new
9496
{
9597
appVer,
96-
passwordInited = await _settingService.HasSuperAdmin(),
98+
passwordInited = _systemInitializationService.HasSa(),
9799
envList,
98100
ssoEnabled = Appsettings.SsoEnabled,
99101
ssoButtonText = Appsettings.SsoButtonText

src/AgileConfig.Server.Apisite/InitService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using AgileConfig.Server.Apisite.Metrics;
66
using AgileConfig.Server.Apisite.Utilites;
77
using AgileConfig.Server.IService;
8+
using AgileConfig.Server.Service;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Hosting;
1011
using Microsoft.Extensions.Logging;
@@ -43,6 +44,8 @@ public async Task StartAsync(CancellationToken cancellationToken)
4344
{
4445
_systemInitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
4546
_systemInitializationService.TryInitJwtSecret();//初始化 jwt secret
47+
_systemInitializationService.TryInitSaPassword(); // init super admin password
48+
_systemInitializationService.TryInitDefaultApp();
4649
_ = _remoteServerNodeProxy.TestEchoAsync();//开启节点检测
4750
_ = _serviceHealthCheckService.StartCheckAsync();//开启服务健康检测
4851
_eventRegister.Register();//注册 eventbus 的回调

src/AgileConfig.Server.Apisite/appsettings.Development.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
"logs": {
1313
"protocol": "http", // http grpc
1414
"endpoint": "http://192.168.0.201:5341/ingest/otlp/v1/logs",
15-
"headers":"X-Seq-ApiKey=aabbcc,X-Other-Head=other_head"
15+
"headers": "X-Seq-ApiKey=aabbcc,X-Other-Head=other_head"
1616
},
1717
"traces": {
1818
"protocol": "http", // http grpc
1919
"endpoint": "http://192.168.0.201:5341/ingest/otlp/v1/traces",
20-
"headers":"X-Seq-ApiKey=aabbcc"
20+
"headers": "X-Seq-ApiKey=aabbcc"
2121
},
2222
"metrics": {
2323
"protocol": "http", // http grpc
2424
"endpoint": "http://localhost:9090/api/v1/otlp/v1/metrics",
25-
"headers":"X-Seq-ApiKey=aabbcc"
25+
"headers": "X-Seq-ApiKey=aabbcc"
2626
}
2727
},
2828
"alwaysTrustSsl": true, // If true, the server will ignore SSL errors.
@@ -31,6 +31,8 @@
3131
"removeServiceInterval": 0, // 如果一个服务超出这个时间没有响应,则直接移除这个服务,单位:秒;如果设定为 <= 0,则不会移除,默认 0 。
3232
"pathBase": "", //使用反向代理的时候,或许需要修改这个值 /xxx 必须/开头
3333
"adminConsole": true,
34+
"saPassword": "123456", //super admin 的密码
35+
"defaultApp": "myapp", // 默认应用,每次重启都会尝试新建
3436
"cluster": false, // 集群模式:服务启动后自动加入节点列表,服务启动的时候会获取容器的ip,端口默认5000,适合 docker compose 环境使用
3537
"preview_mode": false,
3638
"db": {

src/AgileConfig.Server.Apisite/appsettings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
},
1010
"otlp": {
11-
"instanceId": "", // if empty, will generate a new one
11+
"instanceId": "", // if empty, will generate a new one
1212
"logs": {
1313
"protocol": "http", // http grpc
1414
"endpoint": ""
@@ -28,6 +28,8 @@
2828
"removeServiceInterval": 0, // 如果一个服务超出这个时间没有响应,则直接移除这个服务,单位:秒;如果设定为 <= 0,则不会移除,默认 0 。
2929
"pathBase": "", //使用反向代理的时候,或许需要修改这个值 /xxx 必须/开头
3030
"adminConsole": false,
31+
"saPassword": "", // super admin 的密码
32+
"defaultApp": "", // 默认应用
3133
"cluster": false, // 集群模式:服务启动后自动加入节点列表,服务启动的时候会获取容器的ip,端口默认5000,适合 docker compose 环境使用
3234
"preview_mode": false,
3335
"db": {

src/AgileConfig.Server.Data.Abstraction/ISysInitRepository.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ public interface ISysInitRepository
2121
/// </summary>
2222
/// <param name="setting"></param>
2323
void SaveInitSetting(Setting setting);
24+
25+
/// <summary>
26+
/// Init super admin
27+
/// </summary>
28+
/// <param name="password"></param>
29+
/// <returns></returns>
30+
bool InitSa(string password);
31+
32+
bool HasSa();
33+
34+
bool InitDefaultApp(string appName);
2435
}

src/AgileConfig.Server.Data.Entity/App.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using FreeSql.DataAnnotations;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Text;
53
using MongoDB.Bson.Serialization.Attributes;
64
using AgileConfig.Server.Common;
75

src/AgileConfig.Server.Data.Repository.Freesql/SysInitRepository.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,81 @@ public void SaveInitSetting(Setting setting)
3434
{
3535
freeSqlFactory.Create().Insert(setting).ExecuteAffrows();
3636
}
37+
38+
public bool InitSa(string password)
39+
{
40+
if (string.IsNullOrEmpty(password))
41+
{
42+
throw new ArgumentNullException(nameof(password));
43+
}
44+
45+
var newSalt = Guid.NewGuid().ToString("N");
46+
password = Encrypt.Md5((password + newSalt));
47+
48+
var sql = freeSqlFactory.Create();
49+
50+
var user = new User();
51+
user.Id = SystemSettings.SuperAdminId;
52+
user.Password = password;
53+
user.Salt = newSalt;
54+
user.Status = UserStatus.Normal;
55+
user.Team = "";
56+
user.CreateTime = DateTime.Now;
57+
user.UserName = SystemSettings.SuperAdminUserName;
58+
59+
sql.Insert(user).ExecuteAffrows();
60+
61+
var userRoles = new List<UserRole>();
62+
userRoles.Add(new UserRole()
63+
{
64+
Id = Guid.NewGuid().ToString("N"),
65+
Role = Role.SuperAdmin,
66+
UserId = SystemSettings.SuperAdminId
67+
});
68+
userRoles.Add(new UserRole()
69+
{
70+
Id = Guid.NewGuid().ToString("N"),
71+
Role = Role.Admin,
72+
UserId = SystemSettings.SuperAdminId
73+
});
74+
75+
sql.Insert(userRoles).ExecuteAffrows();
76+
77+
return true;
78+
}
79+
80+
public bool HasSa()
81+
{
82+
var anySa = freeSqlFactory.Create().Select<User>().Any(x => x.Id == SystemSettings.SuperAdminId);
83+
84+
return anySa;
85+
}
86+
87+
public bool InitDefaultApp(string appName)
88+
{
89+
if (string.IsNullOrEmpty(appName))
90+
{
91+
throw new ArgumentNullException(nameof(appName));
92+
}
93+
94+
var sql = freeSqlFactory.Create();
95+
var anyDefaultApp = sql.Select<App>().Any(x => x.Id == appName);
96+
;
97+
if (!anyDefaultApp)
98+
{
99+
sql.Insert(new App()
100+
{
101+
Id = appName,
102+
Name = appName,
103+
Group = "",
104+
Secret = "",
105+
CreateTime = DateTime.Now,
106+
Enabled = true,
107+
Type = AppType.PRIVATE,
108+
AppAdmin = SystemSettings.SuperAdminId
109+
}).ExecuteAffrows();
110+
}
111+
112+
return true;
113+
}
37114
}

0 commit comments

Comments
 (0)