Exemplo n.º 1
0
        /// <summary>
        /// Update user password data by Id
        /// </summary>
        /// <param name="id">Id from model user</param>
        /// <param name="newPassword">new password</param>
        /// <returns></returns>
        public bool UpdatePassword(int id, string newPassword)
        {
            if (string.IsNullOrEmpty(newPassword))
            {
                return(false);
            }

            var update = false;

            try
            {
                using (var userRepository = Factory.GetUserRepository())
                {
                    var userDB = userRepository.GetById(id);
                    userDB.Password = PasswordEncryptor.GetHashString(newPassword);
                    userRepository.AddOrUpdate(userDB);
                    userRepository.SaveChanges();
                }

                update = true;
            }
            catch (Exception ex)
            {
                _logger.Error($"Произошла ошибка при изменении пароля:\r\n{ex}");
            }

            return(update);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool RegisterUser(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            user.Password = PasswordEncryptor.GetHashString(user.Password);
            user.UserRole = this.GetDefaultUserRoles();

            try
            {
                using (var userRepository = Factory.GetUserRepository())
                {
                    userRepository.AddOrUpdate(user);
                    userRepository.SaveChanges();
                }

                using (var userDataRepository = Factory.GetUserDataRepository())
                {
                    var userData = new UserData
                    {
                        UserId       = user.Id,
                        CurrencyId   = ServiceHelper.GetDefaultCurrency(Factory).Id,
                        PriceLevelId = ServiceHelper.GetDefaultPriceLevel(Factory)
                    };

                    userDataRepository.AddOrUpdate(userData);
                    userDataRepository.SaveChanges();
                }

                return(true);
            }
            catch (Exception ex)
            {
                _logger.Error($"Произошла ошибка при регистрации пользователя:\r\n{ex}");
                return(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Update user password data by Id
        /// </summary>
        /// <param name="id">Id from model user</param>
        /// <param name="newPassword">new password</param>
        /// <param name="oldPassword">old password</param>
        /// <returns></returns>
        public bool UpdatePassword(int id, string newPassword, string oldPassword)
        {
            if (string.IsNullOrEmpty(newPassword) || string.IsNullOrEmpty(oldPassword))
            {
                return(false);
            }

            var update = false;

            using (var userRepository = Factory.GetUserRepository())
            {
                var userUpdate = userRepository.GetById(id);
                if (userUpdate != null)
                {
                    if (userUpdate.Password.Equals(PasswordEncryptor.GetHashString(oldPassword)))
                    {
                        userUpdate.Password = PasswordEncryptor.GetHashString(newPassword);
                    }
                    else
                    {
                        throw new UserValidationException("Старый пароль введён неверно", "OldPassword");
                    }

                    try
                    {
                        userRepository.AddOrUpdate(userUpdate);
                        userRepository.SaveChanges();
                        update = true;
                    }
                    catch (Exception ex)
                    {
                        _logger.Error($"Произошла ошибка при изменении пароля:\r\n{ex}");
                    }
                }
            }

            return(update);
        }