public async Task <ActionResult> Create(RoleViewModel viewModel) { if (!ModelState.IsValid) { ViewData["Controllers"] = _mvcControllerDiscovery.GetControllers(); return(View(viewModel)); } //var role1 = new IdentityRole { Name = viewModel.Name }; var role = (TRole)Activator.CreateInstance(DynamicAuthorizationOptions.RoleType); role.GetType().GetProperty("Name")?.SetValue(role, viewModel.Name, null); var result = await _roleManager.CreateAsync(role); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } ViewData["Controllers"] = _mvcControllerDiscovery.GetControllers(); return(View(viewModel)); } if (viewModel.SelectedControllers != null && viewModel.SelectedControllers.Any()) { foreach (var controller in viewModel.SelectedControllers) { foreach (var action in controller.Actions) { action.ControllerId = controller.Id; } } var roleAccess = new RoleAccess { Controllers = viewModel.SelectedControllers.ToList(), RoleId = role.GetType().GetProperty("Id")?.GetValue(role).ToString() }; await _roleAccessStore.AddRoleAccessAsync(roleAccess); } return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult> Create(RoleViewModel viewModel) { if (!ModelState.IsValid) { ViewData["Controllers"] = _mvcControllerDiscovery.GetControllers(); return(View(viewModel)); } var role = new IdentityRole { Name = viewModel.Name }; var result = await _roleManager.CreateAsync(role); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } ViewData["Controllers"] = _mvcControllerDiscovery.GetControllers(); return(View(viewModel)); } if (viewModel.SelectedControllers != null && viewModel.SelectedControllers.Any()) { foreach (var controller in viewModel.SelectedControllers) { foreach (var action in controller.Actions) { action.ControllerId = controller.Id; } } var roleAccess = new RoleAccess { Controllers = viewModel.SelectedControllers.ToList(), RoleId = role.Id }; await _roleAccessStore.AddRoleAccessAsync(roleAccess); } return(RedirectToAction(nameof(Index))); }