public async Task<ActionResult> ManageRolesForUser(string id , string[] role , FormCollection collection) { MyIdentityManager myIdentityManager = new MyIdentityManager(); var user = myIdentityManager.SearchUserById(id); #region safety checks if (user == null) { return HttpNotFound("User with id " + id + "not found!"); } if (role == null) //list emptied out { // all roles must be removed myIdentityManager.RemoveUserFromRoles(id); return Json(new { success = true }); } //delete all roles form user, and add the new roles in the table if (!myIdentityManager.RemoveUserFromRoles(id).Succeeded) { return HttpNotFound("removeRolesError"); } var result = false; foreach (var r in role) { if (r.IsNullOrWhiteSpace()) { return HttpNotFound("AddNewRoleToUser: a role in the roles table is null or whitespace!"); } //if role exists and we are not trying to add the user to an non existing role in the db if (!myIdentityManager.RoleExist(r)) { return HttpNotFound("AddNewRoleToUser: system roles not representing roles table"); } //if user is already in this role result = myIdentityManager.AddUserToRole(id , r); } #endregion //add user to every role //var result = myIdentityManager.AddUserToRoles(id, role); myIdentityManager.Dispose(); if (result == true) { return Json(new { success = true }); } return HttpNotFound("AddNewRoleToUser: Error adding role: " + role + " to user with id " + id); }
public async Task<ActionResult> ManageComboItemsforUser(string id , string[] ComboItems , FormCollection collection) { MyIdentityManager myIdentityManager = new MyIdentityManager(); MyComboItemManager myComboItemManager = new MyComboItemManager(); if (ComboItems != null) { foreach (var comboItem in ComboItems) { if (comboItem.IsNullOrWhiteSpace()) { return HttpNotFound("AddNewRoleToUser: a role in the roles table is null or whitespace!"); } //if role exists and we are not trying to add the user to an non existing role in the db } }// if ComboItems==null is checked by UpdateComboItemsforUser, and there is an action for that if (id.IsNullOrWhiteSpace()) return HttpNotFound("User Id Is null! 4956"); //find user var user = myIdentityManager.SearchUserById(id); if (user == null) { return HttpNotFound("User with id " + id + "not found!"); } //if (ComboItems == null) { return HttpNotFound("ManageRolesForUser: ComboItems table zero"); } //Logic to update with new selection of ComboItems bool result = myComboItemManager.UpdateComboItemsforUser(id , ComboItems); myComboItemManager.DisposeAll(); myIdentityManager.Dispose(); if (result == true) { return Json(new { success = true }); } return HttpNotFound("AddNewRoleToUser: Error adding role: " + " to user with id " + id); }
public async Task<ActionResult> UserEdit([Bind (Include = "EnrollmentDate,Email,Id,EmailConfirmed," + "PhoneNumber,PhoneNumberConfirmed," + "TwoFactorEnabled,LockoutEnabled," + "AccessFailedCount,UserName," + "Address," + "LockoutEndDateUtc,ComboItems,")] ApplicationUser applicationUser , string[] ComboItems) { if (ModelState.IsValid) { MyIdentityManager myIdentityManager = new MyIdentityManager(); MyComboItemManager myComboItemManager = new MyComboItemManager(); //get the user based on the id var userEdited = myIdentityManager.SearchUserById(applicationUser.Id); myComboItemManager.UpdateComboItemsforUser(applicationUser.Id , ComboItems); userEdited.EnrollmentDate = applicationUser.EnrollmentDate; userEdited.Email = applicationUser.Email; userEdited.Address = applicationUser.Address; userEdited.EmailConfirmed = applicationUser.EmailConfirmed; userEdited.PhoneNumber = applicationUser.PhoneNumber; userEdited.PhoneNumberConfirmed = applicationUser.PhoneNumberConfirmed; userEdited.TwoFactorEnabled = applicationUser.TwoFactorEnabled; userEdited.LockoutEnabled = applicationUser.LockoutEnabled; userEdited.LockoutEndDateUtc = applicationUser.LockoutEndDateUtc; userEdited.AccessFailedCount = applicationUser.AccessFailedCount; userEdited.UserName = applicationUser.UserName; var userEditResult = myIdentityManager.UpdateUser(userEdited); if (!userEditResult.Succeeded) { return HttpNotFound("not updated"); } myComboItemManager.DisposeAll(); return Json(new { success = true }); } return HttpNotFound("Not Valid mod"); }