public async Task <IActionResult> OnPostAsync() { var existingRole = await _context.GetRoleByNameAsync(Role.Name); if (existingRole != null) { ModelState.AddModelError("Name", $"Name is already taken."); } ; if (!ModelState.IsValid) { return(Page()); } var role = new Role { Name = Role.Name }; _context.AddRole(role); await _context.SaveChangesAsync(); TempData["Message"] = $"The role, {role.Name}, was successfully added."; return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var existingRole = await _context.GetRoleAsync(id); if (existingRole == null) { TempData["Message"] = $"Role not found. Id = {id}."; return(RedirectToPage("./Index")); } if (existingRole.Name != Role.Name) { var existingName = await _context.GetRoleByNameAsync(Role.Name); if (existingName != null) { ModelState.AddModelError("Name", $"This name is already taken."); } } if (!ModelState.IsValid) { return(Page()); } existingRole.Name = Role.Name; _context.Update(existingRole); await _context.SaveChangesAsync(); TempData["Message"] = $"The role, {existingRole.Name}, was successfully updated."; return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var existingUser = await _context.GetUserByUsernameAsync(UserEntity.Username); if (existingUser != null) { ModelState.AddModelError("Username", $"Username is already taken: {existingUser.Username}."); } // Add Roles var roles = new List <Role>(); if (!string.IsNullOrWhiteSpace(UserEntity.Roles)) { var rolenames = UserEntity.Roles.Split(','); foreach (var rolename in rolenames) { if (string.IsNullOrWhiteSpace(rolename)) { continue; } var r = await _context.GetRoleByNameAsync(rolename.Trim()); if (r == null) { ModelState.AddModelError("Roles", $"{rolename} does not exist."); continue; } roles.Add(r); } } // Add Subscriptions var subscriptions = new List <User>(); if (!string.IsNullOrWhiteSpace(UserEntity.Subscriptions)) { var usernames = UserEntity.Subscriptions.Split(','); foreach (var username in usernames) { if (string.IsNullOrWhiteSpace(username)) { continue; } var account = await _context.GetUserByUsernameAsync(username.Trim()); if (account == null) { ModelState.AddModelError("Subscriptions", $"{username} does not exist."); continue; } subscriptions.Add(account); } } if (!ModelState.IsValid) { return(Page()); } var user = new User { Username = UserEntity.Username, Name = UserEntity.Name, Bio = UserEntity.Bio, AvatarUrl = UserEntity.AvatarUrl, CreateDate = DateTime.UtcNow, Roles = roles, Subscriptions = subscriptions, Followers = new List <User>() }; _context.AddUser(user); await _context.SaveChangesAsync(); TempData["Message"] = $"The user, {user.Username}, was successfully added."; _logger.LogWarning(AdminLoggingEvents.AddUserEvent, $"New user added: {user.Username}."); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } List <ImportUserModel> importedUsers; try { importedUsers = JsonSerializer.Deserialize <List <ImportUserModel> >(Json); } catch { ModelState.AddModelError("Json", "Error reading JSON."); return(Page()); } var entities = new List <Entities.User>(); foreach (var user in importedUsers) { var entity = new Entities.User(); entities.Add(entity); entity.Username = user.Username; entity.Name = user.Name; entity.AvatarUrl = user.AvatarUrl; entity.Bio = user.Bio; entity.CreateDate = DateTime.UtcNow; if (!string.IsNullOrWhiteSpace(user.Roles)) { var chosenRoles = user.Roles.Split(','); foreach (var rolename in chosenRoles) { if (string.IsNullOrWhiteSpace(rolename)) { continue; } var r = await _context.GetRoleByNameAsync(rolename.Trim()); if (r == null) { ModelState.AddModelError("Roles", $"{rolename} does not exist."); continue; } entity.Roles.Add(r); } } _context.AddUser(entity); } foreach (var user in importedUsers) { var entity = entities.Find(e => e.Username == user.Username); if (!string.IsNullOrWhiteSpace(user.Following)) { var chosenFollowers = user.Following.Split(','); foreach (var username in chosenFollowers) { if (string.IsNullOrWhiteSpace(username)) { continue; } var account = await _context.GetUserByUsernameAsync(username.Trim()); if (account == null) { var importedAccount = entities.FirstOrDefault(e => e.Username == username); if (importedAccount == null) { ModelState.AddModelError("Following", $"{username} does not exist."); continue; } else { entity.Subscriptions.Add(importedAccount); } } else { entity.Subscriptions.Add(account); } } } } if (!ModelState.IsValid) { return(Page()); } try { await _context.SaveChangesAsync(); TempData["Message"] = $"{importedUsers.Count} user(s) were successfully imported."; return(RedirectToPage("./Index")); } catch { ModelState.AddModelError("Json", "Error saving users to database."); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var existingUser = await _context.GetUserAsync(id); if (existingUser == null) { TempData["Message"] = $"User not found. Id = {id}."; return(RedirectToPage("./Index")); } if (existingUser.Username != UserEntity.Username) { var existingUsername = await _context.GetUserByUsernameAsync(UserEntity.Username); if (existingUsername != null) { ModelState.AddModelError("Username", $"This username is already taken."); } } if (!ModelState.IsValid) { return(Page()); } existingUser.Username = UserEntity.Username; existingUser.Name = UserEntity.Name; existingUser.Bio = UserEntity.Bio; existingUser.AvatarUrl = UserEntity.AvatarUrl; // Add Roles - Delete existing and start over existingUser.Roles.Clear(); var roles = new List <Role>(); if (!string.IsNullOrWhiteSpace(UserEntity.Roles)) { var rolenames = UserEntity.Roles.Split(','); foreach (var rolename in rolenames) { if (string.IsNullOrWhiteSpace(rolename)) { continue; } var r = await _context.GetRoleByNameAsync(rolename.Trim()); if (r == null) { ModelState.AddModelError("Roles", $"{rolename} does not exist."); continue; } ; roles.Add(r); } } existingUser.Roles = roles; _context.Update(existingUser); await _context.SaveChangesAsync(); TempData["Message"] = $"The user, {existingUser.Username}, was successfully updated."; _logger.LogWarning(AdminLoggingEvents.UpdateUserEvent, $"New user updated: {existingUser.Username}."); return(RedirectToPage("./Index")); }