예제 #1
0
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    var userRole = new IdentityUserRole <string>
                    {
                        RoleId = model.RoleId,
                        UserId = userId
                    };

                    await _appIdentityDbContext.UserRoles.AddAsync(userRole);

                    await _appIdentityDbContext.SaveChangesAsync();
                }

                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    var userRole = new IdentityUserRole <string>
                    {
                        RoleId = model.RoleId,
                        UserId = userId
                    };
                    _appIdentityDbContext.UserRoles.Remove(userRole);
                    _appIdentityDbContext.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            return(View("Error", new string[] { "Role Not Found" }));
        }
예제 #2
0
        public ActionResult Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    result = UserManager.AddToRole(userId, model.RoleName);
                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }

                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    result = UserManager.RemoveFromRole(userId, model.RoleName);
                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View("Error", new string[] { "Role Not Found" }));
        }
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    AppUser user = await _userManager.FindByIdAsync(userId);

                    user.AccountActive = true;
                    result             = await _userManager.AddToRoleAsync(user, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }

                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    AppUser user = await _userManager.FindByIdAsync(userId);

                    user.AccountActive = false;
                    result             = await _userManager.RemoveFromRoleAsync(user, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View("Error", new string[] { "Role Not Found" }));
        }
예제 #4
0
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    //因這些函式都使用Role Name 所以RoleModificationModel 才會使用RoleName(即使role也有自己的unique id)
                    result = await UserManager.AddToRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    result = await UserManager.RemoveFromRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View("Error", new string[] { "Role Not Found" }));
        }
        public async Task <IActionResult> AddUser(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid == true)
            {
                ApplicationUser user = await userManager.FindByIdAsync(model.UserId);

                if (user != null)
                {
                    result = await userManager.AddToRoleAsync(user, model.RoleName);

                    if (!result.Succeeded)
                    {
                        AddErrorsFromResult(result);
                    }
                }
            }
            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(await AddUser(model.RoleName));
            }
        }
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (!ModelState.IsValid)
            {
                return(View("Error", new[] { "Role Not Found" }));
            }

            foreach (var userId in model.IdsToAdd ?? Enumerable.Empty <string>())
            {
                var user = await _userManager.FindByIdAsync(userId);

                result = await _userManager.AddToRoleAsync(user, model.RoleName);

                if (!result.Succeeded)
                {
                    return(View("Error", result.Errors));
                }
            }

            foreach (var userId in model.IdsToDelete ?? Enumerable.Empty <string>())
            {
                var user = await _userManager.FindByIdAsync(userId);

                result = await _userManager.RemoveFromRoleAsync(user, model.RoleName);

                if (!result.Succeeded)
                {
                    return(View("Error", result.Errors));
                }
            }

            return(RedirectToAction("Index"));
        }
예제 #7
0
        public async Task <ActionResult> EditRole(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    result = await UserManager.AddToRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    result = await UserManager.RemoveFromRoleAsync(userId,
                                                                   model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View("Error", new string[] { "Роль не найдена" }));
        }
예제 #8
0
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    result = await UserManager.AddToRolesAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    result = await UserManager.RemoveFromRolesAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                var role = await RoleManager.FindByNameAsync(model.RoleName);

                RoleManager.AddRolePermission(role, model.PermissionIds, HttpContext.GetOwinContext().Get <AppIdentityDbContext>());
                RoleManager.Update(role);
                return(RedirectToAction("Index"));
            }
            return(View("Errors", new string[] { "Role Not Found" }));
        }
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    result = await UserManager.AddToRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(Error(result.Errors.ToArray()));
                    }
                }

                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    result = await UserManager.RemoveFromRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(Error(result.Errors.ToArray()));
                    }
                }
                return(RedirectToAction("Index", "Role", new { area = "Back" }));
            }
            return(Error(new string[] { "Role Not Found" }));
        }
예제 #10
0
        public override void Run()
        {
            RoleModificationModel model = new RoleModificationModel();

            Console.Write("User Id: ");
            String id = Console.ReadLine();

            Console.Write("Role Name: ");
            model.RoleName = Console.ReadLine();
            Console.Write("del or add: ");
            String choice = Console.ReadLine();

            if (choice.Equals("del"))
            {
                model.IdsToDelete = new string[] { id };
            }
            else
            {
                model.IdsToAdd = new string[] { id };
            }

            using (var client = CreateClient(Commands.Token)) {
                var dataAsString = JsonConvert.SerializeObject(model);
                var content      = new StringContent(dataAsString);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var response = client.PutAsync(APP_PATH + "api/roleadmin", content).Result;
                ShowHttpStatus(response.StatusCode);
                if (response.IsSuccessStatusCode)
                {
                    ShowResult(response.Content.ReadAsStringAsync().Result);
                }
            }
        }
        public async Task<ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;
            if (ModelState.IsValid)
            {
                foreach (string userId in model.IDsToAdd ?? new string[] { })
                {
                    result = await UserManager.AddToRoleAsync(userId, model.RoleName);
                    if (!result.Succeeded)
                    {
                        return View("Error", result.Errors);
                    }
                }
                foreach (var userId in model.IDsToDelete ?? new string[] { })
                {
                    //演示用,正式部署时去掉
                    var currentUser = await UserManager.FindByIdAsync(userId);
                    if (currentUser.UserName == "Admin" && model.RoleName == "Administrator")
                    {
                        return View("Error", new string[] { "请勿修改Admin的角色!" });
                    }

                    result = await UserManager.RemoveFromRoleAsync(userId, model.RoleName);
                    if (!result.Succeeded)
                    {
                        return View("Error", result.Errors);
                    }
                }
                return RedirectToAction("Index");
            }
            return View("Error", new string[] { "无法找到此角色" });
        }
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Error", new[] { "Model is wrong. Please reload website and try again later." }));
            }

            try
            {
                if (model.IdsToAdd != null)
                {
                    await RoleAdministrating.AddMembers(model.RoleName, model.IdsToAdd);
                }

                if (model.IdsToDelete != null)
                {
                    await RoleAdministrating.DeleteMembers(model.RoleName, model.IdsToDelete);
                }

                return(RedirectToAction("Index"));
            }
            catch (RoleResultException exception)
            {
                return(View("Error", exception.ErrorStrings));
            }
            catch (RoleNotFoundException)
            {
                return(View("Error", new[]  { "Role Not Found" }));
            }
        }
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    result = await UserManager.AddToRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        ViewData["user"] = System.Web.HttpContext.Current.User.Identity.Name;
                        return(View("Error", result.Errors));
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    result = await UserManager.RemoveFromRoleAsync(userId,
                                                                   model.RoleName);

                    if (!result.Succeeded)
                    {
                        ViewData["user"] = System.Web.HttpContext.Current.User.Identity.Name;
                        return(View("Error", result.Errors));
                    }
                }


                return(RedirectToAction("Index"));
            }
            ViewData["user"] = System.Web.HttpContext.Current.User.Identity.Name;
            return(View("Error", new string[] { "Role Not Found" }));
        }
예제 #14
0
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result = null;

            if (model.UsersToAdd != null)
            {
                foreach (var user in model.UsersToAdd)
                {
                    result = await UserManager.AddToRoleAsync(user, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
            }

            if (model.UsersToDelete != null)
            {
                foreach (var user in model.UsersToDelete)
                {
                    result = await UserManager.RemoveFromRoleAsync(user, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
            }
            TempData["message"] = $"Role {model.RoleName} has been successfully updated";
            return(RedirectToAction("Index"));
        }
예제 #15
0
        public async Task <ActionResult> EditUsersList(RoleModificationModel model)
        {
            IdentityResult result;
            var            userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    result = await userManager.AddToRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error"));
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    result = await userManager.RemoveFromRoleAsync(userId,
                                                                   model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(View("Error"));
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View("Error"));
        }
예제 #16
0
        public async Task <List <RoleModificationModel> > GetRoles()
        {
            List <RoleModificationModel> RMMList = new List <RoleModificationModel>();

            foreach (IdentityRole role in roleManager.Roles)
            {
                List <string> members    = new List <string>();
                List <string> notMembers = new List <string>();
                foreach (AppUser user in userManager.Users)
                {
                    Boolean result = await userManager.IsInRoleAsync(user, role.Name);

                    if (result)
                    {
                        members.Add(user.Id);
                    }
                    else
                    {
                        notMembers.Add(user.Id);
                    }
                }
                RoleModificationModel RMM = new RoleModificationModel()
                {
                    RoleName    = role.Name,
                    RoleId      = role.Id,
                    IdsToAdd    = members.ToArray(),
                    IdsToDelete = notMembers.ToArray()
                };
                RMMList.Add(RMM);
            }
            return(RMMList);
        }
예제 #17
0
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    result = await UserManager.AddToRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        AddErrorsFromResult(result);
                        return(RedirectToAction("Index"));
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    result = await UserManager.RemoveFromRoleAsync(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        AddErrorsFromResult(result);
                        return(RedirectToAction("Index"));
                    }
                }
                return(RedirectToAction("Index"));
            }
            TempData["message"] = "Role Not Found";
            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Error", new[] { "Роль не знайдена" }));
            }
            IdentityResult result;

            foreach (var userId in model.IdsToAdd ?? new string[] { })
            {
                result = await UserManager.AddToRoleAsync(userId, model.RoleName);

                if (!result.Succeeded)
                {
                    _loger.Log(result.Errors.ToString(), LogLevel.Error, DateTime.Now, GetType().ToString());
                    return(View("Error", result.Errors));
                }
            }
            foreach (var userId in model.IdsToDelete ?? new string[] { })
            {
                result = await UserManager.RemoveFromRoleAsync(userId,
                                                               model.RoleName);

                if (!result.Succeeded)
                {
                    _loger.Log(result.Errors.ToString(), LogLevel.Error, DateTime.Now, GetType().ToString());
                    return(View("Error", result.Errors));
                }
            }
            return(RedirectToAction("Index"));
        }
예제 #19
0
        public ActionResult Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsForAddition ?? new string[] { })
                {
                    result = UserManager.AddToRole(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                    }
                }

                foreach (string userId in model.IdsForRemoval ?? new string[] { })
                {
                    result = UserManager.RemoveFromRole(userId, model.RoleName);

                    if (!result.Succeeded)
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                    }
                }

                return(RedirectToAction("Index"));
            }

            return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
        }
예제 #20
0
        public async Task <ActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;      //新建结果

            //验证模型无误
            if (ModelState.IsValid)
            {
                //遍历所有要添加到角色的用户,若为NULL则替换为空数组
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    //添加用户到角色,userId=用户ID,model.RoleName=角色名,返回结果
                    result = await UserManager.AddToRoleAsync(userId, model.RoleName);

                    //添加失败,添加错误模型
                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
                //遍历所有在角色中要删除用户,若为NULL则替换为空数组
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    //删除用户从角色,userId=用户ID,model.RoleName=角色名,返回结果
                    result = await UserManager.RemoveFromRolesAsync(userId, model.RoleName);

                    //删除失败,添加错误模型
                    if (!result.Succeeded)
                    {
                        return(View("Error", result.Errors));
                    }
                }
            }
            return(View("Error", new string[] { "Role Not Found" }));
        }
예제 #21
0
        private async Task <bool> RemoveUserFromRole(string userId, RoleModificationModel model)
        {
            IdentityResult removeResult = await UserManager.RemoveFromRoleAsync(userId, model.RoleName);

            IdentityResult addResult = await UserManager.AddToRoleAsync(userId, "User");

            return(removeResult.Succeeded && addResult.Succeeded);
        }
예제 #22
0
        private async Task <bool> AddUserToRole(string userId, RoleModificationModel model)
        {
            bool removeResult = await RemoveUserCurrentRoles(userId);

            IdentityResult addResult = await UserManager.AddToRoleAsync(userId, model.RoleName);

            return(removeResult && addResult.Succeeded);
        }
예제 #23
0
        /*   public ActionResult Edit(int id, IFormCollection collection)
         * {
         *     try
         *     {
         *         // TODO: Add update logic here
         *
         *         return RedirectToAction(nameof(Index));
         *     }
         *     catch
         *     {
         *         return View();
         *     }
         * }*/

        public async Task <IActionResult> Edit(RoleModificationModel roleMod)
        {
            try
            {
                // TODO: Add update logic here
                IdentityResult result;
                if (ModelState.IsValid)
                {
                    IdentityRole rol = await roleManager.FindByIdAsync(roleMod.RoleId);

                    if (rol != null)
                    {
                        rol.Name = roleMod.RoleName;
                        result   = await roleManager.UpdateAsync(rol);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                    //Si es nulo instancio arreglo vacio sino nada
                    foreach (string userId in roleMod.IdsToAdd ?? new string[] { })
                    {
                        AppUser user = await userManager.FindByIdAsync(userId);

                        if (user != null)
                        {
                            result = await userManager.AddToRoleAsync(user, roleMod.RoleName);

                            if (!result.Succeeded)
                            {
                                AddErrorsFromResult(result);
                            }
                        }
                    }
                    foreach (string userId in roleMod.IdsToDelete ?? new string[] { })
                    {
                        AppUser user = await userManager.FindByIdAsync(userId);

                        if (user != null)
                        {
                            result = await userManager.RemoveFromRoleAsync(user, roleMod.RoleName);

                            if (!result.Succeeded)
                            {
                                AddErrorsFromResult(result);
                            }
                        }
                    }
                    return(RedirectToAction(nameof(Index)));
                }
                return(await Edit(roleMod.RoleId));
            }
            catch
            {
                return(View());
            }
        }
예제 #24
0
        public async Task <ActionResult> Edit(RoleModificationModel rmm)
        {
            //create a result to refer to later
            IdentityResult result;

            //if RoleModificationModel is valid, add new users
            if (ModelState.IsValid)
            {
                //if there are users to add, then add them
                if (rmm.IdsToAdd != null)
                {
                    foreach (string userId in rmm.IdsToAdd)
                    {
                        //find the user in the database using their id
                        AppUser user = await _userManager.FindByIdAsync(userId);

                        //attempt to add the user to the role using the UserManager
                        result = await _userManager.AddToRoleAsync(user, rmm.RoleName);

                        //if attempt to add user to role didn't work, show user the error page
                        if (result.Succeeded == false)
                        {
                            //send user to error page
                            return(View("Error", result.Errors));
                        }
                    }
                }

                //if there are users to remove from the role, remove them
                if (rmm.IdsToDelete != null)
                {
                    //loop through all the ids to remove from role
                    foreach (string userId in rmm.IdsToDelete)
                    {
                        //find the user in the database using their id
                        AppUser user = await _userManager.FindByIdAsync(userId);

                        //attempt to remove the user from the role using the UserManager
                        result = await _userManager.RemoveFromRoleAsync(user, rmm.RoleName);

                        //if attempt to remove the user from role didn't work, show the error page
                        if (result.Succeeded == false)
                        {
                            //show user the error page
                            return(View("Error", result.Errors));
                        }
                    }
                }

                //this is the happy path - all edits worked
                //take the user back to the RoleAdmin Index page
                return(RedirectToAction("Index"));
            }

            //this is a sad path - the role was not found
            //show the user the error page
            return(View("Error", new string[] { "Role Not Found" }));
        }
예제 #25
0
        public async Task Edit([FromBody] RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                IdentityRole role = await roleManager.FindByNameAsync(model.RoleName);

                if (role == null)
                {
                    await Response.WriteAsync("Role not found!");

                    return;
                }
                foreach (string userId in model.IdsToAdd ?? new string[] {})
                {
                    AppUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.AddToRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            await Response.WriteAsync($"{result}");

                            return;
                        }
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] {})
                {
                    AppUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.RemoveFromRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            await Response.WriteAsync($"{result}");

                            return;
                        }
                    }
                }
                await Response.WriteAsync("Role successfully edited");
            }
            else
            {
                await Response.WriteAsync("ModelState is not valid!");
            }
        }
예제 #26
0
        public async Task <IActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    ApplicationUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.AddToRoleAsync(user,

                                                                  model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }

                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    ApplicationUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.RemoveFromRoleAsync(user,

                                                                       model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
            }



            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(await Edit(model.RoleId));
            }
        }
예제 #27
0
        public async Task <IActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                // pętla przechodząca przez liste id które mają być dodane do podanej roli
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    // znalezienie użytkownika po id następnie przypisanie użytkownikowi roli
                    AppUser user = await _userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await _userManager.AddToRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }

                // pętla przechodząca przez liste id które mają być usunięte z podanej roli
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    // znalezienie użytkownika po id następnie usunięcie użytkownikowi roli
                    AppUser user = await _userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await _userManager.RemoveFromRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
            }

            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(await Edit(model.RoleId));
            }
        }
예제 #28
0
        public async Task <IActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    AppUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.AddToRoleAsync(user,
                                                                  model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    AppUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.RemoveFromRoleAsync(user,
                                                                       model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
            }

            if (ModelState.IsValid)
            {
                TempData["message"] = $"Пользователи были удалены из роли {model.RoleName} ";
                return(RedirectToAction("Index", "Book"));
            }
            else
            {
                TempData["error"] = $"Пользователи не были удалены из роли {model.RoleName} ";
                return(await Edit(model.RoleId));
            }
        }
예제 #29
0
        public async Task <ActionResult> EditListOfUsers(RoleModificationModel model)
        {
            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd)
                {
                    ApplicationUser user = await this._userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        IdentityResult result = await
                                                this._userManager.AddToRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            return(View("Error"));
                        }
                    }
                    else
                    {
                        return(View("Error"));
                    }
                }

                foreach (string userId in model.IdsToDelete)
                {
                    ApplicationUser user = await this._userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        IdentityResult result = await
                                                this._userManager.RemoveFromRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            return(View("Error"));
                        }
                    }
                    else
                    {
                        return(View("Error"));
                    }
                }
                return(RedirectToAction(nameof(Roles)));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
예제 #30
0
        public async Task <IActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            // Is form valid?
            if (ModelState.IsValid)
            {
                // Add users to role (if any)
                foreach (var userId in model.IdsToAdd ?? new string[] { })
                {
                    ApplicationUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.AddToRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            foreach (var error in result.Errors)
                            {
                                ModelState.AddModelError("", error.Description);
                            }
                        }
                    }
                }
                // Then remove users from role (if any)
                foreach (var userId in model.IdsToRemove ?? new string[] { })
                {
                    ApplicationUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.RemoveFromRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            foreach (var error in result.Errors)
                            {
                                ModelState.AddModelError("", error.Description);
                            }
                        }
                    }
                }
            }

            // then bring them back to the index
            return(RedirectToAction(nameof(Index)));
        }
예제 #31
0
 public async Task<ActionResult> Edit(RoleModificationModel model)
 {
     IdentityResult result;
     if (ModelState.IsValid)
     {
         foreach (string userId in model.IdsToAdd ?? new string[] { })
         {
             result = await UserManager.AddToRoleAsync(userId, model.RoleName);
             if (!result.Succeeded)
             {
                 return View("Error", result.Errors);
             }
         }
         foreach (string userId in model.IdsToDelete ?? new string[] { })
         {
             result = await UserManager.RemoveFromRoleAsync(userId,
             model.RoleName);
             if (!result.Succeeded)
             {
                 return View("Error", result.Errors);
             }
         }
         return RedirectToAction("Index");
     }
     return View("Error", new string[] { "Role Not Found" });
 }