Holy crap auth works (#2099)

* Holy crap auth works

* Fix some usages of UserID instead of UserName

* Refactor preferences.

They be non-async now. Also faster.

* Rename DbContext.

* Guest username assignment.

* Fix saving of profiles.

* Don't store data for guests.

* Fix generating invalid random colors.

* Don't allow dumb garbage for char preferences.

* Bans.

* Lol forgot to fill out the command description.

* Connection log.

* Rename all the tables and columns to be snake_case.

* Re-do migrations.

* Fixing tests and warnings.

* Update submodule
This commit is contained in:
Pieter-Jan Briers
2020-09-29 14:26:00 +02:00
committed by GitHub
parent 8a33e0a9bd
commit 66c8a68891
72 changed files with 4144 additions and 2642 deletions

View File

@@ -1,42 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Content.Server.Database
{
public class PostgresPreferencesDbContext : PreferencesDbContext
{
// This is used by the "dotnet ef" CLI tool.
public PostgresPreferencesDbContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if(!InitializedWithOptions)
options.UseNpgsql("dummy connection string");
}
public PostgresPreferencesDbContext(DbContextOptions<PreferencesDbContext> options) : base(options)
{
}
}
public class SqlitePreferencesDbContext : PreferencesDbContext
{
public SqlitePreferencesDbContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!InitializedWithOptions)
options.UseSqlite("dummy connection string");
}
public SqlitePreferencesDbContext(DbContextOptions<PreferencesDbContext> options) : base(options)
{
}
}
public abstract class PreferencesDbContext : DbContext
public abstract class ServerDbContext : DbContext
{
/// <summary>
/// The "dotnet ef" CLI tool uses the parameter-less constructor.
@@ -44,70 +13,86 @@ namespace Content.Server.Database
/// To use the context within the application, the options need to be passed the constructor instead.
/// </summary>
protected readonly bool InitializedWithOptions;
public PreferencesDbContext()
public ServerDbContext()
{
}
public PreferencesDbContext(DbContextOptions<PreferencesDbContext> options) : base(options)
public ServerDbContext(DbContextOptions<ServerDbContext> options) : base(options)
{
InitializedWithOptions = true;
}
public DbSet<Prefs> Preferences { get; set; } = null!;
public DbSet<HumanoidProfile> HumanoidProfile { get; set; } = null!;
public DbSet<Preference> Preference { get; set; } = null!;
public DbSet<Profile> Profile { get; set; } = null!;
public DbSet<AssignedUserId> AssignedUserId { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Prefs>()
.HasIndex(p => p.Username)
modelBuilder.Entity<Preference>()
.HasIndex(p => p.UserId)
.IsUnique();
modelBuilder.Entity<HumanoidProfile>()
.HasIndex(p => new {p.Slot, p.PrefsId})
modelBuilder.Entity<Profile>()
.HasIndex(p => new {p.Slot, PrefsId = p.PreferenceId})
.IsUnique();
modelBuilder.Entity<Antag>()
.HasIndex(p => new {p.HumanoidProfileId , p.AntagName})
.HasIndex(p => new {HumanoidProfileId = p.ProfileId, p.AntagName})
.IsUnique();
modelBuilder.Entity<AssignedUserId>()
.HasIndex(p => p.UserName)
.IsUnique();
// Can't have two usernames with the same user ID.
modelBuilder.Entity<AssignedUserId>()
.HasIndex(p => p.UserId)
.IsUnique();
}
}
public class Prefs
[Table("preference")]
public class Preference
{
public int PrefsId { get; set; }
public string Username { get; set; } = null!;
public int SelectedCharacterSlot { get; set; }
public List<HumanoidProfile> HumanoidProfiles { get; } = new List<HumanoidProfile>();
[Column("preference_id")] public int Id { get; set; }
[Column("user_id")] public Guid UserId { get; set; }
[Column("selected_character_slot")] public int SelectedCharacterSlot { get; set; }
public List<Profile> Profiles { get; } = new List<Profile>();
}
public class HumanoidProfile
[Table("profile")]
public class Profile
{
public int HumanoidProfileId { get; set; }
public int Slot { get; set; }
public string SlotName { get; set; } = null!;
public string CharacterName { get; set; } = null!;
public int Age { get; set; }
public string Sex { get; set; } = null!;
public string HairName { get; set; } = null!;
public string HairColor { get; set; } = null!;
public string FacialHairName { get; set; } = null!;
public string FacialHairColor { get; set; } = null!;
public string EyeColor { get; set; } = null!;
public string SkinColor { get; set; } = null!;
[Column("profile_id")] public int Id { get; set; }
[Column("slot")] public int Slot { get; set; }
[Column("char_name")] public string CharacterName { get; set; } = null!;
[Column("age")] public int Age { get; set; }
[Column("sex")] public string Sex { get; set; } = null!;
[Column("hair_name")] public string HairName { get; set; } = null!;
[Column("hair_color")] public string HairColor { get; set; } = null!;
[Column("facial_hair_name")] public string FacialHairName { get; set; } = null!;
[Column("facial_hair_color")] public string FacialHairColor { get; set; } = null!;
[Column("eye_color")] public string EyeColor { get; set; } = null!;
[Column("skin_color")] public string SkinColor { get; set; } = null!;
public List<Job> Jobs { get; } = new List<Job>();
public List<Antag> Antags { get; } = new List<Antag>();
public DbPreferenceUnavailableMode PreferenceUnavailable { get; set; }
public int PrefsId { get; set; }
public Prefs Prefs { get; set; } = null!;
[Column("pref_unavailable")] public DbPreferenceUnavailableMode PreferenceUnavailable { get; set; }
[Column("preference_id")] public int PreferenceId { get; set; }
public Preference Preference { get; set; } = null!;
}
[Table("job")]
public class Job
{
public int JobId { get; set; }
public HumanoidProfile Profile { get; set; } = null!;
[Column("job_id")] public int Id { get; set; }
public Profile Profile { get; set; } = null!;
[Column("profile_id")] public int ProfileId { get; set; }
public string JobName { get; set; } = null!;
public DbJobPriority Priority { get; set; }
[Column("job_name")] public string JobName { get; set; } = null!;
[Column("priority")] public DbJobPriority Priority { get; set; }
}
public enum DbJobPriority
@@ -119,13 +104,14 @@ namespace Content.Server.Database
High = 3
}
[Table("antag")]
public class Antag
{
public int AntagId { get; set; }
public HumanoidProfile Profile { get; set; } = null!;
public int HumanoidProfileId { get; set; }
[Column("antag_id")] public int Id { get; set; }
public Profile Profile { get; set; } = null!;
[Column("profile_id")] public int ProfileId { get; set; }
public string AntagName { get; set; } = null!;
[Column("antag_name")] public string AntagName { get; set; } = null!;
}
public enum DbPreferenceUnavailableMode
@@ -134,4 +120,13 @@ namespace Content.Server.Database
StayInLobby = 0,
SpawnAsOverflow,
}
[Table("assigned_user_id")]
public class AssignedUserId
{
[Column("assigned_user_id_id")] public int Id { get; set; }
[Column("user_name")] public string UserName { get; set; } = null!;
[Column("user_id")] public Guid UserId { get; set; }
}
}