Commit 3cc79680 authored by gdlcf88's avatar gdlcf88

Permission check for unpublished products, close #14

parent 1898c1ac
...@@ -20,6 +20,8 @@ namespace EasyAbp.EShop.Products.Products ...@@ -20,6 +20,8 @@ namespace EasyAbp.EShop.Products.Products
Task<ProductDto> UpdateSkuAsync(Guid productId, Guid productSkuId, Guid storeId, UpdateProductSkuDto input); Task<ProductDto> UpdateSkuAsync(Guid productId, Guid productSkuId, Guid storeId, UpdateProductSkuDto input);
Task<ProductDto> GetAsync(Guid id, Guid storeId);
Task<ProductDto> DeleteSkuAsync(Guid productId, Guid productSkuId, Guid storeId); Task<ProductDto> DeleteSkuAsync(Guid productId, Guid productSkuId, Guid storeId);
} }
} }
\ No newline at end of file
...@@ -32,14 +32,17 @@ namespace EasyAbp.EShop.Products.Categories ...@@ -32,14 +32,17 @@ namespace EasyAbp.EShop.Products.Categories
return input.ShowHidden ? query : query.Where(x => !x.IsHidden); return input.ShowHidden ? query : query.Where(x => !x.IsHidden);
} }
public override Task<PagedResultDto<CategoryDto>> GetListAsync(GetCategoryListDto input) public override async Task<PagedResultDto<CategoryDto>> GetListAsync(GetCategoryListDto input)
{ {
if (input.ShowHidden) // Todo: Check if current user is an admin of the store.
var isCurrentUserStoreAdmin = true;
if (input.ShowHidden && (!isCurrentUserStoreAdmin || !await AuthorizationService.IsGrantedAsync(ProductsPermissions.Categories.Default)))
{ {
AuthorizationService.CheckAsync(ProductsPermissions.Products.Default); throw new NotAllowedToGetCategoryListWithShowHiddenException();
} }
return base.GetListAsync(input); return await base.GetListAsync(input);
} }
} }
} }
\ No newline at end of file
using Volo.Abp;
namespace EasyAbp.EShop.Products.Categories
{
public class NotAllowedToGetCategoryListWithShowHiddenException : BusinessException
{
public NotAllowedToGetCategoryListWithShowHiddenException() : base(
message: $"You have no permission to get category list with hidden categories.")
{
}
}
}
\ No newline at end of file
using System;
using Volo.Abp;
namespace EasyAbp.EShop.Products.Products
{
public class NotAllowedToGetProductListWithShowHiddenException : BusinessException
{
public NotAllowedToGetProductListWithShowHiddenException() : base(
message: $"You have no permission to get product list with hidden products.")
{
}
}
}
\ No newline at end of file
...@@ -163,29 +163,62 @@ namespace EasyAbp.EShop.Products.Products ...@@ -163,29 +163,62 @@ namespace EasyAbp.EShop.Products.Products
} }
[RemoteService(false)] [RemoteService(false)]
public override async Task DeleteAsync(Guid id) public override Task DeleteAsync(Guid id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public override async Task<ProductDto> GetAsync(Guid id) [RemoteService(false)]
public override Task<ProductDto> GetAsync(Guid id)
{
throw new NotImplementedException();
}
public virtual async Task<ProductDto> GetAsync(Guid id, Guid storeId)
{ {
var dto = await base.GetAsync(id); var dto = await base.GetAsync(id);
if (!dto.IsPublished)
{
await CheckStoreIsProductOwnerAsync(id, storeId);
}
dto.CategoryIds = (await _productCategoryRepository.GetListByProductIdAsync(dto.Id)) dto.CategoryIds = (await _productCategoryRepository.GetListByProductIdAsync(dto.Id))
.Select(x => x.CategoryId).ToList(); .Select(x => x.CategoryId).ToList();
return dto; return dto;
} }
public override Task<PagedResultDto<ProductDto>> GetListAsync(GetProductListDto input) public override async Task<PagedResultDto<ProductDto>> GetListAsync(GetProductListDto input)
{ {
if (input.ShowHidden) await CheckGetListPolicyAsync();
// Todo: Check if current user is an admin of the store.
var isCurrentUserStoreAdmin = true;
if (input.ShowHidden && (!isCurrentUserStoreAdmin || !await AuthorizationService.IsGrantedAsync(ProductsPermissions.Products.Default)))
{ {
AuthorizationService.CheckAsync(ProductsPermissions.Products.Default); throw new NotAllowedToGetProductListWithShowHiddenException();
} }
var query = CreateFilteredQuery(input);
return base.GetListAsync(input); if (!isCurrentUserStoreAdmin)
{
query = query.Where(x => x.IsPublished);
}
var totalCount = await AsyncQueryableExecuter.CountAsync(query);
query = ApplySorting(query, input);
query = ApplyPaging(query, input);
var entities = await AsyncQueryableExecuter.ToListAsync(query);
return new PagedResultDto<ProductDto>(
totalCount,
entities.Select(MapToGetListOutputDto).ToList()
);
} }
public async Task DeleteAsync(Guid id, Guid storeId) public async Task DeleteAsync(Guid id, Guid storeId)
......
...@@ -58,7 +58,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.Product ...@@ -58,7 +58,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.Product
{MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount}))?.Items {MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount}))?.Items
.Select(dto => new SelectListItem(dto.DisplayName, dto.Id.ToString())).ToList(); .Select(dto => new SelectListItem(dto.DisplayName, dto.Id.ToString())).ToList();
var productDto = await _service.GetAsync(Id); var productDto = await _service.GetAsync(Id, storeId);
var detailDto = await _productDetailAppService.GetAsync(productDto.ProductDetailId); var detailDto = await _productDetailAppService.GetAsync(productDto.ProductDetailId);
...@@ -75,7 +75,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.Product ...@@ -75,7 +75,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.Product
public virtual async Task<IActionResult> OnPostAsync() public virtual async Task<IActionResult> OnPostAsync()
{ {
var product = await _service.GetAsync(Id); var product = await _service.GetAsync(Id, Product.StoreId);
var detail = await _productDetailAppService.GetAsync(product.ProductDetailId); var detail = await _productDetailAppService.GetAsync(product.ProductDetailId);
......
...@@ -44,7 +44,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.ProductSku ...@@ -44,7 +44,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.ProductSku
public virtual async Task OnGetAsync() public virtual async Task OnGetAsync()
{ {
var product = await _productAppService.GetAsync(ProductId); var product = await _productAppService.GetAsync(ProductId, StoreId);
Attributes = new Dictionary<string, ICollection<SelectListItem>>(); Attributes = new Dictionary<string, ICollection<SelectListItem>>();
......
...@@ -36,7 +36,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.ProductSku ...@@ -36,7 +36,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.ProductSku
public virtual async Task OnGetAsync() public virtual async Task OnGetAsync()
{ {
var product = await _productAppService.GetAsync(ProductId); var product = await _productAppService.GetAsync(ProductId, StoreId);
ProductSku = ProductSku =
ObjectMapper.Map<ProductSkuDto, CreateEditProductSkuViewModel>( ObjectMapper.Map<ProductSkuDto, CreateEditProductSkuViewModel>(
......
...@@ -25,7 +25,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.ProductSku ...@@ -25,7 +25,7 @@ namespace EasyAbp.EShop.Products.Web.Pages.EShop.Products.Products.ProductSku
public virtual async Task OnGetAsync() public virtual async Task OnGetAsync()
{ {
ProductDisplayName = (await _productAppService.GetAsync(ProductId)).DisplayName; ProductDisplayName = (await _productAppService.GetAsync(ProductId, StoreId)).DisplayName;
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment