From d8d919f0efdd7d13046267cd4d2f2c4d60c64413 Mon Sep 17 00:00:00 2001 From: Daniele Iasella <2861984+overbit@users.noreply.github.com> Date: Thu, 13 Jun 2024 15:34:06 +0100 Subject: [PATCH] feat(dotnet-auth): add seed dev user existence before creation and new plugin settings for email and password --- .../.amplicationrc.json | 5 +++- .../src/core/create-seed-development-data.ts | 24 +++++++++++++++---- .../dotnet-auth-core-identity/src/index.ts | 2 +- .../dotnet-auth-core-identity/src/types.ts | 4 ++++ .../dotnet-auth-core-identity/src/utils.ts | 21 ++++++++++++++++ 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 plugins/dotnet-auth-core-identity/src/types.ts create mode 100644 plugins/dotnet-auth-core-identity/src/utils.ts diff --git a/plugins/dotnet-auth-core-identity/.amplicationrc.json b/plugins/dotnet-auth-core-identity/.amplicationrc.json index d920fafd..73e15e87 100644 --- a/plugins/dotnet-auth-core-identity/.amplicationrc.json +++ b/plugins/dotnet-auth-core-identity/.amplicationrc.json @@ -1,3 +1,6 @@ { - "settings": {} + "settings": { + "seedUserEmail": "test@email.com", + "seedUserPassword": "P@ssw0rd!" + } } diff --git a/plugins/dotnet-auth-core-identity/src/core/create-seed-development-data.ts b/plugins/dotnet-auth-core-identity/src/core/create-seed-development-data.ts index 9e81c5f6..a1107c84 100644 --- a/plugins/dotnet-auth-core-identity/src/core/create-seed-development-data.ts +++ b/plugins/dotnet-auth-core-identity/src/core/create-seed-development-data.ts @@ -1,6 +1,15 @@ +import { dotnetTypes } from "@amplication/code-gen-types"; import { CodeBlock, CsharpSupport } from "@amplication/csharp-ast"; +import { getPluginSettings } from "../utils"; + +export function CreateSeedDevelopmentDataBody( + resourceName: string, + context: dotnetTypes.DsgContext +): CodeBlock { + const { seedUserEmail, seedUserPassword } = getPluginSettings( + context.pluginInstallations + ); -export function CreateSeedDevelopmentDataBody(resourceName: string): CodeBlock { return new CodeBlock({ references: [ CsharpSupport.classReference({ @@ -24,11 +33,18 @@ export function CreateSeedDevelopmentDataBody(resourceName: string): CodeBlock { .Select(x => x.Value.ToString()) .ToArray(); - var user = new IdentityUser { Email = "test@email.com", UserName = "admin" }; - + var usernameValue = "${seedUserEmail}"; + var passwordValue = "${seedUserPassword}"; + var user = new IdentityUser + { + Email = usernameValue, + UserName = usernameValue, + NormalizedUserName = usernameValue.ToUpperInvariant(), + NormalizedEmail = usernameValue.ToUpperInvariant(), + }; var password = new PasswordHasher(); - var hashed = password.HashPassword(user, "P@ssw0rd!"); + var hashed = password.HashPassword(user, passwordValue); user.PasswordHash = hashed; var userStore = new UserStore(context); await userStore.CreateAsync(user); diff --git a/plugins/dotnet-auth-core-identity/src/index.ts b/plugins/dotnet-auth-core-identity/src/index.ts index 916b5cdf..3416c2f8 100644 --- a/plugins/dotnet-auth-core-identity/src/index.ts +++ b/plugins/dotnet-auth-core-identity/src/index.ts @@ -365,7 +365,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { name: "SeedDevUser", access: "public", isAsync: true, - body: CreateSeedDevelopmentDataBody(resourceName), + body: CreateSeedDevelopmentDataBody(resourceName, context), type: MethodType.STATIC, parameters: [ CsharpSupport.parameter({ diff --git a/plugins/dotnet-auth-core-identity/src/types.ts b/plugins/dotnet-auth-core-identity/src/types.ts new file mode 100644 index 00000000..9f465a1d --- /dev/null +++ b/plugins/dotnet-auth-core-identity/src/types.ts @@ -0,0 +1,4 @@ +export interface Settings { + seedUserEmail: string; + seedUserPassword: string; +} diff --git a/plugins/dotnet-auth-core-identity/src/utils.ts b/plugins/dotnet-auth-core-identity/src/utils.ts new file mode 100644 index 00000000..a94bfe69 --- /dev/null +++ b/plugins/dotnet-auth-core-identity/src/utils.ts @@ -0,0 +1,21 @@ +import { PluginInstallation } from "@amplication/code-gen-types"; +import { name as PackageName } from "../package.json"; +import { Settings } from "./types"; +import defaultSettings from "../.amplicationrc.json"; + +export const getPluginSettings = ( + pluginInstallations: PluginInstallation[] +): Settings => { + const plugin = pluginInstallations.find( + (plugin) => plugin.npm === PackageName + ); + + const userSettings = plugin?.settings ?? {}; + + const settings: Settings = { + ...defaultSettings.settings, + ...userSettings, + }; + + return settings; +};