Beispiel #1
0
 public static void TearDownClass()
 {
     //Drop the user that this unit test class created within SetupClass().
     DbAdminMysql.DropUser(new DataConnection(), USER_LOW_NAME);
     //Put the Middle Tier mock server back to the way it was when this test class was initialized.
     OpenDentalServerProxy.MockOpenDentalServerCur = _middleTierMockOld;
 }
        private bool ConnectToDatabase(string userName, string password)
        {
            int portNum = 3306;          //This is the default port for MySQL.

            if (textPort.Text != "" && !Int32.TryParse(textPort.Text, out portNum))
            {
                MessageBox.Show("Please enter a valid number for the Port.");
                return(false);
            }
            string server = textServer.Text;

            if (portNum != 3306)
            {
                server += ":" + portNum;
            }
            try {
                _dataConnection = DbAdminMysql.ConnectAndTest(userName, password, server);
            }
            catch (Exception ex) {
                MessageBox.Show("Unable to connect to the database. Error message: " + ex.Message);
                return(false);
            }
            SetUsersEnabled(true);
            return(true);
        }
        private void butDrop_Click(object sender, EventArgs e)
        {
            if (listBoxUsers.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a user to drop.");
                return;
            }
            string user = (string)listBoxUsers.Items[listBoxUsers.SelectedIndex];

            if (user.ToLower() == _dataConnection.UserCur.ToLower())
            {
                MessageBox.Show("Cannot drop the current MySQL user.");
                return;
            }
            if (MessageBox.Show($"Are you sure you want to drop '{user}'?", "", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }
            try {
                DbAdminMysql.DropUser(_dataConnection, user);
            }
            catch (Exception ex) {
                MessageBox.Show("Error dropping user: " + ex.Message);
                return;
            }
            FillUsers();
        }
 private void butOK_Click(object sender, EventArgs e)
 {
     if (textUser.Text == "" && (_isNew || _userName != ""))         //If the existing username is '', we will allow it.
     {
         MessageBox.Show("Please enter a MySQL user.");
         return;
     }
     if (textPassword.Text == "")
     {
         MessageBox.Show("Please enter a password.");
         return;
     }
     if (WasPasswordChanged && textPassword.Text != textRetypePassword.Text)
     {
         MessageBox.Show("Passwords do not match.");
         return;
     }
     if (textUser.Text != _userName && !WasPasswordChanged)
     {
         MessageBox.Show("Please enter a password if changing the username.");
         return;
     }
     if (_hasNonStandardPermissions && radioLow.Checked && MessageBox.Show("This user has more permissions than the 'Low' permissions set. Continuing"
                                                                           + " will reduce the user's permission to just the SELECT permission. Do you want to continue?", "", MessageBoxButtons.OKCancel) != DialogResult.OK)
     {
         return;
     }
     if (_userName.ToLower() == _dataConnection.UserCur.ToLower() && radioLow.Checked)
     {
         MessageBox.Show("Cannot change the current user to low permission.");
         return;
     }
     try {
         bool doCreateNew = _isNew;
         if (textUser.Text != _userName)               //It doesn't really work to update a username, so we'll drop and recreate.
         {
             DbAdminMysql.DropUser(_dataConnection, _userName);
             doCreateNew = true;
         }
         if (doCreateNew)
         {
             DbAdminMysql.CreateUser(_dataConnection, textUser.Text, _hasLocalhostUserOnly);
         }
         if (WasPasswordChanged || doCreateNew)
         {
             DbAdminMysql.SetPassword(_dataConnection, textUser.Text, textPassword.Text);
         }
         if (_hasFullPermission != radioFull.Checked || _hasNonStandardPermissions || doCreateNew)
         {
             DbAdminMysql.GrantToUser(_dataConnection, textUser.Text, radioFull.Checked);
         }
     }
     catch (Exception ex) {
         MessageBox.Show("Error updating user: " + ex.Message);
         return;
     }
     DialogResult = DialogResult.OK;
 }
Beispiel #5
0
        public void ODInstallerTests_DbAdminMysql_CreateAndDestroyAdminUsers()
        {
            DataConnection conAdmin = new DataConnection();                                              //A brand new admin connection to the unittest### database.

            Assert.AreEqual(null, DbAdminMysql.ModifyUser(conAdmin, "fakeuser1", "od123", "fakeuser1")); //Grants, verifies new user.
            Assert.AreEqual(null, DbAdminMysql.ModifyUser(conAdmin, "fakeuser2", "abcde", "fakeuser1")); //Drops fakeuser1 and verifies.
            try {
                DbAdminMysql.DropUser(conAdmin, "fakeuser2");                                            //Drops fakeuser2 and verifies.
            }
            catch (Exception ex) {
                Assert.Fail(ex.Message);
            }
        }
        private void SetPermissions()
        {
            if (_isNew)
            {
                return;
            }
            List <string> listGrants;

            try {
                listGrants = DbAdminMysql.ShowGrants(_dataConnection, _userName);
            }
            catch (Exception ex) {
                try {
                    //Sometimes a user will only exist for the localhost.
                    listGrants            = DbAdminMysql.ShowGrants(_dataConnection, _userName, "localhost");
                    _hasLocalhostUserOnly = true;
                }
                catch (Exception e) {
                    MessageBox.Show($"Unable to read permissions for user '{_userName}': " + e.Message);
                    radioLow.Checked = true;
                    return;
                }
                ex.DoNothing();
            }
            _hasFullPermission = listGrants.Any(x => x.StartsWith($"GRANT ALL PRIVILEGES ON *.* TO '{_userName}'@'") && x.EndsWith("WITH GRANT OPTION"));
            foreach (string grant in listGrants)
            {
                if (grant.Contains($" ON *.* TO '{_userName}'@'"))
                {
                    //The root user is typically granted all 27 permissions explicitly. This also should be considered full permissions.
                    int countPrivileges = grant.Substring("GRANT ".Length).SubstringBefore($" ON *.* TO '{_userName}'@'").Split(',').Length;
                    if (countPrivileges >= 27 && grant.EndsWith("WITH GRANT OPTION"))
                    {
                        _hasFullPermission = true;
                    }
                }
            }
            if (_hasFullPermission)
            {
                radioFull.Checked = true;
            }
            else if (listGrants.Any(x => x == $"GRANT SELECT ON *.* TO '{_userName}'@'%'"))
            {
                radioLow.Checked = true;
            }
            else                                   //This user has neither the 'Full' nor 'Low' permission set.
            {
                radioLow.Checked           = true; //We will check this radio button but warn the user before we overwrite their permissions.
                _hasNonStandardPermissions = true;
            }
        }
        private void FillUsers()
        {
            List <string> listUsers;

            try {
                listUsers = DbAdminMysql.GetUsers(_dataConnection)
                            .FindAll(x => !x.In("mysql.sys", "mysql.session"));           //These are reserved MySQL accounts.
            }
            catch (Exception ex) {
                MessageBox.Show("Error getting users: " + ex.Message);
                return;
            }
            listBoxUsers.Items.Clear();
            foreach (string user in listUsers)
            {
                listBoxUsers.Items.Add(user);
            }
        }
Beispiel #8
0
 public static void SetupClass(TestContext testContext)
 {
     //Create and set the Open Dental user called UnitTest so that we don't get trolled by failed login attempts from an invalid Security.CurUser.
     TestBase.CreateAndSetUnitTestUser();
     //Drop any users that already exist with this specific name.
     DbAdminMysql.DropUser(new DataConnection(), USER_LOW_NAME);
     //Create a new user with this unit test method name as the database user name.
     DataCore.NonQ($"CREATE USER '{USER_LOW_NAME}'@'localhost' IDENTIFIED BY '{USER_LOW_PASS}'");
     //Only give the SELECT permission to simulate a user of lower status in life.
     DataCore.NonQ($"GRANT SELECT ON *.* TO '{USER_LOW_NAME}'@'localhost'");
     //Reload all privileges to make sure the proletariat permission takes effect.
     DataCore.NonQ("FLUSH PRIVILEGES");
     //Preserve the old Middle Tier Mock service and replace it with a new one that knows about our new user low.
     _middleTierMockOld = OpenDentalServerProxy.MockOpenDentalServerCur;
     //Pass in new connection settings so that this plebeian is set as our "UserLow" when invoking Reports.GetTable().
     OpenDentalServerProxy.MockOpenDentalServerCur = new OpenDentalServerMockIIS("localhost"
                                                                                 , UnitTestDbName
                                                                                 , "root"
                                                                                 , ""
                                                                                 , USER_LOW_NAME
                                                                                 , USER_LOW_PASS
                                                                                 , DatabaseType.MySql);
 }