mirror of https://github.com/EasyAbp/EShop.git
36 changed files with 390 additions and 588 deletions
@ -1,23 +0,0 @@ |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace EasyAbp.EShop.Stores.Permissions |
|||
{ |
|||
public static class StoreOwnerAuthorizationExtensions |
|||
{ |
|||
public static Task<bool> IsStoreOwnerGrantedAsync(this IAuthorizationService authorizationService, |
|||
Guid storeId, string policyName = null, object resource = null) |
|||
{ |
|||
return authorizationService.IsGrantedAsync(resource, |
|||
new StorePermissionAuthorizationRequirement(storeId, policyName)); |
|||
} |
|||
|
|||
public static Task CheckStoreOwnerAsync(this IAuthorizationService authorizationService, |
|||
Guid storeId, string policyName = null, object resource = null) |
|||
{ |
|||
return authorizationService.CheckAsync(resource, |
|||
new StorePermissionAuthorizationRequirement(storeId, policyName)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Volo.Abp.AutoMapper" Version="3.0.5" /> |
|||
<PackageReference Include="Volo.Abp.Ddd.Application" Version="3.0.5" /> |
|||
<ProjectReference Include="..\EasyAbp.EShop.Stores.Domain.Shared\EasyAbp.EShop.Stores.Domain.Shared.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,22 @@ |
|||
using EasyAbp.EShop.Stores.Permissions; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Application; |
|||
using Volo.Abp.AutoMapper; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace EasyAbp.EShop.Stores |
|||
{ |
|||
[DependsOn( |
|||
typeof(EShopStoresDomainSharedModule), |
|||
typeof(AbpDddApplicationModule), |
|||
typeof(AbpAutoMapperModule) |
|||
)] |
|||
public class EShopStoresApplicationSharedModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddSingleton<IAuthorizationHandler, BasicStorePermissionAuthorizationHandler>(); |
|||
} |
|||
} |
|||
} |
|||
0
modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Contracts/EasyAbp/EShop/Stores/Permissions/BasicStorePermissionAuthorizationHandler.cs → modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Shared/EasyAbp/EShop/Stores/Permissions/BasicStorePermissionAuthorizationHandler.cs
0
modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Contracts/EasyAbp/EShop/Stores/Permissions/BasicStorePermissionAuthorizationHandler.cs → modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Shared/EasyAbp/EShop/Stores/Permissions/BasicStorePermissionAuthorizationHandler.cs
@ -0,0 +1,50 @@ |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Authorization; |
|||
|
|||
namespace EasyAbp.EShop.Stores.Permissions |
|||
{ |
|||
public static class StoreOwnerAuthorizationExtensions |
|||
{ |
|||
public static Task<bool> IsStoreOwnerGrantedAsync(this IAuthorizationService authorizationService, |
|||
Guid storeId, string policyName = null, object resource = null) |
|||
{ |
|||
return authorizationService.IsGrantedAsync(resource, |
|||
new StorePermissionAuthorizationRequirement(storeId, policyName)); |
|||
} |
|||
|
|||
public static async Task<bool> IsMultiStoreGrantedAsync(this IAuthorizationService authorizationService, |
|||
Guid? storeId, string crossStorePolicyName, string policyName, object resource = null) |
|||
{ |
|||
if (storeId.HasValue |
|||
&& await authorizationService.IsStoreOwnerGrantedAsync(storeId.Value, policyName)) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
return await authorizationService.IsGrantedAsync(resource, crossStorePolicyName) |
|||
&& await authorizationService.IsGrantedAsync(resource, policyName); |
|||
} |
|||
|
|||
public static async Task CheckMultiStorePolicyAsync(this IAuthorizationService authorizationService, |
|||
Guid? storeId, string crossStorePolicyName, string policyName, object resource = null) |
|||
{ |
|||
if (storeId.HasValue |
|||
&& await authorizationService.IsStoreOwnerGrantedAsync(storeId.Value, policyName)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await authorizationService.CheckAsync(resource, crossStorePolicyName); |
|||
await authorizationService.CheckAsync(resource, policyName); |
|||
} |
|||
|
|||
public static Task CheckStoreOwnerAsync(this IAuthorizationService authorizationService, |
|||
Guid storeId, string policyName = null, object resource = null) |
|||
{ |
|||
return authorizationService.CheckAsync(resource, |
|||
new StorePermissionAuthorizationRequirement(storeId, policyName)); |
|||
} |
|||
} |
|||
} |
|||
0
modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Contracts/EasyAbp/EShop/Stores/Permissions/StorePermissionAuthorizationRequirement.cs → modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Shared/EasyAbp/EShop/Stores/Permissions/StorePermissionAuthorizationRequirement.cs
0
modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Contracts/EasyAbp/EShop/Stores/Permissions/StorePermissionAuthorizationRequirement.cs → modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Shared/EasyAbp/EShop/Stores/Permissions/StorePermissionAuthorizationRequirement.cs
@ -0,0 +1,125 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using EasyAbp.EShop.Stores.Permissions; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Application.Services; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace EasyAbp.EShop.Stores.Stores |
|||
{ |
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey> |
|||
: MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TEntityDto : IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput> |
|||
: MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto, TEntityDto> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TEntityDto : IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput> |
|||
: MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TCreateInput : IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, |
|||
TUpdateInput> |
|||
: MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, |
|||
TUpdateInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TCreateInput : IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, |
|||
TGetListInput, TCreateInput, TUpdateInput> |
|||
: AbstractKeyCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, |
|||
TGetListInput, TCreateInput, TUpdateInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TCreateInput : IMultiStore |
|||
{ |
|||
protected virtual string CrossStorePolicyName { get; set; } |
|||
|
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
|
|||
public override async Task<TGetOutputDto> GetAsync(TKey id) |
|||
{ |
|||
var entity = await GetEntityByIdAsync(id); |
|||
|
|||
await CheckMultiStorePolicyAsync(entity.StoreId, GetPolicyName); |
|||
|
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
|
|||
public override async Task<TGetOutputDto> CreateAsync(TCreateInput input) |
|||
{ |
|||
await CheckMultiStorePolicyAsync(input.StoreId, CreatePolicyName); |
|||
|
|||
var entity = MapToEntity(input); |
|||
|
|||
TryToSetTenantId(entity); |
|||
|
|||
await Repository.InsertAsync(entity, autoSave: true); |
|||
|
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
|
|||
public override async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input) |
|||
{ |
|||
var entity = await GetEntityByIdAsync(id); |
|||
await CheckMultiStorePolicyAsync(entity.StoreId, UpdatePolicyName); |
|||
|
|||
MapToEntity(input, entity); |
|||
await Repository.UpdateAsync(entity, autoSave: true); |
|||
|
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
|
|||
public override async Task DeleteAsync(TKey id) |
|||
{ |
|||
var entity = await GetEntityByIdAsync(id); |
|||
await CheckMultiStorePolicyAsync(entity.StoreId, DeletePolicyName); |
|||
|
|||
await DeleteByIdAsync(id); |
|||
} |
|||
|
|||
|
|||
protected virtual async Task CheckMultiStorePolicyAsync(Guid? storeId, string policyName) |
|||
{ |
|||
if (storeId.HasValue |
|||
&& await AuthorizationService.IsStoreOwnerGrantedAsync(storeId.Value, policyName)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await CheckPolicyAsync(CrossStorePolicyName); |
|||
await CheckPolicyAsync(policyName); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using EasyAbp.EShop.Stores.Permissions; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Application.Services; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace EasyAbp.EShop.Stores.Stores |
|||
{ |
|||
public abstract class MultiStoreAbstractKeyReadOnlyAppService<TEntity, TEntityDto, TKey> |
|||
: MultiStoreAbstractKeyReadOnlyAppService<TEntity, TEntityDto, TEntityDto, TKey, PagedAndSortedResultRequestDto> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyReadOnlyAppService(IReadOnlyRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyReadOnlyAppService<TEntity, TEntityDto, TKey, TGetListInput> |
|||
: MultiStoreAbstractKeyReadOnlyAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyReadOnlyAppService(IReadOnlyRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, |
|||
TGetListInput> |
|||
: AbstractKeyReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, |
|||
TGetListInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
{ |
|||
protected virtual string CrossStorePolicyName { get; set; } |
|||
|
|||
protected MultiStoreAbstractKeyReadOnlyAppService(IReadOnlyRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
|
|||
public override async Task<TGetOutputDto> GetAsync(TKey id) |
|||
{ |
|||
var entity = await GetEntityByIdAsync(id); |
|||
|
|||
await CheckMultiStorePolicyAsync(entity.StoreId, GetPolicyName); |
|||
|
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
|
|||
protected virtual async Task CheckMultiStorePolicyAsync(Guid? storeId, string policyName) |
|||
{ |
|||
if (storeId.HasValue |
|||
&& await AuthorizationService.IsStoreOwnerGrantedAsync(storeId.Value, policyName)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await CheckPolicyAsync(CrossStorePolicyName); |
|||
await CheckPolicyAsync(policyName); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
|||
<ConfigureAwait ContinueOnCapturedContext="false" /> |
|||
</Weavers> |
|||
@ -0,0 +1,30 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
|||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
|||
<xs:element name="Weavers"> |
|||
<xs:complexType> |
|||
<xs:all> |
|||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
|||
<xs:complexType> |
|||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:all> |
|||
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
|||
<xs:annotation> |
|||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:schema> |
|||
@ -1,211 +0,0 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Application.Services; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.ObjectMapping; |
|||
|
|||
namespace EasyAbp.EShop.Stores.Stores |
|||
{ |
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey> |
|||
: MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TEntityDto : IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput> |
|||
: MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto, TEntityDto> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TEntityDto : IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput> |
|||
: MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TCreateInput : IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, |
|||
TUpdateInput> |
|||
: MultiStoreAbstractKeyCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, |
|||
TUpdateInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TCreateInput : IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
|
|||
protected override Task<TEntityDto> MapToGetListOutputDtoAsync(TEntity entity) |
|||
{ |
|||
return MapToGetOutputDtoAsync(entity); |
|||
} |
|||
|
|||
protected override TEntityDto MapToGetListOutputDto(TEntity entity) |
|||
{ |
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, |
|||
TGetListInput, TCreateInput, TUpdateInput> |
|||
: MultiStoreAbstractKeyReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput>, |
|||
ICrudAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
where TCreateInput : IMultiStore |
|||
{ |
|||
protected IRepository<TEntity> Repository { get; } |
|||
|
|||
protected virtual string CreatePolicyName { get; set; } |
|||
|
|||
protected virtual string UpdatePolicyName { get; set; } |
|||
|
|||
protected virtual string DeletePolicyName { get; set; } |
|||
|
|||
protected MultiStoreAbstractKeyCrudAppService(IRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
Repository = repository; |
|||
} |
|||
|
|||
public virtual async Task<TGetOutputDto> CreateAsync(TCreateInput input) |
|||
{ |
|||
await CheckMultiStorePolicyAsync(input.StoreId, CreatePolicyName); |
|||
|
|||
var entity = await MapToEntityAsync(input); |
|||
|
|||
TryToSetTenantId(entity); |
|||
|
|||
await Repository.InsertAsync(entity, autoSave: true); |
|||
|
|||
return await MapToGetOutputDtoAsync(entity); |
|||
} |
|||
|
|||
public virtual async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input) |
|||
{ |
|||
var entity = await GetEntityByIdAsync(id); |
|||
await CheckMultiStorePolicyAsync(entity.StoreId, UpdatePolicyName); |
|||
//TODO: Check if input has id different than given id and normalize if it's default value, throw ex otherwise
|
|||
await MapToEntityAsync(input, entity); |
|||
await Repository.UpdateAsync(entity, autoSave: true); |
|||
|
|||
return await MapToGetOutputDtoAsync(entity); |
|||
} |
|||
|
|||
public virtual async Task DeleteAsync(TKey id) |
|||
{ |
|||
var entity = await GetEntityByIdAsync(id); |
|||
await CheckMultiStorePolicyAsync(entity.StoreId, DeletePolicyName); |
|||
|
|||
await DeleteByIdAsync(id); |
|||
} |
|||
|
|||
protected abstract Task DeleteByIdAsync(TKey id); |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TCreateInput"/> to <see cref="TEntity"/> to create a new entity.
|
|||
/// It uses <see cref="MapToEntity(TCreateInput)"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// Overriding this has higher priority than overriding the <see cref="MapToEntity(TCreateInput)"/>
|
|||
/// </summary>
|
|||
protected virtual Task<TEntity> MapToEntityAsync(TCreateInput createInput) |
|||
{ |
|||
return Task.FromResult(MapToEntity(createInput)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TCreateInput"/> to <see cref="TEntity"/> to create a new entity.
|
|||
/// It uses <see cref="IObjectMapper"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// </summary>
|
|||
protected virtual TEntity MapToEntity(TCreateInput createInput) |
|||
{ |
|||
var entity = ObjectMapper.Map<TCreateInput, TEntity>(createInput); |
|||
SetIdForGuids(entity); |
|||
return entity; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets Id value for the entity if <see cref="TKey"/> is <see cref="Guid"/>.
|
|||
/// It's used while creating a new entity.
|
|||
/// </summary>
|
|||
protected virtual void SetIdForGuids(TEntity entity) |
|||
{ |
|||
if (entity is IEntity<Guid> entityWithGuidId && entityWithGuidId.Id == Guid.Empty) |
|||
{ |
|||
EntityHelper.TrySetId( |
|||
entityWithGuidId, |
|||
() => GuidGenerator.Create(), |
|||
true |
|||
); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TUpdateInput"/> to <see cref="TEntity"/> to update the entity.
|
|||
/// It uses <see cref="MapToEntity(TUpdateInput, TEntity)"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// Overriding this has higher priority than overriding the <see cref="MapToEntity(TUpdateInput, TEntity)"/>
|
|||
/// </summary>
|
|||
protected virtual Task MapToEntityAsync(TUpdateInput updateInput, TEntity entity) |
|||
{ |
|||
MapToEntity(updateInput, entity); |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TUpdateInput"/> to <see cref="TEntity"/> to update the entity.
|
|||
/// It uses <see cref="IObjectMapper"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// </summary>
|
|||
protected virtual void MapToEntity(TUpdateInput updateInput, TEntity entity) |
|||
{ |
|||
ObjectMapper.Map(updateInput, entity); |
|||
} |
|||
|
|||
protected virtual void TryToSetTenantId(TEntity entity) |
|||
{ |
|||
if (entity is IMultiTenant && HasTenantIdProperty(entity)) |
|||
{ |
|||
var tenantId = CurrentTenant.Id; |
|||
|
|||
if (!tenantId.HasValue) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var propertyInfo = entity.GetType().GetProperty(nameof(IMultiTenant.TenantId)); |
|||
|
|||
if (propertyInfo == null || propertyInfo.GetSetMethod(true) == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
propertyInfo.SetValue(entity, tenantId); |
|||
} |
|||
} |
|||
|
|||
protected virtual bool HasTenantIdProperty(TEntity entity) |
|||
{ |
|||
return entity.GetType().GetProperty(nameof(IMultiTenant.TenantId)) != null; |
|||
} |
|||
} |
|||
} |
|||
@ -1,236 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Dynamic.Core; |
|||
using System.Threading.Tasks; |
|||
using EasyAbp.EShop.Stores.Permissions; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Application.Services; |
|||
using Volo.Abp.Auditing; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.ObjectMapping; |
|||
|
|||
namespace EasyAbp.EShop.Stores.Stores |
|||
{ |
|||
public abstract class MultiStoreAbstractKeyReadOnlyAppService<TEntity, TEntityDto, TKey> |
|||
: MultiStoreAbstractKeyReadOnlyAppService<TEntity, TEntityDto, TEntityDto, TKey, PagedAndSortedResultRequestDto> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyReadOnlyAppService(IReadOnlyRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyReadOnlyAppService<TEntity, TEntityDto, TKey, TGetListInput> |
|||
: MultiStoreAbstractKeyReadOnlyAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
{ |
|||
protected MultiStoreAbstractKeyReadOnlyAppService(IReadOnlyRepository<TEntity> repository) |
|||
: base(repository) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
public abstract class MultiStoreAbstractKeyReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, |
|||
TGetListInput> |
|||
: ApplicationService |
|||
, IReadOnlyAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput> |
|||
where TEntity : class, IEntity, IMultiStore |
|||
{ |
|||
protected IReadOnlyRepository<TEntity> ReadOnlyRepository { get; } |
|||
|
|||
protected virtual string GetPolicyName { get; set; } |
|||
|
|||
protected virtual string GetListPolicyName { get; set; } |
|||
|
|||
protected virtual string CrossStorePolicyName { get; set; } |
|||
|
|||
protected MultiStoreAbstractKeyReadOnlyAppService(IReadOnlyRepository<TEntity> repository) |
|||
{ |
|||
ReadOnlyRepository = repository; |
|||
} |
|||
|
|||
public virtual async Task<TGetOutputDto> GetAsync(TKey id) |
|||
{ |
|||
var entity = await GetEntityByIdAsync(id); |
|||
|
|||
await CheckMultiStorePolicyAsync(entity.StoreId, GetPolicyName); |
|||
|
|||
return await MapToGetOutputDtoAsync(entity); |
|||
} |
|||
|
|||
public virtual async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input) |
|||
{ |
|||
await CheckGetListPolicyAsync(); |
|||
|
|||
var query = CreateFilteredQuery(input); |
|||
|
|||
var totalCount = await AsyncExecuter.CountAsync(query); |
|||
|
|||
query = ApplySorting(query, input); |
|||
query = ApplyPaging(query, input); |
|||
|
|||
var entities = await AsyncExecuter.ToListAsync(query); |
|||
var entityDtos = await MapToGetListOutputDtosAsync(entities); |
|||
|
|||
return new PagedResultDto<TGetListOutputDto>( |
|||
totalCount, |
|||
entityDtos |
|||
); |
|||
} |
|||
|
|||
protected virtual async Task CheckGetListPolicyAsync() |
|||
{ |
|||
await CheckPolicyAsync(GetListPolicyName); |
|||
} |
|||
|
|||
protected abstract Task<TEntity> GetEntityByIdAsync(TKey id); |
|||
|
|||
protected virtual async Task CheckMultiStorePolicyAsync(Guid storeId, string policyName) |
|||
{ |
|||
if (!await AuthorizationService.IsStoreOwnerGrantedAsync(storeId, policyName)) |
|||
{ |
|||
await CheckPolicyAsync(CrossStorePolicyName); |
|||
await CheckPolicyAsync(policyName); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Should apply sorting if needed.
|
|||
/// </summary>
|
|||
/// <param name="query">The query.</param>
|
|||
/// <param name="input">The input.</param>
|
|||
protected virtual IQueryable<TEntity> ApplySorting(IQueryable<TEntity> query, TGetListInput input) |
|||
{ |
|||
//Try to sort query if available
|
|||
if (input is ISortedResultRequest sortInput) |
|||
{ |
|||
if (!sortInput.Sorting.IsNullOrWhiteSpace()) |
|||
{ |
|||
return query.OrderBy(sortInput.Sorting); |
|||
} |
|||
} |
|||
|
|||
//IQueryable.Task requires sorting, so we should sort if Take will be used.
|
|||
if (input is ILimitedResultRequest) |
|||
{ |
|||
return ApplyDefaultSorting(query); |
|||
} |
|||
|
|||
//No sorting
|
|||
return query; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Applies sorting if no sorting specified but a limited result requested.
|
|||
/// </summary>
|
|||
/// <param name="query">The query.</param>
|
|||
protected virtual IQueryable<TEntity> ApplyDefaultSorting(IQueryable<TEntity> query) |
|||
{ |
|||
if (typeof(TEntity).IsAssignableTo<IHasCreationTime>()) |
|||
{ |
|||
return query.OrderByDescending(e => ((IHasCreationTime) e).CreationTime); |
|||
} |
|||
|
|||
throw new AbpException( |
|||
"No sorting specified but this query requires sorting. Override the ApplyDefaultSorting method for your application service derived from AbstractKeyReadOnlyAppService!"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Should apply paging if needed.
|
|||
/// </summary>
|
|||
/// <param name="query">The query.</param>
|
|||
/// <param name="input">The input.</param>
|
|||
protected virtual IQueryable<TEntity> ApplyPaging(IQueryable<TEntity> query, TGetListInput input) |
|||
{ |
|||
//Try to use paging if available
|
|||
if (input is IPagedResultRequest pagedInput) |
|||
{ |
|||
return query.PageBy(pagedInput); |
|||
} |
|||
|
|||
//Try to limit query result if available
|
|||
if (input is ILimitedResultRequest limitedInput) |
|||
{ |
|||
return query.Take(limitedInput.MaxResultCount); |
|||
} |
|||
|
|||
//No paging
|
|||
return query; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// This method should create <see cref="IQueryable{TEntity}"/> based on given input.
|
|||
/// It should filter query if needed, but should not do sorting or paging.
|
|||
/// Sorting should be done in <see cref="ApplySorting"/> and paging should be done in <see cref="ApplyPaging"/>
|
|||
/// methods.
|
|||
/// </summary>
|
|||
/// <param name="input">The input.</param>
|
|||
protected virtual IQueryable<TEntity> CreateFilteredQuery(TGetListInput input) |
|||
{ |
|||
return ReadOnlyRepository; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TEntity"/> to <see cref="TGetOutputDto"/>.
|
|||
/// It internally calls the <see cref="MapToGetOutputDto"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// Overriding this has higher priority than overriding the <see cref="MapToGetOutputDto"/>
|
|||
/// </summary>
|
|||
protected virtual Task<TGetOutputDto> MapToGetOutputDtoAsync(TEntity entity) |
|||
{ |
|||
return Task.FromResult(MapToGetOutputDto(entity)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TEntity"/> to <see cref="TGetOutputDto"/>.
|
|||
/// It uses <see cref="IObjectMapper"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// </summary>
|
|||
protected virtual TGetOutputDto MapToGetOutputDto(TEntity entity) |
|||
{ |
|||
return ObjectMapper.Map<TEntity, TGetOutputDto>(entity); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps a list of <see cref="TEntity"/> to <see cref="TGetListOutputDto"/> objects.
|
|||
/// It uses <see cref="MapToGetListOutputDtoAsync"/> method for each item in the list.
|
|||
/// </summary>
|
|||
protected virtual async Task<List<TGetListOutputDto>> MapToGetListOutputDtosAsync(List<TEntity> entities) |
|||
{ |
|||
var dtos = new List<TGetListOutputDto>(); |
|||
|
|||
foreach (var entity in entities) |
|||
{ |
|||
dtos.Add(await MapToGetListOutputDtoAsync(entity)); |
|||
} |
|||
|
|||
return dtos; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TEntity"/> to <see cref="TGetListOutputDto"/>.
|
|||
/// It internally calls the <see cref="MapToGetListOutputDto"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// Overriding this has higher priority than overriding the <see cref="MapToGetListOutputDto"/>
|
|||
/// </summary>
|
|||
protected virtual Task<TGetListOutputDto> MapToGetListOutputDtoAsync(TEntity entity) |
|||
{ |
|||
return Task.FromResult(MapToGetListOutputDto(entity)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TEntity"/> to <see cref="TGetListOutputDto"/>.
|
|||
/// It uses <see cref="IObjectMapper"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// </summary>
|
|||
protected virtual TGetListOutputDto MapToGetListOutputDto(TEntity entity) |
|||
{ |
|||
return ObjectMapper.Map<TEntity, TGetListOutputDto>(entity); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue