Esempio n. 1
0
        public ActionResult Edit(UserRoleViewModel model)
        {
            ViewBag.Users = UserManager.Users;

            if (String.IsNullOrEmpty(model.SelectedUserId))
            {
                TempData["message"] = MessageHelper.BuildMessage(EnumSevereLevel.Error, "No user is selected!");
                return View(model);
            }            

            IdentityResult result;

            if (ModelState.IsValid)
            {
                //delete existing roles
                var selectedUser = UserManager.Users.Where(x => x.Id == model.SelectedUserId).FirstOrDefault();
                string[] currentRoles = RoleManager.Roles.ToList().Where(x => selectedUser.Roles.Any(y => y.RoleId == x.Id)).Select(x => x.Name).ToArray();
                if (currentRoles.Length > 0)
                {
                    result = UserManager.RemoveFromRoles(model.SelectedUserId, currentRoles);

                    if (!result.Succeeded)
                    {
                        TempData["message"] = MessageHelper.BuildMessage(EnumSevereLevel.Error, result);
                        return View(model);
                    }
                }

                //add new roles
                List<SelectRoleEditorViewModel> selectedRoles = model.Roles.Where(x => x.Selected).ToList();
                foreach (var role in selectedRoles)
                {
                    result = UserManager.AddToRole(model.SelectedUserId, role.RoleName);
                    if (!result.Succeeded)
                    {
                        TempData["message"] = MessageHelper.BuildMessage(EnumSevereLevel.Error, result);
                        return View(model);
                    }
                }

                TempData["message"] = MessageHelper.BuildMessage(EnumSevereLevel.Success, string.Format("{0} has been saved!", selectedUser.UserName));
                if (String.IsNullOrEmpty(model.ControllerName))
                    return RedirectToAction("List"); //Return to current controller
                else
                    return RedirectToAction("List", model.ControllerName); //Return to User controller
            }
            else
            {
                //There is something wrong with the data values
                TempData["message"] = MessageHelper.BuildMessage(EnumSevereLevel.Error, "There is something wrong with the data values, please check!");
                return View(model);
            }
        }
Esempio n. 2
0
        public string GetRoles(string id)
        {
            var selectedUser = UserManager.Users.Where(x => x.Id == id).FirstOrDefault();

            var model = new UserRoleViewModel
            {
                //SelectedRoleIds = selectedUser.Roles.Select(x => x.RoleId).ToArray(),
                Roles = GetViewModelRoles(),
                SelectedUserId = id
            };

            return new JavaScriptSerializer().Serialize(model);
        }
Esempio n. 3
0
 public ViewResult Edit(string id, string controllername = "")
 {
     ViewBag.Users = UserManager.Users;
     var selectedUser = UserManager.Users.Where(x => x.Id == id).FirstOrDefault();
     var model = new UserRoleViewModel(selectedUser, controllername, RoleManager.Roles);
     return View(model);
 }
Esempio n. 4
0
 public ViewResult Roles(string id)
 {
     ViewBag.Users = UserManager.Users;
     var selectedUser = UserManager.Users.Where(x => x.Id == id).FirstOrDefault();
     var model = new UserRoleViewModel(selectedUser, "", RoleManager.Roles);
     return View(model);
 }