Skip to content

Commit

Permalink
1. 环境变量新增 saPassword ,用来配置 sa 的密码,启动的时候初始化
Browse files Browse the repository at this point in the history
2. 环境变量新增 defaultApp,用来配置默认 app,启动的时候会默认创建这个app,密码为空
  • Loading branch information
agile.zhou committed Feb 23, 2025
2 parents ee6b443 + 72e9612 commit ef0ce70
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyVersion>1.9.10</AssemblyVersion>
<Version>1.9.10</Version>
<PackageVersion>1.9.10</PackageVersion>
<AssemblyVersion>1.9.11</AssemblyVersion>
<Version>1.9.11</Version>
<PackageVersion>1.9.11</PackageVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<FileVersion>1.9.10</FileVersion>
<FileVersion>1.9.11</FileVersion>
<Authors>kklldog</Authors>
<Company>kklldog</Company>
</PropertyGroup>
Expand Down
16 changes: 10 additions & 6 deletions src/AgileConfig.Server.Apisite/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ public class AdminController : Controller
private readonly IJwtService _jwtService;
private readonly IOidcClient _oidcClient;
private readonly ITinyEventBus _tinyEventBus;
private readonly ISystemInitializationService _systemInitializationService;

public AdminController(
ISettingService settingService,
IUserService userService,
IPermissionService permissionService,
IJwtService jwtService,
IOidcClient oidcClient,
ITinyEventBus tinyEventBus)
ITinyEventBus tinyEventBus,
ISystemInitializationService systemInitializationService
)
{
_settingService = settingService;
_userService = userService;
_permissionService = permissionService;
_jwtService = jwtService;
_oidcClient = oidcClient;
_tinyEventBus = tinyEventBus;
_systemInitializationService = systemInitializationService;
}


Expand Down Expand Up @@ -155,9 +159,9 @@ public async Task<IActionResult> OidcLoginByCode(string code)
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> PasswordInited()
public IActionResult PasswordInited()
{
var has = await _settingService.HasSuperAdmin();
var has = _systemInitializationService.HasSa();
return Json(new
{
success = true,
Expand All @@ -170,7 +174,7 @@ public async Task<IActionResult> PasswordInited()
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> InitPassword([FromBody] InitPasswordVM model)
public IActionResult InitPassword([FromBody] InitPasswordVM model)
{
var password = model.password;
var confirmPassword = model.confirmPassword;
Expand Down Expand Up @@ -202,7 +206,7 @@ public async Task<IActionResult> InitPassword([FromBody] InitPasswordVM model)
});
}

if (await _settingService.HasSuperAdmin())
if ( _systemInitializationService.HasSa())
{
return Json(new
{
Expand All @@ -211,7 +215,7 @@ public async Task<IActionResult> InitPassword([FromBody] InitPasswordVM model)
});
}

var result = await _settingService.SetSuperAdminPassword(password);
var result = _systemInitializationService.TryInitSaPassword(password);

if (result)
{
Expand Down
14 changes: 8 additions & 6 deletions src/AgileConfig.Server.Apisite/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Linq;
using System.Reflection;
using AgileConfig.Server.Apisite.Utilites;
using AgileConfig.Server.OIDC;

namespace AgileConfig.Server.Apisite.Controllers
{
Expand All @@ -16,27 +15,30 @@ public class HomeController : Controller
private readonly ISettingService _settingService;
private readonly IUserService _userService;
private readonly IPermissionService _permissionService;
private readonly ISystemInitializationService _systemInitializationService;

public HomeController(
ISettingService settingService,
IUserService userService,
IPermissionService permissionService
IPermissionService permissionService,
ISystemInitializationService systemInitializationService
)
{
_settingService = settingService;
_userService = userService;
_permissionService = permissionService;
_systemInitializationService = systemInitializationService;
}

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

if (!await _settingService.HasSuperAdmin())
if (!_systemInitializationService.HasSa())
{
return Redirect(Request.PathBase + "/ui#/user/initpassword");
}
Expand Down Expand Up @@ -83,7 +85,7 @@ public async Task<IActionResult> Sys()
return Json(new
{
appVer,
passwordInited = await _settingService.HasSuperAdmin(),
passwordInited = _systemInitializationService.HasSa(),
ssoEnabled = Appsettings.SsoEnabled,
ssoButtonText = Appsettings.SsoButtonText
});
Expand All @@ -93,7 +95,7 @@ public async Task<IActionResult> Sys()
return Json(new
{
appVer,
passwordInited = await _settingService.HasSuperAdmin(),
passwordInited = _systemInitializationService.HasSa(),
envList,
ssoEnabled = Appsettings.SsoEnabled,
ssoButtonText = Appsettings.SsoButtonText
Expand Down
3 changes: 3 additions & 0 deletions src/AgileConfig.Server.Apisite/InitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using AgileConfig.Server.Apisite.Metrics;
using AgileConfig.Server.Apisite.Utilites;
using AgileConfig.Server.IService;
using AgileConfig.Server.Service;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -43,6 +44,8 @@ public async Task StartAsync(CancellationToken cancellationToken)
{
_systemInitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
_systemInitializationService.TryInitJwtSecret();//初始化 jwt secret
_systemInitializationService.TryInitSaPassword(); // init super admin password
_systemInitializationService.TryInitDefaultApp();
_ = _remoteServerNodeProxy.TestEchoAsync();//开启节点检测
_ = _serviceHealthCheckService.StartCheckAsync();//开启服务健康检测
_eventRegister.Register();//注册 eventbus 的回调
Expand Down
8 changes: 5 additions & 3 deletions src/AgileConfig.Server.Apisite/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
"logs": {
"protocol": "http", // http grpc
"endpoint": "http://192.168.0.201:5341/ingest/otlp/v1/logs",
"headers":"X-Seq-ApiKey=aabbcc,X-Other-Head=other_head"
"headers": "X-Seq-ApiKey=aabbcc,X-Other-Head=other_head"
},
"traces": {
"protocol": "http", // http grpc
"endpoint": "http://192.168.0.201:5341/ingest/otlp/v1/traces",
"headers":"X-Seq-ApiKey=aabbcc"
"headers": "X-Seq-ApiKey=aabbcc"
},
"metrics": {
"protocol": "http", // http grpc
"endpoint": "http://localhost:9090/api/v1/otlp/v1/metrics",
"headers":"X-Seq-ApiKey=aabbcc"
"headers": "X-Seq-ApiKey=aabbcc"
}
},
"alwaysTrustSsl": true, // If true, the server will ignore SSL errors.
Expand All @@ -31,6 +31,8 @@
"removeServiceInterval": 0, // 如果一个服务超出这个时间没有响应,则直接移除这个服务,单位:秒;如果设定为 <= 0,则不会移除,默认 0 。
"pathBase": "", //使用反向代理的时候,或许需要修改这个值 /xxx 必须/开头
"adminConsole": true,
"saPassword": "123456", //super admin 的密码
"defaultApp": "myapp", // 默认应用,每次重启都会尝试新建
"cluster": false, // 集群模式:服务启动后自动加入节点列表,服务启动的时候会获取容器的ip,端口默认5000,适合 docker compose 环境使用
"preview_mode": false,
"db": {
Expand Down
4 changes: 3 additions & 1 deletion src/AgileConfig.Server.Apisite/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
},
"otlp": {
"instanceId": "", // if empty, will generate a new one
"instanceId": "", // if empty, will generate a new one
"logs": {
"protocol": "http", // http grpc
"endpoint": ""
Expand All @@ -28,6 +28,8 @@
"removeServiceInterval": 0, // 如果一个服务超出这个时间没有响应,则直接移除这个服务,单位:秒;如果设定为 <= 0,则不会移除,默认 0 。
"pathBase": "", //使用反向代理的时候,或许需要修改这个值 /xxx 必须/开头
"adminConsole": false,
"saPassword": "", // super admin 的密码
"defaultApp": "", // 默认应用
"cluster": false, // 集群模式:服务启动后自动加入节点列表,服务启动的时候会获取容器的ip,端口默认5000,适合 docker compose 环境使用
"preview_mode": false,
"db": {
Expand Down
11 changes: 11 additions & 0 deletions src/AgileConfig.Server.Data.Abstraction/ISysInitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public interface ISysInitRepository
/// </summary>
/// <param name="setting"></param>
void SaveInitSetting(Setting setting);

/// <summary>
/// Init super admin
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
bool InitSa(string password);

bool HasSa();

bool InitDefaultApp(string appName);
}
2 changes: 0 additions & 2 deletions src/AgileConfig.Server.Data.Entity/App.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Bson.Serialization.Attributes;
using AgileConfig.Server.Common;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,81 @@ public void SaveInitSetting(Setting setting)
{
freeSqlFactory.Create().Insert(setting).ExecuteAffrows();
}

public bool InitSa(string password)
{
if (string.IsNullOrEmpty(password))
{
throw new ArgumentNullException(nameof(password));
}

var newSalt = Guid.NewGuid().ToString("N");
password = Encrypt.Md5((password + newSalt));

var sql = freeSqlFactory.Create();

var user = new User();
user.Id = SystemSettings.SuperAdminId;
user.Password = password;
user.Salt = newSalt;
user.Status = UserStatus.Normal;
user.Team = "";
user.CreateTime = DateTime.Now;
user.UserName = SystemSettings.SuperAdminUserName;

sql.Insert(user).ExecuteAffrows();

var userRoles = new List<UserRole>();
userRoles.Add(new UserRole()
{
Id = Guid.NewGuid().ToString("N"),
Role = Role.SuperAdmin,
UserId = SystemSettings.SuperAdminId
});
userRoles.Add(new UserRole()
{
Id = Guid.NewGuid().ToString("N"),
Role = Role.Admin,
UserId = SystemSettings.SuperAdminId
});

sql.Insert(userRoles).ExecuteAffrows();

return true;
}

public bool HasSa()
{
var anySa = freeSqlFactory.Create().Select<User>().Any(x => x.Id == SystemSettings.SuperAdminId);

return anySa;
}

public bool InitDefaultApp(string appName)
{
if (string.IsNullOrEmpty(appName))
{
throw new ArgumentNullException(nameof(appName));
}

var sql = freeSqlFactory.Create();
var anyDefaultApp = sql.Select<App>().Any(x => x.Id == appName);
;
if (!anyDefaultApp)
{
sql.Insert(new App()
{
Id = appName,
Name = appName,
Group = "",
Secret = "",
CreateTime = DateTime.Now,
Enabled = true,
Type = AppType.PRIVATE,
AppAdmin = SystemSettings.SuperAdminId
}).ExecuteAffrows();
}

return true;
}
}
Loading

0 comments on commit ef0ce70

Please sign in to comment.