partial revert + change

This commit is contained in:
Stanley Dimant
2022-08-25 00:52:58 +02:00
parent 88cec262cd
commit a5a2ce7e34
10 changed files with 61 additions and 236 deletions

View File

@@ -109,20 +109,11 @@ namespace MareSynchronosServer.Hubs
other = otherToUser.UserUID other = otherToUser.UserUID
} into leftJoin } into leftJoin
from otherEntry in leftJoin.DefaultIfEmpty() from otherEntry in leftJoin.DefaultIfEmpty()
join alias in _dbContext.Aliases
on new
{
uid = userToOther.UserUID
} equals new
{
uid = alias.UserUID
} into aliasLeftJoin
from aliasEntry in aliasLeftJoin.DefaultIfEmpty()
where where
userToOther.UserUID == userid userToOther.UserUID == userid
select new select new
{ {
Alias = aliasEntry == null ? string.Empty : aliasEntry.AliasUID, otherEntry.OtherUser.Alias,
userToOther.IsPaused, userToOther.IsPaused,
OtherIsPaused = otherEntry != null && otherEntry.IsPaused, OtherIsPaused = otherEntry != null && otherEntry.IsPaused,
userToOther.OtherUserUID, userToOther.OtherUserUID,
@@ -181,25 +172,18 @@ namespace MareSynchronosServer.Hubs
[HubMethodName(Api.SendUserPairedClientAddition)] [HubMethodName(Api.SendUserPairedClientAddition)]
public async Task SendPairedClientAddition(string uid) public async Task SendPairedClientAddition(string uid)
{ {
string otherUserUid = uid;
if (uid == AuthenticatedUserId) return; if (uid == AuthenticatedUserId) return;
uid = uid.Trim(); uid = uid.Trim();
var user = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false); var user = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
var potentialAlias = _dbContext.Aliases.SingleOrDefault(u => u.AliasUID == uid);
if (potentialAlias != null)
{
otherUserUid = potentialAlias.UserUID;
}
var otherUser = await _dbContext.Users var otherUser = await _dbContext.Users
.SingleOrDefaultAsync(u => u.UID == otherUserUid).ConfigureAwait(false); .SingleOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false);
var existingEntry = var existingEntry =
await _dbContext.ClientPairs.AsNoTracking() await _dbContext.ClientPairs.AsNoTracking()
.FirstOrDefaultAsync(p => .FirstOrDefaultAsync(p =>
p.User.UID == AuthenticatedUserId && p.OtherUser.UID == otherUserUid).ConfigureAwait(false); p.User.UID == AuthenticatedUserId && p.OtherUser.UID == uid).ConfigureAwait(false);
if (otherUser == null || existingEntry != null) return; if (otherUser == null || existingEntry != null) return;
_logger.LogInformation("User {AuthenticatedUserId} adding {uid} to whitelist", AuthenticatedUserId, otherUserUid); _logger.LogInformation("User {AuthenticatedUserId} adding {uid} to whitelist", AuthenticatedUserId, uid);
ClientPair wl = new ClientPair() ClientPair wl = new ClientPair()
{ {
IsPaused = false, IsPaused = false,
@@ -208,7 +192,7 @@ namespace MareSynchronosServer.Hubs
}; };
await _dbContext.ClientPairs.AddAsync(wl).ConfigureAwait(false); await _dbContext.ClientPairs.AddAsync(wl).ConfigureAwait(false);
await _dbContext.SaveChangesAsync().ConfigureAwait(false); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherEntry = OppositeEntry(otherUserUid); var otherEntry = OppositeEntry(uid);
await Clients.User(user.UID) await Clients.User(user.UID)
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto() .SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
{ {
@@ -219,7 +203,7 @@ namespace MareSynchronosServer.Hubs
}, string.Empty).ConfigureAwait(false); }, string.Empty).ConfigureAwait(false);
if (otherEntry != null) if (otherEntry != null)
{ {
await Clients.User(otherUserUid).SendAsync(Api.OnUserUpdateClientPairs, await Clients.User(uid).SendAsync(Api.OnUserUpdateClientPairs,
new ClientPairDto() new ClientPairDto()
{ {
OtherUID = user.UID, OtherUID = user.UID,
@@ -237,7 +221,7 @@ namespace MareSynchronosServer.Hubs
} }
} }
await _metricsClient.IncGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugePairs, Value = 1 }).ConfigureAwait(false); await _metricsClient.IncGaugeAsync(new GaugeRequest() {GaugeName = MetricsAPI.GaugePairs, Value = 1}).ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]

View File

@@ -73,11 +73,11 @@ namespace MareSynchronosServer.Hubs
user.CharacterIdentification = characterIdentification; user.CharacterIdentification = characterIdentification;
await _dbContext.SaveChangesAsync().ConfigureAwait(false); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var alias = await _dbContext.Aliases.SingleOrDefaultAsync(u => u.UserUID == userId).ConfigureAwait(false); var alias = (await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == userId).ConfigureAwait(false))?.Alias ?? string.Empty;
return new ConnectionDto return new ConnectionDto
{ {
ServerVersion = Api.Version, ServerVersion = Api.Version,
UID = alias == null ? userId : alias.AliasUID, UID = string.IsNullOrEmpty(alias) ? userId : alias,
IsModerator = user.IsModerator, IsModerator = user.IsModerator,
IsAdmin = user.IsAdmin IsAdmin = user.IsAdmin
}; };

View File

@@ -166,8 +166,7 @@ public class DiscordBot : IHostedService
while (!hasValidUid) while (!hasValidUid)
{ {
var uid = GenerateRandomString(10); var uid = GenerateRandomString(10);
if (await db.Users.AnyAsync(u => u.UID == uid).ConfigureAwait(false)) continue; if (db.Users.Any(u => u.UID == uid)) continue;
if (await db.Aliases.AnyAsync(u => u.AliasUID == uid).ConfigureAwait(false)) continue;
user.UID = uid; user.UID = uid;
hasValidUid = true; hasValidUid = true;
} }

View File

@@ -1,16 +1,12 @@
using MareSynchronosShared.Models; using MareSynchronosShared.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace MareSynchronosShared.Data; namespace MareSynchronosShared.Data;
public class MareDbContext : DbContext public class MareDbContext : DbContext
{ {
#if DEBUG
public MareDbContext() { } public MareDbContext() { }
public MareDbContext(DbContextOptions<MareDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
@@ -29,6 +25,11 @@ public class MareDbContext : DbContext
base.OnConfiguring(optionsBuilder); base.OnConfiguring(optionsBuilder);
} }
#endif
public MareDbContext(DbContextOptions<MareDbContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; }
public DbSet<FileCache> Files { get; set; } public DbSet<FileCache> Files { get; set; }
@@ -38,7 +39,6 @@ public class MareDbContext : DbContext
public DbSet<Auth> Auth { get; set; } public DbSet<Auth> Auth { get; set; }
public DbSet<LodeStoneAuth> LodeStoneAuth { get; set; } public DbSet<LodeStoneAuth> LodeStoneAuth { get; set; }
public DbSet<BannedRegistrations> BannedRegistrations { get; set; } public DbSet<BannedRegistrations> BannedRegistrations { get; set; }
public DbSet<Alias> Aliases { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
@@ -46,8 +46,6 @@ public class MareDbContext : DbContext
modelBuilder.Entity<Auth>().ToTable("auth"); modelBuilder.Entity<Auth>().ToTable("auth");
modelBuilder.Entity<User>().ToTable("users"); modelBuilder.Entity<User>().ToTable("users");
modelBuilder.Entity<User>().HasIndex(c => c.CharacterIdentification); modelBuilder.Entity<User>().HasIndex(c => c.CharacterIdentification);
modelBuilder.Entity<Alias>().ToTable("aliases");
modelBuilder.Entity<Alias>().HasIndex(c => c.AliasUID);
modelBuilder.Entity<FileCache>().ToTable("file_caches"); modelBuilder.Entity<FileCache>().ToTable("file_caches");
modelBuilder.Entity<FileCache>().HasIndex(c => c.UploaderUID); modelBuilder.Entity<FileCache>().HasIndex(c => c.UploaderUID);
modelBuilder.Entity<ClientPair>().ToTable("client_pairs"); modelBuilder.Entity<ClientPair>().ToTable("client_pairs");

View File

@@ -1,115 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MareSynchronosServer.Migrations
{
public partial class AddAlias : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "fk_auth_users_user_temp_id",
table: "auth");
migrationBuilder.DropForeignKey(
name: "fk_client_pairs_users_other_user_temp_id1",
table: "client_pairs");
migrationBuilder.DropForeignKey(
name: "fk_client_pairs_users_user_temp_id2",
table: "client_pairs");
migrationBuilder.CreateTable(
name: "aliases",
columns: table => new
{
alias_uid = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
user_uid = table.Column<string>(type: "character varying(10)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("pk_aliases", x => x.alias_uid);
table.ForeignKey(
name: "fk_aliases_users_user_temp_id",
column: x => x.user_uid,
principalTable: "users",
principalColumn: "uid");
});
migrationBuilder.CreateIndex(
name: "ix_aliases_alias_uid",
table: "aliases",
column: "alias_uid");
migrationBuilder.CreateIndex(
name: "ix_aliases_user_uid",
table: "aliases",
column: "user_uid");
migrationBuilder.AddForeignKey(
name: "fk_auth_users_user_temp_id1",
table: "auth",
column: "user_uid",
principalTable: "users",
principalColumn: "uid");
migrationBuilder.AddForeignKey(
name: "fk_client_pairs_users_other_user_temp_id2",
table: "client_pairs",
column: "other_user_uid",
principalTable: "users",
principalColumn: "uid",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "fk_client_pairs_users_user_temp_id3",
table: "client_pairs",
column: "user_uid",
principalTable: "users",
principalColumn: "uid",
onDelete: ReferentialAction.Cascade);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "fk_auth_users_user_temp_id1",
table: "auth");
migrationBuilder.DropForeignKey(
name: "fk_client_pairs_users_other_user_temp_id2",
table: "client_pairs");
migrationBuilder.DropForeignKey(
name: "fk_client_pairs_users_user_temp_id3",
table: "client_pairs");
migrationBuilder.DropTable(
name: "aliases");
migrationBuilder.AddForeignKey(
name: "fk_auth_users_user_temp_id",
table: "auth",
column: "user_uid",
principalTable: "users",
principalColumn: "uid");
migrationBuilder.AddForeignKey(
name: "fk_client_pairs_users_other_user_temp_id1",
table: "client_pairs",
column: "other_user_uid",
principalTable: "users",
principalColumn: "uid",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "fk_client_pairs_users_user_temp_id2",
table: "client_pairs",
column: "user_uid",
principalTable: "users",
principalColumn: "uid",
onDelete: ReferentialAction.Cascade);
}
}
}

View File

@@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace MareSynchronosServer.Migrations namespace MareSynchronosServer.Migrations
{ {
[DbContext(typeof(MareDbContext))] [DbContext(typeof(MareDbContext))]
[Migration("20220824222011_AddAlias")] [Migration("20220824225157_AddAlias")]
partial class AddAlias partial class AddAlias
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -24,29 +24,6 @@ namespace MareSynchronosServer.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("MareSynchronosShared.Models.Alias", b =>
{
b.Property<string>("AliasUID")
.HasMaxLength(10)
.HasColumnType("character varying(10)")
.HasColumnName("alias_uid");
b.Property<string>("UserUID")
.HasColumnType("character varying(10)")
.HasColumnName("user_uid");
b.HasKey("AliasUID")
.HasName("pk_aliases");
b.HasIndex("AliasUID")
.HasDatabaseName("ix_aliases_alias_uid");
b.HasIndex("UserUID")
.HasDatabaseName("ix_aliases_user_uid");
b.ToTable("aliases", (string)null);
});
modelBuilder.Entity("MareSynchronosShared.Models.Auth", b => modelBuilder.Entity("MareSynchronosShared.Models.Auth", b =>
{ {
b.Property<string>("HashedKey") b.Property<string>("HashedKey")
@@ -237,6 +214,11 @@ namespace MareSynchronosServer.Migrations
.HasColumnType("character varying(10)") .HasColumnType("character varying(10)")
.HasColumnName("uid"); .HasColumnName("uid");
b.Property<string>("Alias")
.HasMaxLength(100)
.HasColumnType("character varying(100)")
.HasColumnName("alias");
b.Property<string>("CharacterIdentification") b.Property<string>("CharacterIdentification")
.HasMaxLength(100) .HasMaxLength(100)
.HasColumnType("character varying(100)") .HasColumnType("character varying(100)")
@@ -269,22 +251,12 @@ namespace MareSynchronosServer.Migrations
b.ToTable("users", (string)null); b.ToTable("users", (string)null);
}); });
modelBuilder.Entity("MareSynchronosShared.Models.Alias", b =>
{
b.HasOne("MareSynchronosShared.Models.User", "User")
.WithMany()
.HasForeignKey("UserUID")
.HasConstraintName("fk_aliases_users_user_temp_id");
b.Navigation("User");
});
modelBuilder.Entity("MareSynchronosShared.Models.Auth", b => modelBuilder.Entity("MareSynchronosShared.Models.Auth", b =>
{ {
b.HasOne("MareSynchronosShared.Models.User", "User") b.HasOne("MareSynchronosShared.Models.User", "User")
.WithMany() .WithMany()
.HasForeignKey("UserUID") .HasForeignKey("UserUID")
.HasConstraintName("fk_auth_users_user_temp_id1"); .HasConstraintName("fk_auth_users_user_temp_id");
b.Navigation("User"); b.Navigation("User");
}); });
@@ -296,14 +268,14 @@ namespace MareSynchronosServer.Migrations
.HasForeignKey("OtherUserUID") .HasForeignKey("OtherUserUID")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired() .IsRequired()
.HasConstraintName("fk_client_pairs_users_other_user_temp_id2"); .HasConstraintName("fk_client_pairs_users_other_user_temp_id1");
b.HasOne("MareSynchronosShared.Models.User", "User") b.HasOne("MareSynchronosShared.Models.User", "User")
.WithMany() .WithMany()
.HasForeignKey("UserUID") .HasForeignKey("UserUID")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired() .IsRequired()
.HasConstraintName("fk_client_pairs_users_user_temp_id3"); .HasConstraintName("fk_client_pairs_users_user_temp_id2");
b.Navigation("OtherUser"); b.Navigation("OtherUser");

View File

@@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MareSynchronosServer.Migrations
{
public partial class AddAlias : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "alias",
table: "users",
type: "character varying(10)",
maxLength: 10,
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "alias",
table: "users");
}
}
}

View File

@@ -22,29 +22,6 @@ namespace MareSynchronosServer.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("MareSynchronosShared.Models.Alias", b =>
{
b.Property<string>("AliasUID")
.HasMaxLength(10)
.HasColumnType("character varying(10)")
.HasColumnName("alias_uid");
b.Property<string>("UserUID")
.HasColumnType("character varying(10)")
.HasColumnName("user_uid");
b.HasKey("AliasUID")
.HasName("pk_aliases");
b.HasIndex("AliasUID")
.HasDatabaseName("ix_aliases_alias_uid");
b.HasIndex("UserUID")
.HasDatabaseName("ix_aliases_user_uid");
b.ToTable("aliases", (string)null);
});
modelBuilder.Entity("MareSynchronosShared.Models.Auth", b => modelBuilder.Entity("MareSynchronosShared.Models.Auth", b =>
{ {
b.Property<string>("HashedKey") b.Property<string>("HashedKey")
@@ -235,6 +212,11 @@ namespace MareSynchronosServer.Migrations
.HasColumnType("character varying(10)") .HasColumnType("character varying(10)")
.HasColumnName("uid"); .HasColumnName("uid");
b.Property<string>("Alias")
.HasMaxLength(100)
.HasColumnType("character varying(100)")
.HasColumnName("alias");
b.Property<string>("CharacterIdentification") b.Property<string>("CharacterIdentification")
.HasMaxLength(100) .HasMaxLength(100)
.HasColumnType("character varying(100)") .HasColumnType("character varying(100)")
@@ -267,22 +249,12 @@ namespace MareSynchronosServer.Migrations
b.ToTable("users", (string)null); b.ToTable("users", (string)null);
}); });
modelBuilder.Entity("MareSynchronosShared.Models.Alias", b =>
{
b.HasOne("MareSynchronosShared.Models.User", "User")
.WithMany()
.HasForeignKey("UserUID")
.HasConstraintName("fk_aliases_users_user_temp_id");
b.Navigation("User");
});
modelBuilder.Entity("MareSynchronosShared.Models.Auth", b => modelBuilder.Entity("MareSynchronosShared.Models.Auth", b =>
{ {
b.HasOne("MareSynchronosShared.Models.User", "User") b.HasOne("MareSynchronosShared.Models.User", "User")
.WithMany() .WithMany()
.HasForeignKey("UserUID") .HasForeignKey("UserUID")
.HasConstraintName("fk_auth_users_user_temp_id1"); .HasConstraintName("fk_auth_users_user_temp_id");
b.Navigation("User"); b.Navigation("User");
}); });
@@ -294,14 +266,14 @@ namespace MareSynchronosServer.Migrations
.HasForeignKey("OtherUserUID") .HasForeignKey("OtherUserUID")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired() .IsRequired()
.HasConstraintName("fk_client_pairs_users_other_user_temp_id2"); .HasConstraintName("fk_client_pairs_users_other_user_temp_id1");
b.HasOne("MareSynchronosShared.Models.User", "User") b.HasOne("MareSynchronosShared.Models.User", "User")
.WithMany() .WithMany()
.HasForeignKey("UserUID") .HasForeignKey("UserUID")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired() .IsRequired()
.HasConstraintName("fk_client_pairs_users_user_temp_id3"); .HasConstraintName("fk_client_pairs_users_user_temp_id2");
b.Navigation("OtherUser"); b.Navigation("OtherUser");

View File

@@ -1,13 +0,0 @@
using System.ComponentModel.DataAnnotations;
namespace MareSynchronosShared.Models;
public class Alias
{
[Key]
[MaxLength(10)]
[Required]
public string AliasUID { get; set; }
public User User { get; set; }
public string UserUID { get; set; }
}

View File

@@ -17,5 +17,7 @@ namespace MareSynchronosShared.Models
public bool IsAdmin { get; set; } = false; public bool IsAdmin { get; set; } = false;
public DateTime LastLoggedIn { get; set; } public DateTime LastLoggedIn { get; set; }
[MaxLength(10)]
public string Alias { get; set; }
} }
} }