Exemplo n.º 1
0
        /// <summary>
        /// Tests the current connection until the user enters a correct password.
        /// </summary>
        /// <param name="wbConnection">A <see cref="MySqlWorkbenchConnection"/> object representing the connection to a MySQL server instance selected by users.</param>
        /// <param name="tryConnectionBeforeAskingForPassword">Flag indicating whether a connection test is made with the connection as is before asking for a password</param>
        /// <returns>A <see cref="PasswordDialogFlags"/> containing data about the operation.</returns>
        public static PasswordDialogFlags TestConnectionAndRetryOnWrongPassword(this MySqlWorkbenchConnection wbConnection, bool tryConnectionBeforeAskingForPassword = true)
        {
            PasswordDialogFlags passwordFlags = new PasswordDialogFlags(wbConnection)
            {
                // Assume a wrong password at first so if the connection is not tested without a password we ensure to ask for one.
                ConnectionResult = TestConnectionResult.WrongPassword
            };

            // First connection attempt with the connection exactly as loaded (maybe without a password).
            if (tryConnectionBeforeAskingForPassword)
            {
                passwordFlags.ConnectionResult = wbConnection.TestConnectionAndReturnResult(false);
                passwordFlags.Cancelled        = passwordFlags.ConnectionResult == TestConnectionResult.PasswordExpired;

                // If on the first attempt a connection could not be made and not because of a bad password, exit.
                if (!passwordFlags.ConnectionSuccess && !passwordFlags.WrongPassword)
                {
                    return(passwordFlags);
                }
            }

            // If the connection does not have a stored password or the stored password failed then ask for one and retry.
            while (!passwordFlags.ConnectionSuccess && passwordFlags.WrongPassword)
            {
                passwordFlags = PasswordDialog.ShowConnectionPasswordDialog(wbConnection, true);
                if (passwordFlags.Cancelled)
                {
                    break;
                }

                wbConnection.Password = passwordFlags.NewPassword;
            }

            return(passwordFlags);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PasswordDialog"/> class.
 /// </summary>
 /// <param name="wbConnection">A <see cref="MySqlWorkbenchConnection"/> object representing the connection to a MySQL server instance selected by users.</param>
 /// <param name="testConnection">Flag indicating whether the connection is tested after setting the password.</param>
 /// <param name="passwordExpired">Flag indicating if the dialog will be used to set a new password when an old one expired.</param>
 public PasswordDialog(MySqlWorkbenchConnection wbConnection, bool testConnection, bool passwordExpired)
 {
     _testConnection = testConnection;
     _passwordFlags  = new PasswordDialogFlags(wbConnection);
     InitializeComponent();
     PasswordExpiredDialog     = passwordExpired;
     _wbConnection             = wbConnection;
     UserValueLabel.Text       = _wbConnection.UserName;
     ConnectionValueLabel.Text = _wbConnection.Name + @" - " + _wbConnection.HostIdentifier;
     PasswordTextBox.Text      = _wbConnection.Password;
     SetDialogInterface();
 }
Exemplo n.º 3
0
        /// <summary>
        /// Tests the given connection to check if it can successfully connect to the corresponding MySQL instance.
        /// </summary>
        /// <param name="connection">MySQL Workbench connection to a MySQL server instance selected by users.</param>
        /// <param name="displayErrorOnEmptyPassword">Flag indicating whether errors caused by a blank or null password are displayed to the user.</param>
        /// <returns>Enumeration indicating the result of the connection test.</returns>
        public static TestConnectionResult TestConnectionAndReturnResult(this MySqlWorkbenchConnection connection, bool displayErrorOnEmptyPassword)
        {
            if (connection == null)
            {
                return(TestConnectionResult.None);
            }

            Globals.ThisAddIn.Application.Cursor = ExcelInterop.XlMousePointer.xlWait;
            TestConnectionResult connectionResult;
            Exception            connectionException;

            if (connection.TestConnection(out connectionException))
            {
                connectionResult = TestConnectionResult.ConnectionSuccess;
                Globals.ThisAddIn.Application.Cursor = ExcelInterop.XlMousePointer.xlDefault;
                return(connectionResult);
            }

            // If the error returned is about the connection failing the password check, it may be because either the stored password is wrong or no password.
            connectionResult = TestConnectionResult.ConnectionError;
            if (connectionException is MySqlException)
            {
                MySqlException mySqlException = connectionException as MySqlException;
                switch (mySqlException.Number)
                {
                // Connection could not be made.
                case MySqlWorkbenchConnection.MYSQL_EXCEPTION_NUMBER_SERVER_UNREACHABLE:
                    connectionResult = TestConnectionResult.HostUnreachable;
                    InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(
                                              Resources.ConnectFailedWarningTitle,
                                              mySqlException.Message,
                                              null,
                                              mySqlException.InnerException != null ? mySqlException.InnerException.Message : null));
                    break;

                // Wrong password.
                case MySqlWorkbenchConnection.MYSQL_EXCEPTION_NUMBER_WRONG_PASSWORD:
                    connectionResult = TestConnectionResult.WrongPassword;
                    if (!string.IsNullOrEmpty(connection.Password) || displayErrorOnEmptyPassword)
                    {
                        InfoDialog.ShowDialog(InfoDialogProperties.GetWarningDialogProperties(
                                                  Resources.ConnectFailedWarningTitle,
                                                  mySqlException.Message,
                                                  null,
                                                  connection.IsSsl() ? Resources.ConnectSSLFailedDetailWarning : null));
                    }
                    break;

                // Password has expired so any statement can't be run before resetting the expired password.
                case MySqlWorkbenchConnection.MYSQL_EXCEPTION_NUMBER_EXPIRED_PASSWORD:
                    PasswordDialogFlags passwordFlags = PasswordDialog.ShowExpiredPasswordDialog(connection, false);
                    if (!passwordFlags.Cancelled)
                    {
                        connection.Password = passwordFlags.NewPassword;
                    }

                    connectionResult = passwordFlags.Cancelled ? TestConnectionResult.PasswordExpired : TestConnectionResult.PasswordReset;
                    break;

                // Any other exception.
                default:
                    InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(
                                              Resources.ConnectFailedWarningTitle,
                                              string.Format(Resources.GenericConnectionErrorText, mySqlException.Number, mySqlException.Message),
                                              null,
                                              mySqlException.InnerException != null ? mySqlException.InnerException.Message : null));
                    break;
                }
            }
            else
            {
                InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(
                                          Resources.ConnectFailedWarningTitle,
                                          connectionException.Message,
                                          null,
                                          connectionException.InnerException != null ? connectionException.InnerException.Message : null));
            }

            Globals.ThisAddIn.Application.Cursor = ExcelInterop.XlMousePointer.xlDefault;
            return(connectionResult);
        }