tgstation-server
The /tg/station 13 server suite
DatabaseSeeder.cs
Go to the documentation of this file.
1 using Microsoft.EntityFrameworkCore;
2 using System;
3 using System.Linq;
4 using System.Threading;
5 using System.Threading.Tasks;
8 
9 namespace Tgstation.Server.Host.Models
10 {
13  {
18 
23  public DatabaseSeeder(ICryptographySuite cryptographySuite) => this.cryptographySuite = cryptographySuite ?? throw new ArgumentNullException(nameof(cryptographySuite));
24 
29  void SeedAdminUser(IDatabaseContext databaseContext)
30  {
31  var admin = new User
32  {
34  CreatedAt = DateTimeOffset.Now,
36  Name = Api.Models.User.AdminName,
37  CanonicalName = Api.Models.User.AdminName.ToUpperInvariant(),
38  Enabled = true,
39  };
40  cryptographySuite.SetUserPassword(admin, Api.Models.User.DefaultAdminPassword, true);
41  databaseContext.Users.Add(admin);
42  }
43 
45  public async Task SeedDatabase(IDatabaseContext databaseContext, CancellationToken cancellationToken)
46  {
47  SeedAdminUser(databaseContext);
48  await databaseContext.Save(cancellationToken).ConfigureAwait(false);
49  }
50 
52  public async Task ResetAdminPassword(IDatabaseContext databaseContext, CancellationToken cancellationToken)
53  {
54  var admin = await databaseContext.Users.Where(x => x.CanonicalName == Api.Models.User.AdminName.ToUpperInvariant()).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
55  if (admin == default)
56  SeedAdminUser(databaseContext);
57  else
58  {
59  admin.Enabled = true;
60  cryptographySuite.SetUserPassword(admin, Api.Models.User.DefaultAdminPassword, false);
61  }
62 
63  await databaseContext.Save(cancellationToken).ConfigureAwait(false);
64  }
65  }
66 }
async Task ResetAdminPassword(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Changes the admin password in IDatabaseContext back to it's default and enables the account ...
readonly ICryptographySuite cryptographySuite
The ICryptographySuite for the DatabaseContext<TParentContext>
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext
For initially seeding a database
DbSet< User > Users
The Users in the IDatabaseContext
AdministrationRights
Rights for Models.Administration
async Task SeedDatabase(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Initially seed a given databaseContext
Contains various cryptographic functions
InstanceManagerRights
Rights for managing Models.Instances
void SeedAdminUser(IDatabaseContext databaseContext)
Add a default admin User to a given databaseContext