public async Task <IActionResult> ManageUserRoles(List <RolesUserViewModel> model, string userId, string returnUrl)
        {
            try
            {
                var user = await userManager.FindByIdAsync(userId);

                if (user == null)
                {
                    ErrorViewModel vme = new ErrorViewModel("L'utilisateur avec l'id :" + userId + " n'a pas pu être trouvé");
                    return(View("Error", vme));
                }

                var roles = await userManager.GetRolesAsync(user);

                var result = await userManager.RemoveFromRolesAsync(user, roles);

                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", "Ne peut pas retirer l'utilisaeur des rôles existants");
                    return(View(model));
                }

                result = await userManager.AddToRolesAsync(user, model.Where(x => x.isSelected).Select(y => y.RoleName));

                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", "Ne peut pas ajouter l'utilisaeur à ces rôles");
                    return(View(model));
                }

                return(RedirectToAction("EditUser", new { userId, returnUrl }));
            }
            catch (Exception ex)
            {
                ErrorViewModel vme = new ErrorViewModel(ex.Message);
                return(View("Error", vme));
            }
        }
        public IActionResult Index(string returnUrl)
        {
            try
            {
                if (returnUrl == null)
                {
                    returnUrl = CoronaOutWebUrl;
                }

                TableauBordViewModel vm = new TableauBordViewModel();

                vm.UserNames = userManager.Users.Select(u => u.UserName).ToList();
                vm.RoleNames = roleManager.Roles.Select(r => r.Name).ToList();
                vm.ReturnUrl = returnUrl;

                return(View(vm));
            }
            catch (Exception ex)
            {
                ErrorViewModel vme = new ErrorViewModel(ex.Message);
                return(View("Error", vme));
            }
        }
Esempio n. 3
0
 public IActionResult Error(ErrorViewModel vm)
 {
     return(View("Error", vm));
 }