//sign in button
        private void ButtonSignIn_Click(object sender, RoutedEventArgs e)
        {
            //loads the array of usernames
            //then checks if given username is in array
            //if it is in array then checks the sha256 of password within index in array of passwords
            //if returned index is -1 then username doesn't exist
            isRemembered = Convert.ToBoolean(CheckboxRememberMeSignin.IsChecked);
            tempUsername = TextboxUsernameSignin.Text;
            temp_id      = Array.IndexOf(student_usernames, tempUsername);
            int temp_id_teacher = Array.IndexOf(teacher_usernames, tempUsername);

            //check if given username is student
            if (temp_id > -1 && temp_id_teacher == -1)
            {
                isTeacher = false;
                isStudent = true;
            }
            //check if given username is teacher
            else if (temp_id == -1 && temp_id_teacher > -1)
            {
                isTeacher = true;
                isStudent = false;
            }
            //if given username is none them
            else if (temp_id == -1 && temp_id_teacher == -1)
            {
                hasError = true;
                TextStatusSignIn.Text = signin_error;
            }

            //then checks the given password with returned index of username
            //first if previous logic returned isTeacher = true
            //and hasError = false
            if (isTeacher == true && isStudent == false && hasError == false)
            {
                temp_pass = Methods.EncryptPass(TextboxPasswordSignin.Text);
                if (temp_pass == teacher_passwords[temp_id_teacher])
                {
                    hasError     = false;
                    account_type = "teacher";
                }
                else
                {
                    hasError = true;
                    TextStatusSignIn.Text = signin_error;
                }
            }

            //the same logic but for student now
            if (isStudent == true && isTeacher == false && hasError == false)
            {
                temp_pass = Methods.EncryptPass(TextboxPasswordSignin.Text);
                if (temp_pass == student_passwords[temp_id])
                {
                    hasError     = false;
                    account_type = "student";
                }
                else
                {
                    hasError = true;
                    TextStatusSignIn.Text = signin_error;
                }
            }

            //check if username and password should be saved
            if (isRemembered == true && hasError == false)
            {
                m.FileWrite(last_session_path, TextboxUsernameSignin.Text);
                m.FileWrite(remembered_path, Convert.ToString(isRemembered));

                //saving account type
                if (isTeacher == true && isStudent == false)
                {
                    m.FileWrite(login_is_teacher, "teacher");

                    //redirect to teachers page
                    TeacherSubjects teacherSubjects = new TeacherSubjects();
                    teacherSubjects.Show();
                    Close();
                }
                else if (isTeacher == false && isStudent == true)
                {
                    m.FileWrite(login_is_teacher, "student");

                    //redirect to students page
                    SubjectsStudent subjectsStudent = new SubjectsStudent();
                    subjectsStudent.Show();
                    Close();
                }
            }

            //logic for if credentials is not going to be saved
            if (isRemembered == false && hasError == false)
            {
                m.FileWrite(last_session_path, TextboxUsernameSignin.Text);

                if (isTeacher == true && isStudent == false)
                {
                    m.FileWrite(login_is_teacher, "teacher");
                }
                else if (isTeacher == false && isStudent == true)
                {
                    m.FileWrite(login_is_teacher, "student");
                }
            }

            if (account_type == "student" & hasError == false)
            {
                SubjectsStudent subjectsStudent = new SubjectsStudent();
                subjectsStudent.Show();
                Close();
            }
        }
Exemple #2
0
        //completing signup
        //credential checking
        private void ButtonCompleteTeacherSignUp_Click(object sender, RoutedEventArgs e)
        {
            tempfirst     = TextboxFirstNameTeacherSignUp.Text;
            templast      = TextboxLastNameTeacherSignUp.Text;
            tempemail     = TextboxEmailTeacherSignUp.Text;
            temppassword  = TextboxPassword1TeacherSignUp.Text;
            temppassword1 = TextboxPassword2TeacherSignUp.Text;
            tempusername  = TextboxUsernameTeacherSignUp.Text;

            //returns the index of given username or email
            //returns -1 if username or email is taken
            int username_s = Array.IndexOf(student_usernames, tempusername);
            int username_t = Array.IndexOf(teacher_usernames, tempusername);
            int email_s    = Array.IndexOf(student_emails, tempemail);
            int email_t    = Array.IndexOf(teacher_emails, tempemail);

            //checking if username is taken
            if (username_s == -1 & username_t == -1)
            {
                isfreeusername = true;
            }
            else if (username_s > -1 | username_t > -1)
            {
                isfreeusername = false;
            }

            //checking if email is taken
            if (email_s == -1 & email_t == -1)
            {
                isfreemail = true;
            }
            else if (email_s > -1 | email_t > -1)
            {
                isfreemail = false;
            }

            //checking if passwords match to each other
            if (temppassword == temppassword1)
            {
                pass_match = true;
            }
            else
            {
                pass_match = false;
            }

            //checking if passwords are long enough
            if (pass_match & temppassword.Length >= longpass)
            {
                pass_long = true;
            }

            //checking if valid email address is entered
            if (m.IsValidEmail(tempemail))
            {
                valid_email = true;
            }

            //checking if names are entered
            if (tempfirst.Length > 0 & templast.Length > 0)
            {
                entered_names = true;
            }

            //checking which subject is selected
            if (Convert.ToBoolean(RadioButtonMath.IsChecked))
            {
                tempsubject      = math;
                selected_subject = true;
            }
            else if (Convert.ToBoolean(RadioButtonBiology.IsChecked))
            {
                tempsubject      = biology;
                selected_subject = true;
            }
            else if (Convert.ToBoolean(RadioButtonChemistry.IsChecked))
            {
                tempsubject      = chemistry;
                selected_subject = true;
            }
            else if (Convert.ToBoolean(RadioButtonEnglish.IsChecked))
            {
                tempsubject      = english;
                selected_subject = true;
            }
            else if (Convert.ToBoolean(RadioButtonHistory.IsChecked))
            {
                tempsubject      = history;
                selected_subject = true;
            }
            else if (Convert.ToBoolean(RadioButtonInformatics.IsChecked))
            {
                tempsubject      = informatics;
                selected_subject = true;
            }
            else if (Convert.ToBoolean(RadioButtonLiterature.IsChecked))
            {
                tempsubject      = literature;
                selected_subject = true;
            }
            else if (Convert.ToBoolean(RadioButtonPhysics.IsChecked))
            {
                tempsubject      = physics;
                selected_subject = true;
            }

            //assigning error messages
            if (!isfreeusername)
            {
                error_message = "Username is already taken";
            }
            if (!isfreemail)
            {
                error_message = "Email address already is taken";
            }
            if (!pass_match)
            {
                error_message = "Passwords do not match";
            }
            if (!pass_long)
            {
                error_message = "Password should at least contain 8 characters";
            }
            if (!selected_subject)
            {
                error_message = "You did not select any subject";
            }
            if (!valid_email)
            {
                error_message = "Enter valid email address";
            }
            if (!entered_names)
            {
                error_message = "Please enter your name";
            }
            if (!noError)
            {
                StatusMessage.Text = error_message;
            }


            //registring if no error occured
            if (isfreeusername & isfreemail & pass_long & pass_match & selected_subject & valid_email & entered_names)
            {
                noError = true;
                string t_c = teacher_path + teacher_id;
                Directory.CreateDirectory(t_c);
                m.FileWrite(t_c + @"\username", tempusername);
                m.FileWrite(t_c + @"\password", Methods.EncryptPass(temppassword));
                m.FileWrite(t_c + @"\email", tempemail);
                m.FileWrite(t_c + @"\subject", tempsubject);
                m.FileWrite(t_c + @"\firstname", tempfirst);
                m.FileWrite(t_c + @"\lastname", templast);
                teacher_id++;
                m.FileWrite(teacher_id_path, Convert.ToString(teacher_id));
            }

            //redirect to teachers window if successful registration
            if (noError)
            {
                TeacherSubjects teacherSubjects = new TeacherSubjects();
                teacherSubjects.Show();
                this.Close();
            }
        }