Skip to content

Commit

Permalink
修改初始化用户机制
Browse files Browse the repository at this point in the history
  • Loading branch information
Codespilot committed Mar 5, 2024
1 parent a9e5b68 commit d8ea3a2
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public override void ConfigureServices(ServiceConfigurationContext context)
ConfigureCachingServices(context.Services);

ConfigureBusServices(context.Services);

context.Services.AddHostedService<UserInitializeService>();
}

private void ConfigureCachingServices(IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public Task HandleAsync(UserUpdateCommand message, MessageContext context, Cance

business.Email = message.Item2.Email;
business.NickName = message.Item2.NickName;
business.Phone = message.Item2.Phone;
business.IsAdmin = message.Item2.IsAdmin;

business.MarkAsUpdate();

await business.SaveAsync(true, cancellationToken);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Nerosoft.Starfish.Application;

internal class UserInitializeService : BackgroundService
{
private readonly IUserApplicationService _service;
private readonly ILogger<UserInitializeService> _logger;

public UserInitializeService(IUserApplicationService service, ILoggerFactory logger)
{
_service = service;
_logger = logger.CreateLogger<UserInitializeService>();
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await _service.InitializeAsync(stoppingToken);
}
catch (Exception exception)
{
_logger.LogError(exception, exception.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ protected override Task InsertAsync(CancellationToken cancellationToken = defaul
[FactoryUpdate]
protected override Task UpdateAsync(CancellationToken cancellationToken = default)
{
if (!HasChangedProperties)
{
return Task.CompletedTask;
}

if (ChangedProperties.Contains(EmailProperty))
{
Aggregate.SetEmail(Email);
Expand Down
37 changes: 26 additions & 11 deletions Source/Starfish.Service/UseCases/Identity/UserInitializeUseCase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.Extensions.Configuration;
using Nerosoft.Euonia.Application;
using Nerosoft.Euonia.Bus;
using Nerosoft.Euonia.Mapping;
using Nerosoft.Starfish.Application;
using Nerosoft.Starfish.Transit;
using Nerosoft.Starfish.Domain;

namespace Nerosoft.Starfish.UseCases;

Expand All @@ -11,23 +11,38 @@ internal interface IUserInitializeUseCase : IParameterlessUseCase;
internal class UserInitializeUseCase : IUserInitializeUseCase
{
private readonly IBus _bus;
private readonly IUserRepository _repository;
private readonly IConfiguration _configuration;

public UserInitializeUseCase(IBus bus)
public UserInitializeUseCase(IBus bus, IUserRepository repository, IConfiguration configuration)
{
_bus = bus;
_repository = repository;
_configuration = configuration;
}

public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
var userCreateDto = new UserCreateDto
var username = _configuration["InitializeUser:UserName"];

if (string.IsNullOrWhiteSpace(username))
{
return;
}

var exists = await _repository.CheckUserNameExistsAsync(username, cancellationToken);
if (exists)
{
return;
}

var command = new UserCreateCommand
{
UserName = "starfish",
Password = "starfish.888",
NickName = "admin",
IsAdmin = true
UserName = _configuration["InitializeUser:UserName"],
Password = _configuration["InitializeUser:Password"],
IsAdmin = true,
Reserved = true
};
var command = TypeAdapter.ProjectedAs<UserCreateCommand>(userCreateDto);
command.Reserved = true;
await _bus.SendAsync<UserCreateCommand, string>(command, cancellationToken);
await _bus.SendAsync<UserCreateCommand, string>(command, cancellationToken).ContinueWith(task => task.WaitAndUnwrapException());
}
}
12 changes: 0 additions & 12 deletions Source/Starfish.Webapi/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,4 @@ public async Task<IActionResult> ChangePasswordAsync([FromBody] ChangePasswordRe
await _service.ChangePasswordAsync(data.OldPassword, data.NewPassword, HttpContext.RequestAborted);
return Ok();
}

/// <summary>
/// 初始化用户数据
/// </summary>
/// <returns></returns>
[HttpPost("init")]
[AllowAnonymous]
public async Task<IActionResult> InitializeAsync()
{
await _service.InitializeAsync(HttpContext.RequestAborted);
return Ok();
}
}
5 changes: 4 additions & 1 deletion Source/Starfish.Webapi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
"TopicName": "nerosoft.starfish.topic"
}
},
"InitializeUsername": "starfish",
"InitializeUser": {
"UserName": "admin",
"Password": "Starfish.8888"
},
"ReservedUsernames": [
"starfish",
"admin",
Expand Down

0 comments on commit d8ea3a2

Please sign in to comment.