Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User accounts #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 24 additions & 6 deletions DatabaseExporter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,28 @@
var tags = JsonSerializer.Serialize<IEnumerable<string>>(db.Tags.Select(t => t.Name).ToList().Order(), jsonOptions);
var licences = JsonSerializer.Serialize<IEnumerable<LicenceJsonRecord>>(db.Licences.Select(l => new LicenceJsonRecord(l.Name, l.Text)).ToList().OrderBy(l => l.Name), jsonOptions);
var modpacks = JsonSerializer.Serialize<IEnumerable<ModpackJsonRecord>>(db.Modpacks.Select(m => new ModpackJsonRecord(m.Name, m.Author)).ToList().OrderBy(m => m.Name), jsonOptions);
var roles = JsonSerializer.Serialize<IEnumerable<string>>(db.Roles.Select(r => r.Name).ToList().Order(), jsonOptions);

var objs = new List<ObjectMetadata>();
var usrs = new List<UserJsonRecord>();
foreach (var u in db.Users
.Include(x => x.Author)
.ToList()
.OrderBy(x => x.Name))
{
var usr = new UserJsonRecord(
u.Name,
u.DisplayName,
u.Author?.Name,
u.Roles.Select(r => r.Name).Order().ToList(),
u.PasswordHashed,
u.PasswordSalt);
usrs.Add(usr);
}
var users = JsonSerializer.Serialize<IEnumerable<UserJsonRecord>>(usrs, jsonOptions);

var objs = new List<ObjectMetadata>();
foreach (var o in db.Objects
.Include(l => l.Licence)
.Include(x => x.Licence)
.Select(x => new ExpandedTblLocoObject(x, x.Authors, x.Tags, x.Modpacks))
.ToList()
.OrderBy(x => x.Object.Name))
Expand All @@ -30,13 +47,12 @@
o.Object.OriginalName,
o.Object.OriginalChecksum,
o.Object.Description,
o.Authors.Select(a => a.Name).ToList(),
o.Tags.Select(t => t.Name).ToList(),
o.Modpacks.Select(m => m.Name).ToList(),
o.Authors.Select(a => a.Name).Order().ToList(),
o.Tags.Select(t => t.Name).Order().ToList(),
o.Modpacks.Select(m => m.Name).Order().ToList(),
o.Object.Licence?.Name);
objs.Add(obj);
}

var objects = JsonSerializer.Serialize<IEnumerable<ObjectMetadata>>(objs, jsonOptions);

Console.WriteLine("writing");
Expand All @@ -46,5 +62,7 @@
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\licences.json", licences);
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\modpacks.json", modpacks);
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\objects.json", objects);
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\users.json", users);
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\roles.json", roles);

Console.WriteLine("done");
38 changes: 37 additions & 1 deletion DatabaseSeeder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ static void SeedDb(LocoDb db, bool deleteExisting)
if (deleteExisting)
{
Console.WriteLine("Clearing database");
_ = db.Users.ExecuteDelete();
_ = db.Roles.ExecuteDelete();
_ = db.Objects.ExecuteDelete();
_ = db.Tags.ExecuteDelete();
_ = db.Modpacks.ExecuteDelete();
Expand All @@ -56,14 +58,48 @@ static void SeedDb(LocoDb db, bool deleteExisting)

// ...

if (!db.Roles.Any())
{
Console.WriteLine("Seeding Roles");

var roles = JsonSerializer.Deserialize<IEnumerable<string>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\roles.json"), jsonOptions);
if (roles != null)
{
db.AddRange(roles.Select(x => new TblRole() { Name = x }));
_ = db.SaveChanges();
}
}

// ...

Console.WriteLine("Seeding Users");

var users = JsonSerializer.Deserialize<IEnumerable<UserJsonRecord>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\users.json"), jsonOptions);
if (users != null)
{
db.AddRange(users.Select(x => new TblUser()
{
Name = x.Name,
DisplayName = x.DisplayName,
Author = db.Authors.SingleOrDefault(a => a.Name == x.Author),
Roles = [.. db.Roles.Where(r => x.Roles.Contains(r.Name))],
PasswordHashed = x.PasswordHashed,
PasswordSalt = x.PasswordSalt
}));
_ = db.SaveChanges();
}

// ...

if (!db.Authors.Any())
{

Console.WriteLine("Seeding Authors");

var authors = JsonSerializer.Deserialize<IEnumerable<string>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\authors.json"), jsonOptions);
if (authors != null)
{
db.AddRange(authors.Select(x => new TblAuthor() { Name = x }));
db.AddRange(authors.Select(x => new TblUser() { Name = x }));
_ = db.SaveChanges();
}
}
Expand Down
26 changes: 26 additions & 0 deletions Definitions/DTO/DtoCreateUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace OpenLoco.Definitions.DTO
{
public enum DataOperation
{
Create,
Read,
Update,
Delete,
}

public record DtoCreateUser(
string Name,
string DisplayName,
string? Author,
string Password);

public record DtoChangeUserRole(
string UserName,
string RoleName,
DataOperation Op);

public record DtoChangeUserAuthor(
string UserName,
string AuthorName,
DataOperation Op);
}
17 changes: 13 additions & 4 deletions Definitions/Database/LocoDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace OpenLoco.Definitions.Database
{
public class LocoDb : DbContext
{
public DbSet<TblLocoObject> Objects => Set<TblLocoObject>();
public DbSet<TblAuthor> Authors => Set<TblAuthor>();
public DbSet<TblTag> Tags => Set<TblTag>();
public DbSet<TblModpack> Modpacks => Set<TblModpack>();
public DbSet<TblLocoObject> Objects => Set<TblLocoObject>();
public DbSet<TblLicence> Licences => Set<TblLicence>();
public DbSet<TblUser> Users => Set<TblUser>();
public DbSet<TblRole> Roles => Set<TblRole>();

public LocoDb()
{ }
Expand All @@ -25,9 +27,16 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder) => modelBuilder.Entity<TblLocoObject>()
.Property(b => b.UploadDate)
.HasDefaultValueSql("datetime(datetime('now', 'localtime'), 'utc')"); // this is necessary, it seems like a bug in sqlite
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TblLocoObject>()
.Property(x => x.UploadDate)
.HasDefaultValueSql("datetime(datetime('now', 'localtime'), 'utc')"); // this is necessary, it seems like a bug in sqlite

modelBuilder.Entity<TblUser>()
.Property(x => x.CreatedDate)
.HasDefaultValueSql("datetime(datetime('now', 'localtime'), 'utc')"); // this is necessary, it seems like a bug in sqlite
}

public bool DoesObjectExist(S5Header s5Header, out TblLocoObject? existingObject)
=> DoesObjectExist(s5Header.Name, s5Header.Checksum, out existingObject);
Expand Down
14 changes: 14 additions & 0 deletions Definitions/Database/TblRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;

namespace OpenLoco.Definitions.Database
{
[Index(nameof(Name), IsUnique = true)]
public class TblRole
{
public int Id { get; set; }

public string Name { get; set; }

public ICollection<TblUser> Users { get; set; }
}
}
24 changes: 24 additions & 0 deletions Definitions/Database/TblUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;

namespace OpenLoco.Definitions.Database
{
[Index(nameof(Name), IsUnique = true)]
[Index(nameof(DisplayName), IsUnique = true)]
[Index(nameof(PasswordSalt), IsUnique = true)]
public class TblUser
{
public int Id { get; set; }
public string Name { get; set; }

public string DisplayName { get; set; }

public TblAuthor? Author { get; set; }

public ICollection<TblRole> Roles { get; set; }

public string PasswordHashed { get; set; }
public string PasswordSalt { get; set; }

public DateTimeOffset CreatedDate { get; set; }
}
}
Loading