Beispiel #1
0
        public async Task <IActionResult> OnPostAsync(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Article = await _context.Articles.FindAsync(id);

            var authResult = await _auth.AuthorizeAsync(User, Article, ArticlePolicies.DeleteArticles);

            if (!authResult.Succeeded)
            {
                ArticlesHelper.LogFailure(_logger, Article, authResult.Failure);
                return(new ChallengeResult());
            }

            if (Article != null)
            {
                _context.Articles.Remove(Article);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Beispiel #2
0
        public async Task <IActionResult> OnGetAsync(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Article = await _context.Articles.FirstOrDefaultAsync(
                m => m.Id == id);

            var authResult = await _auth.AuthorizeAsync(
                User, Article, ArticlePolicies.ReadArticles);

            if (!authResult.Succeeded)
            {
                ArticlesHelper.LogFailure(_logger, Article, authResult.Failure);
                return(new ChallengeResult());
            }

            if (Article == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Beispiel #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var authResult = await _auth.AuthorizeAsync(User, Article, ArticlePolicies.UpdateArticles);

            if (!authResult.Succeeded)
            {
                ArticlesHelper.LogFailure(_logger, Article, authResult.Failure);
                return(new ChallengeResult());
            }

            _context.Attach(Article).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ArticleExists(Article.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Beispiel #4
0
        public async Task <IActionResult> OnGetAsync()
        {
            var authResult = await _auth.AuthorizeAsync(User,
                                                        ArticlePolicies.ListArticles2);

            if (!authResult.Succeeded)
            {
                ArticlesHelper.LogFailure(_logger, authResult.Failure);
                return(new ChallengeResult());
            }

            var userMaturity = MaturityHelper.GetMaturity(User);
            var userName     = User.Identity.Name;

            Article = await _context.Articles
                      .EnforceAgeAndOwner(userName, userMaturity)
                      .ToListAsync();

            return(Page());

            //var denied = res
            //    .Where(r => !r.AuthResultTask.Result.Succeeded)
            //    .Select(r => new FailureDescriptor()
            //    {
            //        Article = r.Article,
            //        Failure = r.AuthResultTask.Result.Failure,
            //    })
            //    .ToList();

            //ArticlesHelper.LogFailure(_logger, denied);
        }
Beispiel #5
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var authResult = await _auth.AuthorizeAsync(User, Article, ArticlePolicies.CreateArticles);

            if (!authResult.Succeeded)
            {
                ArticlesHelper.LogFailure(_logger, Article, authResult.Failure);
                return(new ChallengeResult());
            }

            _context.Articles.Add(Article);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Beispiel #6
0
        public async Task OnGet1Async()
        {
            //var authResult = await _auth.AuthorizeAsync(User, new Article(), ArticlePolicies.ListArticles);
            //if (!authResult.Succeeded)
            //{
            //    IsAuthorized = false;
            //    Article = new List<Article>();
            //    return;
            //}

            Article = await _context.Articles.ToListAsync();

            var res = Article.Select(a => new
            {
                Article        = a,
                AuthResultTask = _auth.AuthorizeAsync(User, a,
                                                      ArticlePolicies.ListArticles1),
            }).ToArray();

            var results = await Task.WhenAll(res.Select(t => t.AuthResultTask).ToArray());

            Article = res
                      .Where(r => r.AuthResultTask.Result.Succeeded)
                      .Select(r => r.Article)
                      .ToList();

            var denied = res
                         .Where(r => !r.AuthResultTask.Result.Succeeded)
                         .Select(r => new FailureDescriptor()
            {
                Article = r.Article,
                Failure = r.AuthResultTask.Result.Failure,
            })
                         .ToList();

            ArticlesHelper.LogFailure(_logger, denied);
        }