private void addButton_Click(object sender, EventArgs e) { using (var db = new MyDbContext()) { string login = loginTextBox.Text; string firstName = firstNameTextBox.Text; string secondName = secondNameTextBox.Text; string passWord = passwordTextBox.Text; string email = emailTextBox.Text; string phone = phoneTextBox.Text; string status = statusComboBox.Text; string imgPath = avatarPictureBox.ImageLocation; if (avatarPictureBox.ImageLocation == null) { imgPath = "D:\\Univer\\4sem\\practice\\computerShop\\ComputerShop\\img\\noAvatar.png"; } // Error processing. Incorrectly entered data if (login == "") { // TODO: // Add empty login return; } if (firstName == "") { // TODO: // Add empty firstName return; } if (secondName == "") { // TODO: // Add empty secondName return; } if (passWord == "") { // TODO: // Add empty passWord return; } if (email == "") { // TODO: // Add empty email return; } if (phone == "") { // TODO: // Add empty phone return; } // Error processing. Is there such a user? User registration = db.Users.SingleOrDefault(u => u.Login == login); if (registration != null) { // TODO: // Add "wrong login" error message return; } // Registration processing. Registration allowed if (registration == null) { var newUser = new User() { Login = login, FirstName = firstName, SecondName = secondName, PassWord = PasswordHasher.Hash(passWord), Email = email, Phone = phone, Status = status, ImagePath = imgPath }; db.Users.Add(newUser); db.SaveChanges(); } } this.Close(); return; }
private void LoginButton_Click(object sender, EventArgs e) { progressBar1.Visible = true; progressBar1.Value = 0; authorisationErrorLabel.Visible = false; progressBar1.Visible = true; for (int i = 0; i < 100; i++) { progressBar1.Value++; Thread.Sleep(10); } using (MyDbContext db = new MyDbContext()) { string login = LoginTextBox.Text; string password = PasswordTextBox.Text; // Authorization procession. Is user registered? User authorising = db.Users.SingleOrDefault(u => u.Login == login); // Error processing. Wrong password or login if (authorising == null || !PasswordHasher.Verify(password, authorising.PassWord)) { authorisationErrorLabel.Visible = true; progressBar1.Visible = false; return; } // Authorization processing if (authorising.Status == "admin") { CurrentUser.Set(authorising); AdminStartForm form = new AdminStartForm(); form.Show(); progressBar1.Visible = false; Hide(); } // Authorization processing if (authorising.Status == "user") { CurrentUser.Set(authorising); UserStartForm form = new UserStartForm(); form.Show(); progressBar1.Visible = false; Hide(); } } return; }