Ejemplo n.º 1
0
        public async Task <HttpResponseMessage> AddArticle(string code, [FromBody] ArticlePostData body)
        {
            _auditContext.Operation = OperationType.AddArticle;

            if (body == null || string.IsNullOrWhiteSpace(body.Title))
            {
                return(Request.CreateResponse(System.Net.HttpStatusCode.BadRequest));
            }

            var user = _identity.GetUserInfo();

            if (user == null)
            {
                return(Unauthorized());
            }

            var e = Session.Query <Editathon>()
                    .Fetch(_ => _.Rules)
                    .Fetch(_ => _.Articles)
                    .SingleOrDefault(i => i.Code == code);

            if (e == null)
            {
                return(NotFound());
            }

            var now = DateTime.UtcNow;

            if (now < e.Start || now.Date > e.Finish)
            {
                return(Forbidden());
            }

            if (e.Articles.Any(a => a.Name == body.Title))
            {
                return(Forbidden());
            }

            var wiki = MediaWikis.Create(e.Wiki, _identity);

            if (user.Username != body.User)
            {
                if (!e.Jury.Contains(user.Username))
                {
                    return(Forbidden());
                }
                else
                {
                    user = await wiki.GetUser(body.User);

                    if (user == null)
                    {
                        return(Forbidden());
                    }
                }
            }

            var page = await wiki.GetPage(body.Title);

            if (page == null)
            {
                return(Forbidden());
            }

            var rules = e.Rules
                        .Where(r => !r.Flags.HasFlag(RuleFlags.Optional))
                        .Select(r => r.Get())
                        .ToArray();

            if (rules.Any())
            {
                var loader = new ArticleDataLoader(rules.SelectMany(r => r.GetReqs()));
                var data   = await loader.LoadAsync(wiki, body.Title);

                var ctx = new RuleContext {
                    User = user
                };
                foreach (var rule in rules)
                {
                    if (!rule.Check(data, ctx))
                    {
                        return(Forbidden());
                    }
                }
            }

            if (e.Template != null)
            {
                await UpdateTemplate(wiki, user, body.Title, page, (JObject)e.Template.DeepClone());
            }

            e.Articles.Add(new Article
            {
                Name      = body.Title,
                User      = user.Username,
                DateAdded = now,
            });

            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task AddArticle(EditathonCode code, [FromBody] ArticlePostData body)
        {
            if (body == null || string.IsNullOrWhiteSpace(body.Title))
            {
                throw BadRequest();
            }

            var user = _identity.GetUserInfo();

            var e = code.Get(q => q
                             .Fetch(_ => _.Jury)
                             .Fetch(_ => _.Rules)
                             .Fetch(_ => _.Articles));

            var now = DateTime.UtcNow;

            if (now < e.Start || now.Date > e.Finish)
            {
                throw Forbidden();
            }

            if (e.Articles.Any(a => a.Name == body.Title))
            {
                throw Forbidden();
            }

            var wiki = MediaWikis.Create(e.Wiki, _identity);

            if (user.Username != body.User)
            {
                if (!e.Jury.Contains(user.Username))
                {
                    throw Forbidden();
                }
                else
                {
                    user = await wiki.GetUser(body.User);

                    if (user == null)
                    {
                        throw Forbidden();
                    }
                }
            }

            var page = await wiki.GetPage(body.Title);

            if (page == null)
            {
                throw Forbidden();
            }

            var rules = e.Rules
                        .Where(r => !r.Flags.HasFlag(RuleFlags.Optional))
                        .Select(r => r.Get())
                        .ToArray();

            if (rules.Any())
            {
                var loader = new ArticleDataLoader(rules.SelectMany(r => r.GetReqs()));
                var data   = await loader.LoadAsync(wiki, body.Title);

                var ctx = new RuleContext {
                    User = user
                };
                foreach (var rule in rules)
                {
                    if (!rule.Check(data, ctx))
                    {
                        throw Forbidden();
                    }
                }
            }

            if (e.Template != null)
            {
                await UpdateTemplate(wiki, user, body.Title, page, e.Template, ControllerContext.ResolveUrl($"~/editathons/{e.Code}"));
            }

            e.Articles.Add(new Article
            {
                Name      = body.Title,
                User      = user.Username,
                DateAdded = now,
            });
        }