Skip to content

Commit

Permalink
PT-12574: move default user creation to migration (#2671)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Jul 18, 2023
1 parent ddeca43 commit cebdd92
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

Expand Down Expand Up @@ -513,6 +512,53 @@ protected override void Up(MigrationBuilder migrationBuilder)
table: "UserApiKey",
column: "ApiKey",
unique: true);

migrationBuilder.Sql(@"INSERT INTO AspNetUsers (
Id,
IsAdministrator,
UserName,
NormalizedUserName,
PasswordHash,
SecurityStamp,
ConcurrencyStamp,
PasswordExpired,
Email,
NormalizedEmail,
EmailConfirmed,
PhoneNumberConfirmed,
TwoFactorEnabled,
LockoutEnabled,
AccessFailedCount,
CreatedDate
)
SELECT
'1eb2fa8ac6574541afdb525833dadb46',
1,
'admin',
'ADMIN',
'AHQSmKnSLYrzj9vtdDWWnUXojjpmuDW2cHvWloGL9UL3TC9UCfBmbIuR2YCyg4BpNg==',
'IK5VZXH6VRIZ3E6OKHEIS7TC24VT4RD3',
'47548002-dd64-48e0-bb51-e9516fd880c2',
1,
'admin@vc-demostore.com',
'ADMIN@VC-DEMOSTORE.COM',
1,
1,
0,
1,
0,
UTC_TIMESTAMP()
FROM
dual
WHERE
NOT EXISTS (
SELECT
*
FROM
AspNetUsers
WHERE
UserName = 'admin'
);");
}

protected override void Down(MigrationBuilder migrationBuilder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

Expand Down Expand Up @@ -404,6 +403,51 @@ protected override void Up(MigrationBuilder migrationBuilder)
table: "UserApiKey",
column: "ApiKey",
unique: true);

migrationBuilder.Sql(@"INSERT INTO AspNetUsers (
Id,
IsAdministrator,
UserName,
NormalizedUserName,
PasswordHash,
SecurityStamp,
ConcurrencyStamp,
PasswordExpired,
Email,
NormalizedEmail,
EmailConfirmed,
PhoneNumberConfirmed,
TwoFactorEnabled,
LockoutEnabled,
AccessFailedCount,
CreatedDate
)
SELECT
'1eb2fa8ac6574541afdb525833dadb46',
1,
'admin',
'ADMIN',
'AHQSmKnSLYrzj9vtdDWWnUXojjpmuDW2cHvWloGL9UL3TC9UCfBmbIuR2YCyg4BpNg==',
'IK5VZXH6VRIZ3E6OKHEIS7TC24VT4RD3',
'47548002-dd64-48e0-bb51-e9516fd880c2',
1,
'admin@vc-demostore.com',
'ADMIN@VC-DEMOSTORE.COM',
1,
1,
0,
1,
0,
Now()
WHERE
NOT EXISTS (
SELECT
*
FROM
AspNetUsers
WHERE
UserName = 'admin'
);");
}

protected override void Down(MigrationBuilder migrationBuilder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

Expand Down Expand Up @@ -328,6 +327,40 @@ protected override void Up(MigrationBuilder migrationBuilder)
name: "IX_OpenIddictTokens_ApplicationId_Status_Subject_Type",
table: "OpenIddictTokens",
columns: new[] { "ApplicationId", "Status", "Subject", "Type" });

migrationBuilder.Sql(@"IF ((SELECT COUNT(*) FROM AspNetUsers WHERE UserName = 'admin') = 0)
INSERT [AspNetUsers] (
[Id],
[IsAdministrator],
[UserName],
[NormalizedUserName],
[PasswordHash],
[SecurityStamp],
[ConcurrencyStamp],
[PasswordExpired],
[Email],
[NormalizedEmail],
[EmailConfirmed],
[PhoneNumberConfirmed],
[TwoFactorEnabled],
[LockoutEnabled],
[AccessFailedCount])
VALUES (
'1eb2fa8ac6574541afdb525833dadb46',
1,
'admin',
'ADMIN',
'AHQSmKnSLYrzj9vtdDWWnUXojjpmuDW2cHvWloGL9UL3TC9UCfBmbIuR2YCyg4BpNg==',
'IK5VZXH6VRIZ3E6OKHEIS7TC24VT4RD3',
'47548002-dd64-48e0-bb51-e9516fd880c2',
1,
'admin@vc-demostore.com',
'ADMIN@VC-DEMOSTORE.COM',
1,
1,
0,
1,
0)");
}

protected override void Down(MigrationBuilder migrationBuilder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using Hangfire;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using VirtoCommerce.Platform.Core;
using VirtoCommerce.Platform.Core.Bus;
Expand All @@ -19,7 +17,6 @@ namespace VirtoCommerce.Platform.Web.Security
{
public static class ApplicationBuilderExtensions
{

public static IApplicationBuilder UsePlatformPermissions(this IApplicationBuilder appBuilder)
{
//Register PermissionScope type itself to prevent Json serialization error due that fact that will be taken from other derived from PermissionScope type (first in registered types list) in PolymorphJsonContractResolver
Expand Down Expand Up @@ -85,37 +82,5 @@ public static IApplicationBuilder UseAutoAccountsLockoutJob(this IApplicationBui

return appBuilder;
}

public static async Task<IApplicationBuilder> UseDefaultUsersAsync(this IApplicationBuilder appBuilder)
{
using (var scope = appBuilder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();


if (await userManager.FindByNameAsync("admin") == null)
{
#pragma warning disable S2068 // disable check: 'password' detected in this expression, review this potentially hardcoded credential
var admin = new ApplicationUser
{
Id = "1eb2fa8ac6574541afdb525833dadb46",
IsAdministrator = true,
UserName = "admin",
PasswordHash = "AHQSmKnSLYrzj9vtdDWWnUXojjpmuDW2cHvWloGL9UL3TC9UCfBmbIuR2YCyg4BpNg==",
PasswordExpired = true,
Email = "admin@vc-demostore.com"
};
#pragma warning restore S2068 // disable check: 'password' detected in this expression, review this potentially hardcoded credential
var adminUser = await userManager.FindByIdAsync(admin.Id);
if (adminUser == null)
{
await userManager.CreateAsync(admin);
}
}

}
return appBuilder;
}
}

}
3 changes: 0 additions & 3 deletions src/VirtoCommerce.Platform.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<

app.UseEndpoints(SetupEndpoints);

//Seed default users
app.UseDefaultUsersAsync().GetAwaiter().GetResult();

// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();

Expand Down

0 comments on commit cebdd92

Please sign in to comment.