public bool updateUserXML(string usernameOLD, string usernameNEW)
        {
            bool returnType = false;

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml"))//exists
            {
                B_Users tempUsers = getUserFromXML(usernameNEW, false);
                if (tempUsers.username == "")
                {
                    XDocument doc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml");
                    doc.Descendants("UserInfo").Where(theUsername => theUsername.ToString().Remove(0, 20).Split(@"""".ToCharArray())[0] == usernameOLD).ToList()[0]
                    .Attribute("username").Value = usernameNEW;

                    doc.Save("Users.xml");
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml");
                    File.Move(AppDomain.CurrentDomain.BaseDirectory + @"\Users.xml", AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml");
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"\Users.xml");
                    returnType = true;
                }
                else//New Username Exist
                {
                    System.Windows.MessageBox.Show("Username already exists, the user wasn't created. Please choose another username", "User Already exists", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                }
            }
            else
            {
                System.Windows.MessageBox.Show("There are no users set up in the system. Please create a user first", "No Users in the system", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
            }

            return(returnType);
        }
        public List <B_Users> Get_AllUsers()
        {
            List <B_Users> returnUsers = new List <B_Users>();
            SqlConnection  scon        = new SqlConnection(GlobalVariables.connectionString);

            try
            {
                string usernameExists = String.Empty;
                scon.Open();
                SqlCommand    scom  = new SqlCommand(@"Select Username from Users", scon);
                SqlDataReader sread = scom.ExecuteReader();
                while (sread.Read())
                {
                    B_Users returnUser = new B_Users();
                    returnUser.username = sread["Username"].ToString();
                    returnUsers.Add(returnUser);
                }
                sread.Close();
            }
            catch (Exception ex) { }
            finally
            {
                scon.Close();
            }
            return(returnUsers);
        }
        public B_Users Get_User(string Username, string Password)
        {
            B_Users       returnUser = new B_Users();
            SqlConnection scon       = new SqlConnection(GlobalVariables.connectionString);

            try
            {
                string usernameExists = String.Empty;
                scon.Open();
                SqlCommand scom = new SqlCommand(@"Select Username from Users where Username = @Val_Username and Password = @Val_Password", scon);
                scom.Parameters.AddWithValue("@Val_Username", Username);
                scom.Parameters.AddWithValue("@Val_Password", Password);
                SqlDataReader sread = scom.ExecuteReader();
                while (sread.Read())
                {
                    returnUser.username = sread["Username"].ToString();
                }
                sread.Close();
            }
            catch (Exception ex) { }
            finally
            {
                scon.Close();
            }
            return(returnUser);
        }
Beispiel #4
0
        private void Button_Click_CreateUser(object sender, RoutedEventArgs e)
        {
            B_Users testUser = xmlClass.getUserFromXML(textBox_username.Text, false);

            if (string.IsNullOrEmpty(testUser.username))
            {
                //we can create the user in the XML (for offline user)
                xmlClass.insertUserXML(textBox_username.Text, textBox_password.Text, false);
                testUser = xmlClass.getUserFromXML(textBox_username.Text, false);
            }

            if (rBtn_Online.IsChecked == true)
            {
                //we Insert the user into the database if the setup is set to SQL Database
                SqlConnection scon = new SqlConnection(connstr);
                try
                {
                    string usernameExists = String.Empty;
                    scon.Open();
                    SqlCommand scom = new SqlCommand(@"Select Username from Users where Username = @Val_Username", scon);
                    scom.Parameters.AddWithValue("@Val_Username", textBox_username.Text);
                    SqlDataReader sread = scom.ExecuteReader();
                    while (sread.Read())
                    {
                        usernameExists = sread["Username"].ToString();
                    }
                    sread.Close();

                    if (string.IsNullOrEmpty(usernameExists))
                    {
                        SqlCommand scom1 = new SqlCommand(@"Insert Into Users (Username,Password) values(@Val_Username,@Val_Password)", scon);
                        scom1.Parameters.AddWithValue("@Val_Username", textBox_username.Text);
                        scom1.Parameters.AddWithValue("@Val_Password", textBox_password.Text);
                        scom1.CommandType = System.Data.CommandType.Text;
                        scom1.ExecuteNonQuery();
                        MessageBox.Show("User Successfully Inserted");
                        btn_Next.IsEnabled = true;
                    }
                    else
                    {
                        MessageBox.Show("Username already exists, Please choose a different username");
                    }
                }
                catch (Exception ex) { }
                finally
                {
                    scon.Close();
                }
            }
            else
            {
                btn_Next.IsEnabled = true;
            }
        }
        public bool Login(B_Users userDetails)
        {
            bool returnType = false;

            if (XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml").Descendants("UserInfo").Where(theUsername => theUsername.ToString()
                                                                                                                        .Remove(0, 20).Split(@"""".ToCharArray())[0] == userDetails.username).ToList()[0].Attribute("password").Value == userDetails.password)
            {
                returnType = true;
            }
            return(returnType);
        }
Beispiel #6
0
 private void button_Login_Click(object sender, RoutedEventArgs e)
 {
     if (!string.IsNullOrEmpty(GlobalVariables.connectionString))
     {
         B_Users usr = bSQL.Get_User(textBox_username.Text, textBox_password.Text);
         if (!string.IsNullOrEmpty(usr.username))
         {
             GlobalVariables.currentUser = usr.username;
             int unacceptedCount = bSQL.Get_UnacceptedAppointmentsCount(GlobalVariables.currentUser);
             if (unacceptedCount > 0)
             {
                 this.Hide();
                 Appointment_Acceptance aacc = new Appointment_Acceptance();
                 aacc.ShowDialog();
                 MainWindow calendar = new MainWindow();
                 calendar.Show();
             }
             else
             {
                 this.Hide();
                 MainWindow calendar = new MainWindow();
                 calendar.Show();
             }
         }
         else
         {
             MessageBox.Show("Incorrect Username and Password combination. Please try again.", "Incorrect Username/Password combination", MessageBoxButton.OK, MessageBoxImage.Error);
         }
     }
     else
     {
         B_Users testUser = xmlClass.getUserFromXML(textBox_username.Text, true);
         if (!string.IsNullOrEmpty(testUser.username))
         {
             testUser.password = textBox_password.Text;
             if (xmlClass.Login(testUser))
             {
                 MainWindow calendar = new MainWindow();
                 GlobalVariables.currentUser = testUser.username;
                 calendar.Show();
                 this.Hide();
             }
             else
             {
                 MessageBox.Show("Username or password incorrect", "Error logging in", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
             }
         }
         else
         {
             MessageBox.Show("Username or password incorrect", "Error logging in", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
         }
     }
 }
        public bool insertUserXML(string username, string password, bool errorchecking)
        {
            bool returnType = false;

            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\XML"))//creates directory
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\XML");
            }

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml"))//exists
            {
                B_Users tempUsers = getUserFromXML(username, false);
                if (tempUsers.username == "")
                {
                    XDocument doc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml");
                    doc.Descendants("Users").ToList()[0]
                    .Add(
                        new XElement
                            ("UserInfo",
                            new XAttribute("username", username.ToString()),
                            new XAttribute("password", password.ToString())
                            )
                        );
                    doc.Save("Users.xml");
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml");
                    File.Move(AppDomain.CurrentDomain.BaseDirectory + @"\Users.xml", AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml");
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"\Users.xml");
                    returnType = true;
                }
                else//exists
                {
                    if (errorchecking)
                    {
                        System.Windows.MessageBox.Show("Username already exists, the user wasn't created. Please choose another username", "User Already exists", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                    }
                }
            }
            else//create the file and the user
            {
                createUsersXML(new List <B_Users>()
                {
                    new B_Users()
                    {
                        username = username, password = password
                    }
                });
                returnType = true;
            }

            return(returnType);
        }
        public B_Users getUserFromXML(string username, bool errorchecking)
        {
            B_Users returnUser = new B_Users()
            {
                username = "", password = ""
            };

            try
            {
                if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\XML"))//creates directory
                {
                    Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\XML");
                }

                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml"))//checks if file exists
                {
                    if (0 == XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml").Descendants("Users").Descendants("UserInfo").Count())
                    {
                        if (errorchecking)
                        {
                            System.Windows.MessageBox.Show("There are no users set up in the system. Please create a user first", "No Users in the system", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                        }
                    }
                    else
                    {
                        if (XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml").Descendants("UserInfo").Where(tempUser => tempUser.ToString().Remove(0, 20).Split(@"""".ToCharArray())[0] == username).ToList().Count() > 0)//more than 1
                        {
                            returnUser.username = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"\XML\Users.xml").Descendants("UserInfo")
                                                  .Where(
                                tempUser => tempUser.ToString().Remove(0, 20).Split(@"""".ToCharArray())[0]
                                == username
                                )
                                                  .ToList()[0].ToString().Remove(0, 20).Split(@"""".ToCharArray())[0];
                        }
                    }
                }
                else
                {
                    if (errorchecking)
                    {
                        System.Windows.MessageBox.Show("There are no users set up in the system. Please create a user first", "No Users in the system", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                    }
                }
            }
            catch (Exception ex)
            {
                //silent
            }
            return(returnUser);
        }