public async Task <IActionResult> OnPostAsync(string id) { // Get the current user. var user = await _userManager.GetUserAsync(User); // Check if there isn't any ID provided. if (string.IsNullOrEmpty(id)) { // Display a message. TempData["StatusMessage"] = "Error: No ID has been provided."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Index")); } // Get the items with the provided ID. var items = _context.Analyses .Where(item => item.AnalysisDatabases.Any(item1 => item1.Database.DatabaseType.Name == "PPI")) .Where(item => item.IsPublic || item.AnalysisUsers.Any(item1 => item1.User == user)) .Where(item => item.Id == id); // Check if there were no items found. if (items == null || !items.Any()) { // Display a message. TempData["StatusMessage"] = "Error: No item has been found with the provided ID, or you don't have access to it."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Index")); } // Define the view. View = new ViewModel { Analysis = items .Include(item => item.AnalysisUsers) .ThenInclude(item => item.User) .Include(item => item.AnalysisUserInvitations) .First() }; // Check if the user does not exist. if (user == null) { // Display a message. TempData["StatusMessage"] = "Error: You need to be logged in to add a user to the analysis."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Details/Accounts/Users/Index", new { id = View.Analysis.Id })); } // Check if the reCaptcha is valid. if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken)) { // Add an error to the model. ModelState.AddModelError(string.Empty, "The reCaptcha verification failed."); // Return the page. return(Page()); } // Check if the provided model is not valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error was encountered. Please check again the input fields."); // Return the page. return(Page()); } // Check if the provided e-mail address already has access to the network. if (View.Analysis.AnalysisUsers.Any(item => item.User.Email == Input.Email) || View.Analysis.AnalysisUserInvitations.Any(item => item.Email == Input.Email)) { // Add an error to the model. ModelState.AddModelError(string.Empty, "The user with the provided e-mail already has access to the network."); // Return the page. return(Page()); } // Try to get the user with the provided e-mail address. var userToAdd = _context.Users .FirstOrDefault(item => item.Email == Input.Email); // Check if any user has been found. if (userToAdd != null) { // Define a new task. var task = new AnalysisUsersTask { Items = new List <AnalysisUserInputModel> { new AnalysisUserInputModel { Analysis = new AnalysisInputModel { Id = View.Analysis.Id }, User = new UserInputModel { Id = userToAdd.Id } } } }; // Try to run the task. try { // Run the task. await task.CreateAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } } else { // Define a new task. var task = new AnalysisUserInvitationsTask { Items = new List <AnalysisUserInvitationInputModel> { new AnalysisUserInvitationInputModel { Analysis = new AnalysisInputModel { Id = View.Analysis.Id }, Email = Input.Email } } }; // Try to run the task. try { // Run the task. await task.CreateAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } } // Define the view model for the e-mail. var emailAddedToAnalysisViewModel = new EmailAddedToAnalysisViewModel { Email = user.Email, Name = View.Analysis.Name, Url = _linkGenerator.GetUriByPage(HttpContext, "/Content/DatabaseTypes/PPI/Created/Analyses/Details/Index", handler: null, values: new { id = View.Analysis.Id }), AddedEmail = Input.Email, ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null) }; // Send the defined e-mails. await _emailSender.SendAddedToAnalysisEmailAsync(emailAddedToAnalysisViewModel); // Define the view model for the e-mail. var emailWasAddedToAnalysisViewModel = new EmailWasAddedToAnalysisViewModel { Email = Input.Email, Name = View.Analysis.Name, Url = _linkGenerator.GetUriByPage(HttpContext, "/Content/DatabaseTypes/PPI/Created/Analyses/Details/Index", handler: null, values: new { id = View.Analysis.Id }), AddedByEmail = user.Email, ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null) }; // Send the defined e-mails. await _emailSender.SendWasAddedToAnalysisEmailAsync(emailWasAddedToAnalysisViewModel); // Display a message to the user. TempData["StatusMessage"] = "Success: 1 user added successfully to the analysis."; // Redirect to the users page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Details/Accounts/Users/Index", new { id = View.Analysis.Id })); }
public async Task <IActionResult> OnPostAsync() { // Get the current user. var user = await _userManager.GetUserAsync(User); // Check if there isn't any ID provided. if (string.IsNullOrEmpty(Input.Id)) { // Display a message. TempData["StatusMessage"] = "Error: No ID has been provided."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Index")); } // Get the items with the provided ID. var items = _context.Analyses .Where(item => item.AnalysisDatabases.Any(item1 => item1.Database.DatabaseType.Name == "PPI")) .Where(item => item.IsPublic || item.AnalysisUsers.Any(item1 => item1.User == user)) .Where(item => item.Id == Input.Id); // Check if there were no items found. if (items == null || !items.Any()) { // Display a message. TempData["StatusMessage"] = "Error: No item has been found with the provided ID, or you don't have access to it."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Index")); } // Define the view. View = new ViewModel { Analysis = items .First() }; // Check if the user does not exist. if (user == null) { // Display a message. TempData["StatusMessage"] = "Error: You need to be logged in to remove a user from the analysis."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Details/Accounts/Users/Index", new { id = View.Analysis.Id })); } // Get all of the network users and network user invitations. var analysisUsers = _context.AnalysisUsers .Include(item => item.User) .Where(item => item.Analysis == View.Analysis) .Where(item => Input.Emails.Contains(item.User.Email)) .AsEnumerable(); var analysisUserInvitations = _context.AnalysisUserInvitations .Where(item => item.Analysis == View.Analysis) .Where(item => Input.Emails.Contains(item.Email)) .AsEnumerable(); // Define the view items. View.Items = analysisUsers .Select(item => new ItemModel { Email = item.User.Email, DateTimeCreated = item.DateTimeCreated }) .Concat(analysisUserInvitations .Select(item => new ItemModel { Email = item.Email, DateTimeCreated = item.DateTimeCreated })); // Check if there weren't any items found. if (View.Items == null || !View.Items.Any()) { // Display a message. TempData["StatusMessage"] = "Error: No users have been found with the provided e-mails."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Details/Accounts/Users/Index", new { id = View.Analysis.Id })); } // Define the status of the selected items. View.IsCurrentUserSelected = View.Items .Any(item => item.Email == user.Email); View.AreAllUsersSelected = !_context.AnalysisUsers .Where(item => item.Analysis == View.Analysis) .Select(item => item.User.Email) .AsEnumerable() .Except(View.Items.Select(item => item.Email)) .Any(); // Check if there aren't any emails provided. if (Input.Emails == null || !Input.Emails.Any()) { // Display a message. TempData["StatusMessage"] = "Error: No or invalid emails have been provided."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Details/Accounts/Users/Index", new { id = View.Analysis.Id })); } // Check if the reCaptcha is valid. if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken)) { // Add an error to the model. ModelState.AddModelError(string.Empty, "The reCaptcha verification failed."); // Return the page. return(Page()); } // Check if the provided model isn't valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields."); // Redisplay the page. return(Page()); } // Save the number of items found. var itemCount = analysisUsers.Count() + analysisUserInvitations.Count(); // Define the new tasks. var analysisUsersTask = new AnalysisUsersTask { Items = analysisUsers.Select(item => new AnalysisUserInputModel { Analysis = new AnalysisInputModel { Id = View.Analysis.Id }, User = new UserInputModel { Id = item.User.Id } }) }; var analysisUserInvitationsTask = new AnalysisUserInvitationsTask { Items = analysisUserInvitations.Select(item => new AnalysisUserInvitationInputModel { Analysis = new AnalysisInputModel { Id = View.Analysis.Id }, Email = item.Email }) }; // Try to run the tasks. try { // Run the tasks. await analysisUsersTask.DeleteAsync(_serviceProvider, CancellationToken.None); await analysisUserInvitationsTask.DeleteAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } // Check if all of the users have been selected. if (View.AreAllUsersSelected && !View.Analysis.IsPublic) { // Define a new task. var task = new BackgroundTask { DateTimeCreated = DateTime.UtcNow, Name = $"{nameof(IContentTaskManager)}.{nameof(IContentTaskManager.DeleteAnalysesAsync)}", IsRecurring = false, Data = JsonSerializer.Serialize(new AnalysesTask { Items = new List <AnalysisInputModel> { new AnalysisInputModel { Id = View.Analysis.Id } } }) }; // Mark the task for addition. _context.BackgroundTasks.Add(task); // Save the changes to the database. await _context.SaveChangesAsync(); // Create a new Hangfire background job. var jobId = BackgroundJob.Enqueue <IContentTaskManager>(item => item.DeleteAnalysesAsync(task.Id, CancellationToken.None)); // Display a message. TempData["StatusMessage"] = $"Success: A new background job was created to delete 1 analysis."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Index")); } // Display a message. TempData["StatusMessage"] = $"Success: {itemCount} user{(itemCount != 1 ? "s" : string.Empty)} removed successfully."; // Check if the current user was selected. if (View.IsCurrentUserSelected && !View.Analysis.IsPublic) { // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Index")); } // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Details/Accounts/Users/Index", new { id = View.Analysis.Id })); }