Added documentation for the configurators and service extensions methods
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class DbContextConfig {
|
||||
public Type ContextType { get; }
|
||||
public List<TableConfig> Tables { get; } = new();
|
||||
|
||||
public DbContextConfig(Type context) {
|
||||
ContextType = context;
|
||||
|
||||
foreach (var property in ContextType.GetProperties()) {
|
||||
if (!property.PropertyType.IsGenericType) continue;
|
||||
var innerType = property.PropertyType.GenericTypeArguments.First();
|
||||
var setType = typeof(DbSet<>).MakeGenericType(innerType);
|
||||
if (property.PropertyType != setType) continue;
|
||||
|
||||
var table = new TableConfig(this, innerType, property.Name, Tables.Count);
|
||||
Tables.Add(table);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DbContextConfig<TDbContext>(DbContextConfig config) {
|
||||
public DbContextConfig InnerConfig { get; } = config;
|
||||
|
||||
public DbContextConfig<TDbContext> Table<TModel>(Action<TableConfig<TModel>> configurator) where TModel : class {
|
||||
var table = Table<TModel>();
|
||||
configurator.Invoke(table);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> Table<TModel>() where TModel : class {
|
||||
var table = InnerConfig.Tables.Single(table => table.TableType == typeof(TModel));
|
||||
return new TableConfig<TModel>(table);
|
||||
}
|
||||
|
||||
}
|
||||
57
src/HopFrame.Core/Config/DbContextConfigurator.cs
Normal file
57
src/HopFrame.Core/Config/DbContextConfigurator.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class DbContextConfig {
|
||||
public Type ContextType { get; }
|
||||
public List<TableConfig> Tables { get; } = new();
|
||||
|
||||
public DbContextConfig(Type context) {
|
||||
ContextType = context;
|
||||
|
||||
foreach (var property in ContextType.GetProperties()) {
|
||||
if (!property.PropertyType.IsGenericType) continue;
|
||||
var innerType = property.PropertyType.GenericTypeArguments.First();
|
||||
var setType = typeof(DbSet<>).MakeGenericType(innerType);
|
||||
if (property.PropertyType != setType) continue;
|
||||
|
||||
var table = new TableConfig(this, innerType, property.Name, Tables.Count);
|
||||
Tables.Add(table);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="DbContextConfig"/>
|
||||
/// </summary>
|
||||
public class DbContextConfigurator<TDbContext>(DbContextConfig config) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal DbContext configuration that's modified by the helper functions
|
||||
/// </summary>
|
||||
public DbContextConfig InnerConfig { get; } = config;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the table of the DbContext using the provided configurator
|
||||
/// </summary>
|
||||
/// <param name="configurator">Used for configuring the table</param>
|
||||
/// <typeparam name="TModel">The model of the table for identifying the correct one</typeparam>
|
||||
/// <seealso cref="TableConfigurator{TModel}"/>
|
||||
public DbContextConfigurator<TDbContext> Table<TModel>(Action<TableConfigurator<TModel>> configurator) where TModel : class {
|
||||
var table = Table<TModel>();
|
||||
configurator.Invoke(table);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the table of the DbContext
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">The model of the table for identifying the correct one</typeparam>
|
||||
/// <returns>The configurator for the specified table</returns>
|
||||
/// <seealso cref="TableConfigurator{TModel}"/>
|
||||
public TableConfigurator<TModel> Table<TModel>() where TModel : class {
|
||||
var table = InnerConfig.Tables.Single(table => table.TableType == typeof(TModel));
|
||||
return new TableConfigurator<TModel>(table);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,31 +10,59 @@ public class HopFrameConfig {
|
||||
public string? LoginPageRewrite { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="HopFrameConfig"/>
|
||||
/// </summary>
|
||||
public class HopFrameConfigurator(HopFrameConfig config) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal HopFrame configuration that's modified by the helper functions
|
||||
/// </summary>
|
||||
public HopFrameConfig InnerConfig { get; } = config;
|
||||
|
||||
public HopFrameConfigurator AddDbContext<TDbContext>(Action<DbContextConfig<TDbContext>> configurator) where TDbContext : DbContext {
|
||||
/// <summary>
|
||||
/// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator
|
||||
/// </summary>
|
||||
/// <param name="configurator">Used for configuring the DbContext</param>
|
||||
/// <typeparam name="TDbContext">The DbContext from which all tables should be added</typeparam>
|
||||
/// <seealso cref="DbContextConfigurator{TDbContext}"/>
|
||||
public HopFrameConfigurator AddDbContext<TDbContext>(Action<DbContextConfigurator<TDbContext>> configurator) where TDbContext : DbContext {
|
||||
var context = AddDbContext<TDbContext>();
|
||||
configurator.Invoke(context);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DbContextConfig<TDbContext> AddDbContext<TDbContext>() where TDbContext : DbContext {
|
||||
/// <summary>
|
||||
/// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator
|
||||
/// </summary>
|
||||
/// <typeparam name="TDbContext">The DbContext from which all tables should be added</typeparam>
|
||||
/// <returns>The configurator used for the DbContext</returns>
|
||||
/// <seealso cref="DbContextConfigurator{TDbContext}"/>
|
||||
public DbContextConfigurator<TDbContext> AddDbContext<TDbContext>() where TDbContext : DbContext {
|
||||
var context = new DbContextConfig(typeof(TDbContext));
|
||||
InnerConfig.Contexts.Add(context);
|
||||
return new DbContextConfig<TDbContext>(context);
|
||||
return new DbContextConfigurator<TDbContext>(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the name of the currently logged-in user should be displayed in the top right corner of the admin ui
|
||||
/// </summary>
|
||||
public HopFrameConfigurator DisplayUserInfo(bool display) {
|
||||
InnerConfig.DisplayUserInfo = display;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a default policy that every user needs to have in order to access the admin ui
|
||||
/// </summary>
|
||||
public HopFrameConfigurator SetBasePolicy(string basePolicy) {
|
||||
InnerConfig.BasePolicy = basePolicy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a custom login page to redirect to if the request to the admin ui was unauthorized
|
||||
/// </summary>
|
||||
public HopFrameConfigurator SetLoginPage(string url) {
|
||||
InnerConfig.LoginPageRewrite = url;
|
||||
return this;
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class PropertyConfig(PropertyInfo info, TableConfig table, int nthProperty) {
|
||||
public PropertyInfo Info { get; } = info;
|
||||
public TableConfig Table { get; } = table;
|
||||
public string Name { get; set; } = info.Name;
|
||||
public bool List { get; set; } = true;
|
||||
public bool Sortable { get; set; } = true;
|
||||
public bool Searchable { get; set; } = true;
|
||||
public PropertyInfo? DisplayedProperty { get; set; }
|
||||
public Func<object, IServiceProvider, string>? Formatter { get; set; }
|
||||
public Func<object, IServiceProvider, string>? EnumerableFormatter { get; set; }
|
||||
public Func<string, IServiceProvider, object>? Parser { get; set; }
|
||||
public Func<object?, IServiceProvider, Task<IEnumerable<string>>>? Validator { get; set; }
|
||||
public bool Editable { get; set; } = true;
|
||||
public bool Creatable { get; set; } = true;
|
||||
public bool DisplayValue { get; set; } = true;
|
||||
public bool TextArea { get; set; }
|
||||
public int TextAreaRows { get; set; } = 16;
|
||||
public bool IsRelation { get; set; }
|
||||
public bool IsRequired { get; set; }
|
||||
public bool IsEnumerable { get; set; }
|
||||
public bool IsListingProperty { get; set; }
|
||||
public int Order { get; set; } = nthProperty;
|
||||
}
|
||||
|
||||
public class PropertyConfig<TProp>(PropertyConfig config) {
|
||||
public PropertyConfig InnerConfig { get; } = config;
|
||||
|
||||
public PropertyConfig<TProp> SetDisplayName(string displayName) {
|
||||
InnerConfig.Name = displayName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> List(bool list) {
|
||||
InnerConfig.List = list;
|
||||
InnerConfig.Searchable = !list;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> IsSortable(bool sortable) {
|
||||
InnerConfig.Sortable = sortable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> IsSearchable(bool searchable) {
|
||||
InnerConfig.Searchable = searchable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetDisplayedProperty<TInnerProp>(Expression<Func<TProp, TInnerProp>> propertyExpression) {
|
||||
InnerConfig.DisplayedProperty = TableConfig<TProp>.GetPropertyInfo(propertyExpression);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Format(Func<TProp, IServiceProvider, string> formatter) {
|
||||
InnerConfig.Formatter = (obj, provider) => formatter.Invoke((TProp)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> FormatEach<TInnerProp>(Func<TInnerProp, IServiceProvider, string> formatter) {
|
||||
InnerConfig.EnumerableFormatter = (obj, provider) => formatter.Invoke((TInnerProp)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetParser(Func<string, IServiceProvider, TProp> parser) {
|
||||
InnerConfig.Parser = (str, provider) => parser.Invoke(str, provider)!;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetEditable(bool editable) {
|
||||
InnerConfig.Editable = editable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetCreatable(bool creatable) {
|
||||
InnerConfig.Creatable = creatable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> DisplayValue(bool display) {
|
||||
InnerConfig.DisplayValue = display;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> IsTextArea(bool textField) {
|
||||
InnerConfig.TextArea = textField;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetTextAreaRows(int rows) {
|
||||
InnerConfig.TextAreaRows = rows;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetValidator(Func<TProp?, IServiceProvider, IEnumerable<string>> validator) {
|
||||
InnerConfig.Validator = (obj, provider) => Task.FromResult(validator.Invoke((TProp?)obj, provider));
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetValidator(Func<TProp?, IServiceProvider, Task<IEnumerable<string>>> validator) {
|
||||
InnerConfig.Validator = (obj, provider) => validator.Invoke((TProp?)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetOrderIndex(int index) {
|
||||
InnerConfig.Order = index;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
178
src/HopFrame.Core/Config/PropertyConfigurator.cs
Normal file
178
src/HopFrame.Core/Config/PropertyConfigurator.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using System.Collections;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class PropertyConfig(PropertyInfo info, TableConfig table, int nthProperty) {
|
||||
public PropertyInfo Info { get; } = info;
|
||||
public TableConfig Table { get; } = table;
|
||||
public string Name { get; set; } = info.Name;
|
||||
public bool List { get; set; } = true;
|
||||
public bool Sortable { get; set; } = true;
|
||||
public bool Searchable { get; set; } = true;
|
||||
public PropertyInfo? DisplayedProperty { get; set; }
|
||||
public Func<object, IServiceProvider, string>? Formatter { get; set; }
|
||||
public Func<object, IServiceProvider, string>? EnumerableFormatter { get; set; }
|
||||
public Func<string, IServiceProvider, object>? Parser { get; set; }
|
||||
public Func<object?, IServiceProvider, Task<IEnumerable<string>>>? Validator { get; set; }
|
||||
public bool Editable { get; set; } = true;
|
||||
public bool Creatable { get; set; } = true;
|
||||
public bool DisplayValue { get; set; } = true;
|
||||
public bool TextArea { get; set; }
|
||||
public int TextAreaRows { get; set; } = 16;
|
||||
public bool IsRelation { get; set; }
|
||||
public bool IsRequired { get; set; }
|
||||
public bool IsEnumerable { get; set; }
|
||||
public bool IsListingProperty { get; set; }
|
||||
public int Order { get; set; } = nthProperty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="PropertyConfig"/>
|
||||
/// </summary>
|
||||
public class PropertyConfigurator<TProp>(PropertyConfig config) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal property configuration that's modified by the helper functions
|
||||
/// </summary>
|
||||
public PropertyConfig InnerConfig { get; } = config;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the title displayed in the table header and edit dialog
|
||||
/// </summary>
|
||||
public PropertyConfigurator<TProp> SetDisplayName(string displayName) {
|
||||
InnerConfig.Name = displayName;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the property should appear in the table, if not the property is also set to be not searchable
|
||||
/// </summary>
|
||||
/// <seealso cref="IsSearchable"/>
|
||||
public PropertyConfigurator<TProp> List(bool list) {
|
||||
InnerConfig.List = list;
|
||||
InnerConfig.Searchable = !list;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the table can be sorted by the property
|
||||
/// </summary>
|
||||
public PropertyConfigurator<TProp> IsSortable(bool sortable) {
|
||||
InnerConfig.Sortable = sortable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the property get taken into account for search results
|
||||
/// </summary>
|
||||
public PropertyConfigurator<TProp> IsSearchable(bool searchable) {
|
||||
InnerConfig.Searchable = searchable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the value that should be displayed instead of the string representation of the type
|
||||
/// </summary>
|
||||
/// <seealso cref="Format"/>
|
||||
public PropertyConfigurator<TProp> SetDisplayedProperty<TInnerProp>(Expression<Func<TProp, TInnerProp>> propertyExpression) {
|
||||
InnerConfig.DisplayedProperty = TableConfigurator<TProp>.GetPropertyInfo(propertyExpression);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the value that's displayed in the admin ui
|
||||
/// </summary>
|
||||
/// <seealso cref="FormatEach{TInnerProp}"/>
|
||||
/// <seealso cref="SetDisplayedProperty{TInnerProp}"/>
|
||||
public PropertyConfigurator<TProp> Format(Func<TProp, IServiceProvider, string> formatter) {
|
||||
InnerConfig.Formatter = (obj, provider) => formatter.Invoke((TProp)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the value that's displayed for each entry in the list
|
||||
/// </summary>
|
||||
public PropertyConfigurator<TProp> FormatEach<TInnerProp>(Func<TInnerProp, IServiceProvider, string> formatter) {
|
||||
InnerConfig.EnumerableFormatter = (obj, provider) => formatter.Invoke((TInnerProp)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the function used for parsing the value provided in the editor dialog to the actual property value
|
||||
/// </summary>
|
||||
public PropertyConfigurator<TProp> SetParser(Func<string, IServiceProvider, TProp> parser) {
|
||||
InnerConfig.Parser = (str, provider) => parser.Invoke(str, provider)!;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the value can be edited in the admin ui. If true, the value can still be initially set, but not modified
|
||||
/// </summary>
|
||||
/// <seealso cref="SetCreatable"/>
|
||||
public PropertyConfigurator<TProp> SetEditable(bool editable) {
|
||||
InnerConfig.Editable = editable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the initial value can be edited in the admin ui. If true the value will not be visible in the create dialog
|
||||
/// </summary>
|
||||
/// <seealso cref="SetEditable"/>
|
||||
public PropertyConfigurator<TProp> SetCreatable(bool creatable) {
|
||||
InnerConfig.Creatable = creatable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the value should be displayed in the admin ui (useful for secrets like passwords etc.)
|
||||
/// </summary>
|
||||
public PropertyConfigurator<TProp> DisplayValue(bool display) {
|
||||
InnerConfig.DisplayValue = display;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the admin ui should use a text area for modifying the value
|
||||
/// </summary>
|
||||
/// <seealso cref="SetTextAreaRows"/>
|
||||
public PropertyConfigurator<TProp> IsTextArea(bool textField) {
|
||||
InnerConfig.TextArea = textField;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the initial size of the text area field
|
||||
/// </summary>
|
||||
/// <seealso cref="IsTextArea"/>
|
||||
public PropertyConfigurator<TProp> SetTextAreaRows(int rows) {
|
||||
InnerConfig.TextAreaRows = rows;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the validator used for the property value before saving
|
||||
/// </summary>
|
||||
public PropertyConfigurator<TProp> SetValidator(Func<TProp?, IServiceProvider, IEnumerable<string>> validator) {
|
||||
InnerConfig.Validator = (obj, provider) => Task.FromResult(validator.Invoke((TProp?)obj, provider));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the validator used for the property value before saving
|
||||
/// </summary>
|
||||
public PropertyConfigurator<TProp> SetValidator(Func<TProp?, IServiceProvider, Task<IEnumerable<string>>> validator) {
|
||||
InnerConfig.Validator = (obj, provider) => validator.Invoke((TProp?)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the order index for the property in the admin ui
|
||||
/// </summary>
|
||||
/// <seealso cref="TableConfigurator{TModel}.SetOrderIndex"/>
|
||||
public PropertyConfigurator<TProp> SetOrderIndex(int index) {
|
||||
InnerConfig.Order = index;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class TableConfig {
|
||||
public Type TableType { get; }
|
||||
public string PropertyName { get; }
|
||||
public string DisplayName { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DbContextConfig ContextConfig { get; }
|
||||
public bool Ignored { get; set; }
|
||||
public int Order { get; set; }
|
||||
internal bool Seeded { get; set; }
|
||||
|
||||
public string? ViewPolicy { get; set; }
|
||||
public string? CreatePolicy { get; set; }
|
||||
public string? UpdatePolicy { get; set; }
|
||||
public string? DeletePolicy { get; set; }
|
||||
|
||||
public List<PropertyConfig> Properties { get; } = new();
|
||||
|
||||
public TableConfig(DbContextConfig config, Type tableType, string propertyName, int nthTable) {
|
||||
TableType = tableType;
|
||||
PropertyName = propertyName;
|
||||
ContextConfig = config;
|
||||
DisplayName = PropertyName;
|
||||
Order = nthTable;
|
||||
|
||||
var properties = tableType.GetProperties();
|
||||
for (var i = 0; i < properties.Length; i++) {
|
||||
var info = properties[i];
|
||||
var propConfig = new PropertyConfig(info, this, i);
|
||||
|
||||
if (info.GetCustomAttributes(true).Any(a => a is DatabaseGeneratedAttribute)) {
|
||||
propConfig.Creatable = false;
|
||||
propConfig.Editable = false;
|
||||
}
|
||||
|
||||
if (info.GetCustomAttributes(true).Any(a => a is KeyAttribute)) {
|
||||
propConfig.Editable = false;
|
||||
}
|
||||
|
||||
Properties.Add(propConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class TableConfig<TModel>(TableConfig config) {
|
||||
public TableConfig InnerConfig { get; } = config;
|
||||
|
||||
public TableConfig<TModel> Ignore() {
|
||||
InnerConfig.Ignored = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression) {
|
||||
var info = GetPropertyInfo(propertyExpression);
|
||||
var prop = InnerConfig.Properties
|
||||
.Single(prop => prop.Info.Name == info.Name);
|
||||
return new PropertyConfig<TProp>(prop);
|
||||
}
|
||||
|
||||
public TableConfig<TModel> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression, Action<PropertyConfig<TProp>> configurator) {
|
||||
var prop = Property(propertyExpression);
|
||||
configurator.Invoke(prop);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<string> AddListingProperty(string name, Func<TModel, IServiceProvider, string> template) {
|
||||
var prop = new PropertyConfig(InnerConfig.Properties.First().Info, InnerConfig, InnerConfig.Properties.Count);
|
||||
prop.Name = name;
|
||||
prop.IsListingProperty = true;
|
||||
prop.Formatter = (obj, provider) => template.Invoke((TModel)obj, provider);
|
||||
InnerConfig.Properties.Add(prop);
|
||||
return new PropertyConfig<string>(prop);
|
||||
}
|
||||
|
||||
public TableConfig<TModel> AddListingProperty(string name, Func<TModel, IServiceProvider, string> template, Action<PropertyConfig<string>> configurator) {
|
||||
var prop = AddListingProperty(name, template);
|
||||
configurator.Invoke(prop);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> SetDisplayName(string name) {
|
||||
InnerConfig.DisplayName = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> SetDescription(string description) {
|
||||
InnerConfig.Description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> SetOrderIndex(int index) {
|
||||
InnerConfig.Order = index;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> SetViewPolicy(string policy) {
|
||||
InnerConfig.ViewPolicy = policy;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> SetUpdatePolicy(string policy) {
|
||||
InnerConfig.UpdatePolicy = policy;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> SetCreatePolicy(string policy) {
|
||||
InnerConfig.CreatePolicy = policy;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> SetDeletePolicy(string policy) {
|
||||
InnerConfig.DeletePolicy = policy;
|
||||
return this;
|
||||
}
|
||||
|
||||
internal static PropertyInfo GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda) {
|
||||
if (propertyLambda.Body is not MemberExpression member) {
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers to a method, not a property.");
|
||||
}
|
||||
|
||||
if (member.Member is not PropertyInfo propInfo) {
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers to a field, not a property.");
|
||||
}
|
||||
|
||||
Type type = typeof(TSource);
|
||||
if (propInfo.ReflectedType != null && type != propInfo.ReflectedType &&
|
||||
!type.IsSubclassOf(propInfo.ReflectedType)) {
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers to a property that is not from type {type}.");
|
||||
}
|
||||
|
||||
if (propInfo.Name is null)
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers a not existing property.");
|
||||
|
||||
return propInfo;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
202
src/HopFrame.Core/Config/TableConfigurator.cs
Normal file
202
src/HopFrame.Core/Config/TableConfigurator.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class TableConfig {
|
||||
public Type TableType { get; }
|
||||
public string PropertyName { get; }
|
||||
public string DisplayName { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DbContextConfig ContextConfig { get; }
|
||||
public bool Ignored { get; set; }
|
||||
public int Order { get; set; }
|
||||
internal bool Seeded { get; set; }
|
||||
|
||||
public string? ViewPolicy { get; set; }
|
||||
public string? CreatePolicy { get; set; }
|
||||
public string? UpdatePolicy { get; set; }
|
||||
public string? DeletePolicy { get; set; }
|
||||
|
||||
public List<PropertyConfig> Properties { get; } = new();
|
||||
|
||||
public TableConfig(DbContextConfig config, Type tableType, string propertyName, int nthTable) {
|
||||
TableType = tableType;
|
||||
PropertyName = propertyName;
|
||||
ContextConfig = config;
|
||||
DisplayName = PropertyName;
|
||||
Order = nthTable;
|
||||
|
||||
var properties = tableType.GetProperties();
|
||||
for (var i = 0; i < properties.Length; i++) {
|
||||
var info = properties[i];
|
||||
var propConfig = new PropertyConfig(info, this, i);
|
||||
|
||||
if (info.GetCustomAttributes(true).Any(a => a is DatabaseGeneratedAttribute)) {
|
||||
propConfig.Creatable = false;
|
||||
propConfig.Editable = false;
|
||||
}
|
||||
|
||||
if (info.GetCustomAttributes(true).Any(a => a is KeyAttribute)) {
|
||||
propConfig.Editable = false;
|
||||
}
|
||||
|
||||
Properties.Add(propConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="TableConfig"/>
|
||||
/// </summary>
|
||||
public class TableConfigurator<TModel>(TableConfig config) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal property configuration that's modified by the helper functions
|
||||
/// </summary>
|
||||
public TableConfig InnerConfig { get; } = config;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the table should be ignored in the admin ui
|
||||
/// </summary>
|
||||
public TableConfigurator<TModel> Ignore(bool ignore) {
|
||||
InnerConfig.Ignored = ignore;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the property of the table
|
||||
/// </summary>
|
||||
/// <param name="propertyExpression">Used for determining the property</param>
|
||||
/// <returns>The configurator for the specified property</returns>
|
||||
/// <seealso cref="PropertyConfigurator{TProp}"/>
|
||||
public PropertyConfigurator<TProp> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression) {
|
||||
var info = GetPropertyInfo(propertyExpression);
|
||||
var prop = InnerConfig.Properties
|
||||
.Single(prop => prop.Info.Name == info.Name);
|
||||
return new PropertyConfigurator<TProp>(prop);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the property of the table using the provided configurator
|
||||
/// </summary>
|
||||
/// <param name="propertyExpression">Used for determining the property</param>
|
||||
/// <param name="configurator">Used for configuring the property</param>
|
||||
/// <seealso cref="PropertyConfigurator{TProp}"/>
|
||||
public TableConfigurator<TModel> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression, Action<PropertyConfigurator<TProp>> configurator) {
|
||||
var prop = Property(propertyExpression);
|
||||
configurator.Invoke(prop);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a virtual property to the table view (this property will not appear in the editor)
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the virtual property</param>
|
||||
/// <param name="template">The template used for generating the property value</param>
|
||||
/// <returns>The configurator for the virtual property</returns>
|
||||
/// <seealso cref="PropertyConfigurator{TProp}"/>
|
||||
public PropertyConfigurator<string> AddVirtualProperty(string name, Func<TModel, IServiceProvider, string> template) {
|
||||
var prop = new PropertyConfig(InnerConfig.Properties.First().Info, InnerConfig, InnerConfig.Properties.Count);
|
||||
prop.Name = name;
|
||||
prop.IsListingProperty = true;
|
||||
prop.Formatter = (obj, provider) => template.Invoke((TModel)obj, provider);
|
||||
InnerConfig.Properties.Add(prop);
|
||||
return new PropertyConfigurator<string>(prop);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a virtual property to the table view and configures it using the provided configurator (this property will not appear in the editor)
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the virtual property</param>
|
||||
/// <param name="template">The template used for generating the property value</param>
|
||||
/// <param name="configurator">Used for configuring the virtual property</param>
|
||||
/// <seealso cref="PropertyConfigurator{TProp}"/>
|
||||
public TableConfigurator<TModel> AddVirtualProperty(string name, Func<TModel, IServiceProvider, string> template, Action<PropertyConfigurator<string>> configurator) {
|
||||
var prop = AddVirtualProperty(name, template);
|
||||
configurator.Invoke(prop);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the name for the table used in the admin ui and url for the table page
|
||||
/// </summary>
|
||||
public TableConfigurator<TModel> SetDisplayName(string name) {
|
||||
InnerConfig.DisplayName = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the description displayed in the admin ui
|
||||
/// </summary>
|
||||
public TableConfigurator<TModel> SetDescription(string description) {
|
||||
InnerConfig.Description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the order index for the table in the admin ui
|
||||
/// </summary>
|
||||
/// <seealso cref="PropertyConfigurator{TProp}.SetOrderIndex"/>
|
||||
public TableConfigurator<TModel> SetOrderIndex(int index) {
|
||||
InnerConfig.Order = index;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the policy needed by a user in order to view the table
|
||||
/// </summary>
|
||||
public TableConfigurator<TModel> SetViewPolicy(string policy) {
|
||||
InnerConfig.ViewPolicy = policy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the policy needed by a user in order to edit the entries
|
||||
/// </summary>
|
||||
public TableConfigurator<TModel> SetUpdatePolicy(string policy) {
|
||||
InnerConfig.UpdatePolicy = policy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the policy needed by a user in order to create entries
|
||||
/// </summary>
|
||||
public TableConfigurator<TModel> SetCreatePolicy(string policy) {
|
||||
InnerConfig.CreatePolicy = policy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the policy needed by a user in order to delete entries
|
||||
/// </summary>
|
||||
public TableConfigurator<TModel> SetDeletePolicy(string policy) {
|
||||
InnerConfig.DeletePolicy = policy;
|
||||
return this;
|
||||
}
|
||||
|
||||
internal static PropertyInfo GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda) {
|
||||
if (propertyLambda.Body is not MemberExpression member) {
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers to a method, not a property.");
|
||||
}
|
||||
|
||||
if (member.Member is not PropertyInfo propInfo) {
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers to a field, not a property.");
|
||||
}
|
||||
|
||||
Type type = typeof(TSource);
|
||||
if (propInfo.ReflectedType != null && type != propInfo.ReflectedType &&
|
||||
!type.IsSubclassOf(propInfo.ReflectedType)) {
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers to a property that is not from type {type}.");
|
||||
}
|
||||
|
||||
if (propInfo.Name is null)
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers a not existing property.");
|
||||
|
||||
return propInfo;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -7,9 +7,14 @@ namespace HopFrame.Core;
|
||||
|
||||
public static class ServiceCollectionExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// Adds all internal HopFrame services used by the web ui including the default insecure auth handler if not already provided
|
||||
/// </summary>
|
||||
/// <param name="services">The service collection to add the services to</param>
|
||||
/// <returns>The same service collection that is passed in</returns>
|
||||
public static IServiceCollection AddHopFrameServices(this IServiceCollection services) {
|
||||
services.AddTransient<IContextExplorer, ContextExplorer>();
|
||||
services.TryAddTransient<IHopFrameAuthHandler, DefaultAuthHandler>();
|
||||
services.AddScoped<IContextExplorer, ContextExplorer>();
|
||||
services.TryAddScoped<IHopFrameAuthHandler, DefaultAuthHandler>();
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,24 @@
|
||||
namespace HopFrame.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// This handler is used by the HopFrame for authenticating the user that wants to access the admin ui.
|
||||
/// By default, everyone is allowed to access the admin ui, but you can implement and register this interface
|
||||
/// yourself as a scoped dependency if you only want some users to access the admin ui (recommended)
|
||||
/// </summary>
|
||||
public interface IHopFrameAuthHandler {
|
||||
|
||||
/// <summary>
|
||||
/// Gets called by the admin ui in order to verify if the user has all necessary policies to perform an action
|
||||
/// </summary>
|
||||
/// <param name="policy">If specified, the policy the user needs to perform the action</param>
|
||||
/// <returns>True: The user is permitted to perform the action<br /> False: The user is not permitted to perform the action</returns>
|
||||
public Task<bool> IsAuthenticatedAsync(string? policy);
|
||||
|
||||
/// <summary>
|
||||
/// If enabled, this method is used to get the name of the currently logged-in user in order to display it in the admin ui
|
||||
/// </summary>
|
||||
/// <returns>The display name of the currently logged-in user</returns>
|
||||
/// <seealso cref="HopFrame.Core.Config.HopFrameConfigurator.DisplayUserInfo"/>
|
||||
public Task<string> GetCurrentUserDisplayNameAsync();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user