Exemple #1
0
        public static void NewPassword(SqliteConnection connection, SqliteConnection _conn)
        {
            var newPasswordInput = new NSStackView(new CGRect(0, 0, 300, 50));

            var originalPassword = new NSSecureTextField(new CGRect(0, 25, 300, 20));

            originalPassword.PlaceholderAttributedString = new Foundation.NSAttributedString("Type new password...");
            var confirmedPassword = new NSSecureTextField(new CGRect(0, 0, 300, 20));

            confirmedPassword.PlaceholderAttributedString = new Foundation.NSAttributedString("Confirm password...");

            newPasswordInput.AddSubview(originalPassword);
            newPasswordInput.AddSubview(confirmedPassword);

            var newPasswordAlert = new NSAlert()
            {
                AlertStyle      = NSAlertStyle.Informational,
                InformativeText = "Enter new password to secure SittingDucks",
                MessageText     = "Adding New Password",
            };
            var enterButton = newPasswordAlert.AddButton("Enter");

            originalPassword.NextKeyView  = confirmedPassword;
            confirmedPassword.NextKeyView = enterButton;

            newPasswordAlert.AccessoryView = newPasswordInput;
            newPasswordAlert.Layout();
            var result = newPasswordAlert.RunModal();

            if (result == 1000 && originalPassword.StringValue == confirmedPassword.StringValue)
            {
                bool shouldClose;
                var  encryptedPassword = EncryptionTool.Encrypt(originalPassword.StringValue);

                (_conn, shouldClose) = SqliteManager.OpenConnection(connection);

                // Execute query
                using (var command = connection.CreateCommand())
                {
                    // Create new command
                    command.CommandText = "UPDATE [System] SET ID = @COL1, Password = @COL2, INIT = @COL3";

                    // Populate with data from the record
                    command.Parameters.AddWithValue("@COL1", new Guid());
                    command.Parameters.AddWithValue("@COL2", encryptedPassword);
                    command.Parameters.AddWithValue("@COL3", true);

                    // Write to database
                    command.ExecuteNonQuery();
                }

                _conn = SqliteManager.CloseConnection(shouldClose, connection);

                newPasswordAlert.Dispose();

                var confirmPasswordAlert = new NSAlert()
                {
                    AlertStyle      = NSAlertStyle.Informational,
                    InformativeText = "Remember this password, store it somewhere safe! You will not be able to recover it if lost.",
                    MessageText     = "Password sucessfully saved",
                };
                confirmPasswordAlert.AddButton("OK");
                var confirmResult = confirmPasswordAlert.RunModal();

                if (confirmResult == 1000)
                {
                    confirmPasswordAlert.Dispose();
                }
            }
            else if (result == 1000 && originalPassword.StringValue != confirmedPassword.StringValue)
            {
                newPasswordAlert.AlertStyle      = NSAlertStyle.Warning;
                newPasswordAlert.InformativeText = "Passwords do not match";
            }
        }