private void AddErrors(IdentityResult result) { foreach (var error in result.Errors) { TempData.AddDangerToast(error); } }
public ActionResult RespondNotif(int id) { var userId = User.Identity.GetUserId(); var notif = db.TicketNotifications.FirstOrDefault(n => n.Id == id); if (notif == null || notif.ParentTicket == null) { TempData.AddDangerToast("We were unable to locate the notification, or the ticket it is linked to. Please try again later."); return(RedirectToAction("Index", "Tickets")); } if (notif.RecipientId != userId) { TempData.AddDangerToast("You are not allowed to view this resource."); return(RedirectToAction("Index", "Tickets")); } notif.IsRead = true; //also mark other notifs to this user on this ticket as read. foreach (var n in db.TicketNotifications.Where(n => n.RecipientId == userId && n.ParentTicketId == notif.ParentTicketId)) { n.IsRead = true; } db.SaveChanges(); return(RedirectToAction("Show", "Tickets", new { ticketIdentifier = notif.ParentTicket.GetTicketIdentifier() })); }
public async Task <ActionResult> ChangeName(ChangeNameViewModel model) { var userId = User.Identity.GetUserId(); try { if (!ModelState.IsValid) { throw new Exception(); } var oldFullName = db.Users.Find(userId).FullNameStandard; db.Users.Find(User.Identity.GetUserId()).FirstName = model.FirstName; db.Users.Find(User.Identity.GetUserId()).LastName = model.LastName; await db.SaveChangesAsync(); TempData.AddSuccessToast($"Your name has been changed from '{oldFullName}' to '{model.FirstName} {model.LastName}'."); } catch { TempData.AddDangerToast("We were unable to update your name. Please try again later."); } var viewModel = new IndexViewModel() { HasPassword = HasPassword(), PhoneNumber = await UserManager.GetPhoneNumberAsync(userId), TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId), Logins = await UserManager.GetLoginsAsync(userId), BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId), ThisUser = db.Users.Find(User.Identity.GetUserId()) }; return(View(model.ReturnViewName, viewModel)); }
public ActionResult Edit(string ticketIdentifier) { if (string.IsNullOrWhiteSpace(ticketIdentifier)) { TempData.AddDangerToast("We were unable to locate a ticket with this identifier."); return(RedirectToAction("Index", "Projects")); } Ticket ticket = db.Tickets.ToList().FirstOrDefault(t => t.GetTicketIdentifier() == ticketIdentifier); if (ticket == null) { TempData.AddDangerToast("We were unable to locate a ticket with this identifier."); return(RedirectToAction("Index", "Projects")); } if (!_permissionsHelper.CanEditTicket(User, ticket.Id)) { TempData.AddDangerToast("You are not allowed to view this resource / perform this action."); return(RedirectToAction("Index", "Home")); } ProjectWorkflow parentProjWorkflow = ticket.ParentProject.ActiveWorkflow; List <ApplicationUser> solvers = null; if (_permissionsHelper.IsProjectManager(User, ticket.ParentProjectId) || User.IsInRole("ServerAdmin")) { solvers = ticket.ParentProject.GetSolverMembers(); } List <TicketStatus> availStatuses = null; if ((_permissionsHelper.IsUserStaff(User) && parentProjWorkflow.CanStaffSetStatusOnInteract) || (_permissionsHelper.IsTicketOwner(User, ticket.Id) && parentProjWorkflow.CanTicketOwnerSetStatusOnInteract)) { availStatuses = parentProjWorkflow.GetAvailableStatuses(ticket.Id, User, db); } var viewModel = new EditTicketViewModel() { ParentProject = ticket.ParentProject, Reporter = db.Users.Find(User.Identity.GetUserId()), PrioritySelections = db.TicketPriorityTypes.ToDictionary(tp => tp.Name, tp => tp.Id), AvailableTicketTypes = db.TicketTypes.ToList(), AvailableStatuses = availStatuses, CurrentTicket = ticket, AvailableSolvers = solvers }; return(View(viewModel)); }
public ActionResult Show(string ticketIdentifier) { if (string.IsNullOrWhiteSpace(ticketIdentifier)) { TempData.AddDangerToast("You must provide the ticket identifier to display. Please try again."); return(RedirectToAction("Index", "Tickets")); } Ticket ticket = db.Tickets.ToList().FirstOrDefault(t => t.GetTicketIdentifier() == ticketIdentifier); if (ticket == null) { TempData.AddDangerToast("We were unable to locate this ticket. Please try again."); return(RedirectToAction("Index", "Tickets")); } if (!_permissionsHelper.CanViewTicket(User, ticket.Id)) { TempData.AddDangerToast("You are not allowed to view this resource."); return(RedirectToAction("Index", "Tickets")); } ProjectWorkflow parentProjWorkflow = ticket.ParentProject.ActiveWorkflow; List <ApplicationUser> solvers = null; if (User.Identity.GetUserId() == ticket.ParentProject.ProjectManagerId || User.IsInRole("ServerAdmin")) { solvers = ticket.ParentProject.GetSolverMembers(); } List <TicketStatus> availStatuses = null; if (ticket.Permissions(User).CanUpdateTicketStatus) { availStatuses = parentProjWorkflow.GetAvailableStatuses(ticket.Id, User, db); } var viewModel = new EditTicketViewModel() { ParentProject = ticket.ParentProject, Reporter = ticket.Reporter, PrioritySelections = db.TicketPriorityTypes.ToDictionary(tp => tp.Name, tp => tp.Id), AvailableTicketTypes = db.TicketTypes.ToList(), CurrentTicket = ticket, AvailableSolvers = solvers, AvailableStatuses = availStatuses }; return(View(viewModel)); }
public ActionResult ArchiveTicket(int id) { if (!_permissionsHelper.CanArchiveTicket(User, id)) { TempData.AddDangerToast("You are not allowed to perform this action."); RedirectToAction("Show", "Tickets", new { TicketIdentifier = db.Tickets.First(t => t.Id == id).GetTicketIdentifier() }); } Ticket ticket = db.Tickets.First(t => t.Id == id); var historyEntry = ticket.ArchiveTicket(User, db); db.TicketHistoryEntries.Add(historyEntry); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult RemoveWatch(string ticketIdentifier) { if (string.IsNullOrWhiteSpace(ticketIdentifier)) { TempData.AddDangerToast("You must provide the ticket identifier to display. Please try again."); return(RedirectToAction("Index", "Tickets")); } Ticket ticket = db.Tickets.ToList().FirstOrDefault(t => t.GetTicketIdentifier() == ticketIdentifier); if (ticket == null) { TempData.AddDangerToast("We were unable to locate this ticket. Please try again."); return(RedirectToAction("Index", "Tickets")); } if (!_permissionsHelper.CanViewTicket(User, ticket.Id)) { TempData.AddDangerToast("You are not allowed to view this resource."); return(RedirectToAction("Index", "Tickets")); } try { var watches = ticket.Watchers.Where(w => w.WatcherId == User.Identity.GetUserId()).ToList(); if (!watches.Any()) { throw new Exception(); } //throw error if no watch records exist for this user on this ticket //remove any existance of watches on this ticket for this user, and notify if changes saved successfully foreach (TicketNotificationWatchEntry w in watches) { ticket.Watchers.Remove(w); db.TicketNotificationWatchEntries.Remove(w); } db.SaveChanges(); TempData.AddInfoToast($"You have successfully unsubscribed from ticket {ticket.GetTicketIdentifier()}'s notifications. You will be no longer be notified of changes."); } catch { TempData.AddDangerToast("We were unable to unsubscribe you from ticket updates. Were you originally subscribed?"); } return(RedirectToAction("Show", "Tickets", new { ticketIdentifier = ticket.GetTicketIdentifier() })); }
public ActionResult Archive(string ticketIdentifier) { if (string.IsNullOrWhiteSpace(ticketIdentifier)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Ticket ticket = db.Tickets.ToList().FirstOrDefault(t => t.GetTicketIdentifier() == ticketIdentifier); if (ticket == null) { return(HttpNotFound()); } if (!_permissionsHelper.CanArchiveTicket(User, ticket.Id)) { TempData.AddDangerToast("You are not allowed to perform this action."); RedirectToAction("Show", "Tickets", new { ticketIdentifier }); } return(View(ticket)); }
public async Task <ActionResult> ChangePassword(ChangePasswordViewModel model) { if (!ModelState.IsValid) { TempData.AddDangerToast("Unable to update your password. Please verify that you entered your current password correctly, and that both of the new passwords entered are the same."); return(RedirectToAction("Index")); } var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword); if (result.Succeeded) { var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); if (user != null) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); } TempData.AddSuccessToast("Your password has been updated!"); return(RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess })); } AddErrors(result); return(RedirectToAction("Index")); }
public ActionResult AddComment(AddTicketCommentModel model) { if (ModelState.IsValid) { var ticket = db.Tickets.Find(model.TicketId); if (ticket == null) { TempData.AddDangerToast("The ticket no longer exists."); return(RedirectToAction("Index", "Tickets")); } if (!_permissionsHelper.CanCommentOnTicket(User, model.TicketId)) { TempData.AddDangerToast("You are not allowed to perform this action."); return(RedirectToAction("Index", "Tickets")); } var newComment = new TicketComment() { AuthorId = User.Identity.GetUserId() , CreatedAt = DateTime.Now , ParentTicketId = model.TicketId , Body = model.Body }; ticket.AddComment(newComment, User, db, model.StatusId == -1, model.StatusId); return(RedirectToAction("Show", "Tickets", new { TicketIdentifier = ticket.GetTicketIdentifier() })); } TempData.AddDangerToast("Your comment was invalid. Please try again."); return(RedirectToAction("Show", "Tickets", new { model.TicketIdentifier })); }
public ActionResult AddWatch(string ticketIdentifier) { if (string.IsNullOrWhiteSpace(ticketIdentifier)) { TempData.AddDangerToast("You must provide the ticket identifier to display. Please try again."); return(RedirectToAction("Index", "Tickets")); } Ticket ticket = db.Tickets.ToList().FirstOrDefault(t => t.GetTicketIdentifier() == ticketIdentifier); if (ticket == null) { TempData.AddDangerToast("We were unable to locate this ticket. Please try again."); return(RedirectToAction("Index", "Tickets")); } if (!_permissionsHelper.CanViewTicket(User, ticket.Id)) { TempData.AddDangerToast("You are not allowed to view this resource."); return(RedirectToAction("Index", "Tickets")); } try { var newWatch = new TicketNotificationWatchEntry() { WatcherId = User.Identity.GetUserId() , TicketId = ticket.Id }; db.TicketNotificationWatchEntries.Add(newWatch); db.SaveChanges(); TempData.AddSuccessToast($"You have successfully subscribed to ticket {ticket.GetTicketIdentifier()}'s notifications! You will be notified whenever changes occur to this ticket."); return(RedirectToAction("Show", "Tickets", new { ticketIdentifier = ticket.GetTicketIdentifier() })); } catch { TempData.AddDangerToast("We were unable to subscribe you to ticket updates. Please try again."); return(RedirectToAction("Show", "Tickets", new { ticketIdentifier = ticket.GetTicketIdentifier() })); } }
public ActionResult AddUsersToProject(AddRemoveUsersProjectsModel model) { var projectHelper = new ProjectHelper(db); var errors = new List <string>(); var successes = new List <string>(); if (model.UserIds == null) { errors.Add("You must select at least 1 user to manipulate."); } else if (model.ProjectIds == null) { errors.Add("You must select at least 1 project."); } else { foreach (string userId in model.UserIds) { var user = db.Users.FirstOrDefault(u => u.Id == userId); if (user != null) { foreach (int projectId in model.ProjectIds) { var addProjectSuccess = projectHelper.AddUserToProject(userId, projectId); if (!addProjectSuccess) { errors.Add($"Unable to add '{user.FullNameStandard}' to project '{db.Projects.Find( projectId ).Name}'."); } else { successes.Add($"'{user.FullNameStandard}' has been added to the '{db.Projects.Find( projectId ).Name}' project."); } } } else //user ID was not found. { if (!errors.Contains("Unable to locate one or more selected users.")) { errors.Add("Unable to locate one or more selected users."); } } } } if (errors.Count > 0) { foreach (var err in errors) { TempData.AddDangerToast(err); } } if (successes.Count > 0) { foreach (var msg in successes) { TempData.AddSuccessToast(msg); } } ServerConfigViewModel viewModel = new ServerConfigViewModel() { Users = db.Users.ToList(), Projects = db.Projects.ToList(), Workflows = db.ProjectWorkflows.ToList() }; return(View(viewName: model.ReturningViewName ?? null, model: viewModel)); }
public ActionResult SetProjectWorkflow(SetProjectWorkflowModel model) { var projectHelper = new ProjectHelper(db); var errors = new List <string>(); var successes = new List <string>(); if (model.ProjectIds == null) { errors.Add("You must select at least 1 project to set the workflow of."); } else if (model.WorkflowId == null) { errors.Add("You must select at least 1 workflow to assign."); } else { var workflow = db.ProjectWorkflows.FirstOrDefault(pwf => pwf.Id == model.WorkflowId); if (workflow == null) { errors.Add("Unable to locate the selected workflow."); } else { foreach (int projectId in model.ProjectIds) { var project = db.Projects.FirstOrDefault(p => p.Id == projectId); if (project != null) { var result = projectHelper.SetProjectWorkflow(projectId, workflow.Id, db); if (!result) { errors.Add($"Unable to change the workflow of project '{project.Name}' to '{workflow.Name}'."); } else { successes.Add($"{project.Name}'s workflow has been changed to {workflow.Name}"); } } else //project ID was not found. { errors.Add("Unable to locate one of the selected projects."); } } } } db.SaveChanges(); if (errors.Count > 0) { foreach (var err in errors) { TempData.AddDangerToast(err); } } if (successes.Count > 0) { foreach (var msg in successes) { TempData.AddSuccessToast(msg); } } var viewModel = new ServerConfigViewModel() { Users = db.Users.ToList(), Projects = db.Projects.ToList(), Workflows = db.ProjectWorkflows.ToList() }; return(View(viewModel)); }
public ActionResult RemoveUsersFromRoles(AddRemoveUsersRolesModel model) { var roleHelper = new PermissionsHelper(db); var roleDisplays = new RoleDisplayDictionary(); var errors = new List <string>(); var successes = new List <string>(); if (model.UserIds == null) { errors.Add("You must select at least 1 user to manipulate."); } else if (model.Roles == null) { errors.Add("You must select at least 1 role."); } else { foreach (string userId in model.UserIds) { var user = db.Users.FirstOrDefault(u => u.Id == userId); if (user != null) { foreach (string role in model.Roles) { var removeRoleSuccess = roleHelper.RemoveUserFromRole(user.Id, role); if (!removeRoleSuccess) { errors.Add($"Unable to remove '{user.FullNameStandard}' from the role '{roleDisplays[role]}'. Is the user in this role?"); } else { successes.Add($"'{user.FullNameStandard}' has been removed from the '{roleDisplays[role]}' role."); } } } else //user ID was not found. { if (!errors.Contains("Unable to locate one of the selected users.")) { errors.Add("Unable to locate one of the selected users."); } } } } if (errors.Count > 0) { foreach (var err in errors) { TempData.AddDangerToast(err); } } if (successes.Count > 0) { foreach (var msg in successes) { TempData.AddSuccessToast(msg); } } ServerConfigViewModel viewModel = new ServerConfigViewModel() { Users = db.Users.ToList(), Projects = db.Projects.ToList(), Workflows = db.ProjectWorkflows.ToList() }; return(View(viewModel)); }
public ActionResult EditTicket(EditTicketModel model) { if (ModelState.IsValid) { if (!_permissionsHelper.CanEditTicket(User, model.CurrentTicketId)) { TempData.AddDangerToast("You are not allowed to perform this action."); return(RedirectToAction("Index", "Home")); } var now = DateTime.Now; var userId = User.Identity.GetUserId(); var parentProject = db.Projects.FirstOrDefault(p => p.Id == model.ParentProjectId); if (parentProject == null) { TempData.AddDangerToast("Unable to update the ticket; the parent project does not exist. "); return(RedirectToAction("Edit", "Tickets", new { ticketIdentifier = db.Tickets.Find(model.CurrentTicketId)?.GetTicketIdentifier() })); } var nextStatus = db.TicketStatuses.Find(model.TicketStatusId); var priority = db.TicketPriorityTypes.FirstOrDefault(tp => tp.Id == model.TicketPriorityId); if (priority == null) { TempData.AddDangerToast("Unable to update the ticket; the selected priority was not valid. Please try again. "); return(RedirectToAction("Edit", "Tickets", new { ticketIdentifier = db.Tickets.Find(model.CurrentTicketId)?.GetTicketIdentifier() })); } var oldTicket = db.Tickets.Find(model.CurrentTicketId); var newTicket = new Ticket() { Title = model.Title , Description = model.Description , CreatedAt = oldTicket.CreatedAt , LastInteractionAt = now , ParentProjectId = parentProject.Id , TicketTypeId = model.TicketTypeId , TicketType = db.TicketTypes.Find(model.TicketTypeId) , TicketStatusId = nextStatus?.Id ?? oldTicket.TicketStatusId , TicketPriorityLevelId = priority.Id , ReporterId = User.Identity.GetUserId() , AssignedSolverId = model.SolverId }; var histories = oldTicket.UpdateTicket(newTicket, User, db); db.TicketHistoryEntries.AddRange(histories); db.SaveChanges(); TempData.AddSuccessToast($"The ticket '{oldTicket.GetTicketIdentifier()}' has been updated!"); if (model.Attachments.First() != null) { oldTicket.AddAttachments(model.Attachments, User, db, Server); } return(RedirectToAction("Show", "Tickets", new { TicketIdentifier = oldTicket.GetTicketIdentifier() })); } var ticket = db.Tickets.Find(model.CurrentTicketId); return(RedirectToAction("Edit", "Tickets", new { TicketIdentifier = ticket.GetTicketIdentifier() })); }