public async Task <IActionResult> Edit(AccountUserViewModelEdit model)
        {
            if (ModelState.IsValid)
            {
                var existingUser = await CommonContext.UserAccounts.Include(x => x.User)
                                   .Where(m => m.UserId == model.UserId && m.AccountId == CommonAccount.Id).SingleOrDefaultAsync();

                if (existingUser == null)
                {
                    throw new Exception($"User cannot be found. UserId:{model.UserId}");
                }

                if (User.GetLoggedInUserId().Value == model.UserId && !model.Permissions.HasFlag(AccountPermissions.AccountAdministrator))
                {
                    ModelState.AddModelError(string.Empty, "You cannot remove the Account Administrator permission");
                    model.FirstName = existingUser.User.FirstName;
                    model.LastName  = existingUser.User.LastName;
                    model.Email     = existingUser.User.Email;
                    return(View(model));
                }

                if (User.GetLoggedInUserId().Value == model.UserId && model.Disabled)
                {
                    ModelState.AddModelError(string.Empty, "You cannot disable your own account");
                    model.FirstName = existingUser.User.FirstName;
                    model.LastName  = existingUser.User.LastName;
                    model.Email     = existingUser.User.Email;
                    return(View(model));
                }

                existingUser.Permissions  = model.Permissions;
                existingUser.Disabled     = model.Disabled;
                existingUser.UpdateUserId = LoggedInUser.Id;
                existingUser.UpdateUtc    = DateTime.UtcNow;

                using (var tx = CommonContext.Database.BeginTransaction())
                {
                    CommonContext.UserAccounts.Update(existingUser);
                    await CommonContext.SaveChangesAsync();

                    tx.Commit();

                    var cacheKey = WebCacheKey.CommonUserAccount(CommonAccount.Number, existingUser.UserId.Value);
                    await _cache.RemoveAsync(cacheKey);
                }

                return(RedirectToAction("Users"));
            }

            return(View(model));
        }
        public async Task <IActionResult> Edit(Guid Id)
        {
            AccountUserViewModelEdit model = new AccountUserViewModelEdit();

            var existingUser = await CommonContext.UserAccounts.Include(x => x.User)
                               .Where(m => m.UserId == Id && m.AccountId == CommonAccount.Id).SingleOrDefaultAsync();

            model.Id          = existingUser.Id;
            model.FirstName   = existingUser.User.FirstName;
            model.LastName    = existingUser.User.LastName;
            model.Permissions = existingUser.Permissions;
            model.UserId      = existingUser.User.Id;
            model.Email       = existingUser.User.Email;
            model.Disabled    = existingUser.Disabled;
            return(View(model));
        }