diff --git a/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Security/UniqueIdentityTests.cs b/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Security/UniqueIdentityTests.cs index ab3e118..f0a4fec 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Security/UniqueIdentityTests.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Security/UniqueIdentityTests.cs @@ -54,14 +54,15 @@ public async Task AddAsync(string roleName, string controller, string meth } [Theory] - [InlineData("Owner", "User", "Update", "1-2", "1-2", "{}", true)] - [InlineData("Owner", "User", "Update", "1-2", "1-2", @"{""UniqueIdentity"":""1-2-3-4""}", true)] - [InlineData("Owner", "User", "Update", "1-2", "1-2", @"{""UniqueIdentity"":""3-4""}", false)] - [InlineData("Moderator", "User", "Update", "1-2", "1-2", @"{""UniqueIdentity"":""1-2""}", true)] - public async Task UpdateAsync(string roleName, string controller, string method, string fromUniqueIdentity, string toUniqueIdentity, string data, bool canHaveAccess) + [InlineData("Owner", "User", "Update", "1-2", "1-2", "{}", "{}", true)] + [InlineData("Owner", "User", "Update", "1-2", "1-2", @"{""UniqueIdentity"":""1-2-3-4""}", @"{""UniqueIdentity"":""1-2-3-4""}", true)] + [InlineData("Owner", "User", "Update", "1-2", "1-2", @"{""UniqueIdentity"":""3-4""}", @"{""UniqueIdentity"":""3-4""}", true)] + [InlineData("Moderator", "User", "Update", "1-2", "1-2", @"{""UniqueIdentity"":""1-2""}", @"{""UniqueIdentity"":""1-2""}", true)] + //[InlineData("Moderator", "User", "Update", "1-2", "1-2", @"{""UniqueIdentity"":""1-2""}", @"{""UniqueIdentity"":""3-4""}", false)] + public async Task UpdateAsync(string roleName, string controller, string method, string fromUniqueIdentity, string toUniqueIdentity, string addData, string data, bool canHaveAccess) { var model = JsonSerializer.Deserialize(data); - model.Id = await AddAsync(roleName, controller, "Add", fromUniqueIdentity, data, true); + model.Id = await AddAsync(roleName, controller, "Add", fromUniqueIdentity, addData, true); model.UserName = Guid.NewGuid().ToString(); HttpClient currentHttpClient = new HttpClient(); await Login(currentHttpClient, roleName, fromUniqueIdentity); diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj index 11cf092..30b5367 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.92 + 0.0.0.93 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj index b4d3068..26cb5f9 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.92 + 0.0.0.93 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet,aspcore,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj index e0cfaad..9ece0f8 100644 --- a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.92 + 0.0.0.93 core of database. EasyMicroservices@gmail.com core,cores,base,client,clients diff --git a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj index 2cae3d6..1824172 100644 --- a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.92 + 0.0.0.93 core contracts. EasyMicroservices@gmail.com core,cores,base,contract,contracts,dto,dtos diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs index 175f1e1..af7190d 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs @@ -100,22 +100,12 @@ private async Task HasUniqueIdentityPermission(string private async Task> UniqueIdentityQueryMaker(IEasyReadableQueryableAsync easyReadableQueryable, string uniqueIdentity, GetUniqueIdentityType type) where TEntity : class { - bool hasUniqueIdentityRole = await _baseUnitOfWork.HasUniqueIdentityRole(); - if (!typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity))) - { - if (!hasUniqueIdentityRole) - ((MessageContract)(FailedReasonType.AccessDenied, $"type of {typeof(TEntity)} is not inheritance from IUniqueIdentitySchema and user has no UniqueIdentityRole access!")).ThrowsIfFails(); - else - return easyReadableQueryable; - } - var uniqueIdentityManager = await GetIUniqueIdentityManager(); - var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(); + await HasUniqueIdentityPermission(uniqueIdentity).AsCheckedResult(); if (uniqueIdentity.IsNullOrEmpty()) - uniqueIdentity = currentUserUniqueIdentity; - else if (!hasUniqueIdentityRole && DefaultUniqueIdentityManager.CutUniqueIdentityFromEnd(uniqueIdentity, 2) != DefaultUniqueIdentityManager.CutUniqueIdentityFromEnd(currentUserUniqueIdentity, 2)) - ((MessageContract)(FailedReasonType.AccessDenied, "UniqueIdentity access level error!")).ThrowsIfFails(); - if (uniqueIdentity.IsNullOrEmpty() && hasUniqueIdentityRole) return easyReadableQueryable; + var uniqueIdentityManager = await GetIUniqueIdentityManager(); + var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(); + IEasyReadableQueryableAsync queryable = easyReadableQueryable; if (!uniqueIdentityManager.IsUniqueIdentityForThisTable(easyReadableQueryable.Context, uniqueIdentity)) uniqueIdentity += "-"; @@ -148,8 +138,11 @@ private async Task> UniqueIdentityQueryMake private async Task> SetTheUserUniqueIdentityToQuery(IEasyReadableQueryableAsync easyReadableQueryable) where TEntity : class { - var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(); - return await UniqueIdentityQueryMaker(easyReadableQueryable, currentUserUniqueIdentity, GetUniqueIdentityType.All); + var uniqueIdentityPermission = await HasUniqueIdentityPermission(null); + string uniqueIdentity = default; + if (!uniqueIdentityPermission) + uniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(); + return await UniqueIdentityQueryMaker(easyReadableQueryable, uniqueIdentity, GetUniqueIdentityType.All); } #region Get one @@ -226,9 +219,6 @@ public async Task> GetBy(IEasyReadableQueryabl public async Task> GetBy(IEasyReadableQueryableAsync easyReadableQueryable, GetByRequestContract request, Func, IEasyReadableQueryableAsync> query = default, CancellationToken cancellationToken = default) where TEntity : class { - var uniqueIdentityPermission = await HasUniqueIdentityPermission(request.UniqueIdentity); - if (!uniqueIdentityPermission) - return uniqueIdentityPermission.ToContract(); if (!request.Id.Equals(default(TId))) { easyReadableQueryable = easyReadableQueryable.ConvertToReadable(easyReadableQueryable.Where(x => ((IIdSchema)x).Id.Equals(request.Id))); @@ -237,6 +227,12 @@ public async Task> GetBy(IEa { easyReadableQueryable = await UniqueIdentityQueryMaker(easyReadableQueryable, request.UniqueIdentity, request.UniqueIdentityType ?? GetUniqueIdentityType.All); } + else + { + var uniqueIdentityPermission = await HasUniqueIdentityPermission(request.UniqueIdentity); + if (!uniqueIdentityPermission) + return uniqueIdentityPermission.ToContract(); + } var entityResult = await GetBy(easyReadableQueryable, query, false, cancellationToken); if (!entityResult) return entityResult.ToContract(); @@ -352,13 +348,16 @@ public async Task> GetByUniqueIdentity(request.UniqueIdentity); - if (!uniqueIdentityPermission) - return uniqueIdentityPermission.ToContract(); if (request.UniqueIdentity.HasValue() && typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity))) { easyReadableQueryable = await UniqueIdentityQueryMaker(easyReadableQueryable, request.UniqueIdentity, type); } + else + { + var uniqueIdentityPermission = await HasUniqueIdentityPermission(request.UniqueIdentity); + if (!uniqueIdentityPermission) + return uniqueIdentityPermission.ToContract(); + } var entityResult = await GetBy(easyReadableQueryable, query, false, cancellationToken); if (!entityResult) return entityResult.ToContract(); diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs index d1ba905..64ed346 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs @@ -109,76 +109,57 @@ public Task> GetBy(GetByRequestContract return GetBy(_easyReadableQueryable, request, func, cancellationToken); } - TId MapToTId(T result) - { - if (result is IIdSchema schema) - return schema.Id; - else if (result is TId tid) - return tid; - else if ((typeof(TId) == typeof(long) || typeof(TId) == typeof(int)) && result?.GetType() != typeof(string) && (result?.GetType()?.IsClass).GetValueOrDefault()) - throw new Exception($"I think you cannot convert TId from type {typeof(TId).Name} to type {result?.GetType()?.Name}, maybe you don't need to use LongLogic?"); - return MapperProvider.Map(result); - } - /// /// /// - /// + /// /// /// - public async Task> Add(TCreateRequestContract contract, CancellationToken cancellationToken = default) + public Task> GetById(GetByIdRequestContract idRequest, CancellationToken cancellationToken = default) { - var result = await Add(_easyWriteableQueryable, contract, cancellationToken); - if (result) - return MapToTId(result.Result); - return result.ToContract(); + return GetById(_easyReadableQueryable, idRequest, null, cancellationToken); } /// /// /// - /// + /// /// /// /// - public async Task> AddBulk(CreateBulkRequestContract request, CancellationToken cancellationToken = default) + public async Task> GetAll(Func, IQueryable> query = null, CancellationToken cancellationToken = default) { - var result = await AddBulk(_easyWriteableQueryable, request, cancellationToken); - if (result) - { - List items = new List(); - foreach (var item in result.Result) - { - items.Add(MapToTId(item)); - } - return items; - } - return result.ToListContract(); + Func, IEasyReadableQueryableAsync> func = UpdateFunctionQuery(query); + return await GetAll(_easyReadableQueryable, func, cancellationToken); } /// /// /// - /// + /// + /// + /// /// /// /// - public Task> AddEntity(TEntity entity, CancellationToken cancellationToken = default) + public Task> GetAllByUniqueIdentity(IUniqueIdentitySchema request, GetUniqueIdentityType type = GetUniqueIdentityType.All, Func, IQueryable> query = null, CancellationToken cancellationToken = default) { - return Add(_easyWriteableQueryable, entity, cancellationToken); + Func, IEasyReadableQueryableAsync> func = UpdateFunctionQuery(query); + return base.GetAllByUniqueIdentity(_easyReadableQueryable, request, type, func, cancellationToken); } /// /// /// + /// /// + /// /// /// - /// - public async Task> GetAll(Func, IQueryable> query = null, CancellationToken cancellationToken = default) + public Task> GetByUniqueIdentity(IUniqueIdentitySchema request, GetUniqueIdentityType type = GetUniqueIdentityType.All, Func, IQueryable> query = null, CancellationToken cancellationToken = default) { Func, IEasyReadableQueryableAsync> func = UpdateFunctionQuery(query); - return await GetAll(_easyReadableQueryable, func, cancellationToken); + return base.GetByUniqueIdentity(_easyReadableQueryable, request, type, func, cancellationToken); } /// @@ -205,81 +186,84 @@ public Task> Filter(FilterRequestContract return Filter(filterRequest, _easyReadableQueryable, null, cancellationToken); } - /// - /// - /// - /// - /// - /// - public Task> GetById(GetByIdRequestContract idRequest, CancellationToken cancellationToken = default) + TId MapToTId(T result) { - return GetById(_easyReadableQueryable, idRequest, null, cancellationToken); + if (result is IIdSchema schema) + return schema.Id; + else if (result is TId tid) + return tid; + else if ((typeof(TId) == typeof(long) || typeof(TId) == typeof(int)) && result?.GetType() != typeof(string) && (result?.GetType()?.IsClass).GetValueOrDefault()) + throw new Exception($"I think you cannot convert TId from type {typeof(TId).Name} to type {result?.GetType()?.Name}, maybe you don't need to use LongLogic?"); + return MapperProvider.Map(result); } /// /// /// + /// /// /// - public Task SaveChangesAsync(CancellationToken cancellationToken = default) + public async Task> Add(TCreateRequestContract contract, CancellationToken cancellationToken = default) { - return _easyWriteableQueryable.SaveChangesAsync(cancellationToken); + var result = await Add(_easyWriteableQueryable, contract, cancellationToken); + if (result) + return MapToTId(result.Result); + return result.ToContract(); } + /// /// /// - /// + /// /// /// /// - public Task HardDeleteBy(Expression> predicate, CancellationToken cancellationToken = default) - { - return HardDeleteBy(_easyWriteableQueryable, predicate, cancellationToken); - } - /// - /// - /// - /// - /// - /// - public Task> Update(TUpdateRequestContract schema, CancellationToken cancellationToken = default) + public async Task> AddBulk(CreateBulkRequestContract request, CancellationToken cancellationToken = default) { - return Update(_easyWriteableQueryable, schema, false, cancellationToken); + var result = await AddBulk(_easyWriteableQueryable, request, cancellationToken); + if (result) + { + List items = new List(); + foreach (var item in result.Result) + { + items.Add(MapToTId(item)); + } + return items; + } + return result.ToListContract(); } /// /// /// - /// + /// /// /// - public Task> UpdateChangedValuesOnly(TUpdateRequestContract schema, CancellationToken cancellationToken = default) + /// + public Task> AddEntity(TEntity entity, CancellationToken cancellationToken = default) { - return Update(_easyWriteableQueryable, schema, true, cancellationToken); + return Add(_easyWriteableQueryable, entity, cancellationToken); } /// /// /// - /// /// /// - /// - public Task UpdateBulk(UpdateBulkRequestContract schema, CancellationToken cancellationToken = default) + public Task SaveChangesAsync(CancellationToken cancellationToken = default) { - return UpdateBulk(_easyWriteableQueryable, schema, false, cancellationToken); + return _easyWriteableQueryable.SaveChangesAsync(cancellationToken); } - /// /// /// - /// + /// /// /// /// - public Task UpdateBulkChangedValuesOnly(UpdateBulkRequestContract schema, CancellationToken cancellationToken = default) + public Task HardDeleteBy(Expression> predicate, CancellationToken cancellationToken = default) { - return UpdateBulk(_easyWriteableQueryable, schema, true, cancellationToken); + return HardDeleteBy(_easyWriteableQueryable, predicate, cancellationToken); } /// @@ -345,30 +329,47 @@ public Task SoftDeleteBulkByIds(SoftDeleteBulkRequestContract /// /// - /// - /// - /// + /// + /// + /// + public Task> Update(TUpdateRequestContract schema, CancellationToken cancellationToken = default) + { + return Update(_easyWriteableQueryable, schema, false, cancellationToken); + } + + /// + /// + /// + /// + /// + /// + public Task> UpdateChangedValuesOnly(TUpdateRequestContract schema, CancellationToken cancellationToken = default) + { + return Update(_easyWriteableQueryable, schema, true, cancellationToken); + } + + /// + /// + /// + /// /// /// /// - public Task> GetAllByUniqueIdentity(IUniqueIdentitySchema request, GetUniqueIdentityType type = GetUniqueIdentityType.All, Func, IQueryable> query = null, CancellationToken cancellationToken = default) + public Task UpdateBulk(UpdateBulkRequestContract schema, CancellationToken cancellationToken = default) { - Func, IEasyReadableQueryableAsync> func = UpdateFunctionQuery(query); - return base.GetAllByUniqueIdentity(_easyReadableQueryable, request, type, func, cancellationToken); + return UpdateBulk(_easyWriteableQueryable, schema, false, cancellationToken); } /// /// /// - /// - /// - /// + /// /// /// - public Task> GetByUniqueIdentity(IUniqueIdentitySchema request, GetUniqueIdentityType type = GetUniqueIdentityType.All, Func, IQueryable> query = null, CancellationToken cancellationToken = default) + /// + public Task UpdateBulkChangedValuesOnly(UpdateBulkRequestContract schema, CancellationToken cancellationToken = default) { - Func, IEasyReadableQueryableAsync> func = UpdateFunctionQuery(query); - return base.GetByUniqueIdentity(_easyReadableQueryable, request, type, func, cancellationToken); + return UpdateBulk(_easyWriteableQueryable, schema, true, cancellationToken); } /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj index 5744ba6..11b6e5e 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.92 + 0.0.0.93 core of database. EasyMicroservices@gmail.com core,cores,base,database diff --git a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj index d90241d..f7b5bb8 100644 --- a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.92 + 0.0.0.93 ef core of database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj index 5820c17..f1f0383 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.92 + 0.0.0.93 ef core of Relational database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore,Relational