public async Task <IActionResult> EditRoles(EditRolesViewModel viewModel) { if (ModelState.IsValid) { foreach (var item in viewModel.PostBackDict) { var userId = item.Key; var flagList = item.Value; // The list of boolean. Indicates that if user should in role or not. var roleNames = viewModel.RoleNames; var user = await _userManager.FindByIdAsync(userId); // Get user by its id. if (user != null) { for (int i = 0; i < flagList.Count; i++) { var roleName = roleNames[i]; // If user is in role. var isUserInRole = await _userManager.IsInRoleAsync(user, roleName); if (flagList[i]) { // If the flag is true and the user is already in the role, continue to next loop. if (isUserInRole) { continue; } // If the flag is true and the user is not in the role, add it to role. await _userManager.AddToRoleAsync(user, roleName); } else { // If the flag is false and the user is already in the role, remove it form the role. if (isUserInRole) { await _userManager.RemoveFromRoleAsync(user, roleName); } // If the flag is false and the user is not in the role, continue to next loop. continue; } } } } return(RedirectToAction("RoleManage")); } return(View()); }
public IActionResult EditAdminRoles(int adminId) { var centreId = User.GetCentreId(); var adminUser = userDataService.GetAdminUserById(adminId) !; var categories = courseCategoriesDataService.GetCategoriesForCentreAndCentrallyManagedCourses(centreId); categories = categories.Prepend(new Category { CategoryName = "All", CourseCategoryID = 0 }); var numberOfAdmins = centreContractAdminUsageService.GetCentreAdministratorNumbers(centreId); var model = new EditRolesViewModel(adminUser, centreId, categories, numberOfAdmins); return(View(model)); }
EditRolesViewModel_does_set_up_Content_creator_checkbox_when_its_limit_is_reached_and_user_is_a_content_creator() { // Given var adminUser = UserTestHelper.GetDefaultAdminUser(isContentCreator: true); var numberOfAdmins = CentreContractAdminUsageTestHelper.GetDefaultNumberOfAdministrators(ccLicences: 5, ccLicenceSpots: 5); // When var result = new EditRolesViewModel(adminUser, 1, categories, numberOfAdmins); // Then using (new AssertionScope()) { result.Checkboxes.Count.Should().Be(4); result.Checkboxes.Contains(AdminRoleInputs.ContentCreatorCheckbox).Should().BeTrue(); result.NotAllRolesDisplayed.Should().BeFalse(); } }
EditRolesViewModel_does_not_set_up_Trainer_checkbox_when_its_limit_is_reached_and_user_not_a_trainer() { // Given var adminUser = UserTestHelper.GetDefaultAdminUser(isTrainer: false); var numberOfAdmins = CentreContractAdminUsageTestHelper.GetDefaultNumberOfAdministrators(trainers: 5, trainerSpots: 5); // When var result = new EditRolesViewModel(adminUser, 1, categories, numberOfAdmins); // Then using (new AssertionScope()) { result.Checkboxes.Count.Should().Be(3); result.Checkboxes.Contains(AdminRoleInputs.TrainerCheckbox).Should().BeFalse(); result.NotAllRolesDisplayed.Should().BeTrue(); } }
public async Task <IActionResult> EditRole(Guid Id) { var role = await roleManager.FindByIdAsync(Id.ToString()); var model = new EditRolesViewModel { Id = Guid.Parse(role.Id), RoleName = role.Name }; foreach (var user in userManager.Users.ToList()) { if (await userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } return(View(model)); }
EditRolesViewModel_does_set_up_Cms_manager_radio_button_when_its_limit_is_reached_and_user_is_a_cms_manager() { // Given var adminUser = UserTestHelper.GetDefaultAdminUser(importOnly: false, isContentManager: true); var numberOfAdmins = CentreContractAdminUsageTestHelper.GetDefaultNumberOfAdministrators(cmsManagers: 5, cmsManagerSpots: 5); // When var result = new EditRolesViewModel(adminUser, 1, categories, numberOfAdmins); // Then using (new AssertionScope()) { result.Radios.Count.Should().Be(3); result.Radios.Contains(AdminRoleInputs.CmsManagerRadioButton).Should().BeTrue(); result.NotAllRolesDisplayed.Should().BeFalse(); result.NoContentManagerOptionsAvailable.Should().BeFalse(); } }
public void CreateUserRoles(string userId, EditRolesViewModel editRoles) { if (editRoles.IsAdmin) { unitOfWork.AppUserRepository.AddUserToRole(userId, AuthHelper.Admin); } if (editRoles.IsDeaconLeadership) { unitOfWork.AppUserRepository.AddUserToRole(userId, AuthHelper.DeaconLeadership); } if (editRoles.IsDeacon) { unitOfWork.AppUserRepository.AddUserToRole(userId, AuthHelper.Deacon); } if (editRoles.IsPastor) { unitOfWork.AppUserRepository.AddUserToRole(userId, AuthHelper.Pastor); } }
public async Task <IActionResult> EditRole(string id) { var result = await roleManager.FindByIdAsync(id); var model = new EditRolesViewModel { RoleName = result.Name, RoleId = result.Id }; foreach (var users in userManager.Users) { if (await userManager.IsInRoleAsync(users, result.Name)) { model.Users.Add(users.UserName); } } return(View(model)); }
public async Task <IActionResult> EditRoles(EditRolesViewModel model, [FromRoute] string username) { var user = await _userManager.FindByNameAsync(username); if (user == null) { RedirectToAction("NotFoundResponse", "Error"); } var selectedRoles = new List <string>(); if (model.Admin) { selectedRoles.Add("admin"); } if (model.Moderator) { selectedRoles.Add("moderator"); } var userRoles = await _userManager.GetRolesAsync(user); var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles)); if (!result.Succeeded) { return(RedirectToAction("ServerError", "Error", new { msg = "Something went wrong." })); } result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles)); if (!result.Succeeded) { return(RedirectToAction("ServerError", "Error", new { msg = "Something went wrong." })); } return(RedirectToAction("Index", "Admin")); }
public void EditRolesViewModel_sets_ContentManagementRole_none() { // Given var adminUser = UserTestHelper.GetDefaultAdminUser( isContentManager: false, importOnly: false ); var numberOfAdmins = CentreContractAdminUsageTestHelper.GetDefaultNumberOfAdministrators(); // When var result = new EditRolesViewModel( adminUser, 1, categories, numberOfAdmins, ReturnPageQueryHelper.GetDefaultReturnPageQuery() ); // Then result.ContentManagementRole.Should().BeEquivalentTo(ContentManagementRole.NoContentManagementRole); }
public async Task <IActionResult> EditRole(EditRolesViewModel editRolesViewModel) { var Role = await roleManager.FindByIdAsync(editRolesViewModel.RoleId); Role.Name = editRolesViewModel.RoleName; var result = await roleManager.UpdateAsync(Role); if (result.Succeeded) { return(RedirectToAction("ListRoles")); } else { foreach (var errors in result.Errors) { ModelState.AddModelError(string.Empty, errors.Description); } return(View(editRolesViewModel)); } }
public async Task <IActionResult> Roles(EditRolesViewModel model, List <string> roles) { if (ModelState.IsValid) { User user = await _userManager.FindByIdAsync(model.Id); if (user != null) { /*user.Email = model.Email; * user.UserName = model.UserName; * user.PhoneNumber = model.PhoneNumber;*/ var userRoles = await _userManager.GetRolesAsync(user); var allRoles = _roleManager.Roles.ToList(); var addedRoles = roles.Except(userRoles); var removedRoles = userRoles.Except(roles); await _userManager.AddToRolesAsync(user, addedRoles); await _userManager.RemoveFromRolesAsync(user, removedRoles); var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { await _userManager.UpdateSecurityStampAsync(user); return(RedirectToAction("Index")); } else { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } } } return(View(model)); }
public void EditRolesViewModel_sets_expected_properties() { // Given var adminUser = UserTestHelper.GetDefaultAdminUser( firstName: "Test", lastName: "Name", isCentreAdmin: true, isSupervisor: true, isTrainer: true, isContentCreator: true, isContentManager: true, importOnly: true, categoryId: 0 ); var numberOfAdmins = CentreContractAdminUsageTestHelper.GetDefaultNumberOfAdministrators(); // When var result = new EditRolesViewModel( adminUser, 1, categories, numberOfAdmins, ReturnPageQueryHelper.GetDefaultReturnPageQuery() ); // Then using (new AssertionScope()) { result.FullName.Should().Be("Test Name"); result.IsCentreAdmin.Should().BeTrue(); result.IsSupervisor.Should().BeTrue(); result.IsTrainer.Should().BeTrue(); result.IsContentCreator.Should().BeTrue(); result.CentreId.Should().Be(1); result.LearningCategory.Should().Be(0); result.LearningCategories.Count().Should().Be(2); result.ContentManagementRole.Should().BeEquivalentTo(ContentManagementRole.CmsAdministrator); } }
public async Task <IActionResult> EditRoles() { EditRolesViewModel viewModel = new EditRolesViewModel(); // Get all roles and users. viewModel.Roles = _roleManager.Roles.ToList(); viewModel.Users = _userManager.Users.ToList(); viewModel.UserCheckValueDict = new Dictionary <ApplicationUser, List <bool> >(); foreach (var user in viewModel.Users) { List <bool> checkVal = new List <bool>(); foreach (var role in viewModel.Roles) { var isInRole = await _userManager.IsInRoleAsync(user, role.Name); checkVal.Add((isInRole == true) ? true : false); } viewModel.UserCheckValueDict.Add(user, checkVal); } return(View("EditRoles", viewModel)); }
public async Task <IActionResult> Roles(string id) { User user = await _userManager.FindByIdAsync(id); if (user == null) { return(NotFound()); } var userRoles = await _userManager.GetRolesAsync(user); var allRoles = _roleManager.Roles.ToList(); EditRolesViewModel model = new EditRolesViewModel { Id = user.Id, /*Email = user.Email, * UserName = user.UserName, * PhoneNumber = user.PhoneNumber,*/ UserRoles = userRoles, AllRoles = allRoles }; return(View(model)); }
EditRolesViewModel_sets_only_NoCmsPermissions_radio_button_when_both_limits_reached_and_user_has_no_permissions() { // Given var adminUser = UserTestHelper.GetDefaultAdminUser(importOnly: false, isContentManager: false); var numberOfAdmins = CentreContractAdminUsageTestHelper.GetDefaultNumberOfAdministrators( cmsManagers: 5, cmsManagerSpots: 5, cmsAdministrators: 5, cmsAdministratorSpots: 5 ); // When var result = new EditRolesViewModel(adminUser, 1, categories, numberOfAdmins); // Then using (new AssertionScope()) { result.Radios.Count.Should().Be(1); result.Radios.Contains(AdminRoleInputs.NoCmsPermissionsRadioButton).Should().BeTrue(); result.NotAllRolesDisplayed.Should().BeTrue(); result.NoContentManagerOptionsAvailable.Should().BeTrue(); } }