Esempio n. 1
0
        public async Task <IActionResult> EditProfilePost(EditProfileViewModel model)
        {
            var user = await _userManager.FindByIdAsync(model.Id.ToString());

            if (user == null)
            {
                return(NotFound());
            }

            // Validate model state within all view providers
            if (await _editProfileViewProvider.IsModelStateValidAsync(model, this))
            {
                user.DisplayName = model.DisplayName;
                user.Location    = model.Location;
                user.Url         = model.Url;
                user.Biography   = model.Biography;

                // Update user
                var result = await _platoUserManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    // Invoke BuildUpdateAsync within involved view providers
                    await _editProfileViewProvider.ProvideUpdateAsync(model, this);

                    // Ensure model state is still valid after view providers have executed
                    if (ModelState.IsValid)
                    {
                        // Add confirmation
                        _alerter.Success(T["Profile Updated Successfully!"]);
                        // Redirect
                        return(RedirectToAction(nameof(EditProfile)));
                    }
                }
                else
                {
                    // Errors that may have occurred whilst updating the entity
                    foreach (var error in result.Errors)
                    {
                        ViewData.ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }

            // If errors occur manually expire the cache otherwise our
            // modifications made above to the object may persist as the
            // object is not updated and the cache is not invalidated by the store
            _platoUserStore.CancelTokens(user);

            // Display errors
            return(await EditProfile());
        }
Esempio n. 2
0
        public async Task <IActionResult> EditPost(EditUserViewModel model)
        {
            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User,
                                                            Permissions.EditUsers))
            {
                return(Unauthorized());
            }

            // Get user
            var user = await _userManager.FindByIdAsync(model.Id.ToString());

            // Ensure user exists
            if (user == null)
            {
                return(NotFound());
            }

            // Flags to indicate if the username or email address have changed
            var emailChanged    = model.Email != null && !model.Email.Equals(user.Email, StringComparison.OrdinalIgnoreCase);
            var usernameChanged = model.UserName != null && !model.UserName.Equals(user.UserName, StringComparison.OrdinalIgnoreCase);

            // Update user, if update is not successful we clear cache below
            user.DisplayName = model.DisplayName;
            user.UserName    = model.UserName;
            user.Email       = model.Email;
            user.Biography   = model.Biography;
            user.Location    = model.Location;
            user.Signature   = model.Signature;
            user.Url         = model.Url;

            // Validate view providers
            if (await _viewProvider.IsModelStateValidAsync(user, this))
            {
                // Get composed model from involved view providers
                user = await _viewProvider.ComposeModelAsync(user, this);

                // Update user
                var result = await _platoUserManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    if (emailChanged)
                    {
                        // Only call SetEmailAsync if the email address changes
                        // SetEmailAsync internally sets EmailConfirmed to "false"
                        await _userManager.SetEmailAsync(user, model.Email);
                    }

                    if (usernameChanged)
                    {
                        // SetUserNameAsync internally sets a new SecurityStamp
                        // which will invalidate the authentication cookie
                        // This will force the user to be logged out
                        await _userManager.SetUserNameAsync(user, model.UserName);
                    }

                    // Execute view providers ProvideUpdateAsync methods
                    await _viewProvider.ProvideUpdateAsync(result.Response, this);

                    // Add confirmation
                    _alerter.Success(T["User Updated Successfully!"]);

                    // Redirect back to edit user
                    return(RedirectToAction(nameof(Edit), new RouteValueDictionary()
                    {
                        ["id"] = user.Id.ToString()
                    }));
                }
                else
                {
                    // Errors that may have occurred whilst updating the entity
                    foreach (var error in result.Errors)
                    {
                        ViewData.ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }

            // If errors occur manually expire the cache otherwise our
            // modifications made above to the object may persist as the
            // object is not updated and the cache is not invalidated by the store
            _platoUserStore.CancelTokens(user);

            // Redirect back to any errors
            return(await Edit(model.Id.ToString()));
        }