checkpoint
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StatiKosev.Data;
|
||||
|
||||
public static class DbSeeder
|
||||
{
|
||||
public const string UserRole = "User";
|
||||
public const string AdminRole = "Admin";
|
||||
|
||||
//applies pending migrations, creates the roles and makes the configured AdminEmail account an admin
|
||||
public static async Task SeedAsync(IServiceProvider services, string? adminEmail)
|
||||
{
|
||||
using var scope = services.CreateScope();
|
||||
var sp = scope.ServiceProvider;
|
||||
await sp.GetRequiredService<AppDbContext>().Database.MigrateAsync();
|
||||
|
||||
var roles = sp.GetRequiredService<RoleManager<IdentityRole>>();
|
||||
foreach (var role in new[] { UserRole, AdminRole })
|
||||
if (!await roles.RoleExistsAsync(role))
|
||||
await roles.CreateAsync(new IdentityRole(role));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(adminEmail)) return;
|
||||
var users = sp.GetRequiredService<UserManager<IdentityUser>>();
|
||||
var admin = await users.FindByEmailAsync(adminEmail);
|
||||
if (admin is not null && !await users.IsInRoleAsync(admin, AdminRole))
|
||||
await users.AddToRoleAsync(admin, AdminRole);
|
||||
}
|
||||
|
||||
public static bool IsAdminEmail(string? email, string? adminEmail) =>
|
||||
!string.IsNullOrWhiteSpace(adminEmail) && string.Equals(email, adminEmail, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
@@ -7,8 +7,8 @@ public class PrivateMessage
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public bool Anonymous { get; set; }
|
||||
public string? SenderId { get; set; }
|
||||
public string SenderUsername { get; set; } = "";
|
||||
[MaxLength(450)] public string? SenderId { get; set; }
|
||||
[MaxLength(512)] public string SenderUsername { get; set; } = "";
|
||||
[MaxLength(3000)] public string Body { get; set; } = "";
|
||||
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
public bool IsRead { get; set; }
|
||||
@@ -18,7 +18,7 @@ public class PrivateMessage
|
||||
public PrivateMessage(string body, string userAgent)
|
||||
{
|
||||
CreatedAt = DateTime.UtcNow;
|
||||
SenderUsername = userAgent + "___" + CreatedAt.ToString("dd-MM-yyyy___HH:mm:ss");
|
||||
SenderUsername = userAgent[..Math.Min(userAgent.Length, 480)] + "___" + CreatedAt.ToString("dd-MM-yyyy___HH:mm:ss");
|
||||
Anonymous = true;
|
||||
SenderId = null;
|
||||
Body = body;
|
||||
|
||||
Reference in New Issue
Block a user