public async Task <IActionResult> Create(Forum model) { User user = _dbContext.User.SingleOrDefault(u => u.Name == User.Identity.Name); if (!User.IsInRole(Roles.Administrator) || user.IsLocked) { return(Content("<p class='text-danger'>Access Denied!</p>")); } if (ModelState.IsValid) { Forum forum = _dbContext.Forum.SingleOrDefault(f => f.Name == model.Name); if (forum == null) { model.CreateDateTime = DateTime.Now; model.IsLocked = false; model.OwnerId = user.Id; await _dbContext.Forum.AddAsync(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Index")); } ModelState.AddModelError("", "The forum name already exists."); } return(View(model)); }
public async Task <IActionResult> Detail(int user1, int user2) { User firstUser = await _dbContext.User.SingleOrDefaultAsync(u => u.Id == user1); User secondUser = await _dbContext.User.SingleOrDefaultAsync(u => u.Id == user2); if (firstUser?.Name != User.Identity.Name && secondUser?.Name != User.Identity.Name) { return(RedirectToAction("AccessDenied", "Error")); } if (firstUser == null || secondUser == null) { return(Content("Error Finding Users")); } var messages = _dbContext.Message.Where(m => (m.FromUserId == user1 && m.ToUserId == user2) || (m.FromUserId == user2 && m.ToUserId == user1)) .OrderBy(m => m.SendDateTime); //isRead await messages.ForEachAsync(m => m.IsRead = m.ToUser.Name == User.Identity.Name?true : m.IsRead); _dbContext.Message.UpdateRange(messages); await _dbContext.SaveChangesAsync(); ViewBag.SendToUserId = (firstUser.Name == User.Identity.Name) ? secondUser.Id : firstUser.Id; return(View(messages)); }
public async Task <IActionResult> Lock(Forum model) { var forum = GetForum(model); forum.IsLocked = !forum.IsLocked; await _dbContext.SaveChangesAsync(); return(RedirectToAction("Detail", new { forum.Id })); }
public async Task <IActionResult> Create(Forum model) { if (!ModelState.IsValid) { throw new Exception("Informações inválidas"); } model.OwnerId = _dbContext.User.SingleOrDefault(u => u.Name == User.Identity.Name).Id; model.CreateDateTime = DateTime.Now; await _dbContext.Forum.AddAsync(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Create(Message model) { if (!ModelState.IsValid) { throw new Exception("Informação de mensagem inválido"); } var fromUser = _dbContext.User.SingleOrDefault(u => u.Name == User.Identity.Name); model.FromUserId = fromUser.Id; model.SendDateTime = DateTime.Now; await _dbContext.Message.AddAsync(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) { //throw new Exception("Invalid registration information."); TempData["Error"] = "Invalid registration information."; return(RedirectToAction("Index")); } model.Name = model.Name.Trim(); model.Password = model.Password.Trim(); model.RepeatPassword = model.RepeatPassword.Trim(); var targetUser = _dbContext.User.SingleOrDefault(u => u.Name.Equals(model.Name, StringComparison.CurrentCultureIgnoreCase)); if (targetUser != null) { throw new Exception("User name already exists."); } if (!model.Password.Equals(model.RepeatPassword)) { throw new Exception("Passwords do not match."); } var hasher = new PasswordHasher <User>(); targetUser = new User { Name = model.Name, RegisterDateTime = DateTime.Now, Description = model.Description }; targetUser.PasswordHash = hasher.HashPassword(targetUser, model.Password); if (_dbContext.User.Count() == 0) { targetUser.IsAdministrator = true; } await _dbContext.User.AddAsync(targetUser); await _dbContext.SaveChangesAsync(); await LogInUserAsync(targetUser); return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Create(Topic model, int forumId) { if (ModelState.IsValid) { User user = await _dbContext.User.SingleOrDefaultAsync(u => u.Name == User.Identity.Name); Forum forum = await _dbContext.Forum.SingleOrDefaultAsync(f => f.Id == forumId); if (user != null && forum != null && !user.IsLocked && !forum.IsLocked) { model.ForumId = forumId; model.OwnerId = user.Id; model.PostDateTime = DateTime.Now; model.IsLocked = false; await _dbContext.Topic.AddAsync(model); await _dbContext.SaveChangesAsync(); model.RootTopicId = model.Id; _dbContext.Topic.Update(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Read", new { rootTopicId = model.Id })); } return(Content("Access Denied!")); } return(View(model)); }
public async Task <IActionResult> Create(Forum model) { if (!ModelState.IsValid) { //throw new Exception("Invalid forum information."); TempData["Error"] = "Invalid forum information."; return(RedirectToAction("Index")); } model.OwnerId = _dbContext.User.SingleOrDefault(u => u.Name == User.Identity.Name).Id; model.CreateDateTime = DateTime.Now; await _dbContext.Forum.AddAsync(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(Message model) { if (!ModelState.IsValid) { throw new Exception("Information for message is invalid."); } var FromUser = _dbContext.User.SingleOrDefault(u => u.Name == User.Identity.Name); model.FromUserId = FromUser.Id; model.SendDateTime = DateTime.Now; model.IsRead = false; await _dbContext.Message.AddAsync(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Index", "Home")); // --- } // ---
public async Task <IActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) { throw new Exception("Invalid registration information."); } model.Name = model.Name.Trim(); model.Password = model.Password.Trim(); model.RepeatPassword = model.RepeatPassword.Trim(); var targetUser = _dbContext.User .SingleOrDefault(u => u.Name.Equals(model.Name, StringComparison.CurrentCultureIgnoreCase)); if (targetUser != null) { throw new Exception("User name already exists."); } if (!model.Password.Equals(model.RepeatPassword)) { throw new Exception("Passwords are not identical."); } var hasher = new PasswordHasher <User>(); // El hasher cifra la contraseña y luego la almacena en la DB, nunca se guarda la contraseña como texto plano en la DB targetUser = new User { Name = model.Name, RegisterDateTime = DateTime.Now, Description = model.Description }; targetUser.PasswordHash = hasher.HashPassword(targetUser, model.Password); if (_dbContext.User.Count() == 0) { targetUser.IsAdministrator = true; } await _dbContext.User.AddAsync(targetUser); await _dbContext.SaveChangesAsync(); await LogInUserAsync(targetUser); return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Register(RegisterViewModel model) { User user = new Models.User(); if (ModelState.IsValid) { model.Name = model.Name.Trim(); user = _dbContext.User.FirstOrDefault(u => u.Name.Equals(model.Name, StringComparison.CurrentCultureIgnoreCase)); if (user == null) { if (model.Password == model.RepeatPassword) { user = new Models.User { Name = model.Name, Description = model.Description, IsAdministrator = _dbContext.User.Count() == 0 ? true : false, RegisterDateTime = DateTime.Now, IsLocked = false }; PasswordHasher <User> hasher = new PasswordHasher <User>(); user.PasswordHash = hasher.HashPassword(user, model.Password); await _dbContext.User.AddAsync(user); await _dbContext.SaveChangesAsync(); } else { ModelState.AddModelError("", "The repeated password must be identical."); } } else { ModelState.AddModelError("", "There exists a user with the same name"); } } if (ModelState.IsValid) { await LoginUserAsync(user); return(RedirectToAction("Index", "Home")); } else { return(View(model)); } }
public async Task <IActionResult> Create(Topic model) { if (!ModelState.IsValid) { throw new Exception("Invalid topic information."); } var user = _dbContext.User.SingleOrDefault(u => u.Name == User.Identity.Name); model.OwnerId = user.Id; model.PostDateTime = DateTime.Now; await _dbContext.Topic.AddAsync(model); await _dbContext.SaveChangesAsync(); model.RootTopicId = model.Id; _dbContext.Topic.Update(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Index", new { model.ForumId })); }