public async Task <ActionResult> Edit(UpdateResourceOwnerViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }

            await TranslateUserEditView(DefaultLanguage);

            var authenticatedUser = await SetUser();

            if (!ModelState.IsValid)
            {
                ViewBag.IsUpdated = false;
                return(View(viewModel));
            }

            var resourceOwner = await _userActions.GetUser(authenticatedUser);

            var subject = authenticatedUser.GetSubject();

            if (resourceOwner == null)
            {
                var record = viewModel.ToAddUserParameter();
                await _userActions.AddUser(record);
            }
            else
            {
                var parameter = viewModel.ToParameter();
                foreach (var newClaim in resourceOwner.Claims.Where(uc => !parameter.Claims.Any(pc => pc.Type == uc.Type)))
                {
                    parameter.Claims.Add(newClaim);
                }

                parameter.Login = subject;
                await _userActions.UpdateUser(parameter);
            }

            ViewBag.IsUpdated = true;
            return(View(viewModel));
        }
        public async Task <ActionResult> Edit(UpdateResourceOwnerViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }

            // 1. Set view bag
            var subject = (await this.GetAuthenticatedUser(Constants.CookieName)).GetSubject();
            var user    = await GetCurrentUser();

            if (!await SetUserEditViewBag(user))
            {
                throw new IdentityServerException(
                          ErrorCodes.UnhandledExceptionCode,
                          ErrorDescriptions.TheResourceOwnerIsNotALocalAccount);
            }

            // 2. Validate the view model
            if (!ModelState.IsValid)
            {
                ViewBag.IsUpdated = false;
                return(View(viewModel));
            }

            // 3. Update the resource owner
            var parameter = viewModel.ToParameter();

            foreach (var newClaim in user.Claims.Where(uc => !parameter.Claims.Any(pc => pc.Type == uc.Type)))
            {
                parameter.Claims.Add(newClaim);
            }

            parameter.Login = subject;
            await _userActions.UpdateUser(parameter);

            // 4. Returns translated view
            ViewBag.IsUpdated = true;
            return(View(viewModel));
        }