From 8c9a115dc1abd611c53d60a803d2df745dbf28c9 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Mon, 10 Jun 2024 11:13:13 +0300 Subject: [PATCH 01/16] fix(dotnet-auth): add missing references --- .../dotnet-auth-core-identity/src/index.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/plugins/dotnet-auth-core-identity/src/index.ts b/plugins/dotnet-auth-core-identity/src/index.ts index 029ac447..ea669efd 100644 --- a/plugins/dotnet-auth-core-identity/src/index.ts +++ b/plugins/dotnet-auth-core-identity/src/index.ts @@ -92,7 +92,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { files: FileMap ): Promise> { const { resourceInfo } = context; - if (!resourceInfo) return files; + if (!resourceInfo || !resourceInfo.settings.authEntityName) return files; const resourceName = pascalCase(resourceInfo.name); @@ -106,10 +106,17 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { destPath, filePath, context, - CsharpSupport.classReference({ - name: `${resourceName}`, - namespace: `${resourceName}.Infrastructure`, - }) + resourceInfo.settings.authEntityName, + [ + CsharpSupport.classReference({ + name: `${resourceName}`, + namespace: `${resourceName}.Infrastructure`, + }), + CsharpSupport.classReference({ + name: `${resourceName}`, + namespace: `${resourceName}.Infrastructure.Models`, + }), + ] ); const rolesManagerDestPath = `${eventParams.basePath}/src/Infrastructure/RolesManager.cs`; @@ -146,7 +153,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { modelFile.code.parentClassReference = CsharpSupport.classReference({ name: "IdentityUser", - namespace: "", + namespace: "Microsoft.AspNetCore.Identity", }); return files; @@ -173,7 +180,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { modelFile.code.parentClassReference = CsharpSupport.classReference({ name: `IdentityDbContext<${authEntityName}>`, - namespace: "", + namespace: "Microsoft.AspNetCore.Identity.EntityFrameworkCore", }); return files; @@ -191,7 +198,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { const roleNames = roles?.map((role) => role.name).join(","); const controllerBaseFile = files.get( - `${apisDir}/${entity.name}/base/${pascalPluralName}ControllerBase.cs` + `${apisDir}/${entity.name}/Base/${pascalPluralName}ControllerBase.cs` ); const methods = controllerBaseFile?.code.getMethods(); @@ -399,7 +406,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { type: CsharpSupport.Types.reference( CsharpSupport.classReference({ name: "IServiceProvider", - namespace: "", + namespace: `${resourceName}.Infrastructure.Models`, }) ), }), From 424d04bf124702b627cb417a75a54404e77a802a Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Mon, 10 Jun 2024 11:13:52 +0300 Subject: [PATCH 02/16] fix(dotnet-auth): add missing app service --- .../src/core/create-app-services.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/dotnet-auth-core-identity/src/core/create-app-services.ts b/plugins/dotnet-auth-core-identity/src/core/create-app-services.ts index 702f84c2..bb8afca6 100644 --- a/plugins/dotnet-auth-core-identity/src/core/create-app-services.ts +++ b/plugins/dotnet-auth-core-identity/src/core/create-app-services.ts @@ -1,6 +1,12 @@ import { CodeBlock } from "@amplication/csharp-ast"; export function createAppServices(builderServicesBlocks: CodeBlock[]): void { + builderServicesBlocks.push( + new CodeBlock({ + code: `app.UseApiAuthentication();`, + }) + ); + builderServicesBlocks.push( new CodeBlock({ code: `using (var scope = app.Services.CreateScope()) From fa1058e36ab89534fe220e71b034783353265422 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Mon, 10 Jun 2024 11:14:54 +0300 Subject: [PATCH 03/16] fix(dotnet-auth): add missing placeholders --- .../src/core/create-static-file-map.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts b/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts index 07df4f12..92bfd91e 100644 --- a/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts +++ b/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts @@ -7,7 +7,8 @@ export async function createStaticFileFileMap( destPath: string, filePath: string, context: dotnetTypes.DsgContext, - classReference?: ClassReference + authEntityName?: string, + classReferences?: ClassReference[] ): Promise> { const fileMap = new FileMap(context.logger); @@ -15,12 +16,19 @@ export async function createStaticFileFileMap( const resourceName = pascalCase(context.resourceInfo.name); let fileContent = await readFile(filePath, "utf-8"); fileContent = fileContent.replace("ServiceName", resourceName); + fileContent = fileContent.replace( + "ServiceNameDbContext", + `${resourceName}DbContext` + ); + + if (authEntityName) + fileContent = fileContent.replaceAll("authEntityName", authEntityName); const file: IFile = { path: destPath, code: new CodeBlock({ code: fileContent, - references: classReference && [classReference], + references: classReferences && classReferences, }), }; From 3b9f98dbbd6d046acc28de7238cf48de222c2986 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Mon, 10 Jun 2024 11:15:25 +0300 Subject: [PATCH 04/16] fix(dotnet-auth): add missing placeholders --- .../src/static/common/auth/ProgramAuthExtensions.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs b/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs index ed600f00..cec743ca 100644 --- a/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs +++ b/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs @@ -1,9 +1,9 @@ -using GraphQL; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Filters; + namespace ServiceName.APIs; public static class ProgramAuthExtensions @@ -12,14 +12,14 @@ public static void AddApiAuthentication(this IServiceCollection services) { services.AddAuthorization(); services - .AddIdentityApiEndpoints() + .AddIdentityApiEndpoints() .AddRoles() - .AddEntityFrameworkStores(); + .AddEntityFrameworkStores(); } public static void UseApiAuthentication(this WebApplication app) { - app.MapGroup($"/auth").MapIdentityApi(); + app.MapGroup($"/auth").MapIdentityApi(); app.UseAuthorization(); } @@ -34,7 +34,7 @@ this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions options { tag = controllerActionDescriptor.ControllerName; } - tag = tag ?? api.RelativePath?.Split('/')?.FirstOrDefault()?.ToPascalCase(); + tag = tag ?? api.RelativePath?.Split('/')?.FirstOrDefault(); return new[] { tag }; }); From c75dde7011075289e9493b0124174c88b183a4e1 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Mon, 10 Jun 2024 11:16:06 +0300 Subject: [PATCH 05/16] feat(dotnet-auth): version bump: code-gen-types & csharp-ast --- package-lock.json | 16 ++++++++-------- plugins/dotnet-auth-core-identity/package.json | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index aee72fc0..95874057 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15484,9 +15484,9 @@ "version": "0.0.1", "license": "Apache-2.0", "devDependencies": { - "@amplication/code-gen-types": "2.0.33-beta.23", + "@amplication/code-gen-types": "2.0.34", "@amplication/code-gen-utils": "^0.0.9", - "@amplication/csharp-ast": "0.0.3-beta.2", + "@amplication/csharp-ast": "0.0.3", "@babel/parser": "^7.18.11", "@babel/types": "^7.18.10", "@types/lodash": "^4.14.182", @@ -15508,9 +15508,9 @@ } }, "plugins/dotnet-auth-core-identity/node_modules/@amplication/code-gen-types": { - "version": "2.0.33-beta.23", - "resolved": "https://registry.npmjs.org/@amplication/code-gen-types/-/code-gen-types-2.0.33-beta.23.tgz", - "integrity": "sha512-CtmkKPDzRF/px4DKQtS4LWdbsNDTTMt2UXgInpq7WCtM9RXIjKehucvmSAIIhKkXR0H8GdILOYOIpSCPZlgmbw==", + "version": "2.0.34", + "resolved": "https://registry.npmjs.org/@amplication/code-gen-types/-/code-gen-types-2.0.34.tgz", + "integrity": "sha512-RLjFuooQoCRomZ6KBHw+Kp25OsvpJ1eAwLi+jO/6S/9QkAGZ+jHWhUUoSiyI8pmWYvi7T2VI8b6d5K8s1Nzh9w==", "dev": true, "dependencies": { "ast-types": "^0.14.2", @@ -15524,9 +15524,9 @@ } }, "plugins/dotnet-auth-core-identity/node_modules/@amplication/csharp-ast": { - "version": "0.0.3-beta.2", - "resolved": "https://registry.npmjs.org/@amplication/csharp-ast/-/csharp-ast-0.0.3-beta.2.tgz", - "integrity": "sha512-2Qkz0IFvKeYmy0jK/nyeYzzHW/LrWDCibVJT6RLytt+Zy944wk6W6NH8bOWyEsEGkPZNcjK0zD03MFrRgV4TiA==", + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/@amplication/csharp-ast/-/csharp-ast-0.0.3.tgz", + "integrity": "sha512-29tSgnmM7QH1ZGj5Kt1Ijp8phL3r2h3REvi9ThTGIn2TC6DnwJr4dzStwSt8vM613WOxqrF+V0GSJLITJ8jD4w==", "dev": true, "dependencies": { "tslib": "^2.3.0" diff --git a/plugins/dotnet-auth-core-identity/package.json b/plugins/dotnet-auth-core-identity/package.json index 2ffeb9ae..0cf38438 100644 --- a/plugins/dotnet-auth-core-identity/package.json +++ b/plugins/dotnet-auth-core-identity/package.json @@ -12,9 +12,9 @@ "author": "Mor Hagbi", "license": "Apache-2.0", "devDependencies": { - "@amplication/code-gen-types": "2.0.33-beta.23", + "@amplication/code-gen-types": "2.0.34", "@amplication/code-gen-utils": "^0.0.9", - "@amplication/csharp-ast": "0.0.3-beta.2", + "@amplication/csharp-ast": "0.0.3", "@babel/parser": "^7.18.11", "@babel/types": "^7.18.10", "@types/lodash": "^4.14.182", From 5d4f11fe8a5f6a9d2bc48ee7d90b09a55006323d Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Mon, 10 Jun 2024 17:13:00 +0300 Subject: [PATCH 06/16] fix(dotnet-auth): delete authEntity and use IdentityUser --- .../.amplicationrc.json | 5 +- .../src/core/create-seed-development-data.ts | 106 ++++++++---------- .../src/core/create-static-file-map.ts | 4 - .../dotnet-auth-core-identity/src/index.ts | 51 ++------- .../common/auth/ProgramAuthExtensions.cs | 4 +- 5 files changed, 56 insertions(+), 114 deletions(-) diff --git a/plugins/dotnet-auth-core-identity/.amplicationrc.json b/plugins/dotnet-auth-core-identity/.amplicationrc.json index 2d3af765..d920fafd 100644 --- a/plugins/dotnet-auth-core-identity/.amplicationrc.json +++ b/plugins/dotnet-auth-core-identity/.amplicationrc.json @@ -1,6 +1,3 @@ { - "settings": {}, - "systemSettings": { - "requireAuthenticationEntity": "true" - } + "settings": {} } 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 76f3af67..6bf1adef 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,17 +1,6 @@ -import { Entity, EnumDataType } from "@amplication/code-gen-types"; import { CodeBlock, CsharpSupport } from "@amplication/csharp-ast"; -import { camelCase } from "lodash"; -import { pascalCase } from "pascal-case"; -export function CreateSeedDevelopmentDataBody( - resourceName: string, - authEntity: Entity, - entities: Entity[] -): CodeBlock { - const { name, pluralName } = authEntity; - const entityNameToCamelCase = camelCase(name); - const entityNamePluralize = pascalCase(pluralName); - const entityFirstLetter = entityNameToCamelCase.slice(0, 1); +export function CreateSeedDevelopmentDataBody(resourceName: string): CodeBlock { return new CodeBlock({ references: [ CsharpSupport.classReference({ @@ -34,66 +23,63 @@ export function CreateSeedDevelopmentDataBody( .Where(x => x.Value != null) .Select(x => x.Value.ToString()) .ToArray(); - - ${authEntityDto(authEntity, entities)} - - - if (!context.${entityNamePluralize}.Any(${entityFirstLetter} => ${entityFirstLetter}.UserName == ${entityNameToCamelCase}.UserName)) - { - var password = new PasswordHasher<${name}>(); - var hashed = password.HashPassword(${entityNameToCamelCase}, "password"); - ${entityNameToCamelCase}.PasswordHash = hashed; - var userStore = new UserStore<${name}>(context); - await userStore.CreateAsync(${entityNameToCamelCase}); + + var user = new IdentityUser { Email = "test@email.com", UserName = "admin" }; + + + var password = new PasswordHasher(); + var hashed = password.HashPassword(user, "password"); + user.PasswordHash = hashed; + var userStore = new UserStore(context); + await userStore.CreateAsync(user); var _roleManager = serviceProvider.GetRequiredService>(); foreach (var role in amplicationRoles) { - await userStore.AddToRoleAsync(${entityNameToCamelCase}, _roleManager.NormalizeKey(role)); + await userStore.AddToRoleAsync(user, _roleManager.NormalizeKey(role)); } - } + await context.SaveChangesAsync();`, }); } -const authEntityDto = (authEntity: Entity, entities: Entity[]): string => { - const { fields } = authEntity; - let codeBlock = ""; +// const authEntityDto = (entities: Entity[]): string => { +// let codeBlock = ""; - for (const field of fields) { - const fieldNamePascalCase = pascalCase(field.name); +// for (const field of fields) { +// const fieldNamePascalCase = pascalCase(field.name); - if (field.dataType == EnumDataType.Lookup) { - const relatedEntity = entities.find( - (entity) => entity.id === field.properties?.relatedEntityId - ); +// if (field.dataType == EnumDataType.Lookup) { +// const relatedEntity = entities.find( +// (entity) => entity.id === field.properties?.relatedEntityId +// ); - const relatedEntityFieldName = pascalCase(field.name); +// const relatedEntityFieldName = pascalCase(field.name); - if (field.properties?.allowMultipleSelection) { - // the "many" side of the relation - codeBlock = - codeBlock + - `${fieldNamePascalCase} = model.${relatedEntityFieldName}.Select(x => new ${relatedEntity?.name}IdDto {Id = x.Id}).ToList(),\n`; - } else { - if (field.properties.fkHolderName === authEntity.name) { - break; - } else { - // the "one" side of the relation - codeBlock = - codeBlock + - `${fieldNamePascalCase} = new ${relatedEntity?.name}IdDto { Id = model.${fieldNamePascalCase}Id},\n`; - } - } - } else { - codeBlock = - codeBlock + `${fieldNamePascalCase} = model.${fieldNamePascalCase},\n`; - } - } +// if (field.properties?.allowMultipleSelection) { +// // the "many" side of the relation +// codeBlock = +// codeBlock + +// `${fieldNamePascalCase} = model.${relatedEntityFieldName}.Select(x => new ${relatedEntity?.name}IdDto {Id = x.Id}).ToList(),\n`; +// } else { +// if (field.properties.fkHolderName === authEntity.name) { +// break; +// } else { +// // the "one" side of the relation +// codeBlock = +// codeBlock + +// `${fieldNamePascalCase} = new ${relatedEntity?.name}IdDto { Id = model.${fieldNamePascalCase}Id},\n`; +// } +// } +// } else { +// codeBlock = +// codeBlock + `${fieldNamePascalCase} = model.${fieldNamePascalCase},\n`; +// } +// } - return `var ${camelCase(authEntity.name)} = new ${authEntity.name} - { - ${codeBlock} - };`; -}; +// return `var ${camelCase(authEntity.name)} = new ${authEntity.name} +// { +// ${codeBlock} +// };`; +// }; diff --git a/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts b/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts index 92bfd91e..60cfc0dc 100644 --- a/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts +++ b/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts @@ -7,7 +7,6 @@ export async function createStaticFileFileMap( destPath: string, filePath: string, context: dotnetTypes.DsgContext, - authEntityName?: string, classReferences?: ClassReference[] ): Promise> { const fileMap = new FileMap(context.logger); @@ -21,9 +20,6 @@ export async function createStaticFileFileMap( `${resourceName}DbContext` ); - if (authEntityName) - fileContent = fileContent.replaceAll("authEntityName", authEntityName); - const file: IFile = { path: destPath, code: new CodeBlock({ diff --git a/plugins/dotnet-auth-core-identity/src/index.ts b/plugins/dotnet-auth-core-identity/src/index.ts index ea669efd..60188de2 100644 --- a/plugins/dotnet-auth-core-identity/src/index.ts +++ b/plugins/dotnet-auth-core-identity/src/index.ts @@ -33,9 +33,6 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { CreateControllerBaseModuleFile: { after: this.afterCreateControllerBaseModule, }, - CreateEntityModel: { - after: this.afterCreateEntityModel, - }, CreateResourceDbContextFile: { after: this.afterCreateResourceDbContextFile, }, @@ -92,7 +89,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { files: FileMap ): Promise> { const { resourceInfo } = context; - if (!resourceInfo || !resourceInfo.settings.authEntityName) return files; + if (!resourceInfo) return files; const resourceName = pascalCase(resourceInfo.name); @@ -106,7 +103,6 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { destPath, filePath, context, - resourceInfo.settings.authEntityName, [ CsharpSupport.classReference({ name: `${resourceName}`, @@ -136,41 +132,12 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { return files; } - afterCreateEntityModel( - context: dotnetTypes.DsgContext, - eventParams: dotnet.CreateEntityModelParams, - files: FileMap - ): FileMap { - const { apisDir, entity } = eventParams; - const { resourceInfo } = context; - const authEntityName = resourceInfo?.settings.authEntityName; - - if (entity.name !== authEntityName) return files; - - const modelFile = files.get(`${apisDir}${authEntityName}.cs`); - - if (!modelFile) return files; - - modelFile.code.parentClassReference = CsharpSupport.classReference({ - name: "IdentityUser", - namespace: "Microsoft.AspNetCore.Identity", - }); - - return files; - } - afterCreateResourceDbContextFile( context: dotnetTypes.DsgContext, eventParams: dotnet.CreateResourceDbContextFileParams, files: FileMap ): FileMap { - const { resourceDbContextPath, entities, resourceName } = eventParams; - const { resourceInfo } = context; - const authEntityName = resourceInfo?.settings.authEntityName; - - const authEntityCheck = entities.find((e) => e.name === authEntityName); - - if (!authEntityCheck) return files; + const { resourceDbContextPath, resourceName } = eventParams; const modelFile = files.get( `${resourceDbContextPath}${resourceName}DbContext.cs` @@ -179,8 +146,8 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { if (!modelFile) return files; modelFile.code.parentClassReference = CsharpSupport.classReference({ - name: `IdentityDbContext<${authEntityName}>`, - namespace: "Microsoft.AspNetCore.Identity.EntityFrameworkCore", + name: `IdentityDbContext`, + namespace: "Microsoft.AspNetCore.Identity", }); return files; @@ -384,13 +351,9 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { files: FileMap ): FileMap { const { seedFilePath, resourceName } = eventParams; - const { resourceInfo, entities } = context; - - const authEntity = entities?.find( - (e) => e.name === resourceInfo?.settings.authEntityName - ); + const { entities } = context; - if (!authEntity || !entities) return files; + if (!entities) return files; const seedFile = files.get(seedFilePath); seedFile?.code.addMethod( @@ -398,7 +361,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { name: "SeedDevUser", access: "public", isAsync: true, - body: CreateSeedDevelopmentDataBody(resourceName, authEntity, entities), + body: CreateSeedDevelopmentDataBody(resourceName), type: MethodType.STATIC, parameters: [ CsharpSupport.parameter({ diff --git a/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs b/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs index cec743ca..82f3988f 100644 --- a/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs +++ b/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs @@ -12,14 +12,14 @@ public static void AddApiAuthentication(this IServiceCollection services) { services.AddAuthorization(); services - .AddIdentityApiEndpoints() + .AddIdentityApiEndpoints() .AddRoles() .AddEntityFrameworkStores(); } public static void UseApiAuthentication(this WebApplication app) { - app.MapGroup($"/auth").MapIdentityApi(); + app.MapGroup($"/auth").MapIdentityApi(); app.UseAuthorization(); } From c35dd6a9038114f28c2d95f48202eb3d74ab467d Mon Sep 17 00:00:00 2001 From: morhag90 <97830649+morhag90@users.noreply.github.com> Date: Mon, 10 Jun 2024 19:00:58 +0300 Subject: [PATCH 07/16] Update plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs Co-authored-by: Daniele Iasella <2861984+overbit@users.noreply.github.com> --- .../src/static/common/auth/ProgramAuthExtensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs b/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs index 82f3988f..96dcb19a 100644 --- a/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs +++ b/plugins/dotnet-auth-core-identity/src/static/common/auth/ProgramAuthExtensions.cs @@ -3,7 +3,6 @@ using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Filters; - namespace ServiceName.APIs; public static class ProgramAuthExtensions From 4d3dd494b343c9add5efab7ae8b86f6e503fbd9b Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Thu, 13 Jun 2024 13:01:01 +0300 Subject: [PATCH 08/16] refactor(dotnet-auth): remove unused using from programAuthExtensionsFile --- plugins/dotnet-auth-core-identity/src/index.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/plugins/dotnet-auth-core-identity/src/index.ts b/plugins/dotnet-auth-core-identity/src/index.ts index 60188de2..35e63323 100644 --- a/plugins/dotnet-auth-core-identity/src/index.ts +++ b/plugins/dotnet-auth-core-identity/src/index.ts @@ -108,10 +108,6 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { name: `${resourceName}`, namespace: `${resourceName}.Infrastructure`, }), - CsharpSupport.classReference({ - name: `${resourceName}`, - namespace: `${resourceName}.Infrastructure.Models`, - }), ] ); From edd4260d50dc8a22d3fea49a382495755d99e6b4 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Thu, 13 Jun 2024 13:44:50 +0300 Subject: [PATCH 09/16] fix(dotnet-auth): rename the reference name for ResourceDbContext --- plugins/dotnet-auth-core-identity/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/dotnet-auth-core-identity/src/index.ts b/plugins/dotnet-auth-core-identity/src/index.ts index 35e63323..6e3f3978 100644 --- a/plugins/dotnet-auth-core-identity/src/index.ts +++ b/plugins/dotnet-auth-core-identity/src/index.ts @@ -105,7 +105,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { context, [ CsharpSupport.classReference({ - name: `${resourceName}`, + name: `${resourceName}DbContext`, namespace: `${resourceName}.Infrastructure`, }), ] From 25096880270c6fa750356f0cc660d0bca0a9adde Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Thu, 13 Jun 2024 13:49:20 +0300 Subject: [PATCH 10/16] refactor(dotnet-auth): use replaceAll with one placeholder --- .../src/core/create-static-file-map.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts b/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts index 60cfc0dc..8d1df962 100644 --- a/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts +++ b/plugins/dotnet-auth-core-identity/src/core/create-static-file-map.ts @@ -14,11 +14,7 @@ export async function createStaticFileFileMap( if (!context.resourceInfo) return fileMap; const resourceName = pascalCase(context.resourceInfo.name); let fileContent = await readFile(filePath, "utf-8"); - fileContent = fileContent.replace("ServiceName", resourceName); - fileContent = fileContent.replace( - "ServiceNameDbContext", - `${resourceName}DbContext` - ); + fileContent = fileContent.replaceAll("ServiceName", resourceName); const file: IFile = { path: destPath, From 02b8e3015f3f055cca9c1aa499c6ba2d5ecb3302 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Thu, 13 Jun 2024 15:07:07 +0300 Subject: [PATCH 11/16] fix(dotnet-auth): add missing reference for resourceDbContext file --- plugins/dotnet-auth-core-identity/src/index.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/plugins/dotnet-auth-core-identity/src/index.ts b/plugins/dotnet-auth-core-identity/src/index.ts index 6e3f3978..916b5cdf 100644 --- a/plugins/dotnet-auth-core-identity/src/index.ts +++ b/plugins/dotnet-auth-core-identity/src/index.ts @@ -141,9 +141,17 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin { if (!modelFile) return files; - modelFile.code.parentClassReference = CsharpSupport.classReference({ - name: `IdentityDbContext`, - namespace: "Microsoft.AspNetCore.Identity", + modelFile.code.parentClassReference = CsharpSupport.genericClassReference({ + reference: CsharpSupport.classReference({ + name: `IdentityDbContext`, + namespace: "Microsoft.AspNetCore.Identity.EntityFrameworkCore", + }), + innerType: CsharpSupport.Types.reference( + CsharpSupport.classReference({ + name: `IdentityUser`, + namespace: "Microsoft.AspNetCore.Identity", + }) + ), }); return files; From 39d2b2502c6d814ffa3d73a4f45720a9ffafd07a Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Thu, 13 Jun 2024 15:09:10 +0300 Subject: [PATCH 12/16] feat(dotnet-auth): bump plugin version --- package-lock.json | 2 +- plugins/dotnet-auth-core-identity/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 95874057..6d91e6d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15481,7 +15481,7 @@ }, "plugins/dotnet-auth-core-identity": { "name": "@amplication/plugin-dotnet-auth-core-identity", - "version": "0.0.1", + "version": "0.0.2", "license": "Apache-2.0", "devDependencies": { "@amplication/code-gen-types": "2.0.34", diff --git a/plugins/dotnet-auth-core-identity/package.json b/plugins/dotnet-auth-core-identity/package.json index 0cf38438..c2954785 100644 --- a/plugins/dotnet-auth-core-identity/package.json +++ b/plugins/dotnet-auth-core-identity/package.json @@ -1,6 +1,6 @@ { "name": "@amplication/plugin-dotnet-auth-core-identity", - "version": "0.0.1", + "version": "0.0.2", "description": "Add Authentication and Authorization to your .NET Services", "main": "dist/index.js", "nx": {}, From 199e27d16d95bc19b62dd877e9a55f0eb0ff0e88 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Thu, 13 Jun 2024 16:27:18 +0300 Subject: [PATCH 13/16] refactor(dotnet-auth): remove commented code --- .../src/core/create-seed-development-data.ts | 40 ------------------- 1 file changed, 40 deletions(-) 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 6bf1adef..fe2766dd 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 @@ -43,43 +43,3 @@ export function CreateSeedDevelopmentDataBody(resourceName: string): CodeBlock { await context.SaveChangesAsync();`, }); } - -// const authEntityDto = (entities: Entity[]): string => { -// let codeBlock = ""; - -// for (const field of fields) { -// const fieldNamePascalCase = pascalCase(field.name); - -// if (field.dataType == EnumDataType.Lookup) { -// const relatedEntity = entities.find( -// (entity) => entity.id === field.properties?.relatedEntityId -// ); - -// const relatedEntityFieldName = pascalCase(field.name); - -// if (field.properties?.allowMultipleSelection) { -// // the "many" side of the relation -// codeBlock = -// codeBlock + -// `${fieldNamePascalCase} = model.${relatedEntityFieldName}.Select(x => new ${relatedEntity?.name}IdDto {Id = x.Id}).ToList(),\n`; -// } else { -// if (field.properties.fkHolderName === authEntity.name) { -// break; -// } else { -// // the "one" side of the relation -// codeBlock = -// codeBlock + -// `${fieldNamePascalCase} = new ${relatedEntity?.name}IdDto { Id = model.${fieldNamePascalCase}Id},\n`; -// } -// } -// } else { -// codeBlock = -// codeBlock + `${fieldNamePascalCase} = model.${fieldNamePascalCase},\n`; -// } -// } - -// return `var ${camelCase(authEntity.name)} = new ${authEntity.name} -// { -// ${codeBlock} -// };`; -// }; From b13d71d52663dc1a162757a94ab55426c2e3f48f Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Thu, 13 Jun 2024 17:00:21 +0300 Subject: [PATCH 14/16] feat(dotnet-auth): update the password value --- .../src/core/create-seed-development-data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fe2766dd..9e81c5f6 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 @@ -28,7 +28,7 @@ export function CreateSeedDevelopmentDataBody(resourceName: string): CodeBlock { var password = new PasswordHasher(); - var hashed = password.HashPassword(user, "password"); + var hashed = password.HashPassword(user, "P@ssw0rd!"); user.PasswordHash = hashed; var userStore = new UserStore(context); await userStore.CreateAsync(user); From f3f1f7a68b8751b7b8edec252c8dd67ef6dfbe8f Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Thu, 13 Jun 2024 17:01:47 +0300 Subject: [PATCH 15/16] feat(dotnet-auth): version bump --- package-lock.json | 2 +- plugins/dotnet-auth-core-identity/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6d91e6d2..f702d7df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15481,7 +15481,7 @@ }, "plugins/dotnet-auth-core-identity": { "name": "@amplication/plugin-dotnet-auth-core-identity", - "version": "0.0.2", + "version": "0.0.3", "license": "Apache-2.0", "devDependencies": { "@amplication/code-gen-types": "2.0.34", diff --git a/plugins/dotnet-auth-core-identity/package.json b/plugins/dotnet-auth-core-identity/package.json index c2954785..db051672 100644 --- a/plugins/dotnet-auth-core-identity/package.json +++ b/plugins/dotnet-auth-core-identity/package.json @@ -1,6 +1,6 @@ { "name": "@amplication/plugin-dotnet-auth-core-identity", - "version": "0.0.2", + "version": "0.0.3", "description": "Add Authentication and Authorization to your .NET Services", "main": "dist/index.js", "nx": {}, 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 16/16] 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; +};