diff --git a/EduNetworkBuilder/NB.cs b/EduNetworkBuilder/NB.cs index 7dc7dad..5d92bc6 100644 --- a/EduNetworkBuilder/NB.cs +++ b/EduNetworkBuilder/NB.cs @@ -310,6 +310,8 @@ namespace EduNetworkBuilder public static int UntaggedVLAN = -1; //If the packet is not tagged. public static int MaxPacketsBeforeOptimizing = 50; + public static string AllowedPasswordCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=!~@#$%^&*()_+{}[]/?<>,."; + public static string AllowedUsernameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890._"; /// /// Find the global random number generator. /// @@ -896,6 +898,22 @@ namespace EduNetworkBuilder } return result; } + public static bool ValidateString(string tovalidate, string AllowedCharacters) + { + if (tovalidate == null || tovalidate == "") return false; + if (AllowedCharacters == null || AllowedCharacters == "") return false; + foreach (char one in tovalidate) + if (!AllowedCharacters.Contains(one)) return false; + return true; + } + public static bool ValidatePassword(string password) + { + return ValidateString(password, AllowedPasswordCharacters); + } + public static bool ValidateUsername(string password) + { + return ValidateString(password, AllowedUsernameCharacters); + } } } diff --git a/EduNetworkBuilder/PersonProfileForm.Designer.cs b/EduNetworkBuilder/PersonProfileForm.Designer.cs index 73698c0..5188a16 100644 --- a/EduNetworkBuilder/PersonProfileForm.Designer.cs +++ b/EduNetworkBuilder/PersonProfileForm.Designer.cs @@ -177,6 +177,7 @@ this.btnImportStudents.TabIndex = 0; this.btnImportStudents.Text = "Import Students"; this.btnImportStudents.UseVisualStyleBackColor = true; + this.btnImportStudents.Click += new System.EventHandler(this.btnImportStudents_Click); // // TabHomework // diff --git a/EduNetworkBuilder/PersonProfileForm.cs b/EduNetworkBuilder/PersonProfileForm.cs index 655fded..e0a07ab 100644 --- a/EduNetworkBuilder/PersonProfileForm.cs +++ b/EduNetworkBuilder/PersonProfileForm.cs @@ -8,9 +8,20 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; +using Microsoft.VisualBasic.FileIO; //for CSV parsing namespace EduNetworkBuilder { + //A temporary holding structure when loading in students from CSV for validation. + //Once loaded and parsed, we then make actual students from them. + class StudentHolder + { + public string Name = ""; + public string Password = ""; + public string FullName = ""; + public bool ForceToChangePass = true; + } + public partial class PersonProfileForm : Form { enum TopTab { profiletab=0, classworktab=1} @@ -378,5 +389,48 @@ namespace EduNetworkBuilder Close(); //Exit out of the profile } } + + private void btnImportStudents_Click(object sender, EventArgs e) + { + //Get a csv file + //try to read it in + //Import them all + OpenFileDialog mydialog = new OpenFileDialog(); + mydialog.AddExtension = true; + string filter = "Comma Separated List (*.csv)|*.csv"; + mydialog.Filter = filter; + mydialog.Multiselect = false; + mydialog.CheckFileExists = true; + mydialog.ShowHelp = true; + if (CurrentUser.UserSettings.LastPath != null && CurrentUser.UserSettings.LastPath != "") + mydialog.FileName = CurrentUser.UserSettings.LastPath; + DialogResult result = mydialog.ShowDialog(); + if (result == System.Windows.Forms.DialogResult.Cancel) return; + + List tList = new List(); + + //Now we have a csv file. Try to parse it + using (TextFieldParser parser = new TextFieldParser(mydialog.FileName)) + { + parser.TextFieldType = FieldType.Delimited; + parser.SetDelimiters(","); + while (!parser.EndOfData) + { + //Process row + int index = 0; + string[] fields = parser.ReadFields(); + StudentHolder TStudent = new StudentHolder(); + foreach (string field in fields) + { + //TODO: Process field + if (index == 0) TStudent.Name = field; + if (index == 1) TStudent.FullName = field; + if (index == 2) TStudent.Password = field; + index++; //Track which field we are in. + } + tList.Add(TStudent); + } + } + } } } \ No newline at end of file