Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
3.2.0
Browse files Browse the repository at this point in the history
Added encryption support
  • Loading branch information
DefGh committed Oct 30, 2023
1 parent bb892a0 commit 6bcac8c
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 47 deletions.
3 changes: 3 additions & 0 deletions IEnumerableExtenders.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;&#xD;
&lt;Assembly Path="C:\Users\defgh\.nuget\packages\pandatech.crypto\2.0.0\lib\net7.0\Pandatech.Crypto.dll" /&gt;&#xD;
&lt;/AssemblyExplorer&gt;</s:String>
<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean></wpf:ResourceDictionary>
63 changes: 52 additions & 11 deletions IEnumerableExtenders/EnumerableExtendersV3.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using PandaTech.IEnumerableFilters.Attributes;
using PandaTech.IEnumerableFilters.Dto;
using PandaTech.IEnumerableFilters.Exceptions;
using PandaTech.IEnumerableFilters.Helpers;
using PandaTech.IEnumerableFilters.PostgresContext;
using static System.Linq.Expressions.Expression;

namespace PandaTech.IEnumerableFilters;
Expand All @@ -24,7 +26,7 @@ public static class EnumerableExtendersV3
ComparisonTypesDefault.ByteArray => DefaultComparisonTypes.ByteArray,
_ => null
};

private static ComparisonTypesDefault GetComparisonTypesDefault(Type type)
{
if (type == typeof(int) || type == typeof(long) || type == typeof(decimal) || type == typeof(double) ||
Expand Down Expand Up @@ -123,6 +125,39 @@ public static IQueryable<TModel> ApplyFilters<TModel, TDto>(this IQueryable<TMod
}
}


if (filter.Attribute.Encrypted)
{
if (filterDto.ComparisonType != ComparisonType.Equal &&
filterDto.ComparisonType != ComparisonType.In)
throw new ComparisonNotSupportedException(
$"Comparison type {filterDto.ComparisonType} not supported for encrypted property");


// var hashes = filterDto.Values.Select(x => Pandatech.Crypto.Sha3.Hash(x.ToString()!)).ToList();
//.Where(x => hashes.Contains(PostgresDbContext.substr(x.SomeBytes, 1, 64)));

var parameter = Parameter(typeof(TModel));
var property = Property(parameter, targetProperty);

var hashes = filterDto.Values.Select(x => Pandatech.Crypto.Sha3.Hash(x.ToString()!)).ToList();

var substrMethod = typeof(PostgresDbContext).GetMethod("substr", new[] {typeof(byte[]), typeof(int), typeof(int)});

var substrExpression = Call(substrMethod!, property, Constant(1), Constant(64));

var containsMethod = typeof(List<byte[]>).GetMethod("Contains", new[] {typeof(byte[])});

var containsExpression = Call(Constant(hashes), containsMethod!, substrExpression);

var lambda = Lambda<Func<TModel, bool>>(containsExpression, parameter);

q = q.Where(lambda);

return q;
}


var converter =
Activator.CreateInstance(filter.Attribute.ConverterType ?? typeof(DirectConverter));

Expand Down Expand Up @@ -210,11 +245,14 @@ public static DistinctColumnValuesResult DistinctColumnValues<T, TDto>(this IQue
{
x.Name,
Attribute = x.GetCustomAttribute<MappedToPropertyAttribute>()!,
Type = x.PropertyType
Type = x.PropertyType,
}).ToDictionary(x => x.Name, x => new { x.Attribute, x.Type });

var filter = mappedProperties[columnName];

if (filter.Attribute.Encrypted)
throw new Exception("Encrypted column not supported");

var targetProperty = typeof(T).GetProperty(filter.Attribute.TargetPropertyName);
if (targetProperty is null)
throw new PropertyNotFoundException(
Expand Down Expand Up @@ -286,7 +324,10 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
}).ToDictionary(x => x.Name, x => new { x.Attribute, x.Type });

var filter = mappedProperties[columnName];


if (filter.Attribute.Encrypted)
throw new Exception("Encrypted column not supported");

var targetProperty = typeof(Tmodel).GetProperty(filter.Attribute.TargetPropertyName);
if (targetProperty is null)
throw new PropertyNotFoundException(
Expand Down Expand Up @@ -371,14 +412,14 @@ public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
PropertyName = x.Name,
Table = tableName,
ComparisonTypes = (x.GetCustomAttribute<MappedToPropertyAttribute>()!.ComparisonTypes ??
ComparisonTypes(GetComparisonTypesDefault(x.PropertyType))
?? new[]
{
ComparisonType.Equal,
ComparisonType.NotEqual,
ComparisonType.In,
ComparisonType.NotIn
}).ToList()
ComparisonTypes(GetComparisonTypesDefault(x.PropertyType))
?? new[]
{
ComparisonType.Equal,
ComparisonType.NotEqual,
ComparisonType.In,
ComparisonType.NotIn
}).ToList()
}
);

Expand Down
4 changes: 3 additions & 1 deletion IEnumerableExtenders/IEnumerableExtenders.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>PandaTech.IEnumerableFilters</RootNamespace>
<Version>3.1.0</Version>
<Version>3.2.0</Version>
<Authors>PandaTech</Authors>
<Description>This NuGet helps with filtering tables.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-ienumerable-extenders-filters.git</RepositoryUrl>
Expand All @@ -15,6 +15,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.5" />
<PackageReference Include="PandaTech.BaseConverter" Version="1.0.12" />
<PackageReference Include="Pandatech.Crypto" Version="2.0.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.5" />
</ItemGroup>

Expand Down
23 changes: 23 additions & 0 deletions IEnumerableExtenders/PostgresContext/PostgresDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;

namespace PandaTech.IEnumerableFilters.PostgresContext;

public abstract class PostgresDbContext: DbContext
{
protected PostgresDbContext(DbContextOptions options): base(options)
{
}

[DbFunction("substr", IsBuiltIn = true)]
public static byte[] substr(byte[] target, int start, int count)
{
throw new Exception();
}

[DbFunction("rtrim", Schema = "public")]
public static byte[] rtrim(byte[] from, byte[] remove)
{
throw new NotSupportedException();
}

}
3 changes: 2 additions & 1 deletion TestFilters/Controllers/Context.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Microsoft.EntityFrameworkCore;
using PandaTech.IEnumerableFilters;
using PandaTech.IEnumerableFilters.Dto;
using PandaTech.IEnumerableFilters.PostgresContext;
using TestFilters.Controllers.Models;

namespace TestFilters.Controllers;

public class Context : DbContext
public class Context : PostgresDbContext
{
public virtual DbSet<Person> Persons { get; set; } = null!;
public virtual DbSet<Cat> Cats { get; set; } = null!;
Expand Down
2 changes: 2 additions & 0 deletions TestFilters/Controllers/Models/Cat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class Cat
public string Name { get; set; } = null!;
public int Age { get; set; }

public byte[] SomeBytes { get; set; } = null!;

public override string ToString() => $"Cat {Name} {Age} {Id}";

}
3 changes: 3 additions & 0 deletions TestFilters/Controllers/Models/CatDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class CatDto

[MappedToProperty(nameof(Cat.Age))]
public int Age { get; set; }

[MappedToProperty(nameof(Cat.SomeBytes), Encrypted = true)]
public string EncryptedString { get; set; } = null!;
}

public class PandaFilterBaseConverter : IConverter<string, long>
Expand Down
40 changes: 19 additions & 21 deletions TestFilters/Controllers/SomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using PandaTech.IEnumerableFilters;
using PandaTech.IEnumerableFilters.Attributes;
using PandaTech.IEnumerableFilters.Dto;
using PandaTech.Mapper;
using PandaTech.IEnumerableFilters.PostgresContext;
using TestFilters.Controllers.bulk;
using TestFilters.Controllers.Models;
using JsonSerializer = System.Text.Json.JsonSerializer;
Expand All @@ -16,24 +16,13 @@ namespace TestFilters.Controllers;
public class SomeController : ControllerBase
{
private readonly Context _context;
private readonly Counter _counter;
private readonly UpCounter2 _upCounter2;
private readonly UpCounter _upCounter;

private readonly IServiceProvider _serviceProvider;
private readonly HttpClient _client;
private readonly IMapping<Person, PersonDto> _personDtoMapper;

public SomeController(Context context, Counter counter, UpCounter2 upCounter2, UpCounter upCounter,
IServiceProvider serviceProvider, HttpClient client, IMapping<Person, PersonDto> personDtoMapper
)
public SomeController(Context context, IServiceProvider serviceProvider)
{
_context = context;
_counter = counter;
_upCounter2 = upCounter2;
_upCounter = upCounter;
_serviceProvider = serviceProvider;
_client = client;
_personDtoMapper = personDtoMapper;
}

[HttpGet("[action]/{tableName}")]
Expand Down Expand Up @@ -166,11 +155,14 @@ public IActionResult PopulateDb()

for (var j = 0; j < catCount; j++)
{
var name = NameProvider.GetRandomName();

person.Cats.Add(new Cat
{
Id = catId++,
Name = NameProvider.GetRandomName(),
Name = name,
Age = Random.Shared.Next(1, 20),
SomeBytes = Pandatech.Crypto.Aes256.EncryptWithHash(name)
});
}

Expand Down Expand Up @@ -246,17 +238,23 @@ public async Task<FilteredDataResult<Person>> GetPersons([FromBody] GetDataReque
[HttpPost("GetCats")]
public async Task<FilteredDataResult<CatDto>> GetCats([FromBody] GetDataRequest request, int page, int pageSize)
{
var query = _context.Cats.ApplyFilters<Cat, CatDto>(request.Filters)
.ApplyOrdering<Cat, CatDto>(request.Order);
//var hash = Pandatech.Crypto.Sha3.Hash(test);

var query = _context.Cats
.ApplyFilters<Cat, CatDto>(request.Filters)
.ApplyOrdering<Cat, CatDto>(request.Order)
;//.Where(x => PostgresDbContext.substr(x.SomeBytes, 1, 64) == hash);

var count = await query.LongCountAsync();
var data = await query.Skip((page - 1) * pageSize).Take(pageSize)
.Select(x => new CatDto { Id = x.Id, Name = x.Name, Age = x.Age }).ToListAsync();

.Select(x => new CatDto
{
Id = x.Id, Name = x.Name, Age = x.Age, EncryptedString = Pandatech.Crypto.Aes256.DecryptIgnoringHash(x.SomeBytes)
}).ToListAsync();

var aggregates = await query.GetAggregatesAsync<Cat, CatDto>(request.Aggregates);


return new FilteredDataResult<CatDto>()
return new FilteredDataResult<CatDto>
{
Data = data,
TotalCount = count,
Expand Down
13 changes: 3 additions & 10 deletions TestFilters/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

var builder = WebApplication.CreateBuilder(args);


// Add services to the container.

builder.Services.AddControllers();
Expand All @@ -19,25 +20,17 @@
builder.Services.AddDbContext<Context>(
optionsBuilder =>
optionsBuilder.UseNpgsql("Server=127.0.0.1;Database=xyz;Username=postgres;Password=example")
// .UseSnakeCaseNamingConvention()
// .UseSnakeCaseNamingConvention()
, ServiceLifetime.Scoped
);

#region Mappers

builder.Services.AddScoped<FilterProvider>();
builder.Services.AddScoped<IMapping<Person, PersonDto>, PersonDtoMapper>();

builder.Services.AddSingleton<Counter>();

builder.Services.AddScoped<UpCounter2>();
builder.Services.AddScoped<FilterProvider>();
builder.Services.AddScoped<UpCounter>();

builder.Services.AddHttpClient();

#endregion

builder.Services.AddHttpClient();

var app = builder.Build();

Expand Down
3 changes: 2 additions & 1 deletion TestFilters/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"launchUrl": "swagger",
"applicationUrl": "https://*:444;http://*:80",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"AES_KEY": "M5pfvJCKBwpJdA7YfeX3AkAKJmfBf4piybEPDtWKWw4="
}
}
}
Expand Down
1 change: 0 additions & 1 deletion TestFilters/TestFilters.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
<PackageReference Include="PandaTech.BaseConverter" Version="1.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.5" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="PandaTech.BaseConverter" Version="1.0.7" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.3.0" />
<PackageReference Include="Testcontainers.RabbitMq" Version="3.3.0" />
</ItemGroup>
Expand Down

0 comments on commit 6bcac8c

Please sign in to comment.