예제 #1
0
        private void btn_login_Click(object sender, EventArgs e)
        {
            User lastUser = userList.UserList.Last();

            lblErrorInput.Hide();

            String decryptedPass;
            String encryptedPass = Encrypt(txtBoxPassword.Text, GenerateEncryptionKey());

            if (!txtBoxUsername.Text.Equals("") || !txtBoxPassword.Text.Equals(""))
            {
                foreach (User user in userList.UserList)
                {
                    decryptedPass = Decrypt(user.Password, GenerateEncryptionKey());

                    //add pass and name of user to string
                    nameList.Add(user.Name);
                    passList.Add(user.Password);

                    if (txtBoxUsername.Text.Equals(user.Name) && txtBoxPassword.Text.Equals(decryptedPass))
                    {
                        switch (user.Position)
                        {
                        case "Cashier":
                        {
                            CashierMenuForm menuPage = new CashierMenuForm(user);
                            this.Hide();
                            menuPage.ShowDialog();
                            this.Close();         //close previous form
                            break;
                        }

                        case "Kitchen":
                        {
                            KitchenForm kitchenPage = new KitchenForm();
                            this.Hide();
                            kitchenPage.ShowDialog();
                            this.Close();         //close previous form
                            break;
                        }

                        case "Admin":
                        {
                            ProductViewForm productPage = new ProductViewForm();
                            this.Hide();
                            productPage.ShowDialog();
                            this.Close();         //close previous form
                            break;
                        }

                        default:
                        {
                            lblErrorInput.Text = "User doesnt have a position. Please contact the admin.";
                            lblErrorInput.Show();
                            break;
                        }
                        } //end switch
                    }     //end if


                    if (user.Equals(lastUser))
                    {
                        //check in list if name or pass exists
                        if (!nameList.Contains(txtBoxUsername.Text))
                        {
                            loginCounter      -= 1;
                            lblErrorInput.Text = "Please enter valid username . " + loginCounter + " tries left.";
                            lblErrorInput.Show();
                            break;
                        }//end if else
                        if (!passList.Contains(encryptedPass))
                        {
                            loginCounter      -= 1;
                            lblErrorInput.Text = "Please enter valid password. " + loginCounter + " tries left.";
                            lblErrorInput.Show();
                            break;
                        }//end if else

                        //if both name and pass exists among users, check if both entered name and password are from different users or not
                        if (nameList.IndexOf(txtBoxUsername.Text) != passList.IndexOf(txtBoxPassword.Text))
                        {
                            loginCounter      -= 1;
                            lblErrorInput.Text = "Wrong password for account. " + loginCounter + " tries left.";
                            lblErrorInput.Show();
                            break;
                        }
                    }
                } //end foreach
            }     //end if
            else
            {
                loginCounter      -= 1;
                lblErrorInput.Text = "Please enter username or password. " + loginCounter + " tries left.";
                lblErrorInput.Show();
            }//end else

            //if 5 tries of logins failed
            if (loginCounter == 0)
            {
                MessageBox.Show("Invalid user. Closing application now.");
                this.Close();
            }
        }