private bool CheckLogin(string userName, string rawPassword) { // TODO: HASH PASSWORD DbContext = new voteAppEntities(); if (!DbContext.Users.Any()) { return(false); } // Authentication _loginUser = DbContext.Users.FirstOrDefault(login => login.Name.Equals(userName, StringComparison.Ordinal)); if (_loginUser == null) { return(false); } string hashPassword = PwdUtils.GetHashedPassword(rawPassword, _loginUser.Salt); if (_loginUser.Password.Equals(hashPassword, StringComparison.Ordinal) == false) { return(false); } if (!_loginUser.Enabled) { MessageBox.Show($"User: {_loginUser.Name} disabled"); textBoxPassword.Text = string.Empty; return(false); } return(true); }
private void ButtonUpdateUser_Click(object sender, EventArgs e) { if (listBoxUsers.SelectedItem == null) { return; } string fullName = textBoxEditFullName.Text.Trim(); string rawPassword = textBoxEditPasswowrd.Text.Trim(); string userName = textBoxEditLogin.Text.Trim(); User selUser = ((DisplayItem <User>)listBoxUsers.SelectedItem).Item; if (!DataValidator.IsValidFullName(fullName)) { Name = null; } if (!DataValidator.IsValidUserName(userName)) { userName = null; } if (!DataValidator.IsValidPassword(rawPassword)) { rawPassword = null; } (string hashPassword, string salt) = PwdUtils.GetSaltyPassword(rawPassword); // TODO: refact? user selUser directly? User currentUser = DbUtils.AppEntities.Users.FirstOrDefault(u => u.Name == selUser.Name); if (currentUser == null) { Debug.WriteLine("Sel user not found!"); return; } currentUser.Name = userName ?? currentUser.Name; currentUser.FullName = fullName; if (string.IsNullOrEmpty(hashPassword) == false) { currentUser.Password = hashPassword; currentUser.Salt = salt; } //var user = DbUtils.AppEntities.Users.FirstOrDefault(usr => usr.Name.Equals(_userContext.User.Name)); //USEFUL: _userContext.VoteDbContext.Entry(_userContext.User).State = System.Data.Entity.EntityState. //var set = DbUtils.AppEntities.Set(typeof(User)); //DbUtils.AppEntities.Users. //_userContext.VoteDbContext.SaveChanges(); DbUtils.AppEntities.SaveChanges(); //_voteEntities.Entry(null). UpdateUserView(); MessageBox.Show("user info updated!", "User updated", MessageBoxButtons.OK, MessageBoxIcon.Information); }
private void ButtonSubmit_Click(object sender, EventArgs e) { // validation string userName = textBoxUserName.Text; string fullName = textBoxFullName.Text; string password = textBoxPassword.Text; string passwordConfirm = textBoxConfirmPassword.Text; if (!DataValidator.IsValidUserName(userName)) { return; } if (!DataValidator.IsValidFullName(fullName)) { return; } if (!DataValidator.IsValidPassword(password, passwordConfirm)) { return; } bool isProvinceAdmin = _configs.TypeUser == TypeUser.Admin; if (isProvinceAdmin && comboBoxProvince.SelectedItem == null) { return; } (string hashPassword, string salt) = PwdUtils.GetSaltyPassword(password); // create new super admin User newUser = new User { DateCreation = DateTime.Now, FullName = fullName, Password = hashPassword, Salt = salt, Enabled = true, Name = userName, Type = isProvinceAdmin ? TypeUser.Admin : TypeUser.SuperAdmin, OwnerId = 0, ProvinceId = isProvinceAdmin ? ((DisplayItem <Province>)comboBoxProvince.SelectedItem).Item.Id : 0 }; using (voteAppEntities dbContext = new voteAppEntities()) { // check if admin for the selected province doesn't already exits if (_configs.TypeUser == TypeUser.Admin) { if (dbContext.Users.Any(user => user.ProvinceId == newUser.ProvinceId)) { MessageBox.Show("Province already contain a admin, please choose another province"); // deselect selected province return; } } dbContext.Users.Add(newUser); dbContext.SaveChanges(); User = newUser; } MessageBox.Show("User added succesfully", "User added with success", MessageBoxButtons.OK, MessageBoxIcon.Information); if (_configs.TypeUser == TypeUser.SuperAdmin) { ButtonButtonOK_Click(null, null); } else { ClearControls(); } }
private void buttonAddUser_Click(object sender, EventArgs e) { // Add standard user to same province as logged in user. string userName = textBoxUserName.Text.Trim(); string fullName = textBoxFullName.Text.Trim(); string password = textBoxPassword.Text.Trim(); string passwordConfirm = textBoxConfirmPassword.Text.Trim(); if (_userContext.VoteDbContext.Users.Any(u => u.Name.Equals(userName, StringComparison.Ordinal))) { // user exits MessageBox.Show("Users already exits!"); return; } if (!DataValidator.IsValidUserName(userName)) { MessageBox.Show("Invalid user name!"); // notify error return; } if (string.IsNullOrEmpty(fullName)) { MessageBox.Show("Invalid Full-Name"); return; } if (!DataValidator.IsValidFullName(fullName)) { MessageBox.Show("Invalid user name!"); textBoxFullName.Focus(); return; } if (!password.Equals(passwordConfirm, StringComparison.Ordinal)) { MessageBox.Show("Invalid password!"); return; } if (comboBoxTypeUser.SelectedItem == null) { MessageBox.Show("Select type user!"); comboBoxTypeUser.Focus(); return; } Province province = ((DisplayItem <Province>)comboBoxProvince.SelectedItem).Item; TypeUser typeUser = ((DisplayItem <TypeUser>)comboBoxTypeUser.SelectedItem).Item; (string hashPassword, string salt) = PwdUtils.GetSaltyPassword(password); User user = new User { Enabled = true, Name = userName, FullName = fullName, Password = hashPassword, DateCreation = DateTime.Now, Type = typeUser, ProvinceId = comboBoxProvince.Enabled ? province.Id : 0,// 0 is none for non-admin users OwnerId = _userContext.ID, Salt = salt }; _userContext.VoteDbContext.Users.Add(user); _userContext.VoteDbContext.SaveChanges(); MessageBox.Show("User added"); UpdateUserView(); }