Exemple #1
0
        public async Task <IActionResult> UpdateCurrentUserPassword([FromBody] UpdateCurrentUserData data)
        {
            try
            {
                _ = int.TryParse(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value, out int userId);
                User user = _DbContext.Users.Find(userId);


                if (!await _UserManager.CheckPasswordAsync(user, data.CurrentPassword).ConfigureAwait(false))
                {
                    CoreFunc.Error(ref ErrorsList, "Current Password is incorrect.");
                    return(StatusCode(412, ErrorsList));
                }


                user.Password = data.User.Password;
                User result = await UpdatePassword(user).ConfigureAwait(false);

                if (result == null)
                {
                    return(StatusCode(412, ErrorsList));
                }
                return(Ok(result));
            }
            catch (Exception ex)
            {
                CoreFunc.Error(ref ErrorsList, _LoggingService.LogException(Request.Path, ex, User));
                return(StatusCode(417, ErrorsList));
            }
        }
Exemple #2
0
        public async Task <IActionResult> UpdateCurrentUser([FromBody] UpdateCurrentUserData currentUserData)
        {
            try
            {
                _ = int.TryParse(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value, out int userId);
                User user = await _DbContext.Users
                            .Include(u => u.RegistrationMethod)
                            .FirstOrDefaultAsync(u => u.Id == userId)
                            .ConfigureAwait(false);

                if (user == null || currentUserData.User.Id != user.Id)
                {
                    CoreFunc.Error(ref ErrorsList, "Information access is denied.");
                    return(UnprocessableEntity(ErrorsList));
                }

                ModelState.Clear();
                TryValidateModel(currentUserData.User);
                ModelState.Remove("PasswordHash");
                if (!ModelState.IsValid)
                {
                    CoreFunc.ExtractErrors(ModelState, ref ErrorsList);
                    return(UnprocessableEntity(ErrorsList));
                }

                if (user.RegistrationMethod.Type == RegistrationTypes.Application && !await _UserManager.CheckPasswordAsync(user, currentUserData.CurrentPassword).ConfigureAwait(false))
                {
                    CoreFunc.Error(ref ErrorsList, "Current Password is incorrect.");
                    return(StatusCode(412, ErrorsList));
                }

                user.FirstName = currentUserData.User.FirstName;
                user.Surname   = currentUserData.User.Surname;
                if (user.RegistrationMethod.Type == RegistrationTypes.Application && user.Email != currentUserData.User.Email)
                {
                    if (await _DbContext.Users.AnyAsync(d => d.NormalizedEmail == currentUserData.User.Email.ToUpper()).ConfigureAwait(false))
                    {
                        CoreFunc.Error(ref ErrorsList, "This email is already registered.");
                        return(StatusCode(412, ErrorsList));
                    }

                    await _DbContext.Communications.Where(c => c.Email == user.Email)
                    .ForEachAsync(c => c.Email = currentUserData.User.Email).ConfigureAwait(false);

                    await _DbContext.Newsletters.Where(c => c.Email == user.Email)
                    .ForEachAsync(c => c.Email = currentUserData.User.Email).ConfigureAwait(false);

                    user.Email           = currentUserData.User.Email;
                    user.NormalizedEmail = currentUserData.User.Email.ToUpper();
                }
                user.PhoneNumber = currentUserData.User.PhoneNumber;
                _DbContext.Users.Update(user);
                await _DbContext.SaveChangesAsync().ConfigureAwait(false);

                return(Ok(user));
            }
            catch (Exception ex)
            {
                CoreFunc.Error(ref ErrorsList, _LoggingService.LogException(Request.Path, ex, User));
                return(StatusCode(417, ErrorsList));
            }
        }