public IActionResult Create(TaskViewModel task) { string loggedInUser = User.Identity.Name; var teacher = _teachersService.GetTeacherId(loggedInUser); task.Description = HtmlEncoder.Default.Encode(task.Description); if (task != null) { if (task.Deadline > DateTime.Now) { task.TeacherId = teacher.Id; _tasksService.CreateTask(task); return(Redirect("/Home/Index")); } else { TempData["feedback"] = "Invalid date"; return(RedirectToAction("Create")); } } else { _logger.LogError("Task is not found"); throw new ArgumentNullException(); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var myKeys = Encryption.GenerateAsymmetricKeys(); //user = asp.net user roles var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, Address = Input.Address }; var result = await _userManager.CreateAsync(user, Input.Password = CreateRandomPassword()); await _userManager.AddToRoleAsync(user, "STUDENT"); var teacher = _teachersService.GetTeacherId(User.Identity.Name); _studentsService.AddStudent( new AssignmentTask.Application.ViewModels.StudentViewModel { Email = Input.Email, Name = Input.FirstName, Surname = Input.LastName, TeacherID = teacher.Id, PublicKey = myKeys.PublicKey, PrivateKey = myKeys.PrivateKey }); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var token = await _userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationLink = Url.Action("ConfirmEmail", "Email", new { token, email = user.Email }, Request.Scheme); EmailHelper emailHelper = new EmailHelper(); string studentPassword = Input.Password; bool emailResponse = emailHelper.SendEmail(user.Email, confirmationLink, studentPassword); if (emailResponse) { return(RedirectToAction("/Home/Index")); } else { // log email failed } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public IActionResult Create(CommentViewModel comment, Guid id) { string loggedInUser = User.Identity.Name; comment.CommentArea = HtmlEncoder.Default.Encode(comment.CommentArea); if (User.IsInRole("STUDENT")) { var student = _studentsService.GetStudent(loggedInUser); comment.TeacherID = student.TeacherID; comment.StudentID = student.Id; } else { var teacher = _teachersService.GetTeacherId(loggedInUser); comment.TeacherID = teacher.Id; var assignment = _assignmentsService.GetAssignmentById(id); comment.StudentID = assignment.StudentId; } comment.AssignmentID = id; _commentsService.AddComment(comment); return(Redirect("/Tasks/List")); }