Skip to content

Commit

Permalink
check for valid identifiers for --dataContext option (#3036)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepchoudhery authored Oct 23, 2024
1 parent a03521d commit b4afd5d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal class Constants
public const string ViewModelExtension = ".cshtml.cs";
public const string T4TemplateExtension = ".tt";
public const string GlobalNamespace = "<global namespace>";
public const string NewDbContext = nameof(NewDbContext);

public class CliOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ internal class IdentityModel
public required string UserClassName { get; internal set; }
public required string UserClassNamespace { get; internal set; }
public string? DbContextNamespace { get; set; }
public required string DbContextName { get; set; }
//Database type eg. SQL Server or SQLite
public string? DatabaseProvider { get; set; }
public required string BaseOutputPath { get; set; }
public bool Overwrite { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Core.Steps;
using Microsoft.DotNet.Scaffolding.Internal.Services;
Expand All @@ -10,6 +11,7 @@
using Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps.Settings;
using Microsoft.Extensions.Logging;
using Constants = Microsoft.DotNet.Scaffolding.Internal.Constants;
using AspNetConstants = Microsoft.DotNet.Tools.Scaffold.AspNet.Common.Constants;

namespace Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps;

Expand Down Expand Up @@ -93,19 +95,19 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
{
if (string.IsNullOrEmpty(Project) || !_fileSystem.FileExists(Project))
{
_logger.LogError("Missing/Invalid --project option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.ProjectCliOption} option.");
return null;
}

if (string.IsNullOrEmpty(Model))
{
_logger.LogError("Missing/Invalid --model option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.ModelCliOption} option.");
return null;
}

if (string.IsNullOrEmpty(Page))
{
_logger.LogError("Missing/Invalid --page option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.PageTypeOption} option.");
return null;
}
else if (!string.IsNullOrEmpty(Page) && !BlazorCrudHelper.CRUDPages.Contains(Page, StringComparer.OrdinalIgnoreCase))
Expand All @@ -116,12 +118,22 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell

if (string.IsNullOrEmpty(DataContext))
{
_logger.LogError("Missing/Invalid --dataContext option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.DataContextOption} option.");
return null;
}
else if (string.IsNullOrEmpty(DatabaseProvider) || !PackageConstants.EfConstants.EfPackagesDict.ContainsKey(DatabaseProvider))
else
{
DatabaseProvider = PackageConstants.EfConstants.SqlServer;
if (!SyntaxFacts.IsValidIdentifier(DataContext) || DataContext.Equals("DbContext", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation($"Invalid {AspNetConstants.CliOptions.DataContextOption} option");
_logger.LogInformation($"Using default '{AspNetConstants.NewDbContext}'");
DataContext = AspNetConstants.NewDbContext;
}

if (string.IsNullOrEmpty(DatabaseProvider) || !PackageConstants.EfConstants.EfPackagesDict.ContainsKey(DatabaseProvider))
{
DatabaseProvider = PackageConstants.EfConstants.SqlServer;
}
}

return new CrudSettings
Expand Down Expand Up @@ -149,7 +161,7 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
var modelClassSymbol = allClasses.FirstOrDefault(x => x.Name.Equals(settings.Model, StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrEmpty(settings.Model) || modelClassSymbol is null)
{
_logger.LogError($"Invalid --model '{settings.Model}'");
_logger.LogError($"Invalid {AspNetConstants.CliOptions.ModelCliOption} '{settings.Model}'");
return null;
}
else
Expand All @@ -160,7 +172,7 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
var validateModelInfoResult = ClassAnalyzers.ValidateModelForCrudScaffolders(modelInfo, _logger);
if (!validateModelInfoResult)
{
_logger.LogError($"Invalid --model '{settings.Model}'");
_logger.LogError($"Invalid {AspNetConstants.CliOptions.ModelCliOption} '{settings.Model}'");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Core.Steps;
using Microsoft.DotNet.Scaffolding.Internal.Services;
Expand All @@ -10,6 +11,7 @@
using Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps.Settings;
using Microsoft.Extensions.Logging;
using Constants = Microsoft.DotNet.Scaffolding.Internal.Constants;
using AspNetConstants = Microsoft.DotNet.Tools.Scaffold.AspNet.Common.Constants;

namespace Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps;

Expand Down Expand Up @@ -85,19 +87,19 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
{
if (string.IsNullOrEmpty(Project) || !_fileSystem.FileExists(Project))
{
_logger.LogError("Missing/Invalid --project option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.ProjectCliOption} option.");
return null;
}

if (string.IsNullOrEmpty(Model))
{
_logger.LogError("Missing/Invalid --model option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.ModelCliOption} option.");
return null;
}

if (string.IsNullOrEmpty(ControllerName))
{
_logger.LogError("Missing/Invalid --controller option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.ControllerNameOption} option.");
return null;
}
else
Expand All @@ -107,7 +109,7 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell

if (string.IsNullOrEmpty(ControllerType))
{
_logger.LogError($"Missing/Invalid '{nameof(ValidateEfControllerStep.ControllerType)}' value.");
_logger.LogError($"Missing/Invalid '{nameof(ControllerType)}' value.");
return null;
}
else if (
Expand All @@ -121,12 +123,22 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell

if (string.IsNullOrEmpty(DataContext))
{
_logger.LogError("Missing/Invalid --dataContext option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.DataContextOption} option.");
return null;
}
else if (string.IsNullOrEmpty(DatabaseProvider) || !PackageConstants.EfConstants.EfPackagesDict.ContainsKey(DatabaseProvider))
else
{
DatabaseProvider = PackageConstants.EfConstants.SqlServer;
if (!SyntaxFacts.IsValidIdentifier(DataContext) || DataContext.Equals("DbContext", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation($"Invalid {AspNetConstants.CliOptions.DataContextOption} option");
_logger.LogInformation($"Using default '{AspNetConstants.NewDbContext}'");
DataContext = AspNetConstants.NewDbContext;
}

if (string.IsNullOrEmpty(DatabaseProvider) || !PackageConstants.EfConstants.EfPackagesDict.ContainsKey(DatabaseProvider))
{
DatabaseProvider = PackageConstants.EfConstants.SqlServer;
}
}

return new EfControllerSettings
Expand Down Expand Up @@ -156,7 +168,7 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
var modelClassSymbol = allClasses.FirstOrDefault(x => x.Name.Equals(settings.Model, StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrEmpty(settings.Model) || modelClassSymbol is null)
{
_logger.LogError($"Invalid --model '{settings.Model}'");
_logger.LogError($"Invalid {AspNetConstants.CliOptions.ModelCliOption} '{settings.Model}'");
return null;
}
else
Expand All @@ -167,7 +179,7 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
var validateModelInfoResult = ClassAnalyzers.ValidateModelForCrudScaffolders(modelInfo, _logger);
if (!validateModelInfoResult)
{
_logger.LogError($"Invalid --model '{settings.Model}'");
_logger.LogError($"Invalid {AspNetConstants.CliOptions.ModelCliOption} '{settings.Model}'");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Core.Steps;
using Microsoft.DotNet.Scaffolding.Internal.Services;
Expand All @@ -9,7 +10,8 @@
using Microsoft.DotNet.Tools.Scaffold.AspNet.Models;
using Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps.Settings;
using Microsoft.Extensions.Logging;
using static Microsoft.DotNet.Scaffolding.Internal.Constants;
using Constants = Microsoft.DotNet.Scaffolding.Internal.Constants;
using AspNetConstants = Microsoft.DotNet.Tools.Scaffold.AspNet.Common.Constants;

namespace Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps;

Expand Down Expand Up @@ -56,8 +58,8 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
else
{
context.Properties.Add(nameof(IdentityModel), identityModel);
codeModifierProperties.Add(CodeModifierPropertyConstants.IdentityNamespace, identityModel.IdentityNamespace);
codeModifierProperties.Add(CodeModifierPropertyConstants.UserClassNamespace, identityModel.UserClassNamespace);
codeModifierProperties.Add(Constants.CodeModifierPropertyConstants.IdentityNamespace, identityModel.IdentityNamespace);
codeModifierProperties.Add(Constants.CodeModifierPropertyConstants.UserClassNamespace, identityModel.UserClassNamespace);
}

//Install packages and add a DbContext (if needed)
Expand All @@ -74,7 +76,7 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
var projectBasePath = Path.GetDirectoryName(identitySettings.Project);
if (!string.IsNullOrEmpty(projectBasePath))
{
context.Properties.Add(StepConstants.BaseProjectPath, projectBasePath);
context.Properties.Add(Constants.StepConstants.BaseProjectPath, projectBasePath);
}

var dbCodeModifierProperties = AspNetDbContextHelper.GetDbContextCodeModifierProperties(identityModel.DbContextInfo);
Expand All @@ -83,29 +85,39 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
codeModifierProperties.TryAdd(kvp.Key, kvp.Value);
}

codeModifierProperties.TryAdd(CodeModifierPropertyConstants.UserClassName, identityModel.UserClassName);
codeModifierProperties.TryAdd(Constants.CodeModifierPropertyConstants.UserClassName, identityModel.UserClassName);
}

context.Properties.Add(StepConstants.CodeModifierProperties, codeModifierProperties);
context.Properties.Add(Constants.StepConstants.CodeModifierProperties, codeModifierProperties);
return true;
}

private IdentitySettings? ValidateIdentitySettings()
{
if (string.IsNullOrEmpty(Project) || !_fileSystem.FileExists(Project))
{
_logger.LogError("Missing/Invalid --project option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.ProjectCliOption} option.");
return null;
}

if (string.IsNullOrEmpty(DataContext))
{
_logger.LogError("Missing/Invalid --dataContext option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.DataContextOption} option.");
return null;
}
else if (string.IsNullOrEmpty(DatabaseProvider) || !PackageConstants.EfConstants.IdentityEfPackagesDict.ContainsKey(DatabaseProvider))
else
{
DatabaseProvider = PackageConstants.EfConstants.SqlServer;
if (!SyntaxFacts.IsValidIdentifier(DataContext) || DataContext.Equals("DbContext", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation($"Invalid {AspNetConstants.CliOptions.DataContextOption} option");
_logger.LogInformation($"Using default '{AspNetConstants.NewDbContext}'");
DataContext = AspNetConstants.NewDbContext;
}

if (string.IsNullOrEmpty(DatabaseProvider) || !PackageConstants.EfConstants.IdentityEfPackagesDict.ContainsKey(DatabaseProvider))
{
DatabaseProvider = PackageConstants.EfConstants.SqlServer;
}
}

return new IdentitySettings
Expand Down Expand Up @@ -154,9 +166,8 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
ProjectInfo = projectInfo,
DbContextInfo = dbContextInfo,
IdentityNamespace = identityNamespace,
UserClassName = Constants.Identity.UserClassName,
UserClassName = AspNetConstants.Identity.UserClassName,
UserClassNamespace = userClassNamespace,
DbContextName = Constants.Identity.DbContextName,
BaseOutputPath = projectDirectory,
Overwrite = settings.Overwrite
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Core.Steps;
using Microsoft.DotNet.Scaffolding.Internal;
Expand All @@ -11,6 +12,7 @@
using Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps.Settings;
using Microsoft.Extensions.Logging;
using Constants = Microsoft.DotNet.Scaffolding.Internal.Constants;
using AspNetConstants = Microsoft.DotNet.Tools.Scaffold.AspNet.Common.Constants;

namespace Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps;

Expand Down Expand Up @@ -98,20 +100,29 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
{
if (string.IsNullOrEmpty(Project) || !_fileSystem.FileExists(Project))
{
_logger.LogError("Missing/Invalid --project option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.ProjectCliOption} option.");
return null;
}

if (string.IsNullOrEmpty(Model))
{
_logger.LogError("Missing/Invalid --model option.");
_logger.LogError($"Missing/Invalid {AspNetConstants.CliOptions.ModelCliOption} option.");
return null;
}

if (!string.IsNullOrEmpty(DataContext) &&
(string.IsNullOrEmpty(DatabaseProvider) || !PackageConstants.EfConstants.EfPackagesDict.ContainsKey(DatabaseProvider)))
if (!string.IsNullOrEmpty(DataContext))
{
DatabaseProvider = PackageConstants.EfConstants.SqlServer;
if (!SyntaxFacts.IsValidIdentifier(DataContext) || DataContext.Equals("DbContext", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation($"Invalid {AspNetConstants.CliOptions.DataContextOption} option");
_logger.LogInformation($"Using default '{AspNetConstants.NewDbContext}'");
DataContext = AspNetConstants.NewDbContext;
}

if(string.IsNullOrEmpty(DatabaseProvider) || !PackageConstants.EfConstants.EfPackagesDict.ContainsKey(DatabaseProvider))
{
DatabaseProvider = PackageConstants.EfConstants.SqlServer;
}
}

var commandSettings = new MinimalApiSettings
Expand Down Expand Up @@ -142,7 +153,7 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
var modelClassSymbol = allClasses.FirstOrDefault(x => x.Name.Equals(settings.Model, StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrEmpty(settings.Model) || modelClassSymbol is null)
{
_logger.LogError($"Invalid --model '{settings.Model}' provided");
_logger.LogError($"Invalid {AspNetConstants.CliOptions.ModelCliOption} '{settings.Model}' provided");
return null;
}
else
Expand All @@ -153,7 +164,7 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
var validateModelInfoResult = ClassAnalyzers.ValidateModelForCrudScaffolders(modelInfo, _logger);
if (!validateModelInfoResult)
{
_logger.LogError($"Invalid --model '{settings.Model}'");
_logger.LogError($"Invalid {AspNetConstants.CliOptions.ModelCliOption} '{settings.Model}'");
return null;
}

Expand Down
Loading

0 comments on commit b4afd5d

Please sign in to comment.