private bool removeAccount(int accountID) { using (LeafSecurityEntities db = new LeafSecurityEntities()) { AccountInformation account = (from acc in db.AccountInformations where acc.AccountID.Equals(accountID) select acc).FirstOrDefault(); // If there are no accounts if (account == null) { return(false); } /** * We don't want to remove access to the main account. */ // Disabling Removing Main Admin Account if (account.AccountID == 1) { return(false); } UserInformation userInfo = (from user in db.UserInformations where user.AccountID.Equals(account.AccountID) select user).Single(); IEnumerable <FingerprintTemplate> fTemplates = (from fTemp in db.FingerprintTemplates where fTemp.AccountID.Equals(account.AccountID) select fTemp); fTemplates.ToList().ForEach(t => { db.MinutiaeTemplatePaths.Remove(t.MinutiaeTemplatePath); }); db.FingerprintTemplates.RemoveRange(fTemplates); db.UserInformations.Remove(userInfo); db.AccountInformations.Remove(account); db.SaveChanges(); } return(true); }
private void doneBtn_Click(object sender, EventArgs e) { errorEncountered = false; using (LeafSecurityEntities db = new LeafSecurityEntities()) { // Checking Personal Information Data. (First Name) if (firstNameTxt.Text.Length.Equals(0)) { errorEncountered = true; firstNameLbl.ForeColor = Color.Red; errorLbl.Show(); } // Checking Personal Information Data. (Last Name) if (lastNameTxt.Text.Length.Equals(0)) { errorEncountered = true; lastNameLbl.ForeColor = Color.Red; errorLbl.Show(); } // Checking Personal Information Data. (Email) if (emailTxt.Text.Length.Equals(0)) { inputTab.SelectTab(0); emailLbl.ForeColor = Color.Blue; DialogResult result = MessageBox.Show("Are you sure you want to leave some fields blank", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.No) { errorEncountered = true; } } // Checking Personal Information Data. (Phone Number) if (phoneNumberTxt.Text.Length.Equals(0)) { inputTab.SelectTab(0); phoneNumberLbl.ForeColor = Color.Blue; DialogResult result = MessageBox.Show("Are you sure you want to leave some fields blank", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.No) { errorEncountered = true; } } // Checking Personal Information Data. if (addressTxt.Text.Length.Equals(0)) { inputTab.SelectTab(0); addressLbl.ForeColor = Color.Blue; DialogResult result = MessageBox.Show("Are you sure you want to leave some fields blank", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.No) { errorEncountered = true; } } // Checking Account Data IEnumerable <AccountInformation> anotherAcc = from acc in db.AccountInformations where acc.AccountNumber == usernameTxt.Text select acc; if (!anotherAcc.Count().Equals(0)) { errorEncountered = true; usernameLbl.ForeColor = Color.Red; usernameErrorTooltip.SetToolTip(this.usernameLbl, "User name exists"); usernameErrorTooltip.SetToolTip(this.usernameTxt, "User name exists"); errorLbl.Show(); } if (usernameTxt.Text.Length.Equals(0)) { errorEncountered = true; usernameLbl.ForeColor = Color.Red; usernameErrorTooltip.SetToolTip(this.usernameLbl, "User name not entered."); usernameErrorTooltip.SetToolTip(this.usernameTxt, "User name not entered."); errorLbl.Show(); } if (passwordTxt.Text.Length.Equals(0)) { errorEncountered = true; passwordLbl.ForeColor = Color.Red; confirmPasswordLbl.ForeColor = Color.Red; passwordErrorTooltip.SetToolTip(this.passwordLbl, "Password not entered."); passwordErrorTooltip.SetToolTip(this.confirmPasswordLbl, "Password not entered."); passwordErrorTooltip.SetToolTip(this.passwordTxt, "Password not entered."); passwordErrorTooltip.SetToolTip(this.confirmPasswordTxt, "Password not entered."); errorLbl.Show(); } if (!passwordTxt.Text.Equals(confirmPasswordTxt.Text) && accountTypeCombo.SelectedIndex.Equals(0)) { errorEncountered = true; passwordLbl.ForeColor = Color.Red; confirmPasswordLbl.ForeColor = Color.Red; passwordErrorTooltip.SetToolTip(this.passwordLbl, "Password does not match."); passwordErrorTooltip.SetToolTip(this.confirmPasswordLbl, "Password does not match."); passwordErrorTooltip.SetToolTip(this.passwordTxt, "Password does not match."); passwordErrorTooltip.SetToolTip(this.confirmPasswordTxt, "Password does not match."); errorLbl.Show(); } if (accountTypeCombo.SelectedIndex == -1) { errorEncountered = true; accountTypeLbl.ForeColor = Color.Red; accountTypeErrorTooltip.SetToolTip(this.accountTypeLbl, "Account type not selected."); accountTypeErrorTooltip.SetToolTip(this.accountTypeCombo, "Account type not selected."); } // Checking Template Information // If account type does require fingerprint template if (!accountTypeCombo.SelectedIndex.Equals(0)) { // If template file not provided // and If template is nessesary, meaning fingerprint sensor is not connected if (imageFilePath.Text.Length.Equals(0) && !fingerprint_device_connected) { errorEncountered = true; templatePathLbl.ForeColor = Color.Red; templatePathErrorTooltip.SetToolTip(this.templatePathLbl, "Template not provided."); errorLbl.Show(); } if (!File.Exists(imageFilePath.Text) && !fingerprint_device_connected) { errorEncountered = true; templatePathLbl.ForeColor = Color.Red; templatePathErrorTooltip.SetToolTip(this.templatePathLbl, "Template file doest not exist."); errorLbl.Show(); } // if Fingerprint Sensor is connected but // fingerprint not read yet. if (!this.fingerprintTaken) { errorEncountered = true; fingerprint_preview_lbl.ForeColor = Color.Red; templatePathErrorTooltip.SetToolTip(this.fingerprint_preview_lbl, "Fingerprint Not Provided."); errorLbl.Show(); } } // Entering Verified Data if (!errorEncountered) { if (accountTypeCombo.SelectedIndex.Equals(0)) { // Account Information AccountInformation accountInfo = new AccountInformation(); accountInfo.AccountNumber = usernameTxt.Text; accountInfo.AccountUsername = passwordTxt.Text; accountInfo.TypeID = 1; // Since Account Type 'Admin' is identified as 1 accountInfo.AccountCreationDate = DateTime.Now; db.AccountInformations.Add(accountInfo); db.SaveChanges(); Console.WriteLine("Account ID: {0}", accountInfo.AccountID); // User Information UserInformation userInformation = new UserInformation(); //userInformation.AccountID = (from accTemp in db.AccountInformations // where accTemp.AccountNumber == accountInfo.AccountNumber // select accTemp).First().AccountID; userInformation.AccountID = accountInfo.AccountID; userInformation.FirstName = firstNameTxt.Text; userInformation.LastName = lastNameTxt.Text; userInformation.PhoneNumber = phoneNumberTxt.Text; userInformation.Email = emailTxt.Text; userInformation.Address = addressTxt.Text; db.UserInformations.Add(userInformation); db.SaveChanges(); MessageBox.Show(String.Format("User {0} added to database.", userInformation.FirstName + " " + userInformation.LastName), "User successfully added to database", MessageBoxButtons.OK, MessageBoxIcon.Information); Console.WriteLine("User ID: {0}", userInformation.UserID); Console.WriteLine("Account added to database."); } // If User Account Type is created. else if (accountTypeCombo.SelectedIndex.Equals(1)) { // Account Information AccountInformation accountInfo = new AccountInformation(); accountInfo.AccountNumber = usernameTxt.Text; accountInfo.AccountUsername = passwordTxt.Text; accountInfo.TypeID = 2; // Since Account Type 'User' is identified as 2 accountInfo.AccountCreationDate = DateTime.Now; db.AccountInformations.Add(accountInfo); db.SaveChanges(); Console.WriteLine("Account ID: {0}", accountInfo.AccountID); // User Information UserInformation userInformation = new UserInformation(); //userInformation.AccountID = (from accTemp in db.AccountInformations // where accTemp.AccountNumber == accountInfo.AccountNumber // select accTemp).First().AccountID; userInformation.AccountID = accountInfo.AccountID; userInformation.FirstName = firstNameTxt.Text; userInformation.LastName = lastNameTxt.Text; userInformation.PhoneNumber = phoneNumberTxt.Text; userInformation.Email = emailTxt.Text; userInformation.Address = addressTxt.Text; db.UserInformations.Add(userInformation); db.SaveChanges(); // Minutiae TemplatePath Information DirectoryOps.DefaultApplicationDirectory appDatabaseDir = new DirectoryOps.DefaultApplicationDirectory(Program.defaultApplicationDatabaseDirName); DirectoryOps.DefaultApplicationDirectory matlabGeneratedDir = new DirectoryOps.DefaultApplicationDirectory(Program.defaultMatlabGeneratedDirName); /* Declaring variables used to store: * tempFingerprintFileName - Temporary Fingerprint File from fingerprint sensor. * generatedTemplateFileName - Matlab Generated Minutiea Template File. * */ string tempFingerprintFileName; string generatedTemplateFileName; // Generating Template file using matlab // If fingerprint sensor is connected, generateTemplate() using sensor image // else use fingerprint image from image file if (fingerprint_device_connected) { tempFingerprintFileName = saveFingerprintImage(); generatedTemplateFileName = generateTemplate(tempFingerprintFileName); } else { generatedTemplateFileName = generateTemplate(); } try { if (FileOps.FileOperation.Save( Path.Combine(matlabGeneratedDir.Path, generatedTemplateFileName), appDatabaseDir.Path, stringHashTxt.Text)) { Console.WriteLine("File saved as: {0}", Path.Combine(appDatabaseDir.Path, stringHashTxt.Text)); } } catch (Exception exc) { MessageBox.Show( exc.Message, "Exception Occured!", MessageBoxButtons.OK, MessageBoxIcon.Warning ); } MinutiaeTemplatePath minutiaeTemplatePath = new MinutiaeTemplatePath(); minutiaeTemplatePath.TemplatePath = Path.Combine(appDatabaseDir.Path, stringHashTxt.Text); db.MinutiaeTemplatePaths.Add(minutiaeTemplatePath); db.SaveChanges(); // Template Information FingerprintTemplate fingerprintTemplate = new FingerprintTemplate(); fingerprintTemplate.AccountID = accountInfo.AccountID; fingerprintTemplate.MinutiaeTemplateID = minutiaeTemplatePath.ID; db.FingerprintTemplates.Add(fingerprintTemplate); db.SaveChanges(); MessageBox.Show(String.Format("User {0} added to database.", userInformation.FirstName + " " + userInformation.LastName), "User successfully added to database", MessageBoxButtons.OK, MessageBoxIcon.Information); Console.WriteLine("User ID: {0}", userInformation.UserID); Console.WriteLine("Account added to database."); this.Close(); } } } }