Ejemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterRequest req, [FromQuery] string redirect = "")
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            try
            {
                ViewData["RedirectTo"] = new PathString(redirect);
            }
            catch (ArgumentException)
            {
                ViewData["RedirectTo"] = new PathString("");
            }
            var user = await Db.Users.SingleOrDefaultAsync(x => x.Username == req.Username && x.FullName == req.FullName);

            if (user != null)
            {
                ModelState.AddModelError("", "User already exists");
                return(View());
            }
            Invite inv = await Db.Invites
                         .SingleAsync(x => x.Uid == req.InviteID);

            User newUser = new User()
            {
                RoleId       = inv.RoleId, // Anonymous role
                Username     = req.Username,
                FullName     = req.FullName,
                PasswordSalt = AuthUtils.GetRandomData(64)
            };

            newUser.PasswordHash = AuthUtils.GetHashFor(req.Password, newUser.PasswordSalt);
            await Db.AddAsync(newUser);

            Db.Remove(inv);
            await Db.SaveChangesAsync();

            var userIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);

            userIdentity.AddClaims(new Claim[] {
                new Claim(ClaimTypes.PrimarySid, newUser.Id.ToString()),
                new Claim(ClaimTypes.NameIdentifier, newUser.Username),
                new Claim(ClaimTypes.Name, newUser.FullName),
                new Claim(ClaimTypes.Role, newUser.RoleId.ToString())
            });
            var principal = new ClaimsPrincipal(userIdentity);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

            try
            {
                return(Redirect(new PathString(redirect)));
            }
            catch (ArgumentException)
            {
                return(RedirectToAction("News", "Issue"));
            }
        }
        public async Task RemoveAsync(string key)
        {
            using (var ctx = new PBugContext(dbOptions))
            {
                Session s = await ctx.Sessions.FindAsync(Convert.FromBase64String(key));

                if (s != null)
                {
                    ctx.Remove(s);
                    await ctx.SaveChangesAsync();
                }
            }
        }
        public async Task <IActionResult> Create([FromRoute] string path, CreatePageRequest req)
        {
            if (!ModelState.IsValid)
            {
                return(View((object)(path.Replace("%2F", "/", true, CultureInfo.InvariantCulture))));
            }
            if (!HttpContext.UserCan("kb.secrecy." + ((byte)req.Secrecy).ToString()))
            {
                return(Forbid());
            }
            var page = (await Db.Infopages.AddAsync(new Infopage()
            {
                AuthorId = HttpContext.User.IsAnonymous() ? null : new uint?((uint)HttpContext.User.GetUserId()),
                EditorId = HttpContext.User.IsAnonymous() ? null : new uint?((uint)HttpContext.User.GetUserId()),
                DateOfCreation = DateTime.UtcNow,
                DateOfEdit = DateTime.UtcNow,
                Path = path.Replace("%2F", "/", true, CultureInfo.InvariantCulture),
                Name = req.Name,
                Tags = req.Tags ?? "",
                ContainedText = req.Text,
                Secrecy = (byte)req.Secrecy
            })).Entity;

            await Db.KBActivities.AddKBActivity(HttpContext, 0, new CreatePageActivity()
            {
                Infopage      = page,
                Name          = req.Name,
                ContainedText = req.Text,
                Tags          = req.Tags ?? "",
                Secrecy       = (int?)req.Secrecy
            });

            await Db.SaveChangesAsync();

            return(RedirectToAction("ViewPage", new
            {
                path = path.Replace("%2F", "/", true, CultureInfo.InvariantCulture)
            }));
        }
        public async Task RenewAsync(string key, AuthenticationTicket ticket)
        {
            using (var ctx = new PBugContext(dbOptions))
            {
                Session s = await ctx.Sessions.FindAsync(Convert.FromBase64String(key));

                if (s != null)
                {
                    s.SessionData = TicketSerializer.Default.Serialize(ticket);
                    s.Expires     = ticket.Properties.ExpiresUtc.Value;
                    await ctx.SaveChangesAsync();
                }
            }
        }
        public async Task <string> StoreAsync(AuthenticationTicket ticket)
        {
            using (var ctx = new PBugContext(dbOptions))
            {
                Session s = (await ctx.AddAsync(new Session()
                {
                    Id = AuthUtils.GetRandomData(64),
                    SessionData = TicketSerializer.Default.Serialize(ticket),
                    Expires = ticket.Properties.ExpiresUtc
                })).Entity;
                await ctx.SaveChangesAsync();

                return(Convert.ToBase64String(s.Id));
            }
        }
        public async Task <IActionResult> CreateProject(CreateProjectRequest req)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            await Db.Projects.AddAsync(new Project()
            {
                AuthorId       = HttpContext.User.GetUserId() == -1 ? null : new uint?((uint)HttpContext.User.GetUserId()),
                Name           = req.Name,
                ShortProjectId = req.ShortID
            });

            await Db.SaveChangesAsync();

            return(RedirectToAction("Main", "Admin"));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Create(CreateIssueRequest req)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            if (HttpContext.Request.Form.Files.Sum(x => x.Length) >= 128 * 1024 * 1024)
            {
                ModelState.AddModelError("", "File size too large.");
                return(View());
            }
            Issue i = (await Db.Issues.AddAsync(new Issue()
            {
                Name = req.Name,
                Tags = req.Tags ?? "",
                Description = req.Description,
                ProjectId = req.ProjectID,
                AssigneeId = req.AssigneeID == -1 ? null : new uint?((uint)req.AssigneeID),
                AuthorId = HttpContext.User.IsAnonymous() ? null : new uint?((uint)HttpContext.User.GetUserId()),
                DateOfCreation = DateTime.UtcNow,
                Status = IssueStatus.Open
            })).Entity;

            if (!HttpContext.User.IsAnonymous())
            {
                await Db.AddAsync(new IssueWatcher()
                {
                    Issue     = i,
                    WatcherId = (uint)HttpContext.User.GetUserId()
                });
            }

            await Db.IssueActivities.AddIssueActivity(HttpContext, 0, new CreateIssueActivity()
            {
                Issue       = i,
                Name        = req.Name,
                Description = req.Description,
                Tags        = req.Tags ?? "",
                ProjectId   = req.ProjectID,
                AssigneeId  = req.AssigneeID == -1 ? null : new uint?((uint)req.AssigneeID),
            });

            List <(IssueFile, IFormFile)> toProcess = new List <(IssueFile, IFormFile)>();

            foreach (IFormFile file in HttpContext.Request.Form.Files)
            {
                toProcess.Add((
                                  (await Db.IssueFiles.AddAsync(new IssueFile()
                {
                    Issue = i,
                    FileName = file.FileName,
                    FileId = Convert.ToBase64String(AuthUtils.GetRandomData(12))
                             .Replace('/', '_')
                             .Replace('+', '-')
                })).Entity,
                                  file));
            }

            await Db.SaveChangesAsync();

            foreach ((IssueFile, IFormFile)pack in toProcess)
            {
                using (FileStream fs = System.IO.File.OpenWrite(Path.Combine("files", pack.Item1.FileId)))
                    await pack.Item2.CopyToAsync(fs);
            }

            return(RedirectToAction("ViewTalk", new { id = i.Id }));
        }