public async Task<ActionResult> Edit(UserAccountInfoViewModel model)
        {
            var tdPendingRole = RoleManager.Roles.FirstOrDefault(r => r.Name == "TdPendingUsers");
            ViewBag.TdPendingUsersRoleId = tdPendingRole == null ? "" : tdPendingRole.Id;
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            var user = await UserManager.FindByIdAsync(model.User.Id);
           
            //deal with locks first
            if (await SetLockout(model, user))
            {
                //do user info updates
                if (await UpdateUserInfo(user, model))
                {
                  
                    //if any roles other than pending are selected, make sure we remove the pending role from the model first
                    if (tdPendingRole != null)
                    {
                        if (model.Roles.Any(r => r != tdPendingRole.Id))
                        {
                            var mRoles = model.Roles.ToList();
                            mRoles.Remove(tdPendingRole.Id);
                            model.Roles = mRoles;
                        }
                    }

                    //get role changes
                    var roleIdsToRemove = user.Roles.Select(ur => ur.RoleId).Except(model.Roles).ToList();
                    var roleIdsToAdd = model.Roles.Except(user.Roles.Select(ur => ur.RoleId)).ToList();
                    //do role removes
                    if (await RemoveRoles(roleIdsToRemove, user))
                    {
                        //do role adds
                        if (await AddRoles(roleIdsToAdd, user))
                        {
                            //everything worked, return to index (if anything failed, will return to the edit user view)
                            return RedirectToAction("Index");
                        }
                    }
                }
            }
            //Since the above operations could be partially committed before a failure, 
            //  we will re-get the user so we are sure the screen will accurately show 
            //  the current data. Model errors will have been added by the respective 
            //  update operations that failed
            user = await UserManager.FindByIdAsync(model.User.Id);
            model = new UserAccountInfoViewModel(user, user.Roles.Select(r => r.RoleId));
            return View(model);
        }
 public async Task<ActionResult> Edit(string id)
 {
     //ViewBag.AllRolesList = await RoleManager.Roles.ToListAsync();
     var user = await UserManager.FindByIdAsync(id);
     if (user == null)
     {
         return RedirectToAction("Index");
     }
     var model = new UserAccountInfoViewModel(user, user.Roles.Select(r => r.RoleId));
     var tdPendingRole = RoleManager.Roles.FirstOrDefault(r => r.Name == "TdPendingUsers");
     
     ViewBag.TdPendingUsersRoleId = tdPendingRole == null ? "" : tdPendingRole.Id; 
     return View(model);
 }
        private async Task<bool> SetLockout(UserAccountInfoViewModel model, TicketDeskUser user)
        {
            var currentlyLocked = UserManager.IsLockedOut(model.User.Id);
            var currentlyDisabled = currentlyLocked && (user.LockoutEndDateUtc ?? DateTime.MinValue).ToUniversalTime() == DateTime.MaxValue.ToUniversalTime();

            if (currentlyDisabled != model.IsDisabled || currentlyLocked != model.IsLocked)
            {
                DateTimeOffset newLockoutDate = DateTimeOffset.MinValue;
                if (model.IsDisabled)
                {
                    newLockoutDate = DateTimeOffset.MaxValue;
                }
                else if (model.IsLocked)
                {
                    newLockoutDate = DateTimeOffset.Now.Add(UserManager.DefaultAccountLockoutTimeSpan);
                }

                var result = await UserManager.SetLockoutEndDateAsync(model.User.Id, newLockoutDate);
                if (!result.Succeeded)
                {
                    AddErrors(result);
                }
                return result.Succeeded;
            }
            return true;
        }
 private async Task<bool> UpdateUserInfo(TicketDeskUser user, UserAccountInfoViewModel model)
 {
     user.UserName = model.User.Email;
     user.Email = model.User.Email;
     user.DisplayName = model.User.DisplayName;
     var result = await UserManager.UpdateAsync(user);
     if (!result.Succeeded)
     {
         AddErrors(result);
     }
     return result.Succeeded;
 }