Skip to content

Commit

Permalink
Add support for LogicOptions for UniqueIdentity Strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Jan 3, 2024
1 parent 7f2ba73 commit 490e20e
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public async Task Invoke(HttpContext httpContext, IUnitOfWork baseUnitOfWork)
httpContext.Response.StatusCode = (int)HttpStatusCode.OK;
MessageContract response = FailedReasonType.SessionAccessDenied;
response.Error.ServiceDetails.MethodName = httpContext.Request.Path.ToString();
response.Error.Details = $"StatusCode: {httpContext.Response.StatusCode}";
var json = JsonSerializer.Serialize(response);
await httpContext.Response.WriteAsync(json);
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace EasyMicroservices.Cores.DataTypes;
/// <summary>
///
/// </summary>
public enum UniqueIdentityStrategy : byte
{
/// <summary>
/// value is none, Never use the None to return values
/// </summary>
None = 0,
/// <summary>
/// error value is default
/// </summary>
Default = 1,
/// <summary>
/// for the filter values from web admin panel you can sent all for types
/// </summary>
All = 2,
/// <summary>
/// there is other error that is not in the types
/// </summary>
Other = 3,
/// <summary>
/// the error type is uknown to us
/// </summary>
Unknown = 4,
/// <summary>
/// there is nothing to show or validate error
/// </summary>
Nothing = 5,
/// <summary>
///
/// </summary>
Full = 6,
/// <summary>
///
/// </summary>
BusinessTwoSegment = 7,
/// <summary>
///
/// </summary>
UserFourSegment = 8,
/// <summary>
///
/// </summary>
ObjectSixSegment = 9
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using EasyMicroservices.Cores.DataTypes;
using System;

namespace EasyMicroservices.Cores.Models;
/// <summary>
///
/// </summary>
public struct LogicOptions
{
/// <summary>
///
/// </summary>
public LogicOptions()
{

}
/// <summary>
///
/// </summary>
public UniqueIdentityStrategy UniqueIdentityStrategy { get; set; } = UniqueIdentityStrategy.Default;
/// <summary>
///
/// </summary>
/// <param name="uniqueIdentityStrategy"></param>

public static implicit operator LogicOptions(UniqueIdentityStrategy uniqueIdentityStrategy)
{
return new LogicOptions()
{
UniqueIdentityStrategy = uniqueIdentityStrategy
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using EasyMicroservices.Cores.Database.Managers;
using EasyMicroservices.Cores.DataTypes;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Cores.Models;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.Mapper.Interfaces;
using EasyMicroservices.ServiceContracts;
Expand All @@ -29,12 +30,15 @@ public class DatabaseLogicInfrastructure : IDisposable
/// </summary>
internal protected readonly IMapperProvider MapperProvider;
readonly IBaseUnitOfWork _baseUnitOfWork;
readonly LogicOptions _logicOptions;
/// <summary>
///
/// </summary>
/// <param name="baseUnitOfWork"></param>
public DatabaseLogicInfrastructure(IBaseUnitOfWork baseUnitOfWork)
/// <param name="logicOptions"></param>
public DatabaseLogicInfrastructure(IBaseUnitOfWork baseUnitOfWork, LogicOptions logicOptions = default)
{
_logicOptions = logicOptions;
_baseUnitOfWork = baseUnitOfWork;
MapperProvider = baseUnitOfWork.GetMapper();
}
Expand All @@ -61,7 +65,7 @@ private async Task<MessageContract> CheckUniqueIdentityAccess(params IEntityEntr
{
bool hasUniqueIdentityRole = await _baseUnitOfWork.HasUniqueIdentityRole();

var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity();
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(_logicOptions);
foreach (var item in items)
{
if (item.Entity is IUniqueIdentitySchema uniqueIdentitySchema)
Expand All @@ -83,13 +87,15 @@ private async Task<MessageContract> CheckUniqueIdentityAccess(params IEntityEntr
private async Task<MessageContract> HasUniqueIdentityPermission<TEntity>(string uniqueIdentity)
where TEntity : class
{
if (_logicOptions.UniqueIdentityStrategy == UniqueIdentityStrategy.Full)
return true;
bool hasUniqueIdentityRole = await _baseUnitOfWork.HasUniqueIdentityRole();
if (hasUniqueIdentityRole)
return true;
if (!typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity)))
return (FailedReasonType.AccessDenied, $"type of {typeof(TEntity)} is not inheritance from IUniqueIdentitySchema and user has no UniqueIdentityRole access!");

var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity();
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(_logicOptions);
if (uniqueIdentity.IsNullOrEmpty() && !hasUniqueIdentityRole)
return (FailedReasonType.AccessDenied, $"With the UniqueIdentity: {currentUserUniqueIdentity} you have not access, please send me your UniqueIdentity!");
else if (!uniqueIdentity.StartsWith(currentUserUniqueIdentity))
Expand All @@ -104,7 +110,7 @@ private async Task<IEasyReadableQueryableAsync<TEntity>> UniqueIdentityQueryMake
if (uniqueIdentity.IsNullOrEmpty())
return easyReadableQueryable;
var uniqueIdentityManager = await GetIUniqueIdentityManager();
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity();
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(_logicOptions);

IEasyReadableQueryableAsync<TEntity> queryable = easyReadableQueryable;
if (!uniqueIdentityManager.IsUniqueIdentityForThisTable<TEntity>(easyReadableQueryable.Context, uniqueIdentity))
Expand Down Expand Up @@ -141,7 +147,7 @@ private async Task<IEasyReadableQueryableAsync<TEntity>> SetTheUserUniqueIdentit
var uniqueIdentityPermission = await HasUniqueIdentityPermission<TEntity>(null);
string uniqueIdentity = default;
if (!uniqueIdentityPermission)
uniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity();
uniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(_logicOptions);
return await UniqueIdentityQueryMaker(easyReadableQueryable, uniqueIdentity, GetUniqueIdentityType.All);
}

Expand Down Expand Up @@ -935,7 +941,7 @@ public async Task<MessageContract<TEntity>> Add<TEntity>(IEasyWritableQueryableA
await easyWritableQueryable.SaveChangesAsync();
if (typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity)))
{
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity();
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(_logicOptions);
var uniqueIdentityManager = await GetIUniqueIdentityManager();
if (uniqueIdentityManager.UpdateUniqueIdentity(currentUserUniqueIdentity, easyWritableQueryable.Context, result.Entity))
{
Expand Down Expand Up @@ -977,7 +983,7 @@ public async Task<ListMessageContract<TEntity>> AddBulk<TEntity>(IEasyWritableQu
bool anyUpdate = false;
if (typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity)))
{
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity();
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(_logicOptions);
var uniqueIdentityManager = await GetIUniqueIdentityManager();
foreach (var item in result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.DataTypes;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Cores.Models;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.ServiceContracts;
using System;
Expand Down Expand Up @@ -32,7 +33,8 @@ public class IdSchemaDatabaseMappedLogicBase<TEntity, TCreateRequestContract, TU
/// </summary>
/// <param name="easyReadableQueryable"></param>
/// <param name="baseUnitOfWork"></param>
public IdSchemaDatabaseMappedLogicBase(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IBaseUnitOfWork baseUnitOfWork) : base(baseUnitOfWork)
/// <param name="logicOptions"></param>
public IdSchemaDatabaseMappedLogicBase(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IBaseUnitOfWork baseUnitOfWork, LogicOptions logicOptions = default) : base(baseUnitOfWork, logicOptions)
{
_easyReadableQueryable = easyReadableQueryable;
}
Expand All @@ -42,7 +44,8 @@ public IdSchemaDatabaseMappedLogicBase(IEasyReadableQueryableAsync<TEntity> easy
/// </summary>
/// <param name="easyWriteableQueryable"></param>
/// <param name="baseUnitOfWork"></param>
public IdSchemaDatabaseMappedLogicBase(IEasyWritableQueryableAsync<TEntity> easyWriteableQueryable, IBaseUnitOfWork baseUnitOfWork) : base(baseUnitOfWork)
/// <param name="logicOptions"></param>
public IdSchemaDatabaseMappedLogicBase(IEasyWritableQueryableAsync<TEntity> easyWriteableQueryable, IBaseUnitOfWork baseUnitOfWork, LogicOptions logicOptions = default) : base(baseUnitOfWork, logicOptions)
{
_easyWriteableQueryable = easyWriteableQueryable;
}
Expand All @@ -53,7 +56,8 @@ public IdSchemaDatabaseMappedLogicBase(IEasyWritableQueryableAsync<TEntity> easy
/// <param name="easyReadableQueryable"></param>
/// <param name="easyWriteableQueryable"></param>
/// <param name="baseUnitOfWork"></param>
public IdSchemaDatabaseMappedLogicBase(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IEasyWritableQueryableAsync<TEntity> easyWriteableQueryable, IBaseUnitOfWork baseUnitOfWork) : base(baseUnitOfWork)
/// <param name="logicOptions"></param>
public IdSchemaDatabaseMappedLogicBase(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IEasyWritableQueryableAsync<TEntity> easyWriteableQueryable, IBaseUnitOfWork baseUnitOfWork, LogicOptions logicOptions = default) : base(baseUnitOfWork, logicOptions)
{
_easyWriteableQueryable = easyWriteableQueryable;
_easyReadableQueryable = easyReadableQueryable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Cores.Models;
using EasyMicroservices.Database.Interfaces;

namespace EasyMicroservices.Cores.Database.Logics
Expand All @@ -19,7 +20,8 @@ public class LongIdMappedDatabaseLogicBase<TEntity, TCreateRequestContract, TUpd
/// </summary>
/// <param name="easyReadableQueryable"></param>
/// <param name="baseUnitOfWork"></param>
public LongIdMappedDatabaseLogicBase(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IBaseUnitOfWork baseUnitOfWork) : base(easyReadableQueryable, baseUnitOfWork)
/// <param name="logicOptions"></param>
public LongIdMappedDatabaseLogicBase(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IBaseUnitOfWork baseUnitOfWork, LogicOptions logicOptions = default) : base(easyReadableQueryable, baseUnitOfWork, logicOptions)
{
}

Expand All @@ -28,7 +30,8 @@ public LongIdMappedDatabaseLogicBase(IEasyReadableQueryableAsync<TEntity> easyRe
/// </summary>
/// <param name="easyWriteableQueryable"></param>
/// <param name="baseUnitOfWork"></param>
public LongIdMappedDatabaseLogicBase(IEasyWritableQueryableAsync<TEntity> easyWriteableQueryable, IBaseUnitOfWork baseUnitOfWork) : base(easyWriteableQueryable, baseUnitOfWork)
/// <param name="logicOptions"></param>
public LongIdMappedDatabaseLogicBase(IEasyWritableQueryableAsync<TEntity> easyWriteableQueryable, IBaseUnitOfWork baseUnitOfWork, LogicOptions logicOptions = default) : base(easyWriteableQueryable, baseUnitOfWork, logicOptions)
{
}

Expand All @@ -38,7 +41,8 @@ public LongIdMappedDatabaseLogicBase(IEasyWritableQueryableAsync<TEntity> easyWr
/// <param name="easyReadableQueryable"></param>
/// <param name="easyWriteableQueryable"></param>
/// <param name="baseUnitOfWork"></param>
public LongIdMappedDatabaseLogicBase(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IEasyWritableQueryableAsync<TEntity> easyWriteableQueryable, IBaseUnitOfWork baseUnitOfWork) : base(easyReadableQueryable, easyWriteableQueryable, baseUnitOfWork)
/// <param name="logicOptions"></param>
public LongIdMappedDatabaseLogicBase(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IEasyWritableQueryableAsync<TEntity> easyWriteableQueryable, IBaseUnitOfWork baseUnitOfWork, LogicOptions logicOptions = default) : base(easyReadableQueryable, easyWriteableQueryable, baseUnitOfWork, logicOptions)
{

}
Expand Down
Loading

0 comments on commit 490e20e

Please sign in to comment.