Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 3.2.2
- Feature: 新增登录功能
## 3.2.1
- Feature: 镜像新增linux/386和linux/arm/v7架构
## 3.2.0
Expand Down
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Authors>Ray</Authors>
<Version>3.2.1</Version>
<Version>3.2.2</Version>
<NoWarn>$(NoWarn);CS1591;CS0436</NoWarn>
</PropertyGroup>
</Project>
15 changes: 15 additions & 0 deletions src/Ray.BiliBiliTool.Domain/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Ray.BiliBiliTool.Domain;

[Table("bili_user")]
public class User
{
[Key]
public long Id { get; set; }
public required string Username { get; set; }
public required string PasswordHash { get; set; }
public required string Salt { get; set; }
public List<string> Roles { get; set; } = [];
}
17 changes: 17 additions & 0 deletions src/Ray.BiliBiliTool.Infrastructure.EF/BiliDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class BiliDbContext(IConfiguration config) : DbContext
{
public DbSet<ExecutionLog> ExecutionLogs { get; set; }
public DbSet<BiliLogs> BiliLogs { get; set; }
public DbSet<User> Users { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand Down Expand Up @@ -69,6 +70,22 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasIndex(x => x.FireInstanceIdComputed) // 在计算列上创建索引
.HasDatabaseName("IX_Logs_FireInstanceIdComputed");
});

modelBuilder.Entity<User>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Username).IsRequired().HasMaxLength(50);
entity.Property(e => e.PasswordHash).IsRequired();
entity.Property(e => e.Salt).IsRequired();
entity
.Property(e => e.Roles)
.HasConversion(
v => string.Join(',', v),
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList()
);

entity.HasIndex(e => e.Username).IsUnique();
});
}

private void AddSqliteDateTimeOffsetSupport(ModelBuilder builder)
Expand Down
38 changes: 38 additions & 0 deletions src/Ray.BiliBiliTool.Infrastructure.EF/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore;
using Ray.BiliBiliTool.Domain;
using Ray.BiliBiliTool.Infrastructure.Helpers;

namespace Ray.BiliBiliTool.Infrastructure.EF;

public class DbInitializer(BiliDbContext context)
{
private const string DefaultUserName = "admin";
private const string DefaultPassword = "BiliTool@2233";

Comment on lines +10 to +11
Copy link

Copilot AI Jun 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding a default admin password poses a security risk. Consider fetching this from configuration or prompting for a secure setup during deployment.

Suggested change
private const string DefaultPassword = "BiliTool@2233";
private readonly string DefaultPassword = Environment.GetEnvironmentVariable("DEFAULT_ADMIN_PASSWORD")
?? throw new InvalidOperationException("Default admin password is not set in the environment variables.");

Copilot uses AI. Check for mistakes.
public async Task InitializeAsync()
{
await context.Database.MigrateAsync();

await InitUserAsync();
}

private async Task InitUserAsync()
{
if (await context.Users.AnyAsync())
{
return;
}

var (hash, salt) = PasswordHelper.HashPassword(DefaultPassword);
var adminUser = new User
{
Username = DefaultUserName,
PasswordHash = hash,
Salt = salt,
Roles = ["Administrator"],
};

context.Users.Add(adminUser);
await context.SaveChangesAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static class ServiceCollectionExtension
public static IServiceCollection AddEF(this IServiceCollection services)
{
services.AddDbContextFactory<BiliDbContext>();
services.AddScoped<DbInitializer>();
return services;
}
}
Loading
Loading