public ActionResult SetPermissions(UserPermissionsViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            var user = _userManager.FindById(model.UserId);
            if (user == null)
                return HttpNotFound();

            var currentRoles = _userManager.GetRoles(model.UserId);
            var rolesToRemove = model.Roles.Where(x => !x.Value && currentRoles.Contains(x.Key)).Select(x => x.Key).ToArray();
            var rolesToAdd = model.Roles.Where(x => x.Value && !currentRoles.Contains(x.Key)).Select(x => x.Key).ToArray();

            var removeResult = _userManager.RemoveFromRoles(model.UserId, rolesToRemove);
            if (!removeResult.Succeeded)
            {
                ModelState.AddModelError("", removeResult.Errors.First());
                return View(model);
            }

            var addResult = _userManager.AddToRoles(model.UserId, rolesToAdd);
            if (!addResult.Succeeded)
            {
                ModelState.AddModelError("", removeResult.Errors.First());
                return View(model);
            }

            return RedirectToAction("Index");
        }
        public ActionResult Permissions(int id)
        {
            var user = _userManager.FindById(id);

            if (user == null)
                return HttpNotFound();

            var availableRoles = _roleManager.Roles.Select(x => x.Name).ToList();
            var givenRoles = _userManager.GetRoles(id);

            var viewModel = new UserPermissionsViewModel()
            {
                UserId = id,
                UserName = user.UserName,
                Roles = availableRoles.ToDictionary(key => key, value => givenRoles.Contains(value))
            };

            return View(viewModel);
        }