Skip to content

Commit

Permalink
Filter factory fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Nov 3, 2022
1 parent 90e0074 commit 8f2acb6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
76 changes: 39 additions & 37 deletions NeuroSpeech.EntityAccessControl/Security/FilterFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;
using NeuroSpeech.EntityAccessControl.Internal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -51,75 +53,75 @@ public bool Is<TR>(Expression<Func<T,TR>> exp)
return false;
}

public IQueryContext Set()
{
return factory.Set();
}

public IQueryContext Filtered()
{
return factory.Filtered();
}


public IQueryContext<TEntity> Set<TEntity>()
where TEntity: class
{
return factory.Set<TEntity>();
}

public IQueryContext<TEntity> Filtered<TEntity>()
where TEntity : class
{
return factory.Filtered<TEntity>();
}

public IQueryContext Filtered()
{
return FilterFactoryHelper.Instance.Filtered(factory);
}
}

public readonly struct FilterFactory
public class FilterFactoryHelper
{
private readonly Func<IQueryContext> filteredSet;
private readonly Func<IQueryContext> set;

internal static FilterFactory From<T>(ISecureQueryProvider db)
where T: class
public static FilterFactoryHelper Instance = new FilterFactoryHelper();

public IQueryContext Filtered(FilterFactory factory)
{
var qc = () => new QueryContext<T>(db, db.Set<T>());
var fqc = () =>
{
var feqc = new QueryContext<T>(db, db.Set<T>());
var eh = db.GetEntityEvents(typeof(T));
if (eh == null)
{
throw new EntityAccessException($"Access to {typeof(T).Name} denied");
}
return eh.ModifyFilter(feqc);
};
return new FilterFactory(fqc, qc);
return this.GetInstanceGenericMethod(nameof(FilteredSet), factory.fkPrimaryEntityType)
.As<IQueryContext>()
.Invoke(factory);
}

internal FilterFactory(Func<IQueryContext> filteredSet, Func<IQueryContext> set)
public IQueryContext FilteredSet<T>(FilterFactory factory)
where T: class
{
this.filteredSet = filteredSet;
this.set = set;
return factory.Filtered<T>();
}
}

public readonly struct FilterFactory
{
private readonly ISecureQueryProvider db;
internal readonly Type fkPrimaryEntityType;

public IQueryContext Filtered()
internal static FilterFactory From(ISecureQueryProvider db, Type fkPrimaryEntityType)
{
return this.filteredSet();
return new FilterFactory(db, fkPrimaryEntityType);
}

public IQueryContext Set()
internal FilterFactory(ISecureQueryProvider db, Type fkPrimaryEntityType)
{
return this.set();
this.db = db;
this.fkPrimaryEntityType = fkPrimaryEntityType;
}

public IQueryContext<T> Filtered<T>()
where T: class
{
return (this.filteredSet() as IQueryContext<T>)!;
var feqc = new QueryContext<T>(db, db.Set<T>());
var eh = db.GetEntityEvents(typeof(T));
if (eh == null)
{
throw new EntityAccessException($"Access to {typeof(T).Name} denied");
}
return (eh.ModifyFilter(feqc) as IQueryContext<T>)!;
}

public IQueryContext<T> Set<T>()
where T: class
{
return (this.set() as IQueryContext<T>)!;
return new QueryContext<T>(db, db.Set<T>());
}
}
}
6 changes: 3 additions & 3 deletions NeuroSpeech.EntityAccessControl/VerificationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private IQueryContext<T> ApplyFilter<T>(
}

private IQueryContext<T>? ApplyFKFilter<TP, T>(
EntityEntry entry, object? value,PropertyInfo fkProperty)
EntityEntry entry, PropertyInfo key, object? value,PropertyInfo fkProperty)
where T : class
where TP: class
{
Expand All @@ -146,7 +146,7 @@ private IQueryContext<T> ApplyFilter<T>(
{
case EntityState.Modified:
case EntityState.Added:
var fs = FilterFactory.From<T>(db);
var fs = FilterFactory.From(db, key.DeclaringType);
return (IQueryContext<T>?)eh.ForeignKeyFilter(entry, fkProperty, value, fs);
}
return null;
Expand Down Expand Up @@ -288,7 +288,7 @@ public int QueueEntityKeyForeignKey<TP, T>(
{
foreach(var (key,value, fk) in keys)
{
var fkc = ApplyFKFilter<TP, T>(e, value, fk);
var fkc = ApplyFKFilter<TP, T>(e, key, value, fk);
if (fkc != null)
{
var fkq = fkc.ToQuery();
Expand Down

0 comments on commit 8f2acb6

Please sign in to comment.