Exemplo n.º 1
0
        protected virtual IActionResult Get(MaterialView materialView)
        {
            if (materialView == null)
            {
                return(BadRequest());
            }

            var category = categoriesCache.GetCategory(materialView.CategoryName);

            if (!materialsAuthorization.CanGet(User.Roles, category))
            {
                return(Unauthorized());
            }

            if (materialView.IsHidden && !materialsAuthorization.CanHide(User.Roles, category))
            {
                return(Unauthorized());
            }

            if (materialView.DeletedDate != null && !materialsAuthorization.CanRestoreAsync(User, category.Id))
            {
                return(Unauthorized());
            }

            materialView.VisitsCount += materialsVisitsCounterCache.CountMaterial(UserOrIpKey, materialView.Id);

            return(Json(materialView));
        }
Exemplo n.º 2
0
        public virtual async Task <IActionResult> GetNewTopics(string categoryName, int page = 1)
        {
            var categoryParent = categoriesCache.GetCategory(categoryName);

            if (categoryParent == null)
            {
                return(BadRequest());
            }

            var allCategories = categoryParent.AllSubCategories.Where(x => x.IsMaterialsContainer);

            var categories =
                authorizationService.GetAllowedCategories(User.Roles, allCategories,
                                                          OperationKeys.MaterialAndCommentsRead);

            var categoriesIds = categories.Select(x => x.Id).ToArray();

            async Task <IPagedList <TopicInfoView> > LoadDataAsync()
            {
                return(await forumPresenter.GetNewTopics(categoriesIds,
                                                         page, forumOptions.NewTopicsPageSize, forumOptions.NewTopicsMaxPages));
            }

            return(await CacheContentAsync(categoryParent, categoriesIds, LoadDataAsync, page));
        }
Exemplo n.º 3
0
        protected virtual IActionResult Get(MaterialView materialView)
        {
            if (materialView == null)
            {
                return(BadRequest());
            }

            var category = categoriesCache.GetCategory(materialView.CategoryName);

            if (!materialsAuthorization.CanGet(User.Roles, category))
            {
                return(Unauthorized());
            }

            if (materialView.IsHidden && !materialsAuthorization.CanHide(User.Roles, category))
            {
                return(Unauthorized());
            }

            if (materialView.IsDeleted && !materialsAuthorization.CanRestoreAsync(User, category.Id))
            {
                return(Unauthorized());
            }

            return(Json(materialView));
        }
Exemplo n.º 4
0
        public virtual async Task <IActionResult> GetById(int id)
        {
            int?categoryId = await materialsManager.GetMaterialCategoryIdAsync(id);

            if (categoryId == null)
            {
                return(BadRequest());
            }

            var category = categoriesCache.GetCategory(categoryId.Value);

            if (!materialsAuthorization.CanGet(User.Roles, category))
            {
                return(Unauthorized());
            }


            var materialViewModel = await materialsPresenter.GetViewModelAsync(id);

            return(Json(materialViewModel));
        }
        public virtual async Task <IActionResult> GetArticles(
            string categoryName, OrderType sort = OrderType.PublishDate, int page = 1, bool showDeleted = false)
        {
            CategoryCached category = categoriesCache.GetCategory(categoryName);

            if (category == null)
            {
                return(BadRequest());
            }

            if (!authorizationService.HasAccess(User.Roles, category, OperationKeys.MaterialAndCommentsRead))
            {
                return(Unauthorized());
            }

            var options = new MaterialsShowOptions
            {
                CategoryId = category.Id,
                orderType  = sort,
                Page       = page,
                PageSize   = articlesOptions.CurrentValue.CategoryPageSize
            };

            if (authorizationService.HasAccess(User.Roles, category, OperationKeys.MaterialHide))
            {
                options.ShowHidden = true;
            }

            if (showDeleted && authorizationService.HasAccess(User.Roles, category, OperationKeys.MaterialDeleteAny))
            {
                options.ShowDeleted = true;
            }


            async Task <IPagedList <ArticleInfoView> > LoadDataAsync()
            {
                return(await articlesPresenter.GetArticlesAsync(options));
            }

            if (showDeleted)
            {
                return(Ok(await LoadDataAsync()));
            }

            return(await CacheContentAsync(category, category.Id, LoadDataAsync, page));
        }
Exemplo n.º 6
0
        public virtual async Task <IActionResult> GetPosts(string categoryName, int page = 1, bool showDeleted = false)
        {
            var category = categoriesCache.GetCategory(categoryName);

            if (category == null)
            {
                return(BadRequest());
            }


            if (!authorizationService.HasAccess(User.Roles, category, OperationKeys.MaterialAndCommentsRead))
            {
                return(Unauthorized());
            }


            var options = new MaterialsShowOptions
            {
                CategoryId = category.Id,
                Page       = page,
                PageSize   = blogOptions.CurrentValue.PostsPageSize
            };

            if (authorizationService.HasAccess(User.Roles, category, OperationKeys.MaterialHide))
            {
                options.ShowHidden = true;
            }

            if (showDeleted && authorizationService.HasAccess(User.Roles, category, OperationKeys.MaterialDeleteAny))
            {
                options.ShowDeleted = true;
            }

            if (showDeleted)
            {
                return(Ok(await LoadDataAsync()));
            }

            return(await CacheContentAsync(category, category.Id, LoadDataAsync, page));


            async Task <IPagedList <PostView> > LoadDataAsync() => await blogPresenter.GetPostsAsync(options);
        }
Exemplo n.º 7
0
        public virtual async Task <IActionResult> GetArticles(string categoryName, ArticlesOrderType sort = ArticlesOrderType.PublishDate, int page = 1)
        {
            CategoryCached category = categoriesCache.GetCategory(categoryName);

            if (category == null)
            {
                return(BadRequest());
            }

            if (!authorizationService.HasAccess(User.Roles, category, OperationKeys.MaterialAndCommentsRead))
            {
                return(Unauthorized());
            }

            async Task <IPagedList <ArticleInfoView> > LoadDataAsync()
            {
                return(await articlesPresenter.GetArticlesAsync(category.Id, sort, page, articlesOptions.CategoryPageSize));
            }

            return(await CacheContentAsync(category, category.Id, LoadDataAsync, page));
        }
Exemplo n.º 8
0
        public virtual async Task <IActionResult> GetPosts(string categoryName, int page = 1)
        {
            var category = categoriesCache.GetCategory(categoryName);

            if (category == null)
            {
                return(BadRequest());
            }

            if (!authorizationService.HasAccess(User.Roles, category, OperationKeys.MaterialAndCommentsRead))
            {
                return(Unauthorized());
            }

            async Task <IPagedList <PostView> > LoadDataAsync()
            {
                return(await blogPresenter.GetPostsAsync(category.Id, page, blogOptions.PostsPageSize));
            }

            return(await CacheContentAsync(category, category.Id, LoadDataAsync, page));
        }
Exemplo n.º 9
0
 private int?GetCategoryId(string categoryName)
 {
     return(categoriesCache.GetCategory(Normalizer.Normalize(categoryName))?.Id);
 }
Exemplo n.º 10
0
 public bool HasAccess(IReadOnlyDictionary <string, RoleCached> roles, int categoryId, int operationKey)
 {
     return(HasAccess(roles, categoriesCache.GetCategory(categoryId), operationKey));
 }