Example #1
0
        private void ButtonLogin_Click(object sender, EventArgs e)
        {
            if (!(textBoxEmail.Text == "Email") && !(textBoxPassword.Text == "Password") && !(string.IsNullOrWhiteSpace(textBoxEmail.Text)) && !(string.IsNullOrWhiteSpace(textBoxPassword.Text)))
            {
                LoadingForm loadingForm = new LoadingForm();
                loadingForm.Show();

                loadingForm.Step(20);
                var           controller = new UserController();
                Argon2Hashing hashing    = new Argon2Hashing();
                try
                {
                    FacultyUserModel faculty = controller.GetByEmail(textBoxEmail.Text);
                    if (faculty == null)
                    {
                        textBoxPassword.Clear();
                        textBoxPassword.Focus();
                    }
                    else
                    {
                        //Console.WriteLine("Got hash: " + faculty.Password);
                        //Console.WriteLine("Got salt: " + faculty.salt);
                        //Console.WriteLine("Verification: " + hashing.VerifyHash(textBoxPassword.Text, Convert.FromBase64String(faculty.salt), Convert.FromBase64String(faculty.Password)));
                        loadingForm.Step(20);
                        if (hashing.VerifyHash(textBoxPassword.Text, Convert.FromBase64String(faculty.salt), Convert.FromBase64String(faculty.Password)))
                        {
                            loadingForm.Step(60);
                            loadingForm.Close();
                            var dash = new FormDashboard(faculty);
                            dash.FormClosed += new FormClosedEventHandler(dash_FormClosed);
                            dash.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("Incorrect email or password. Please try again");
                        }
                    }
                }catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                loadingForm.Close();
            }
            else
            {
                MessageBox.Show("Please insert your login credentials first");
            }
        }
Example #2
0
        private void ButtonSignUp_Click(object sender, EventArgs e)
        {
            if (!(textBoxFirstNameSignUp.Text == "First Name") && !(textBoxLastNameSignUp.Text == "Last Name"))
            {
                if (textBoxPassSignUp.Text == textBoxConfirmPassSignUp.Text)
                {
                    LoadingForm loading = new LoadingForm();
                    loading.Show();
                    FacultyUserModel faculty = new FacultyUserModel();
                    faculty.AcademicId = textBoxAcademicIdSignUp.Text;
                    faculty.FirstName  = textBoxFirstNameSignUp.Text.Trim();
                    faculty.LastName   = textBoxLastNameSignUp.Text.Trim();
                    faculty.Email      = textBoxEmailSignup.Text.ToLower().Trim();
                    faculty.Password   = textBoxPassSignUp.Text;

                    try
                    {
                        faculty.IsValid();
                        loading.Step(20);
                        //hashing now
                        Argon2Hashing hashing = new Argon2Hashing();
                        faculty.salt     = Convert.ToBase64String(hashing.CreateSalt());
                        faculty.Password = Convert.ToBase64String(hashing.HashPassword(textBoxPassSignUp.Text, Convert.FromBase64String(faculty.salt)));
                        loading.Step(20);
                        //hashing done
                        //Console.WriteLine("Salt: " + faculty.salt);
                        //Console.WriteLine("Hashed password: "******"Verify hash: " + hashing.VerifyHash(textBoxPassSignUp.Text, Convert.FromBase64String(faculty.salt), Convert.FromBase64String(faculty.Password)));
                        loading.Step(20);
                        try
                        {
                            var controller = new UserController();
                            controller.Create(faculty);
                            loading.Step(20);
                            loading.Close();
                            MessageBox.Show("Account created. Please login with your email and password");
                            buttonCancel.PerformClick();
                        }
                        catch (SQLiteException ex)
                        {
                            loading.Close();
                            if (ex.ErrorCode == 19)
                            {
                                MessageBox.Show("An account is already registered with this email or ID");
                            }
                            else
                            {
                                MessageBox.Show(ex.Message + ex.ErrorCode);
                            }
                        }catch (Exception ex)
                        {
                            loading.Close();
                            MessageBox.Show(ex.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        loading.Close();
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("Passwords do not match");
                }
            }
            else
            {
                MessageBox.Show("Please fill up all fields");
            }
        }
        private void ButtonCreate_Click(object sender, EventArgs e)
        {
            SectionModel section = new SectionModel();

            section.SectionName = textBoxSectionName.Text.Trim();
            section.FacultyId   = faculty.Id;

            SectionTimeController   stcontroller = new SectionTimeController();
            List <SectionTimeModel> sectionTimes = stcontroller.GetByFaculty(faculty.Id);

            try
            {
                section.IsValid();

                //Console.WriteLine("Section name: " + section.SectionName);
                //Console.WriteLine("Section facultyID: " + section.FacultyId);

                SectionTimeModel sectionTime1 = new SectionTimeModel();
                if (comboBoxClassType1.SelectedIndex == 0)
                {
                    sectionTime1.ClassType = ClassTypes.Lab;
                }
                else if (comboBoxClassType1.SelectedIndex == 1)
                {
                    sectionTime1.ClassType = ClassTypes.Theory;
                }

                sectionTime1.RoomNo      = textBoxRoomNo1.Text;
                sectionTime1.StartTimeId = comboBoxStartTime1.SelectedIndex + 1;
                sectionTime1.EndTimeId   = comboBoxEndTime1.SelectedIndex + 1;
                sectionTime1.WeekDayId   = comboBoxWeekDay1.SelectedIndex + 1;

                //Console.WriteLine("Section Time 1 info:");
                //Console.WriteLine("Start Id: " + sectionTime1.StartTimeId);
                //Console.WriteLine("End Id: " + sectionTime1.EndTimeId);
                //Console.WriteLine("Day Id: " + sectionTime1.WeekDayID);
                //Console.WriteLine("Room no: " + sectionTime1.RoomNo);
                //Console.WriteLine("Class type: " + sectionTime1.ClassType.ToString());
                //Console.WriteLine("Class length: " + (sectionTime1.EndTimeId - sectionTime1.StartTimeId).ToString());

                try
                {
                    sectionTime1.IsValid();

                    foreach (SectionTimeModel model in sectionTimes)
                    {
                        if (model.WeekDayId == sectionTime1.WeekDayId)
                        {
                            if (sectionTime1.StartTimeId < model.EndTimeId && sectionTime1.StartTimeId >= model.StartTimeId)
                            {
                                //Console.WriteLine("Clash 1 with\nstart id: " + model.StartTimeId + "\nend id: " + model.EndTimeId);
                                throw new Exception("Section 1 time clashes with another section. Please choose a different time");
                            }
                            if (sectionTime1.EndTimeId <= model.EndTimeId && sectionTime1.EndTimeId > model.StartTimeId)
                            {
                                //Console.WriteLine("Clash 2 with\nstart id: " + model.StartTimeId + "\nend id: " + model.EndTimeId);
                                throw new Exception("Section 1 time clashes with another section. Please choose a different time");
                            }
                            if (sectionTime1.StartTimeId < model.StartTimeId && sectionTime1.EndTimeId > model.EndTimeId)
                            {
                                //Console.WriteLine("Clash 3 with\nstart id: " + model.StartTimeId + "\nend id: " + model.EndTimeId);
                                throw new Exception("Section 1 time clashes with another section. Please choose a different time");
                            }
                        }
                    }

                    SectionTimeModel sectionTime2 = new SectionTimeModel();
                    if (!checkBoxIgnore.Checked)
                    {
                        if (comboBoxClassType2.SelectedIndex == 0)
                        {
                            sectionTime2.ClassType = ClassTypes.Lab;
                        }
                        else if (comboBoxClassType2.SelectedIndex == 1)
                        {
                            sectionTime2.ClassType = ClassTypes.Theory;
                        }
                        sectionTime2.RoomNo      = textBoxRoomNo2.Text;
                        sectionTime2.StartTimeId = comboBoxStartTime2.SelectedIndex + 1;
                        sectionTime2.EndTimeId   = comboBoxEndTime2.SelectedIndex + 1;
                        sectionTime2.WeekDayId   = comboBoxWeekDay2.SelectedIndex + 1;

                        //Console.WriteLine("Section Time 2 info:");
                        //Console.WriteLine("Start Id: " + sectionTime2.StartTimeId);
                        //Console.WriteLine("End Id: " + sectionTime2.EndTimeId);
                        //Console.WriteLine("Day Id: " + sectionTime2.WeekDayID);
                        //Console.WriteLine("Room no: " + sectionTime2.RoomNo);
                        //Console.WriteLine("Class type: " + sectionTime2.ClassType.ToString());
                        //Console.WriteLine("Class length: " + (sectionTime2.EndTimeId - sectionTime2.StartTimeId).ToString());
                    }

                    SectionModel createdSection = new SectionModel();
                    try
                    {
                        if (!checkBoxIgnore.Checked)
                        {
                            sectionTime2.IsValid();
                            foreach (SectionTimeModel model in sectionTimes)
                            {
                                if (model.WeekDayId == sectionTime2.WeekDayId)
                                {
                                    if (sectionTime2.StartTimeId < model.EndTimeId && sectionTime2.StartTimeId >= model.StartTimeId)
                                    {
                                        throw new Exception("Section 2 time clashes with another section. Please choose a different time");
                                    }
                                    if (sectionTime2.EndTimeId <= model.EndTimeId && sectionTime2.EndTimeId > model.StartTimeId)
                                    {
                                        throw new Exception("Section 2 time clashes with another section. Please choose a different time");
                                    }
                                    if (sectionTime2.StartTimeId < model.StartTimeId && sectionTime2.EndTimeId > model.EndTimeId)
                                    {
                                        //Console.WriteLine("Clash 3 with\nstart id: " + model.StartTimeId + "\nend id: " + model.EndTimeId);
                                        throw new Exception("Section 2 time clashes with another section. Please choose a different time");
                                    }
                                }
                            }
                        }
                        SectionController controller = new SectionController();

                        try
                        {
                            createdSection = controller.Create(section);

                            //Console.WriteLine("Created section id: " + createdSection.Id);

                            sectionTime1.SectionId = createdSection.Id;
                            if (!checkBoxIgnore.Checked)
                            {
                                sectionTime2.SectionId = createdSection.Id;
                            }

                            SectionTimeController controller2 = new SectionTimeController();

                            try
                            {
                                controller2.Create(sectionTime1);
                                if (!checkBoxIgnore.Checked)
                                {
                                    controller2.Create(sectionTime2);
                                }

                                MessageBox.Show(createdSection.SectionName + " created!");
                                LoadingForm loadingForm = new LoadingForm();

                                if (studentList.Count != 0)
                                {
                                    UserController ucontroller = new UserController();

                                    //loading screen

                                    loadingForm.Show();
                                    //Console.WriteLine("Starting loop");
                                    foreach (StudentUserModel student in studentList)
                                    {
                                        try
                                        {
                                            var createdStudent = ucontroller.Create(student);
                                            try
                                            {
                                                SectionStudentController sscontroller = new SectionStudentController();
                                                sscontroller.Create(createdSection.Id, student.Id);
                                            }
                                            catch (SQLiteException exc)
                                            {
                                                if (exc.ErrorCode == 19)
                                                {
                                                    MessageBox.Show("Error: Student already exists in this section");
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show(ex.Message);
                                            }
                                        }
                                        catch (SQLiteException ex)
                                        {
                                            var existingStudent = ucontroller.GetByAcademicId(student.AcademicId);
                                            try
                                            {
                                                SectionStudentController sscontroller = new SectionStudentController();
                                                sscontroller.Create(createdSection.Id, existingStudent.Id);
                                            }
                                            catch (SQLiteException exc)
                                            {
                                                if (exc.ErrorCode == 19)
                                                {
                                                    MessageBox.Show("Error: Student already exists in this section");
                                                }
                                            }
                                            catch (Exception exc)
                                            {
                                                MessageBox.Show(exc.Message);
                                            }
                                        }
                                        loadingForm.Step(1);
                                    }
                                    //Console.WriteLine("Ending loop");
                                }
                                loadingForm.Close();

                                buttonBack.PerformClick();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void buttonSave_Click(object sender, System.EventArgs e)
        {
            LoadingForm loadingForm = new LoadingForm();

            loadingForm.Show();
            //try
            {
                UserController controller = new UserController();
                Argon2Hashing  hashing    = new Argon2Hashing();
                //Console.WriteLine("getting pass with id: " + faculty.Id);
                //Console.WriteLine("Verification: " + hashing.VerifyHash(textBoxPassword.Text, Convert.FromBase64String(faculty.salt), Convert.FromBase64String(faculty.Password)));
                if (hashing.VerifyHash(textBoxPassword.Text, Convert.FromBase64String(faculty.salt), Convert.FromBase64String(faculty.Password)))
                {
                    loadingForm.Step(20);
                    if (textBoxNewPassword.Text == textBoxConfirmPassword.Text)
                    {
                        faculty.Password = textBoxNewPassword.Text;
                        //try
                        {
                            faculty.IsValid();
                            faculty.salt     = Convert.ToBase64String(hashing.CreateSalt());
                            faculty.Password = Convert.ToBase64String(hashing.HashPassword(faculty.Password, Convert.FromBase64String(faculty.salt)));
                            loadingForm.Step(20);
                            if (hashing.VerifyHash(textBoxNewPassword.Text, Convert.FromBase64String(faculty.salt), Convert.FromBase64String(faculty.Password)))
                            {
                                loadingForm.Step(20);
                                //try
                                {
                                    controller.UpdatePasswordByUser(faculty.Id, faculty.Password, faculty.salt);
                                    loadingForm.Step(60);
                                    loadingForm.Close();
                                    MessageBox.Show("Password successfully updated");
                                    this.Hide();
                                }
                                //catch(Exception ex)
                                //{
                                //    loadingForm.Close();
                                //    MessageBox.Show(ex.Message);
                                //}
                            }
                            else
                            {
                                loadingForm.Close();
                                MessageBox.Show("Hash verification failed");
                            }
                        }
                        //catch (Exception ex)
                        //{
                        //    loadingForm.Close();
                        //    MessageBox.Show(ex.Message+" here");
                        //}
                    }
                    else
                    {
                        loadingForm.Close();
                        MessageBox.Show("Passwords do not match. Please make sure you confirm your password correctly");
                    }
                }
                else
                {
                    loadingForm.Close();
                    MessageBox.Show("Wrong current password. Please try again");
                }
            }

            //catch (Exception ex)
            //{

            //    loadingForm.Close();
            //    MessageBox.Show(ex.Message);
            //}
        }