//add new card to selected list private void button4_Click(object sender, EventArgs e) { ListMember member = ListUtilities.GenerateNewMember((string)checkedListBox1.SelectedItem); FillTabPage3(member.GetValues().SubArray(0, 9).Cast <string>().ToArray()); tabPage3.Name = "Add"; tabControl1.TabPages.Add(tabPage3); tabControl1.SelectedTab = tabPage3; }
//save the editor values private void button3_Click_1(object sender, EventArgs e) { if (!FileChecks(label10.Text)) { return; } try { ListUtilities.WriteMemberToList(new ListMember ( label10.Text, Convert.ToInt32(label11.Text), Convert.ToBoolean(label12.Text), Convert.ToInt32(textBox2.Text.Trim()), //Convert.ToUInt64(textBox3.Text.Trim()), new Func <UInt64>(() => { string tbt = textBox3.Text.Trim(); if (tbt.Length > 1) { if (tbt[0] == '*' && tbt[1] == 'h') { tbt = UInt64.Parse(tbt.Substring(2), System.Globalization.NumberStyles.HexNumber) + ""; } } return(Convert.ToUInt64(tbt)); }).Invoke(), textBox4.Text.Trim(), textBox5.Text.Trim(), textBox6.Text.Trim(), textBox7.Text.Trim() )); } catch (Exception ex) { return; } tabControl1.TabPages.Remove(tabPage3); tabControl1.SelectedTab = tabPage2; }
private void GenerateList(int type) { using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Filter = "Supported Extentions (*.xlsx)|*.xlsx"; sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (sfd.ShowDialog() == DialogResult.OK) { ListUtilities.CreateWorkbook(sfd.FileName, type); checkedListBox1.Items.Add(sfd.FileName); if (MessageBox.Show(this, "Sync", "Sync Checked List Members With New List?", MessageBoxButtons.YesNo) == DialogResult.Yes) { SyncListMembers(); } } } }
private async void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { if (tabControl1.SelectedTab == tabPage2) { string[] list_paths = checkedListBox1.CheckedItems.Cast <string>().ToArray(); //check to make sure none of the source lists are in use if (!FileChecks(list_paths)) { return; } //update the members list because they may have changed members = await ListUtilities.GetListMembers(list_paths); //force a search because the search items may have changed previous_search = ""; await SearchAndFill(); textBox1.Focus(); } if (tabControl1.SelectedTab != tabPage3) { AcceptButton = null; if (tabControl1.TabPages.Contains(tabPage3)) { tabControl1.TabPages.Remove(tabPage3); } tabPage3.Name = "Edit"; } else { AcceptButton = button3; } }
private async Task ModifyLM() { //public ListMember(string source_filename, int source_row, bool is_active, int serial, UInt64 uid, string name, string description, string active_days, string active_times) string table_name = GetSelectedTableName(); if (table_name == null) { MessageBox.Show(this, "A table must be selected to continue."); return; } //if (MessageBox.Show(this, "Are you sure that you want to add this card?", "Prompt", MessageBoxButtons.OKCancel) == DialogResult.Cancel) // return; UInt64 uid = GetUIDFromTextBox(); if (uid > 0) { textBox1.Text = ""; } else { MessageBox.Show(this, "Invalid UID"); return; } int uid_search_result = ListUtilities.FindUID(table_name, uid); if (uid_search_result > -1 && prompt_if_exists) { if (MessageBox.Show(this, "The UID was found in the selected list. Do you want to continue?", "Prompt", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } } ListMember to_update = uid_search_result == -1 ? null : (await ListUtilities.GetListMembers(new string[] { table_name })).Where(x => x.UID.CompareTo(uid) == 1).First(); string[] default_values = GenerateDefaults(selected_default); if (to_update != null) { to_update.ActiveDays = default_values[0]; to_update.ActiveTimes = default_values[1]; ListUtilities.WriteMemberToList(to_update); } else { int next_row_index = ListUtilities.GetRowRange(table_name) + 1; int next_list_serial = ListUtilities.GetSerialRange(table_name) + 1; ListUtilities.WriteMemberToList(new ListMember( table_name, next_row_index, false, next_list_serial, uid, "", "", default_values[0], default_values[1])); } }
private async void button1_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex == 0) { List <string> files_to_import = new List <string>(); using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Multiselect = true; ofd.Filter = "Supported Extentions (*.db2bak)|*.db2bak"; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); ofd.FileName = ""; if (ofd.ShowDialog() == DialogResult.OK) { files_to_import.AddRange(ofd.FileNames); } } if (files_to_import.Count() == 0) { return; } foreach (string path in files_to_import) { ImportDB2Bak(path); } } else if (listBox1.SelectedIndex == 2) //import nuid set { List <string> to_import = new List <string>(); using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Multiselect = true; ofd.Filter = "Supported Extentions (*.txt)|*.txt"; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); ofd.FileName = ""; if (ofd.ShowDialog() == DialogResult.OK) { to_import.AddRange(ofd.FileNames); } } if (to_import.Count() == 0) { return; } using (MySqlConnection sqlconn = new MySqlConnection(MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties.ConnectionString)) { await sqlconn.OpenAsync(); foreach (string path in to_import) { UInt64[] nuids = File.ReadAllLines(path).Where(x => x.Trim() != "").Select(v => Convert.ToUInt64(BaseConverter.DecodeFromString(v.Trim()))).ToArray(); foreach (var nuid in nuids) { await DatabaseUtilities.AddCardToDatabase(sqlconn, nuid); } } } } else if (listBox1.SelectedIndex == 1) { foreach (Control c in Controls) { c.Enabled = false; } UseWaitCursor = true; List <string> files_to_import = new List <string>(); using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Multiselect = true; ofd.Filter = "Supported Extentions (*.xlsx)|*.xlsx"; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); ofd.FileName = ""; if (ofd.ShowDialog() == DialogResult.OK) { files_to_import.AddRange(ofd.FileNames); } } if (files_to_import.Count() == 0) { return; } if (listBox1.SelectedIndex == 0) //import database backup { } else if (listBox1.SelectedIndex == 1) //import master controller list { ProgressDialog pgd = new ProgressDialog("Importing List"); pgd.Show(this); pgd.LabelText = "Importing " + files_to_import.Count() + " files."; foreach (string file_name in files_to_import) { //import users //load file pgd.Reset(); pgd.LabelText = "Parsing list " + (files_to_import.IndexOf(file_name) + 1) + " of " + files_to_import.Count(); List <ListMember> members = (await ListUtilities.GetListMembers(new string[] { file_name })).ToList(); pgd.Step(); using (MySqlConnection sqlconn = new MySqlConnection(MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties.ConnectionString)) { pgd.Reset(); pgd.LabelText = "Opening Database Connection"; await sqlconn.OpenAsync(); pgd.Step(); //check to make sure the user table exists List <string> table_names = new List <string>(); using (MySqlCommand cmdName = new MySqlCommand("show tables", sqlconn)) using (MySqlDataReader reader = cmdName.ExecuteReader()) while (await reader.ReadAsync()) { table_names.Add(reader.GetString(0)); } if (!table_names.Contains("users")) { string cmdstr = "CREATE TABLE `accesscontrol`.`users` (`user_id` BIGINT UNSIGNED NOT NULL,`name` VARCHAR(255) NOT NULL,`description` VARCHAR(255) NULL,PRIMARY KEY (`user_id`),UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC));"; using (MySqlCommand sqlcmd = new MySqlCommand(cmdstr, sqlconn)) await sqlcmd.ExecuteNonQueryAsync(); } //generate subset of listmembers that dont exist in the current user list //this will be based off of name and description matching pgd.Reset(); pgd.LabelText = "Generating List of New Users"; if (members.Count() > 0) { pgd.Maximum = members.Count() - 1; } List <ListMember> new_users = new List <ListMember>(); foreach (ListMember lm in members) { if (lm.Name.Trim() == "") { pgd.Step(); continue; } string cmd = "SELECT * FROM `users` WHERE name=@name AND description=@description;"; using (MySqlCommand sqlcmd = new MySqlCommand(cmd, sqlconn)) { sqlcmd.Parameters.AddWithValue("@name", lm.Name.Trim()); sqlcmd.Parameters.AddWithValue("@description", lm.Description.Trim()); using (MySqlDataReader reader = sqlcmd.ExecuteReader()) if (!(await reader.ReadAsync())) { new_users.Add(lm); } } pgd.Step(); } pgd.Reset(); pgd.LabelText = "Adding New Users to the Database"; if (new_users.Count() > 0) { pgd.Maximum = new_users.Count() - 1; } foreach (ListMember lm in new_users) { using (MySqlCommand sqlcmd = new MySqlCommand("insert into `users` (user_id, name, description) values (@user_id, @name, @description)", sqlconn)) { sqlcmd.Parameters.AddWithValue("@user_id", await DatabaseUtilities.GenerateUniqueUserID(sqlconn)); sqlcmd.Parameters.AddWithValue("@name", lm.Name.Trim()); sqlcmd.Parameters.AddWithValue("@description", lm.Description.Trim()); await sqlcmd.ExecuteNonQueryAsync(); } pgd.Step(); } //check to see if the cards table exists if (!table_names.Contains("cards")) { string cmd = "CREATE TABLE `accesscontrol`.`cards` (`uid` BIGINT UNSIGNED NOT NULL,`user_id` BIGINT UNSIGNED NULL,PRIMARY KEY (`uid`),UNIQUE INDEX `uid_UNIQUE` (`uid` ASC));"; using (MySqlCommand sqlcmd = new MySqlCommand(cmd, sqlconn)) await sqlcmd.ExecuteNonQueryAsync(); } //generate a list of new cards that will be added to the database pgd.Reset(); pgd.LabelText = "Adding New Cards to the Database"; List <ListMember> new_cards = new List <ListMember>(); foreach (ListMember lm in members) { if (lm.UID == 0) { continue; } string cmd = "SELECT uid FROM `cards` WHERE uid=@uid;"; using (MySqlCommand sqlcmd = new MySqlCommand(cmd, sqlconn)) { sqlcmd.Parameters.AddWithValue("@uid", lm.UID); using (MySqlDataReader reader = sqlcmd.ExecuteReader()) if (!(await reader.ReadAsync())) { new_cards.Add(lm); } } } if (new_cards.Count() > 0) { pgd.Maximum = new_cards.Count() - 1; } //add the new cards to the database foreach (ListMember lm in new_cards) { using (MySqlCommand sqlcmd = new MySqlCommand("insert into `cards` (uid, user_id) values (@uid, @user_id)", sqlconn)) { sqlcmd.Parameters.AddWithValue("@uid", lm.UID); sqlcmd.Parameters.AddWithValue("@user_id", 0); await sqlcmd.ExecuteNonQueryAsync(); } pgd.Step(); } //associate card uids with user_ids pgd.Reset(); pgd.LabelText = "Associating Cards With Users"; pgd.Maximum = members.Count() - 1; foreach (ListMember lm in members) { if (lm.Name.Trim() == "" || lm.UID == 0) { pgd.Step(); continue; } //obtain user_id from database UInt64 user_id = 0; string cmd = "SELECT user_id FROM `users` WHERE name=@name AND description=@description;"; using (MySqlCommand sqlcmd = new MySqlCommand(cmd, sqlconn)) { sqlcmd.Parameters.AddWithValue("@name", lm.Name.Trim()); sqlcmd.Parameters.AddWithValue("@description", lm.Description.Trim()); using (MySqlDataReader reader = sqlcmd.ExecuteReader()) if (await reader.ReadAsync()) { user_id = reader.GetUInt64(0); } } //complete card association using (MySqlCommand sqlcmd = new MySqlCommand("update `cards` set user_id=@user_id where uid=@uid;", sqlconn)) { sqlcmd.Parameters.AddWithValue("@user_id", user_id); sqlcmd.Parameters.AddWithValue("@uid", lm.UID); await sqlcmd.ExecuteNonQueryAsync(); } pgd.Step(); } //generate access control list //check to see if the lists table exists if (!table_names.Contains("lists")) { string cmd = "CREATE TABLE `accesscontrol`.`lists` (`uid` BIGINT UNSIGNED NOT NULL,`alias` VARCHAR(255) NULL,`type` TINYINT UNSIGNED NOT NULL,PRIMARY KEY(`uid`),UNIQUE INDEX `uid_UNIQUE` (`uid` ASC));"; using (MySqlCommand sqlcmd = new MySqlCommand(cmd, sqlconn)) await sqlcmd.ExecuteNonQueryAsync(); } //create a master list table entry pgd.Reset(); pgd.LabelText = "Adding List to Master Table"; UInt64 list_uid = await DatabaseUtilities.GenerateUniqueListUID(sqlconn); using (MySqlCommand sqlcmd = new MySqlCommand("insert into `lists` (uid, alias, type) values (@uid, @alias, @type)", sqlconn)) { sqlcmd.Parameters.AddWithValue("@uid", list_uid); sqlcmd.Parameters.AddWithValue("@alias", new FileInfo(file_name).Name.Split('.')[0]); sqlcmd.Parameters.AddWithValue("@type", 0); await sqlcmd.ExecuteNonQueryAsync(); } pgd.Step(); //create list table pgd.Reset(); pgd.LabelText = "Creating New List Table"; string sqlcmdstr = "CREATE TABLE `accesscontrol`.`" + list_uid + "` (`user_id` BIGINT UNSIGNED NOT NULL,`days` VARCHAR(255) NOT NULL,`times` VARCHAR(255) NOT NULL,`enabled` TINYINT UNSIGNED NOT NULL,PRIMARY KEY(`user_id`),UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC));"; using (MySqlCommand sqlcmd = new MySqlCommand(sqlcmdstr, sqlconn)) await sqlcmd.ExecuteNonQueryAsync(); pgd.Step(); //populate list table pgd.Reset(); pgd.LabelText = "Populating New List"; pgd.Maximum = members.Count() - 1; foreach (ListMember lm in members) { if (lm.Name.Trim() == "" || lm.UID == 0 || lm.ActiveDays.Trim() == "" || lm.ActiveTimes.Trim() == "") { pgd.Step(); continue; } UInt64 user_id = 0; string cmd = "SELECT user_id FROM `users` WHERE name=@name AND description=@description;"; using (MySqlCommand sqlcmd = new MySqlCommand(cmd, sqlconn)) { sqlcmd.Parameters.AddWithValue("@name", lm.Name.Trim()); sqlcmd.Parameters.AddWithValue("@description", lm.Description.Trim()); using (MySqlDataReader reader = sqlcmd.ExecuteReader()) if (await reader.ReadAsync()) { user_id = reader.GetUInt64(0); } } if (user_id == 0) { continue; } //check to see if the user is already present in the list and if necessary resolve the contention cmd = "SELECT * FROM `" + list_uid + "` WHERE user_id=@user_id;"; bool contention_detected = false; string resolved_access_days = ""; string resolved_access_times = ""; using (MySqlCommand sqlcmd = new MySqlCommand(cmd, sqlconn)) { sqlcmd.Parameters.AddWithValue("@user_id", user_id); using (MySqlDataReader reader = sqlcmd.ExecuteReader()) if (await reader.ReadAsync()) { contention_detected = true; //there were multiple occurences of the user in the source list. merge the access privileges. string stored_access_days = (string)reader["days"]; string stored_access_times = (string)reader["times"]; string contending_access_days = lm.ActiveDays; string contending_access_times = lm.ActiveTimes; //parse entry times uint stored_start_hour, stored_start_minute, stored_end_hour, stored_end_minute; uint contending_start_hour, contending_start_minute, contending_end_hour, contending_end_minute; stored_start_hour = uint.Parse(stored_access_times.Substring(0, stored_access_times.IndexOf(':')).Trim()); stored_access_times = stored_access_times.Substring(stored_access_times.IndexOf(':') + 1); stored_start_minute = uint.Parse(stored_access_times.Substring(0, stored_access_times.IndexOf('-')).Trim()); stored_access_times = stored_access_times.Substring(stored_access_times.IndexOf('-') + 1); stored_end_hour = uint.Parse(stored_access_times.Substring(0, stored_access_times.IndexOf(':')).Trim()); stored_access_times = stored_access_times.Substring(stored_access_times.IndexOf(':') + 1); stored_end_minute = uint.Parse(stored_access_times.Trim()); // contending_start_hour = uint.Parse(contending_access_times.Substring(0, contending_access_times.IndexOf(':')).Trim()); contending_access_times = contending_access_times.Substring(contending_access_times.IndexOf(':') + 1); contending_start_minute = uint.Parse(contending_access_times.Substring(0, contending_access_times.IndexOf('-')).Trim()); contending_access_times = contending_access_times.Substring(contending_access_times.IndexOf('-') + 1); contending_end_hour = uint.Parse(contending_access_times.Substring(0, contending_access_times.IndexOf(':')).Trim()); contending_access_times = contending_access_times.Substring(contending_access_times.IndexOf(':') + 1); contending_end_minute = uint.Parse(contending_access_times.Trim()); //complete the time conflict resolution if (stored_start_hour * 24 + stored_start_minute < contending_start_hour * 24 + contending_start_minute) { resolved_access_times += stored_start_hour.ToString("00") + ":" + stored_start_minute.ToString("00") + "-"; } else { resolved_access_times += contending_start_hour.ToString("00") + ":" + contending_start_minute.ToString("00") + "-"; } if (stored_end_hour * 24 + stored_end_minute > contending_end_hour * 24 + contending_end_minute) { resolved_access_times += stored_end_hour.ToString("00") + ":" + stored_end_minute.ToString("00"); } else { resolved_access_times += contending_end_hour.ToString("00") + ":" + contending_end_minute.ToString("00"); } //begin day of week conflict resolution byte merged_days = (byte)(ListEntryUtilities.ConvertDaysString(stored_access_days) | ListEntryUtilities.ConvertDaysString(contending_access_days)); List <string> to_merge = new List <string>(); for (int i = 6; i >= 0; i--) { if (((byte)(merged_days >> i) & 1) == 1) { to_merge.Add(days_of_the_week[6 - i]); } } for (int i = 0; i < to_merge.Count(); i++) { resolved_access_days += to_merge[i]; if (i + 1 != to_merge.Count()) { resolved_access_days += ":"; } } } } if (contention_detected) { using (MySqlCommand sqlcmd = new MySqlCommand("update `" + list_uid + "` set days=@days, times=@times where user_id=@user_id;", sqlconn)) { sqlcmd.Parameters.AddWithValue("@days", resolved_access_days); sqlcmd.Parameters.AddWithValue("@times", resolved_access_times); sqlcmd.Parameters.AddWithValue("@user_id", user_id); await sqlcmd.ExecuteNonQueryAsync(); } } else { using (MySqlCommand sqlcmd = new MySqlCommand("insert into `" + list_uid + "` (user_id, days, times, enabled) values (@user_id, @days, @times, @enabled)", sqlconn)) { sqlcmd.Parameters.AddWithValue("@user_id", user_id); sqlcmd.Parameters.AddWithValue("@days", lm.ActiveDays); sqlcmd.Parameters.AddWithValue("@times", lm.ActiveTimes); sqlcmd.Parameters.AddWithValue("@enabled", 1); await sqlcmd.ExecuteNonQueryAsync(); } } pgd.Step(); } } } pgd.Dispose(); } foreach (Control c in Controls) { c.Enabled = true; } UseWaitCursor = false; Cursor = Cursors.Default; Refresh(); } }