Skip to content

Commit

Permalink
Added IsAuthorized to ResourceDto
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlcf88 committed Apr 30, 2020
1 parent 5477c73 commit bac3c59
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 19 deletions.
2 changes: 1 addition & 1 deletion common.props
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>0.1.3</Version>
<Version>0.1.4</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Expand Up @@ -14,5 +14,7 @@ public class ResourceDto : FullAuditedEntityDto<Guid>
public string PreviewMediaResources { get; set; }

public bool IsPublished { get; set; }

public bool IsAuthorized { get; set; }
}
}
Expand Up @@ -23,23 +23,19 @@ public class ResourceAppService : CrudAppService<Resource, ResourceDto, Guid, Ge
protected override string UpdatePolicyName { get; set; } = SharedResourcesPermissions.Resources.Update;

private readonly ICategoryDataPermissionProvider _categoryDataPermissionProvider;
private readonly IResourceUserRepository _resourceUserRepository;
private readonly IResourceRepository _repository;

public ResourceAppService(
ICategoryDataPermissionProvider categoryDataPermissionProvider,
IResourceUserRepository resourceUserRepository,
IResourceRepository repository) : base(repository)
{
_categoryDataPermissionProvider = categoryDataPermissionProvider;
_resourceUserRepository = resourceUserRepository;
_repository = repository;
}

protected override IQueryable<Resource> CreateFilteredQuery(GetResourceListDto input)
{
var query = input.AuthorizedOnly ? _repository.GetUserAuthorizedOnlyQueryable(CurrentUser.GetId()) : _repository;

return query.Where(x => x.CategoryId == input.CategoryId);
}

public override async Task<ResourceDto> GetAsync(Guid id)
{
var resource = await GetEntityByIdAsync(id);
Expand All @@ -49,28 +45,37 @@ public override async Task<ResourceDto> GetAsync(Guid id)
throw new EntityNotFoundException(typeof(Resource), id);
}

return MapToGetOutputDto(resource);
var dto = MapToGetOutputDto(resource);

dto.IsAuthorized = await _resourceUserRepository.FindAsync(id, CurrentUser.GetId()) != null;

return dto;
}

public override async Task<PagedResultDto<ResourceDto>> GetListAsync(GetResourceListDto input)
{
var query = CreateFilteredQuery(input);
var query = _repository.GetQueryableWithAuthorizationStatus(CurrentUser.GetId(), input.AuthorizedOnly)
.Where(x => x.Resource.CategoryId == input.CategoryId);

if (!await _categoryDataPermissionProvider.IsCurrentUserAllowedToManageAsync(input.CategoryId))
{
query = query.Where(x => x.IsPublished);
query = query.Where(x => x.Resource.IsPublished);
}

var totalCount = await AsyncQueryableExecuter.CountAsync(query);

query = ApplySorting(query, input);
query = ApplyPaging(query, input);

query = query.PageBy(input);

var resources = await AsyncQueryableExecuter.ToListAsync(query);

return new PagedResultDto<ResourceDto>(
totalCount,
resources.Select(MapToGetListOutputDto).ToList()
resources.Select(x =>
{
var dto = ObjectMapper.Map<Resource, ResourceDto>(x.Resource);
dto.IsAuthorized = x.IsAuthorized;
return dto;
}).ToList()
);
}

Expand Down
Expand Up @@ -21,7 +21,8 @@ public SharedResourcesApplicationAutoMapperProfile()
CreateMap<Category, CategoryDto>();
CreateMap<CreateUpdateCategoryDto, Category>(MemberList.Source)
.ForSourceMember(dto => dto.SetToCommon, opt => opt.DoNotValidate());
CreateMap<Resource, ResourceDto>();
CreateMap<Resource, ResourceDto>()
.Ignore(dto => dto.IsAuthorized);
CreateMap<CreateUpdateResourceDto, Resource>(MemberList.Source);
CreateMap<ResourceItem, ResourceItemDto>();
CreateMap<CreateUpdateResourceItemDto, ResourceItem>(MemberList.Source);
Expand Down
Expand Up @@ -7,5 +7,8 @@ namespace EasyAbp.SharedResources.Resources
public interface IResourceRepository : IRepository<Resource, Guid>
{
IQueryable<Resource> GetUserAuthorizedOnlyQueryable(Guid userId);

IQueryable<ResourceAuthorizationQueryModel> GetQueryableWithAuthorizationStatus(Guid userId,
bool getAuthorizedOnly);
}
}
@@ -0,0 +1,9 @@
namespace EasyAbp.SharedResources.Resources
{
public class ResourceAuthorizationQueryModel
{
public Resource Resource { get; set; }

public bool IsAuthorized { get; set; }
}
}
Expand Up @@ -19,5 +19,41 @@ public virtual IQueryable<Resource> GetUserAuthorizedOnlyQueryable(Guid userId)
where resourceUser.UserId == userId
select resource;
}

public virtual IQueryable<ResourceAuthorizationQueryModel> GetQueryableWithAuthorizationStatus(Guid userId,
bool getAuthorizedOnly)
{
if (getAuthorizedOnly)
{
return from resource in DbContext.Resources
join resourceUser in DbContext.ResourceUsers on
new
{
ResourceId = resource.Id,
UserId = userId
}
equals new
{
ResourceId = resourceUser.ResourceId,
UserId = resourceUser.UserId
}
select new ResourceAuthorizationQueryModel {Resource = resource, IsAuthorized = true};
}

return from resource in DbContext.Resources
join resourceUser in DbContext.ResourceUsers on
new
{
ResourceId = resource.Id,
UserId = userId
}
equals new
{
ResourceId = resourceUser.ResourceId,
UserId = resourceUser.UserId
} into resourceUsers
from ru in resourceUsers.DefaultIfEmpty()
select new ResourceAuthorizationQueryModel {Resource = resource, IsAuthorized = ru != null};
}
}
}
Expand Up @@ -18,9 +18,12 @@ public SharedResourcesWebAutoMapperProfile()
/* You can configure your AutoMapper mapping configuration here.
* Alternatively, you can split your mapping configurations
* into multiple profile classes for a better organization. */
CreateMap<CategoryDto, CreateEditCategoryViewModel>().Ignore(model => model.SetToCommon);
CreateMap<CreateEditCategoryViewModel, CreateUpdateCategoryDto>().Ignore(dto => dto.CustomMark);
CreateMap<ResourceDto, CreateEditResourceViewModel>();
CreateMap<CategoryDto, CreateEditCategoryViewModel>()
.Ignore(model => model.SetToCommon);
CreateMap<CreateEditCategoryViewModel, CreateUpdateCategoryDto>()
.Ignore(dto => dto.CustomMark);
CreateMap<ResourceDto, CreateEditResourceViewModel>()
.ForSourceMember(dto => dto.IsAuthorized, opt => opt.DoNotValidate());
CreateMap<CreateEditResourceViewModel, CreateUpdateResourceDto>();
CreateMap<ResourceItemDto, CreateEditResourceItemViewModel>();
CreateMap<CreateEditResourceItemViewModel, CreateUpdateResourceItemDto>();
Expand Down

0 comments on commit bac3c59

Please sign in to comment.