Exemplo n.º 1
0
        private void SetupUserAndTicket(Guid userId, Guid ticketId, TrackilyContext context)
        {
            var user = new TrackilyUser()
            {
                Id             = userId,
                UserName       = MockUsername,
                CreatedTickets = new List <Ticket>(),
                AssignedTo     = new List <UserTicket>()
            };

            context.Users.Add(user);
            context.SaveChanges();

            var userSaved = context.Users.Find(userId);
            var ticket    = new Ticket()
            {
                Project  = new Project(),
                Creator  = userSaved,
                TicketId = ticketId,
                Assigned = new List <UserTicket>()
            };

            context.Tickets.Add(ticket);
            context.SaveChanges();
        }
Exemplo n.º 2
0
 public UserTicket CreateUserTicket(TrackilyUser user, Ticket ticket)
 {
     return(new UserTicket
     {
         Id = user.Id,
         User = user,
         TicketId = ticket.TicketId,
         Ticket = ticket
     });
 }
Exemplo n.º 3
0
 public UserProject CreateUserProject(TrackilyUser user, Project project)
 {
     return(new UserProject()
     {
         Id = user.Id,
         User = user,
         ProjectId = project.ProjectId,
         Project = project
     });
 }
Exemplo n.º 4
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            if (ModelState.IsValid)
            {
                var user = new TrackilyUser
                {
                    FirstName = Input.FirstName,
                    LastName  = Input.LastName,
                    UserName  = Input.UserName,
                    Role      = Input.UserRole,
                    Email     = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    var userClaims = new List <Claim>
                    {
                        new Claim("UserName", Input.UserName)
                    };
                    if (Input.UserRole == TrackilyUser.UserRole.Manager)
                    {
                        userClaims.Add(new Claim("IsManager", "True"));
                    }
                    await _userManager.AddClaimsAsync(user, userClaims);

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Exemplo n.º 5
0
        // Remove the tickets created by the user from the project and unassign user from all tickets in the project.
        public void RemoveTicketsFromProject(TrackilyUser user, Project project)
        {
            var ticketsCreatedByUser = _context.UserTickets
                                       .Include(ut => ut.Ticket)
                                       .ThenInclude(t => t.Creator)
                                       .Include(ut => ut.Ticket)
                                       .ThenInclude(t => t.Project)
                                       .Where(ut => ut.Ticket.Creator.Id == user.Id &
                                              ut.Ticket.Project.ProjectId == project.ProjectId)
                                       .ToList();

            foreach (var userTicket in ticketsCreatedByUser)
            {
                project.Tickets.Remove(userTicket.Ticket);
                _context.UserTickets.Remove(userTicket);
            }

            var projectTicketsUserAssignedTo = _context.UserTickets.Where(ut => ut.User == user).ToList();

            _context.UserTickets.RemoveRange(projectTicketsUserAssignedTo);
        }
Exemplo n.º 6
0
        public static void SeedUsers(UserManager <TrackilyUser> userManager)
        {
            if (userManager.FindByNameAsync(DevGuestName).Result == null)
            {
                var devGuest = new TrackilyUser
                {
                    Role      = TrackilyUser.UserRole.Developer,
                    FirstName = "Developer",
                    LastName  = "Guest",
                    UserName  = DevGuestName,
                    Email     = "*****@*****.**"
                };

                var createResult = userManager.CreateAsync(devGuest, DevGuestPassword).Result;
                if (!createResult.Succeeded)
                {
                    throw new Exception("Error seeding database with devguest.");
                }
            }

            if (userManager.FindByNameAsync(ManagerGuestName).Result == null)
            {
                var manGuest = new TrackilyUser
                {
                    Role      = TrackilyUser.UserRole.Manager,
                    FirstName = "Manager",
                    LastName  = "Guest",
                    UserName  = ManagerGuestName,
                    Email     = "*****@*****.**"
                };

                var createResult = userManager.CreateAsync(manGuest, ManagerGuestPassword).Result;
                if (!createResult.Succeeded)
                {
                    throw new Exception("Error seeding database with manguest.");
                }
            }
        }
Exemplo n.º 7
0
        public void DeletingTicket_DeletesUserTicket()
        {
            var ticketId = Guid.NewGuid();
            var userId   = Guid.NewGuid();

            var options = GetInMemoryDbContextOptions();

            using (var context = new TrackilyContext(options))
            {
                context.Database.EnsureCreated();

                var user = new TrackilyUser()
                {
                    Id             = userId,
                    CreatedTickets = new List <Ticket>(),
                    AssignedTo     = new List <UserTicket>()
                };

                context.Users.Add(user); // User must exist in the database before creating a Ticket/UserTicket due to FK constraint.
                context.SaveChanges();
            }

            using (var context = new TrackilyContext(options))
            {
                var userSaved = context.Users.Find(userId);
                Assert.NotNull(userSaved);

                var ticket = new Ticket()
                {
                    Project  = new Project(),
                    Creator  = userSaved,
                    TicketId = ticketId,
                    Assigned = new List <UserTicket>()
                };

                var userTicket = new UserTicket()
                {
                    Id       = userId,
                    TicketId = ticketId,
                    Ticket   = ticket,
                    User     = userSaved
                };

                ticket.Assigned.Add(userTicket);

                context.Tickets.Add(ticket);
                context.SaveChanges();
            }

            using (var context = new TrackilyContext(options))
            {
                var ticketSaved = context.Tickets.Find(ticketId);
                Assert.NotNull(ticketSaved);

                var userTicketSaved = context.UserTickets.Find(userId, ticketId);
                Assert.NotNull(userTicketSaved);

                context.Tickets.Remove(ticketSaved);
                context.SaveChanges();
            }

            using (var context = new TrackilyContext(options))
            {
                var tickets = context.Tickets.ToList();
                Assert.Empty(tickets);

                var userTickets = context.UserTickets.ToList();
                Assert.Empty(userTickets);
            }
        }