public async Task <ActionResult> IzmeniPrava(string userId)
        {
            // userID : ID korisnikia cija prava menja admin
            // Pitati profesora
            var user = await UserManager.FindByIdAsync(userId);

            if (user != null)
            {
                var allRoles = RoleManager.Roles.Select(role => role.Name);
                var model    = new RoleModifyModel
                {
                    UserId   = user.Id,
                    UserName = user.UserName,
                    IsInRole = new Dictionary <string, bool>(),
                    AllRoles = allRoles
                };

                foreach (var roleName in allRoles)
                {
                    bool isInRole = await UserManager.IsInRoleAsync(user.Id, roleName);

                    model.IsInRole.Add(key: roleName, value: isInRole);
                }

                return(View(model));
            }
            return(View("AdminErrorPage"));
        }
Example #2
0
        public async Task <ActionResult> Edit(RoleModifyModel 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[] { "Роль не найдена" }));
        }
Example #3
0
        public async Task <IActionResult> Modify(RoleModifyModel model)
        {
            IdentityResult result;

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

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

                        if (!result.Succeeded)
                        {
                            Errors(result);
                        }
                    }
                }
                foreach (string userId in model.DeleteIds ?? new string[] { })
                {
                    SimUser user = await _userManager.FindByIdAsync(userId);

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

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

            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(await Modify(model.RoleId));
            }
        }