-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
342 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
NeuroSpeech.EntityAccessControl.Tests/Model/Events/PostActivityEvents.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using NeuroSpeech.EntityAccessControl.Security; | ||
using System.Reflection; | ||
|
||
namespace NeuroSpeech.EntityAccessControl.Tests.Model.Events | ||
{ | ||
internal class PostActivityEvents : AppEntityEvents<PostActivity> | ||
{ | ||
public PostActivityEvents(AppDbContext db) : base(db) | ||
{ | ||
} | ||
|
||
public override IQueryContext<PostActivity> Filter(IQueryContext<PostActivity> q) | ||
{ | ||
return q.Where(x => x.AccountID == db.UserID); | ||
} | ||
|
||
protected override IQueryContext ForeignKeyFilter( | ||
ForeignKeyInfo<PostActivity> fk) | ||
{ | ||
if(fk.Is(x => x.PostID)) | ||
{ | ||
return null; | ||
} | ||
return base.ForeignKeyFilter(fk); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
NeuroSpeech.EntityAccessControl/Security/FilterFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace NeuroSpeech.EntityAccessControl.Security | ||
{ | ||
public readonly ref struct ForeignKeyInfo<T> | ||
where T : class | ||
{ | ||
public readonly EntityEntry<T> Entry; | ||
public readonly PropertyInfo Property; | ||
public readonly object Value; | ||
private readonly FilterFactory factory; | ||
public readonly string Name; | ||
|
||
public ForeignKeyInfo( | ||
EntityEntry<T> entry, | ||
PropertyInfo property, | ||
object value, | ||
FilterFactory factory) | ||
{ | ||
this.Entry = entry; | ||
this.Property = property; | ||
this.Value = value; | ||
this.factory = factory; | ||
this.Name = property.Name; | ||
} | ||
|
||
public bool Is(string name) | ||
{ | ||
return this.Name == name; | ||
} | ||
|
||
public bool Is<TR>(Expression<Func<T,TR>> exp) | ||
{ | ||
if (exp.Body is MemberExpression me) | ||
{ | ||
if(me.Member is PropertyInfo mp) | ||
{ | ||
if (mp == this.Property) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public IQueryContext Set() | ||
{ | ||
return factory.Set(); | ||
} | ||
|
||
public IQueryContext Filtered() | ||
{ | ||
return factory.Filtered(); | ||
} | ||
|
||
|
||
public IQueryContext<TEntity> Set<TEntity>() | ||
{ | ||
return factory.Set<TEntity>(); | ||
} | ||
|
||
public IQueryContext<TEntity> Filtered<TEntity>() | ||
{ | ||
return factory.Filtered<TEntity>(); | ||
} | ||
} | ||
|
||
public readonly struct FilterFactory | ||
{ | ||
private readonly Func<IQueryContext> filteredSet; | ||
private readonly Func<IQueryContext> set; | ||
|
||
internal static FilterFactory From<T>(ISecureQueryProvider db, IQueryContext<T> feqc) | ||
where T: class | ||
{ | ||
var qc = () => feqc ?? new QueryContext<T>(db, db.Set<T>()); | ||
var fqc = () => | ||
{ | ||
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); | ||
} | ||
|
||
internal FilterFactory(Func<IQueryContext> filteredSet, Func<IQueryContext> set) | ||
{ | ||
this.filteredSet = filteredSet; | ||
this.set = set; | ||
} | ||
|
||
|
||
public IQueryContext Filtered() | ||
{ | ||
return this.filteredSet(); | ||
} | ||
|
||
public IQueryContext Set() | ||
{ | ||
return this.set(); | ||
} | ||
|
||
public IQueryContext<T> Filtered<T>() | ||
{ | ||
return (this.filteredSet() as IQueryContext<T>)!; | ||
} | ||
|
||
public IQueryContext<T> Set<T>() | ||
{ | ||
return (this.set() as IQueryContext<T>)!; | ||
} | ||
} | ||
} |
Oops, something went wrong.