last ...
This commit is contained in:
@@ -0,0 +1,280 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using xIds.DbContext;
|
||||
using xIds.Interfaces;
|
||||
using xCommons.Extensions;
|
||||
using xIds.Configurations;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Linq.Expressions;
|
||||
using IdentityServer4.Extensions;
|
||||
using xIdentityModels.Extensions;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
/// <summary>
|
||||
/// a Service For Manage Security Applications and also Api Key(s) ...
|
||||
/// </summary>
|
||||
public partial class XApplicationProvider : IXApplicationProvider
|
||||
{
|
||||
private readonly string permittedRole;
|
||||
private readonly XIdentityDbContext dbContext;
|
||||
private readonly IXIdentityManager identityManager;
|
||||
private readonly XDataServiceConfiguration configuration;
|
||||
private readonly XApiKeyConfiguration apiKeyConfiguration;
|
||||
|
||||
public XApplicationProvider(
|
||||
XIdentityDbContext dbContext,
|
||||
IXIdentityManager identityManager,
|
||||
XDataServiceConfiguration configuration,
|
||||
XApiKeyConfiguration apiKeyConfiguration,
|
||||
string permittedRole = "admin"
|
||||
)
|
||||
{
|
||||
//
|
||||
this.dbContext = dbContext;
|
||||
this.permittedRole = permittedRole;
|
||||
this.configuration = configuration;
|
||||
this.identityManager = identityManager;
|
||||
this.apiKeyConfiguration = apiKeyConfiguration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Owner ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XPersonDto> GetOwner(string ownerId)
|
||||
{
|
||||
//
|
||||
if (ownerId.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var user = await identityManager.GetUserAsync(ownerId);
|
||||
XPersonDto result = user.ToXPerson();
|
||||
if (result.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Specifed User Info is Owned for Action or not ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsOwned(
|
||||
XApplication source,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!userInfo.IsNullOrDefault() &&
|
||||
(userInfo.UserId == source.OwnerId ||
|
||||
userInfo.Roles
|
||||
.Any(r =>
|
||||
r.ToNormalString() == permittedRole.ToNormalString()));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Specifed User Info is Owned for Action or not ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsOwned(
|
||||
XApplicationDto source,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!userInfo.IsNullOrDefault() &&
|
||||
(userInfo.UserId == source.OwnerId ||
|
||||
userInfo.Roles
|
||||
.Any(r =>
|
||||
r.ToNormalString() == permittedRole.ToNormalString()));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalize an XQuery based on Data Service Configurations ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <returns></returns>
|
||||
public XQuery NormalizeQuery(
|
||||
XQuery source,
|
||||
XDataServiceConfiguration configuration //
|
||||
)
|
||||
{
|
||||
//
|
||||
if (source.IsNull())
|
||||
{
|
||||
source = new XQuery();
|
||||
}
|
||||
|
||||
//
|
||||
// Page Size Normalization ...
|
||||
if (source.PageSize < configuration.PagingConfiguration.MinAvailablePageSize)
|
||||
{
|
||||
source.PageSize = configuration.PagingConfiguration.MinAvailablePageSize;
|
||||
}
|
||||
if (source.PageSize > configuration.PagingConfiguration.MaxAvailablePageSize)
|
||||
{
|
||||
source.PageSize = configuration.PagingConfiguration.MaxAvailablePageSize;
|
||||
}
|
||||
|
||||
//
|
||||
return source;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepare a Queryable of Items ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <returns></returns>
|
||||
public IQueryable<XApplication> GetApplicationsQeryable(
|
||||
Expression<Func<XApplication, bool>> predicate = null,
|
||||
Func<IQueryable<XApplication>, IOrderedQueryable<XApplication>> orderBuilder = null,
|
||||
Func<IQueryable<XApplication>, IIncludableQueryable<XApplication, object>> includeBuilder = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Global Result ...
|
||||
var result = dbContext.Applications.AsQueryable();
|
||||
|
||||
//
|
||||
// Handle Filtering ...
|
||||
if (!predicate.IsNull())
|
||||
{
|
||||
result = result.Where(predicate);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Including ...
|
||||
if (!includeBuilder.IsNull())
|
||||
{
|
||||
result = includeBuilder(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Ordering ...
|
||||
if (!orderBuilder.IsNull())
|
||||
{
|
||||
result = orderBuilder(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Return result ...
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepare a Queryable of Items ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <returns></returns>
|
||||
public IQueryable<XApiKey> GetApiKeysQeryable(
|
||||
Expression<Func<XApiKey, bool>> predicate = null,
|
||||
Func<IQueryable<XApiKey>, IOrderedQueryable<XApiKey>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKey>, IIncludableQueryable<XApiKey, object>> includeBuilder = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Global Result ...
|
||||
var result = dbContext.ApiKeys.AsQueryable();
|
||||
|
||||
//
|
||||
// Handle Filtering ...
|
||||
if (!predicate.IsNull())
|
||||
{
|
||||
result = result.Where(predicate);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Including ...
|
||||
if (!includeBuilder.IsNull())
|
||||
{
|
||||
result = includeBuilder(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Ordering ...
|
||||
if (!orderBuilder.IsNull())
|
||||
{
|
||||
result = orderBuilder(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Return result ...
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepare a Queryable of Items ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <returns></returns>
|
||||
public IQueryable<XApiKeyUsage> GetApiKeyUsagesQeryable(
|
||||
Expression<Func<XApiKeyUsage, bool>> predicate = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IOrderedQueryable<XApiKeyUsage>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IIncludableQueryable<XApiKeyUsage, object>> includeBuilder = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Global Result ...
|
||||
var result = dbContext.ApiKeyUsage.AsQueryable();
|
||||
|
||||
//
|
||||
// Handle Filtering ...
|
||||
if (!predicate.IsNull())
|
||||
{
|
||||
result = result.Where(predicate);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Including ...
|
||||
if (!includeBuilder.IsNull())
|
||||
{
|
||||
result = includeBuilder(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Ordering ...
|
||||
if (!orderBuilder.IsNull())
|
||||
{
|
||||
result = orderBuilder(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Return result ...
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,773 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Linq.Expressions;
|
||||
using xDataService.Extensions;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityModels.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
public partial class XApplicationProvider
|
||||
{
|
||||
//
|
||||
#region ApiKey ...
|
||||
/// <summary>
|
||||
/// Add Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> AddApiKeyAsync(
|
||||
XApiKey item,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!item.IsValidForAdd())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Permission ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Application Exists ...
|
||||
var application = await GetApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
id: item.ApplicationId,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Validate Owner ...
|
||||
if (!IsOwned(application, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check COunt of API Keys ...
|
||||
var appApiKeysCount = await dbContext.ApiKeys.CountAsync(
|
||||
cancellationToken: cancellationToken,
|
||||
predicate: x => x.ApplicationId == item.ApplicationId
|
||||
);
|
||||
if (appApiKeysCount >= apiKeyConfiguration.MaxKeysPerApplication)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
item.CreatedOn = DateTime.UtcNow;
|
||||
|
||||
//
|
||||
var entity = item.MapConvert<XApiKey, XApiKeyDto>();
|
||||
if (entity.Id.IsValidIntId())
|
||||
{
|
||||
entity.Id = -1;
|
||||
}
|
||||
|
||||
//
|
||||
var entry = await dbContext.ApiKeys.AddAsync(
|
||||
entity: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
//
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> UpdateApiKeyAsync(
|
||||
Guid id,
|
||||
XApiKeyDto item,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
id != item.Id ||
|
||||
!id.IsDefaultGuid() ||
|
||||
!item.IsValidForAdd() ||
|
||||
item.IsNullOrDefault() ||
|
||||
userInfo.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Exists Entity ...
|
||||
var exists = await dbContext.ApiKeys
|
||||
.FirstOrDefaultAsync(x => x.Id == id && !x.Deleted);
|
||||
if (exists.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Permission Checking ...
|
||||
var application = await GetApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
id: item.ApplicationId,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
if (!IsOwned(application, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Update Data on Exists Entity ...
|
||||
exists = exists.UpdateData(
|
||||
updateWith: item,
|
||||
propertyWhiteList: [
|
||||
nameof(XApiKey.KeyHash),
|
||||
nameof(XApiKey.KeyPrefix),
|
||||
nameof(XApiKey.ExpiresAt),
|
||||
nameof(XApiKey.CreatedOn),
|
||||
nameof(XApiKey.RevokedAt),
|
||||
nameof(XApiKey.RevokedBy),
|
||||
nameof(XApiKey.LastUsedAt),
|
||||
nameof(XApiKey.AllowedIPs),
|
||||
nameof(XApiKey.ApplicationId),
|
||||
nameof(XApiKey.AllowedScopes),
|
||||
nameof(XApiKey.RateLimitPerMinute),
|
||||
]
|
||||
);
|
||||
|
||||
//
|
||||
var entry = dbContext.ApiKeys.Attach(exists);
|
||||
entry.State = EntityState.Modified;
|
||||
|
||||
//
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="softDelete"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> RemoveApiKeyAsync(
|
||||
Guid id,
|
||||
bool softDelete = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
id.IsDefaultGuid() ||
|
||||
userInfo.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Exists Entity ...
|
||||
var exists = await dbContext.ApiKeys
|
||||
.FirstOrDefaultAsync(x => x.Id == id && !x.Deleted);
|
||||
if (exists.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Permission Checking ...
|
||||
var application = await GetApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
id: exists.ApplicationId,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
if (!IsOwned(application, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
EntityEntry<XApiKey> entry = null;
|
||||
if (softDelete)
|
||||
{
|
||||
//
|
||||
exists.Deleted = true;
|
||||
entry = dbContext.ApiKeys.Update(exists);
|
||||
}
|
||||
else
|
||||
{
|
||||
entry = dbContext.ApiKeys.Remove(exists);
|
||||
}
|
||||
|
||||
//
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNull()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an Item by Providing it's ID ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> GetApiKeyAsync(
|
||||
Guid id,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKey>, IIncludableQueryable<XApiKey, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (id.IsDefaultGuid() ||
|
||||
userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Condition ...
|
||||
Expression<Func<XApiKey, bool>> predicate = x =>
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
|
||||
//
|
||||
// Retrieve Queryable ...
|
||||
var queryable = GetApiKeysQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entity = await queryable.FirstOrDefaultAsync(
|
||||
predicate: x => x.Id == id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (entity.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Permission Checking ...
|
||||
var application = await GetApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
id: entity.ApplicationId,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
if (!IsOwned(application, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Items ...
|
||||
/// </summary>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyDto>> GetAllApiKeyAsync(
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKey>, IOrderedQueryable<XApiKey>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKey>, IIncludableQueryable<XApiKey, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var applications = await GetAllApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
orderBuilder: null,
|
||||
includeBuilder: null,
|
||||
cancellationToken: cancellationToken,
|
||||
ignoreSoftDeleteds: ignoreSoftDeleteds
|
||||
);
|
||||
applications = applications.Where(a => IsOwned(a, userInfo));
|
||||
var appIds = applications
|
||||
.Select(a => a.Id)
|
||||
.ToList();
|
||||
|
||||
//
|
||||
Expression<Func<XApiKey, bool>> predicate = x =>
|
||||
appIds.Contains(x.ApplicationId) &&
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeysQeryable(
|
||||
predicate: predicate,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var items = await queryable.ToListAsync(cancellationToken);
|
||||
var result = await EnrichRangeAsync(
|
||||
sources: items,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Items as Async Enumerable ...
|
||||
/// </summary>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async IAsyncEnumerable<XApiKeyDto> GetAllApiKeyAsAsyncEnumerable(
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKey>, IOrderedQueryable<XApiKey>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKey>, IIncludableQueryable<XApiKey, object>> includeBuilder = null,
|
||||
[EnumeratorCancellation]
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var applications = await GetAllApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
orderBuilder: null,
|
||||
includeBuilder: null,
|
||||
cancellationToken: cancellationToken,
|
||||
ignoreSoftDeleteds: ignoreSoftDeleteds
|
||||
);
|
||||
applications = applications.Where(a => IsOwned(a, userInfo));
|
||||
var appIds = applications
|
||||
.Select(a => a.Id)
|
||||
.ToList();
|
||||
|
||||
//
|
||||
Expression<Func<XApiKey, bool>> predicate = x =>
|
||||
appIds.Contains(x.ApplicationId) &&
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeysQeryable(
|
||||
predicate: predicate,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var enumerator = queryable.AsAsyncEnumerable();
|
||||
await foreach (var item in enumerator)
|
||||
{
|
||||
//
|
||||
// Handle Cancellation Token ...
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
//
|
||||
var dto = await EnrichAsync(
|
||||
source: item,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
yield return dto;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find an Item By Providing a Conditional Expression ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> FindOneApiKeyAsync(
|
||||
Expression<Func<XApiKey, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKey>, IIncludableQueryable<XApiKey, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
Expression<Func<XApiKey, bool>> _predicate = x => !ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeysQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: _predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entity = (await queryable
|
||||
.ToListAsync(cancellationToken))
|
||||
.FirstOrDefault(x => predicate.Compile()(x));
|
||||
|
||||
//
|
||||
// Permission Checking ...
|
||||
var application = await GetApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
id: entity.ApplicationId,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
if (!IsOwned(application, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a Collection of Items By Providing a Conditional Expression ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyDto>> FindManyApiKeyAsync(
|
||||
Expression<Func<XApiKey, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKey>, IOrderedQueryable<XApiKey>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKey>, IIncludableQueryable<XApiKey, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
Expression<Func<XApiKey, bool>> _predicate = x => !ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeysQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: _predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entities = (await queryable
|
||||
.ToListAsync(cancellationToken))
|
||||
.Where(x => predicate.Compile()(x))
|
||||
.ToList();
|
||||
|
||||
//
|
||||
var appIds = entities
|
||||
.Select(k => k.ApplicationId)
|
||||
.ToList();
|
||||
|
||||
//
|
||||
foreach (var appId in appIds)
|
||||
{
|
||||
//
|
||||
// Permission Checking ...
|
||||
var application = await GetApplicationAsync(
|
||||
id: appId,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (
|
||||
application.IsNullOrDefault() ||
|
||||
!IsOwned(application, userInfo))
|
||||
{
|
||||
entities = [.. entities.Where(e => e.ApplicationId != appId)];
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichRangeAsync(
|
||||
sources: entities,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Items based on XQuery Pagination Structure ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XQueryResult<XApiKeyDto>> QueryApiKeyAsync(
|
||||
XQuery query,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Expression<Func<XApiKey, bool>> predicate = null,
|
||||
Func<IQueryable<XApiKey>, IOrderedQueryable<XApiKey>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKey>, IIncludableQueryable<XApiKey, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Normalize Query ...
|
||||
query = NormalizeQuery(query, configuration);
|
||||
|
||||
//
|
||||
Expression<Func<XApiKey, bool>> _predicate = x => !ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeysQeryable(
|
||||
predicate: _predicate,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var items = (await queryable
|
||||
.ToListAsync(cancellationToken))
|
||||
.Where(x => predicate.Compile()(x));
|
||||
|
||||
//
|
||||
var appIds = items
|
||||
.Select(k => k.ApplicationId)
|
||||
.ToList();
|
||||
|
||||
//
|
||||
foreach (var appId in appIds)
|
||||
{
|
||||
//
|
||||
// Permission Checking ...
|
||||
var application = await GetApplicationAsync(
|
||||
id: appId,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (
|
||||
application.IsNullOrDefault() ||
|
||||
!IsOwned(application, userInfo))
|
||||
{
|
||||
items = [.. items.Where(e => e.ApplicationId != appId)];
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
var totalItemsCount = items.Count();
|
||||
|
||||
//
|
||||
// Apply Filter ...
|
||||
if (!query.Filter.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
items = items
|
||||
.AsEnumerable()
|
||||
.ApplyFilter(query.Filter);
|
||||
}
|
||||
int filteredItemsCount = items.Count();
|
||||
|
||||
//
|
||||
// Count Pages ...
|
||||
var totalPagesCount = query.CountPages(totalItemsCount);
|
||||
var filteredPagesCount = query.CountPages(filteredItemsCount);
|
||||
|
||||
//
|
||||
// Apply Paging and Sorting ...
|
||||
if (totalItemsCount > 0 &&
|
||||
filteredItemsCount > 0
|
||||
)
|
||||
{
|
||||
//
|
||||
// Apply Sorting ...
|
||||
items = items
|
||||
.ToList()
|
||||
.ApplySorting(
|
||||
query.SortBy,
|
||||
query.IsAscending
|
||||
);
|
||||
|
||||
//
|
||||
// Apply Paging ...
|
||||
items = items
|
||||
.ToList()
|
||||
.ApplyPaging(
|
||||
query.Page,
|
||||
query.PageSize
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
var resultItems = await EnrichRangeAsync(
|
||||
sources: items,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Prepare Result ...
|
||||
var result = new XQueryResult<XApiKeyDto>
|
||||
{
|
||||
Page = query.Page,
|
||||
Items = [.. resultItems],
|
||||
PageSize = query.PageSize,
|
||||
TotalPages = totalPagesCount,
|
||||
TotalItems = totalItemsCount,
|
||||
TotalFilteredPages = filteredPagesCount,
|
||||
TotalFilteredItems = filteredItemsCount
|
||||
};
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,531 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Linq.Expressions;
|
||||
using xDataService.Extensions;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityModels.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
public partial class XApplicationProvider
|
||||
{
|
||||
//
|
||||
#region ApiKeyUsage ...
|
||||
/// <summary>
|
||||
/// Add Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyUsageDto> AddApiKeyUsageAsync(
|
||||
XApiKeyUsageDto item,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!item.IsValidForAdd())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check ApiKey Exists ...
|
||||
var apiKey = await dbContext.ApiKeys
|
||||
.FirstOrDefaultAsync(x => x.Id == item.ApiKeyId);
|
||||
if (apiKey.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check apiKey Activation ...
|
||||
if (!apiKey.IsActive())
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var entity = item.MapConvert<XApiKeyUsage, XApiKeyUsageDto>();
|
||||
var entry = await dbContext.ApiKeyUsages.AddAsync(
|
||||
entity: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyUsageDto> UpdateApiKeyUsageAsync(
|
||||
long id,
|
||||
XApiKeyUsageDto item,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
id <= 0 ||
|
||||
id != item.Id ||
|
||||
!item.IsValidForAdd() ||
|
||||
item.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Item Exists ...
|
||||
var exists = dbContext.ApiKeyUsages.FirstOrDefault(x => x.Id == id);
|
||||
if (exists.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Update Data on Exists Entity ...
|
||||
exists = exists.UpdateData(
|
||||
updateWith: item,
|
||||
propertyWhiteList: [
|
||||
nameof(XApiKeyUsage.ApiKeyId),
|
||||
nameof(XApiKeyUsage.Endpoint),
|
||||
nameof(XApiKeyUsage.ClientIP),
|
||||
nameof(XApiKeyUsage.HttpMethod),
|
||||
nameof(XApiKeyUsage.StatusCode),
|
||||
nameof(XApiKeyUsage.RequestedOn),
|
||||
nameof(XApiKeyUsage.ResponseTimeMs),
|
||||
]
|
||||
);
|
||||
|
||||
//
|
||||
var entry = dbContext.ApiKeyUsages.Attach(exists);
|
||||
entry.State = EntityState.Modified;
|
||||
|
||||
//
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="softDelete"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyUsageDto> RemoveApiKeyUsageAsync(
|
||||
long id,
|
||||
bool softDelete = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (id <= 0)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Exists Entity ...
|
||||
var exists = await dbContext.ApiKeyUsages
|
||||
.FirstOrDefaultAsync(x => x.Id == id && !x.Deleted);
|
||||
if (exists.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
EntityEntry<XApiKeyUsage> entry = null;
|
||||
if (softDelete)
|
||||
{
|
||||
//
|
||||
exists.Deleted = true;
|
||||
entry = dbContext.ApiKeyUsages.Update(exists);
|
||||
}
|
||||
else
|
||||
{
|
||||
entry = dbContext.ApiKeyUsages.Remove(exists);
|
||||
}
|
||||
|
||||
//
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNull()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an Item by Providing it's ID ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyUsageDto> GetApiKeyUsageAsync(
|
||||
long id,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IIncludableQueryable<XApiKeyUsage, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (id <= 0)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Condition ...
|
||||
Expression<Func<XApiKeyUsage, bool>> predicate = x =>
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
|
||||
//
|
||||
// Retrieve Queryable ...
|
||||
var queryable = GetApiKeyUsagesQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entity = await queryable.FirstOrDefaultAsync(
|
||||
predicate: x => x.Id == id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (entity.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Items ...
|
||||
/// </summary>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyUsageDto>> GetAllApiKeyUsageAsync(
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IOrderedQueryable<XApiKeyUsage>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IIncludableQueryable<XApiKeyUsage, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
Expression<Func<XApiKeyUsage, bool>> predicate = x =>
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeyUsagesQeryable(
|
||||
predicate: predicate,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var items = await queryable.ToListAsync(cancellationToken);
|
||||
var result = await EnrichRangeAsync(
|
||||
sources: items,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Items as Async Enumerable ...
|
||||
/// </summary>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async IAsyncEnumerable<XApiKeyUsageDto> GetAllApiKeyUsageAsAsyncEnumerable(
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IOrderedQueryable<XApiKeyUsage>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IIncludableQueryable<XApiKeyUsage, object>> includeBuilder = null,
|
||||
[EnumeratorCancellation]
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
Expression<Func<XApiKeyUsage, bool>> predicate = x =>
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeyUsagesQeryable(
|
||||
predicate: predicate,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var enumerator = queryable.AsAsyncEnumerable();
|
||||
await foreach (var item in enumerator)
|
||||
{
|
||||
//
|
||||
// Handle Cancellation Token ...
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
//
|
||||
var dto = await EnrichAsync(
|
||||
source: item,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
yield return dto;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find an Item By Providing a Conditional Expression ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyUsageDto> FindOneApiKeyUsageAsync(
|
||||
Expression<Func<XApiKeyUsage, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IIncludableQueryable<XApiKeyUsage, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
Expression<Func<XApiKeyUsage, bool>> _predicate = x => !ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeyUsagesQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: _predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entity = (await queryable
|
||||
.ToListAsync(cancellationToken))
|
||||
.FirstOrDefault(x => predicate.Compile()(x));
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a Collection of Items By Providing a Conditional Expression ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyUsageDto>> FindManyApiKeyUsageAsync(
|
||||
Expression<Func<XApiKeyUsage, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IOrderedQueryable<XApiKeyUsage>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IIncludableQueryable<XApiKeyUsage, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
Expression<Func<XApiKeyUsage, bool>> _predicate = x => !ignoreSoftDeleteds || !x.Deleted;
|
||||
var queryable = GetApiKeyUsagesQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: _predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entities = (await queryable
|
||||
.ToListAsync(cancellationToken))
|
||||
.Where(x => predicate.Compile()(x))
|
||||
.ToList();
|
||||
|
||||
//
|
||||
var result = await EnrichRangeAsync(
|
||||
sources: entities,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Items based on XQuery Pagination Structure ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XQueryResult<XApiKeyUsageDto>> QueryApiKeyUsageAsync(
|
||||
XQuery query,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Expression<Func<XApiKeyUsage, bool>> predicate = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IOrderedQueryable<XApiKeyUsage>> orderBuilder = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IIncludableQueryable<XApiKeyUsage, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Normalize Query ...
|
||||
query = NormalizeQuery(query, configuration);
|
||||
|
||||
//
|
||||
var items = await GetAllApiKeyUsageAsync(
|
||||
userInfo: userInfo,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder,
|
||||
cancellationToken: cancellationToken,
|
||||
ignoreSoftDeleteds: ignoreSoftDeleteds
|
||||
);
|
||||
var totalItemsCount = items.Count();
|
||||
|
||||
//
|
||||
// Apply Filter ...
|
||||
if (!query.Filter.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
items = items
|
||||
.ApplyFilter(query.Filter);
|
||||
}
|
||||
int filteredItemsCount = items.Count();
|
||||
|
||||
//
|
||||
// Count Pages ...
|
||||
var totalPagesCount = query.CountPages(totalItemsCount);
|
||||
var filteredPagesCount = query.CountPages(filteredItemsCount);
|
||||
|
||||
//
|
||||
// Apply Paging and Sorting ...
|
||||
if (totalItemsCount > 0 &&
|
||||
filteredItemsCount > 0
|
||||
)
|
||||
{
|
||||
//
|
||||
// Apply Sorting ...
|
||||
items = items
|
||||
.ToList()
|
||||
.ApplySorting(
|
||||
query.SortBy,
|
||||
query.IsAscending
|
||||
);
|
||||
|
||||
//
|
||||
// Apply Paging ...
|
||||
items = items
|
||||
.ToList()
|
||||
.ApplyPaging(
|
||||
query.Page,
|
||||
query.PageSize
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Result ...
|
||||
var result = new XQueryResult<XApiKeyUsageDto>
|
||||
{
|
||||
Page = query.Page,
|
||||
Items = [.. items],
|
||||
PageSize = query.PageSize,
|
||||
TotalPages = totalPagesCount,
|
||||
TotalItems = totalItemsCount,
|
||||
TotalFilteredPages = filteredPagesCount,
|
||||
TotalFilteredItems = filteredItemsCount
|
||||
};
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,624 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Linq.Expressions;
|
||||
using xDataService.Extensions;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityModels.Extensions;
|
||||
using xIdentityHelper.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
public partial class XApplicationProvider
|
||||
{
|
||||
//
|
||||
#region Application ...
|
||||
/// <summary>
|
||||
/// Add Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApplicationDto> AddApplicationAsync(
|
||||
XApplicationDto item,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!item.IsValidForAdd())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Permission ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
item.OwnerId = userInfo.UserId;
|
||||
item.CreatedOn = DateTime.UtcNow;
|
||||
|
||||
//
|
||||
var entity = item.MapConvert<XApplication, XApplicationDto>();
|
||||
if (entity.Id.IsValidIntId())
|
||||
{
|
||||
entity.Id = -1;
|
||||
}
|
||||
|
||||
//
|
||||
var entry = await dbContext.Applications.AddAsync(
|
||||
entity: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
//
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApplicationDto> UpdateApplicationAsync(
|
||||
int id,
|
||||
XApplicationDto item,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
id != item.Id ||
|
||||
!id.IsValidIntId() ||
|
||||
item.IsNullOrDefault() ||
|
||||
userInfo.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Exists Entity ...
|
||||
var exists = await dbContext.Applications
|
||||
.FirstOrDefaultAsync(x => x.Id == id && !x.Deleted);
|
||||
if (exists.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Permission Checking ...
|
||||
if (!IsOwned(exists, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Update Data on Exists Entity ...
|
||||
exists = exists.UpdateData(
|
||||
updateWith: item,
|
||||
propertyWhiteList: [
|
||||
nameof(XApplication.Name),
|
||||
nameof(XApplication.Type),
|
||||
nameof(XApplication.OwnerId),
|
||||
nameof(XApplication.IsActive),
|
||||
nameof(XApplication.CreatedOn),
|
||||
nameof(XApplication.UpdatedAt),
|
||||
nameof(XApplication.Description),
|
||||
]
|
||||
);
|
||||
|
||||
//
|
||||
var entry = dbContext.Applications.Attach(exists);
|
||||
entry.State = EntityState.Modified;
|
||||
|
||||
//
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="softDelete"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApplicationDto> RemoveApplicationAsync(
|
||||
int id,
|
||||
bool softDelete = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
!id.IsValidIntId() ||
|
||||
userInfo.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Exists Entity ...
|
||||
var exists = await dbContext.Applications
|
||||
.FirstOrDefaultAsync(x => x.Id == id && !x.Deleted);
|
||||
if (exists.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Permission Checking ...
|
||||
if (!IsOwned(exists, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
EntityEntry<XApplication> entry = null;
|
||||
if (softDelete)
|
||||
{
|
||||
//
|
||||
exists.Deleted = true;
|
||||
entry = dbContext.Applications.Update(exists);
|
||||
}
|
||||
else
|
||||
{
|
||||
entry = dbContext.Applications.Remove(exists);
|
||||
}
|
||||
|
||||
//
|
||||
var changes = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
if (
|
||||
changes <= 0 ||
|
||||
entry.IsNull() ||
|
||||
entry.Entity.IsNull()
|
||||
)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entry.Entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an Item by Providing it's ID ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApplicationDto> GetApplicationAsync(
|
||||
int id,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApplication>, IIncludableQueryable<XApplication, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!id.IsValidIntId() ||
|
||||
userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Condition ...
|
||||
Expression<Func<XApplication, bool>> predicate = x =>
|
||||
x.OwnerId != userInfo.UserId ||
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
if (userInfo.IsAdmin())
|
||||
{
|
||||
//
|
||||
predicate = x =>
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Queryable ...
|
||||
var queryable = GetApplicationsQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entity = await queryable.FirstOrDefaultAsync(
|
||||
predicate: x => x.Id == id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Items ...
|
||||
/// </summary>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApplicationDto>> GetAllApplicationAsync(
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApplication>, IOrderedQueryable<XApplication>> orderBuilder = null,
|
||||
Func<IQueryable<XApplication>, IIncludableQueryable<XApplication, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Condition ...
|
||||
Expression<Func<XApplication, bool>> predicate = x =>
|
||||
x.OwnerId == userInfo.UserId &&
|
||||
(!ignoreSoftDeleteds || !x.Deleted);
|
||||
if (userInfo.IsAdmin())
|
||||
{
|
||||
//
|
||||
predicate = x =>
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Queryable ...
|
||||
var queryable = GetApplicationsQeryable(
|
||||
predicate: predicate,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var items = await queryable.ToListAsync(cancellationToken);
|
||||
var result = await EnrichRangeAsync(
|
||||
sources: items,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Items as Async Enumerable ...
|
||||
/// </summary>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async IAsyncEnumerable<XApplicationDto> GetAllApplicationAsAsyncEnumerable(
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApplication>, IOrderedQueryable<XApplication>> orderBuilder = null,
|
||||
Func<IQueryable<XApplication>, IIncludableQueryable<XApplication, object>> includeBuilder = null,
|
||||
[EnumeratorCancellation]
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Condition ...
|
||||
Expression<Func<XApplication, bool>> predicate = x =>
|
||||
x.OwnerId == userInfo.UserId &&
|
||||
(!ignoreSoftDeleteds || !x.Deleted);
|
||||
if (userInfo.IsAdmin())
|
||||
{
|
||||
//
|
||||
predicate = x =>
|
||||
!ignoreSoftDeleteds || !x.Deleted;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Queryable ...
|
||||
var queryable = GetApplicationsQeryable(
|
||||
predicate: predicate,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var enumerator = queryable.AsAsyncEnumerable();
|
||||
await foreach (var item in enumerator)
|
||||
{
|
||||
//
|
||||
// Handle Cancellation Token ...
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
//
|
||||
var dto = await EnrichAsync(
|
||||
source: item,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
yield return dto;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find an Item By Providing a Conditional Expression ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApplicationDto> FindOneApplicationAsync(
|
||||
Expression<Func<XApplication, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApplication>, IIncludableQueryable<XApplication, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
Expression<Func<XApplication, bool>> _predicate = x => !ignoreSoftDeleteds || !x.Deleted;
|
||||
if (!userInfo.IsAdmin())
|
||||
{
|
||||
_predicate = x =>
|
||||
x.OwnerId == userInfo.UserId &&
|
||||
(!ignoreSoftDeleteds || !x.Deleted);
|
||||
}
|
||||
|
||||
//
|
||||
var queryable = GetApplicationsQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: _predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entity = (await queryable
|
||||
.ToListAsync(cancellationToken))
|
||||
.FirstOrDefault(x => predicate.Compile()(x));
|
||||
|
||||
//
|
||||
var result = await EnrichAsync(
|
||||
source: entity,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a Collection of Items By Providing a Conditional Expression ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApplicationDto>> FindManyApplicationAsync(
|
||||
Expression<Func<XApplication, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApplication>, IOrderedQueryable<XApplication>> orderBuilder = null,
|
||||
Func<IQueryable<XApplication>, IIncludableQueryable<XApplication, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
Expression<Func<XApplication, bool>> _predicate = x => !ignoreSoftDeleteds || !x.Deleted;
|
||||
if (!userInfo.IsAdmin())
|
||||
{
|
||||
_predicate = x =>
|
||||
x.OwnerId == userInfo.UserId &&
|
||||
(!ignoreSoftDeleteds || !x.Deleted);
|
||||
}
|
||||
|
||||
//
|
||||
var queryable = GetApplicationsQeryable(
|
||||
orderBuilder: null,
|
||||
predicate: _predicate,
|
||||
includeBuilder: includeBuilder
|
||||
);
|
||||
|
||||
//
|
||||
var entities = (await queryable
|
||||
.ToListAsync(cancellationToken))
|
||||
.Where(x => predicate.Compile()(x));
|
||||
|
||||
//
|
||||
var result = await EnrichRangeAsync(
|
||||
sources: entities,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Items based on XQuery Pagination Structure ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XQueryResult<XApplicationDto>> QueryApplicationAsync(
|
||||
XQuery query,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Expression<Func<XApplication, bool>> predicate = null,
|
||||
Func<IQueryable<XApplication>, IOrderedQueryable<XApplication>> orderBuilder = null,
|
||||
Func<IQueryable<XApplication>, IIncludableQueryable<XApplication, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Normalize Query ...
|
||||
query = NormalizeQuery(query, configuration);
|
||||
|
||||
//
|
||||
var items = await GetAllApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
orderBuilder: orderBuilder,
|
||||
includeBuilder: includeBuilder,
|
||||
cancellationToken: cancellationToken,
|
||||
ignoreSoftDeleteds: ignoreSoftDeleteds
|
||||
);
|
||||
var totalItemsCount = items.Count();
|
||||
|
||||
//
|
||||
// Apply Filter ...
|
||||
if (!query.Filter.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
items = items
|
||||
.ApplyFilter(query.Filter);
|
||||
}
|
||||
int filteredItemsCount = items.Count();
|
||||
|
||||
//
|
||||
// Count Pages ...
|
||||
var totalPagesCount = query.CountPages(totalItemsCount);
|
||||
var filteredPagesCount = query.CountPages(filteredItemsCount);
|
||||
|
||||
//
|
||||
// Apply Paging and Sorting ...
|
||||
if (totalItemsCount > 0 &&
|
||||
filteredItemsCount > 0
|
||||
)
|
||||
{
|
||||
//
|
||||
// Apply Sorting ...
|
||||
items = items
|
||||
.ToList()
|
||||
.ApplySorting(
|
||||
query.SortBy,
|
||||
query.IsAscending
|
||||
);
|
||||
|
||||
//
|
||||
// Apply Paging ...
|
||||
items = items
|
||||
.ToList()
|
||||
.ApplyPaging(
|
||||
query.Page,
|
||||
query.PageSize
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Result ...
|
||||
var result = new XQueryResult<XApplicationDto>
|
||||
{
|
||||
Page = query.Page,
|
||||
Items = [.. items],
|
||||
PageSize = query.PageSize,
|
||||
TotalPages = totalPagesCount,
|
||||
TotalItems = totalItemsCount,
|
||||
TotalFilteredPages = filteredPagesCount,
|
||||
TotalFilteredItems = filteredItemsCount
|
||||
};
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
public partial class XApplicationProvider
|
||||
{
|
||||
//
|
||||
#region Custom Actions ...
|
||||
/// <summary>
|
||||
/// Generate a New ApiKey and Add It to DB ...
|
||||
/// </summary>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="expiration"></param>
|
||||
/// <param name="scopes"></param>
|
||||
/// <param name="allowedIPs"></param>
|
||||
/// <param name="rateLimit"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> CreateApiKey(
|
||||
int applicationId,
|
||||
TimeSpan? expiration = null,
|
||||
IEnumerable<string> scopes = null,
|
||||
IEnumerable<string> allowedIPs = null,
|
||||
int? rateLimit = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
userInfo.IsNullOrDefault() ||
|
||||
!applicationId.IsValidIntId()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Application Exists ...
|
||||
var application = await GetApplicationAsync(
|
||||
id: applicationId,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Validate Owner ...
|
||||
if (!IsOwned(application, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Normalizing ...
|
||||
if (!rateLimit.HasValue)
|
||||
{
|
||||
rateLimit = apiKeyConfiguration.DefaultRateLimit;
|
||||
}
|
||||
if (!expiration.HasValue)
|
||||
{
|
||||
expiration = TimeSpan.FromMinutes(apiKeyConfiguration.DefaultExpirationMinutes);
|
||||
}
|
||||
else if (expiration.Value > TimeSpan.FromMinutes(apiKeyConfiguration.MaxExpirationMinutes))
|
||||
{
|
||||
expiration = TimeSpan.FromMinutes(apiKeyConfiguration.MaxExpirationMinutes);
|
||||
}
|
||||
|
||||
//
|
||||
var
|
||||
|
||||
//
|
||||
var item = new XApiKeyDto
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Revoke Specified Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> RevokeApiKey(
|
||||
Guid apiKeyId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Renew Specified Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="newExpiration"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> RotateApiKey(
|
||||
Guid apiKeyId,
|
||||
TimeSpan? newExpiration = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve All Api Keys for Specified Application ...
|
||||
/// </summary>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyDto>> GetApplicationApiKeys(
|
||||
Guid applicationId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Validate Specified Api Key in based on ClientIP ...
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="clientIP"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> ValidateApiKey(
|
||||
string apiKey,
|
||||
string clientIP,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Dtos;
|
||||
using xIdentityModels.Models;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
public partial class XApplicationProvider
|
||||
{
|
||||
//
|
||||
#region Enrichers ...
|
||||
/// <summary>
|
||||
/// Enrich Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> EnrichAsync(
|
||||
XApiKey source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> EnrichAsync(
|
||||
XApiKeyDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Api Key Usage ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyUsageDto> EnrichAsync(
|
||||
XApiKeyUsage source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Application ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApplicationDto> EnrichAsync(
|
||||
XApplication source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Api Key Usage ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyUsageDto> EnrichAsync(
|
||||
XApiKeyUsageDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Application ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApplicationDto> EnrichAsync(
|
||||
XApplicationDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Api Key(s) ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApiKey> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Api Key(s) ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApiKeyDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Api Key Usage(s) ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyUsageDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApiKeyUsage> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Application(s) ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApplicationDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApplication> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Api Key Usage(s) ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApiKeyUsageDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApiKeyUsageDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Application(s) ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XApplicationDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApplicationDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user