Ejemplo n.º 1
0
        /// <summary>
        /// Updates a specific value from the client data store for the user profile details
        /// and attempts to update the server to match those details.
        /// For example, updating the first name of the user.
        /// </summary>
        /// <param name="displayName">The display name for logging and display purposes of the property we are updating</param>
        /// <param name="propertyToUpdate">The property from the <see cref="LoginCredentialsDataModel"/> to be updated</param>
        /// <param name="newValue">The new value to update the property to</param>
        /// <param name="setApiModel">Sets the correct property in the <see cref="UpdateUserProfileApiModel"/> model that this property maps to</param>
        /// <returns></returns>
        private async Task <bool> UpdateUserCredentialsValueAsync(string displayName, Expression <Func <LoginCredentialsDataModel, string> > propertyToUpdate, string newValue, Action <UpdateUserProfileApiModel, string> setApiModel)
        {
            // Log it
            Logger.LogDebugSource($"Saving {displayName}...");

            // Get the current known credentials
            var credentials = await ClientDataStore.GetLoginCredentialsAsync();

            // Get the property to update from the credentials
            var toUpdate = propertyToUpdate.GetPropertyValue(credentials);

            // Log it
            Logger.LogDebugSource($"{displayName} currently {toUpdate}, updating to {newValue}");

            // Check if the value is the same. If so...
            if (toUpdate == newValue)
            {
                // Log it
                Logger.LogDebugSource($"{displayName} is the same, ignoring");

                // Return true
                return(true);
            }

            // Set the property
            propertyToUpdate.SetPropertyValue(newValue, credentials);

            // Create update details
            var updateApiModel = new UpdateUserProfileApiModel();

            // Ask caller to set appropriate value
            setApiModel(updateApiModel, newValue);

            // Update the server with the details
            var result = await WebRequests.PostAsync <ApiResponse>(
                // Set URL
                RouteHelpers.GetAbsoluteRoute(ApiRoutes.UpdateUserProfile),
                // Pass the Api model
                updateApiModel,
                // Pass in user Token
                bearerToken : credentials.Token);

            // If the response has an error...
            if (await result.DisplayErrorIfFailedAsync($"Update {displayName}"))
            {
                // Log it
                Logger.LogDebugSource($"Failed to update {displayName}. {result.ErrorMessage}");

                // Return false
                return(false);
            }

            // Log it
            Logger.LogDebugSource($"Successfully updated {displayName}. Saving to local database cache...");

            // Store the new user credentials the data store
            await ClientDataStore.SaveLoginCredentialsAsync(credentials);

            // Return successful
            return(true);
        }
Ejemplo n.º 2
0
        public async Task <ApiResponse> UpdateUserProfileAsync([FromBody] UpdateUserProfileApiModel model)
        {
            #region Declare Variables

            // Make a list of empty errors
            var errors = new List <string>();

            // Keep track of email change
            var emailChanged = false;

            #endregion

            #region Get User

            // Get the current user
            var user = await mUserManager.GetUserAsync(HttpContext.User);

            // If we have no user...
            if (user == null)
            {
                return new ApiResponse
                       {
                           // TODO: Localization
                           ErrorMessage = "User not found"
                       }
            }
            ;

            #endregion

            #region Update Profile

            // If we have a first name...
            if (model.FirstName != null)
            {
                // Update the profile details
                user.FirstName = model.FirstName;
            }

            // If we have a last name...
            if (model.LastName != null)
            {
                // Update the profile details
                user.LastName = model.LastName;
            }

            // If we have a email...
            if (model.Email != null &&
                // And it is not the same...
                !string.Equals(model.Email.Replace(" ", ""), user.NormalizedEmail))
            {
                // Update the email
                user.Email = model.Email;

                // Un-verify the email
                user.EmailConfirmed = false;

                // Flag we have changed email
                emailChanged = true;
            }

            // If we have a username...
            if (model.Username != null)
            {
                // Update the profile details
                user.UserName = model.Username;
            }

            #endregion

            #region Save Profile

            // Attempt to commit changes to data store
            var result = await mUserManager.UpdateAsync(user);

            // If successful, send out email verification
            if (result.Succeeded)
            {
                // Send email verification
                await SendUserEmailVerificationAsync(user);
            }

            #endregion

            #region Respond

            // If we were successful...
            if (result.Succeeded)
            {
                // Return successful response
                return(new ApiResponse());
            }
            // Otherwise if it failed...
            else
            {
                // Return the failed response
                return new ApiResponse
                       {
                           ErrorMessage = result.Errors.AggregateErrors()
                       }
            };

            #endregion
        }
Ejemplo n.º 3
0
        public async Task <ApiResponse> UpdateUserProfileAsync([FromBody] UpdateUserProfileApiModel model)
        {
            #region Declare Variables

            // Make a list of empty errors
            var errors = new List <string>();

            #endregion

            #region Get User

            // Get the current user
            var user = await mUserManager.FindByIdAsync(model.Id);

            // If we have no user...
            if (user == null)
            {
                return new ApiResponse
                       {
                           // TODO: Localization
                           ErrorMessage = "User not found"
                       }
            }
            ;


            #endregion

            #region Update Profile

            // If we have a first name...
            if (model.FirstName != null)
            {
                // Update the profile details
                user.FirstName = model.FirstName;
            }

            // If we have a last name...
            if (model.LastName != null)
            {
                // Update the profile details
                user.LastName = model.LastName;
            }

            // If we have a username...
            if (model.Username != null)
            {
                // Update the profile details
                user.UserName = model.Username;
            }

            #endregion

            #region Save Profile

            // Attempt to commit changes to data store
            var result = await mUserManager.UpdateAsync(user);



            #endregion
            var roles = await mUserManager.GetRolesAsync(user);

            string role = "";
            foreach (var r in roles)
            {
                role = r;
            }
            #region Respond

            // If we were successful...
            if (result.Succeeded)
            {
                // Return successful response
                return new ApiResponse <UserProfileDetailsApiModel>
                       {
                           // Pass back the user details and the token
                           Response = new UserProfileDetailsApiModel
                           {
                               Id        = user.Id,
                               FirstName = user.FirstName,
                               LastName  = user.LastName,
                               Email     = user.Email,
                               Username  = user.UserName,
                               Token     = user.GenerateJwtToken(role),
                           }
                       }
            }
            ;
            // Otherwise if it failed...
            else
            {
                // Return the failed response
                return new ApiResponse
                       {
                           ErrorMessage = result.Errors.AggregateErrors()
                       }
            };

            #endregion
        }
Ejemplo n.º 4
0
        public async Task <ApiResponse> UpdateUserProfileAsync([FromBody] UpdateUserProfileApiModel model)
        {
            // Get the user
            var user = await mUserManager.GetUserAsync(HttpContext.User);

            // If we have no user...
            if (user == null)
            {
                // Return error
                return new ApiResponse
                       {
                           ErrorMessage = "User not found"
                       }
            }
            ;

            // If we have a first name...
            if (model.FirstName != null)
            {
                // Update the profile details
                user.FirstName = model.FirstName;
            }

            // If we have a last name...
            if (model.LastName != null)
            {
                // Update the profile details
                user.LastName = model.LastName;
            }

            // If we have an email...
            if (model.Email != null)
            {
                // Update the profile details
                user.Email = model.Email;
            }

            // If we have a username...
            if (model.Username != null)
            {
                // Update the profile details
                user.UserName = model.Username;
            }

            // Attempt to commit changes to the data store
            var result = await mUserManager.UpdateAsync(user);

            // If update was successful...
            if (result.Succeeded)
            {
                // Return successful response
                return(new ApiResponse());
            }
            // Otherwise if it failed...
            else
            {
                // Return the failed response
                return new ApiResponse
                       {
                           ErrorMessage = result.Errors.AggregateErrors()
                       }
            };
        }