4using System.Threading.Tasks;
6using Microsoft.EntityFrameworkCore;
7using Microsoft.Extensions.Logging;
8using Microsoft.Extensions.Options;
17using Z.EntityFramework.Plus;
42 readonly ILogger<DatabaseSeeder>
logger;
62 bool alreadyExists = tgsUser !=
null;
63 tgsUser ??=
new User()
65 CreatedAt = DateTimeOffset.UtcNow,
71 tgsUser.PasswordHash =
"_";
72 tgsUser.Enabled =
false;
91 IOptions<GeneralConfiguration> generalConfigurationOptions,
92 IOptions<DatabaseConfiguration> databaseConfigurationOptions,
94 ILogger<DatabaseSeeder>
logger)
98 databaseConfiguration = databaseConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(databaseConfigurationOptions));
99 generalConfiguration = generalConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(generalConfigurationOptions));
101 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
107 ArgumentNullException.ThrowIfNull(databaseContext);
111 logger.LogCritical(
"DropDatabase configuration option set! Dropping any existing database...");
112 await databaseContext.
Drop(cancellationToken);
118 logger.LogInformation(
"Seeding database...");
125 logger.LogWarning(
"Enabling and resetting admin password due to configuration!");
136 ArgumentNullException.ThrowIfNull(databaseContext);
137 ArgumentNullException.ThrowIfNull(downgradeVersion);
160 CreatedAt = DateTimeOffset.UtcNow,
182 await databaseContext.
Save(cancellationToken);
184 adminUser.CreatedBy = tgsUser;
186 await databaseContext.
Save(cancellationToken);
197 var admin = await
GetAdminUser(databaseContext, cancellationToken);
200 if (admin.PermissionSet !=
null)
209 if (admin.CreatedBy ==
null)
211 var tgsUser = await databaseContext
215 .FirstOrDefaultAsync(cancellationToken);
219 "A user named TGS (Canonically) exists but isn't marked as the admin's creator. This may be because it was created manually. This user is going to be adapted to use as the starter of system jobs.");
222 admin.CreatedBy = tgsUser;
229 var allInstances = await databaseContext
232 .ToListAsync(cancellationToken);
233 foreach (var instance
in allInstances)
234 instance.Path = instance.Path!.Replace(
'\\',
'/');
239 var ids = await databaseContext
242 .Where(x => x.TopicRequestTimeout == 0)
244 .ToListAsync(cancellationToken);
246 var rowsUpdated = ids.Count;
247 foreach (var
id in ids)
260 "Updated {count} instances to use database backed BYOND topic timeouts from configuration setting of {timeout}",
265 await databaseContext.
Save(cancellationToken);
276 var admin = await
GetAdminUser(databaseContext, cancellationToken);
279 admin.Enabled =
true;
282 if (admin.PermissionSet ==
null)
285 admin.GroupId =
null;
297 await databaseContext.
Save(cancellationToken);
308 var admin = await databaseContext
312 .Include(x => x.CreatedBy)
313 .Include(x => x.PermissionSet)
314 .Include(x => x.Group)
315 .FirstOrDefaultAsync(cancellationToken);
316 if (admin ==
default)
Represents initial credentials used by the server.
static readonly string DefaultAdminUserPassword
The default admin password.
static readonly string AdminUserName
The name of the default admin user.
Configuration options for the Database.DatabaseContext.
bool DropDatabase
If the database should be deleted on application startup. Should not be used in production!...
bool ResetAdminPassword
If the admin user should be enabled and have it's password reset.
DatabaseType DatabaseType
The Configuration.DatabaseType to create.
General configuration options.
uint ByondTopicTimeout
The timeout in milliseconds for sending and receiving topics to/from DreamDaemon. Note that a single ...
readonly ILogger< DatabaseContext > databaseLogger
The ILogger used for IDatabaseContexts.
async ValueTask< User?> GetAdminUser(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Get or create the admin User.
async ValueTask ResetAdminPassword(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Changes the admin password in IDatabaseContext back to it's default, enables the account,...
readonly GeneralConfiguration generalConfiguration
The GeneralConfiguration for the DatabaseSeeder.
User SeedAdminUser(IDatabaseContext databaseContext)
Add a default admin User to a given databaseContext .
async ValueTask SeedDatabase(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Initially seed a given databaseContext .
readonly DatabaseConfiguration databaseConfiguration
The DatabaseConfiguration for the DatabaseSeeder.
readonly ICryptographySuite cryptographySuite
The ICryptographySuite for the DatabaseSeeder.
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the DatabaseSeeder.
ValueTask Downgrade(IDatabaseContext databaseContext, Version downgradeVersion, CancellationToken cancellationToken)
Migrate a given databaseContext down. A ValueTask representing the running operation.
DatabaseSeeder(ICryptographySuite cryptographySuite, IPlatformIdentifier platformIdentifier, IOptions< GeneralConfiguration > generalConfigurationOptions, IOptions< DatabaseConfiguration > databaseConfigurationOptions, ILogger< DatabaseContext > databaseLogger, ILogger< DatabaseSeeder > logger)
Initializes a new instance of the DatabaseSeeder class.
readonly ILogger< DatabaseSeeder > logger
The ILogger for the DatabaseSeeder.
async ValueTask SanitizeDatabase(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Correct invalid database data caused by previous versions (NOT user fuckery).
async ValueTask Initialize(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Setup up a given databaseContext . A ValueTask representing the running operation.
static User SeedSystemUser(IDatabaseContext databaseContext, User? tgsUser=null)
Add a default system User to a given databaseContext .
const string TgsSystemUserName
Username used when creating jobs automatically.
static string CanonicalizeName(string name)
Change a UserName.Name into a CanonicalName.
void Attach(TModel model)
Attach a given model to the the working set.
void Add(TModel model)
Add a given model to the the working set.
ValueTask< bool > Migrate(ILogger< DatabaseContext > logger, CancellationToken cancellationToken)
Creates and migrates the IDatabaseContext.
ValueTask SchemaDowngradeForServerVersion(ILogger< DatabaseContext > logger, Version targetVersion, DatabaseType currentDatabaseType, CancellationToken cancellationToken)
Attempt to downgrade the schema to the migration used for a given server targetVersion .
Task Drop(CancellationToken cancellationToken)
Attempts to delete all tables and drop the database in use.
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext.
IDatabaseCollection< DreamDaemonSettings > DreamDaemonSettings
The Models.DreamDaemonSettings in the IDatabaseContext.
IDatabaseCollection< User > Users
The Users in the IDatabaseContext.
IDatabaseCollection< Instance > Instances
The Instances in the IDatabaseContext.
For initially setting up a database.
Contains various cryptographic functions.
void SetUserPassword(User user, string newPassword, bool newUser)
Sets a User.PasswordHash for a given user .
InstanceManagerRights
Rights for managing Models.Instances.
AdministrationRights
Administration rights for the server.