EduNetworkBuilder/EduNetworkBuilder/PersonClass.cs

178 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
namespace EduNetworkBuilder
{
public class PersonClass
{
public string filepath=""; //the path of the file. We use the username as the file-name.
string _UserName="";
/// <summary>
/// The username of the person. Will be used as a filename. Cannot be changed once it is set
/// </summary>
public string UserName { get { return _UserName; } }
/// <summary>
/// The full name of the person. Can have spaces and punctuation. Can change as one wants it changed.
/// </summary>
public string FullName = "";
List<string> Passwords = new List<string>();
string PasswordHint = "";
/// <summary>
/// The AltPassword is mainly used to hold the admin decrypting password. The student account will
/// use this to encrypt the student password, so the admin can open their file.
/// </summary>
string AltPassword = "";
/// <summary>
/// Used to determine if the user we are working with is the admin account.
/// </summary>
public bool isAdmin { get; protected set; }
/// <summary>
/// EverCompletedPuzzles is a list of puzzles that they have completed once.
/// </summary>
List<string> EverCompletedPuzzles = new List<string>();
List<SchoolworkClass> Projects = new List<SchoolworkClass>();
private PersonClass()
{ }
public PersonClass(String User, bool MakeAdmin)
{
_UserName = User;
isAdmin = MakeAdmin;
}
public PersonClass(string Filename)
{
}
public string Password(int index = 0)
{
if (Passwords.Count == 0) return "";
if (index < 0) return "";
if (index >= Passwords.Count) return "";
return Passwords[index];
}
public void ChangePassword(string NewPassword)
{
Passwords.Insert(0, NewPassword);
int maxPWs = 1;
if (isAdmin)
maxPWs = 10; //Admins store the last 10 passwords. If the admin changed his PW, we still need to decrypt the student's files
for (int a = Passwords.Count - 1; a >= maxPWs; a--)
Passwords.RemoveAt(a); //Remove all but the one password
}
#region Load and Save
//Load and save functions
public void Load()
{
Load(@"C:\Users\tyoung\Desktop\Test.enbx");
}
public void Load(string filename)
{
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(filename))
{
xmlDoc.Load(filename);
Load(xmlDoc, filename);
}
}
public void Load(XmlNode TheNode, string Name)
{
foreach (XmlNode Individual in TheNode.ChildNodes)
{
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch (Individual.Name.ToLower())
{
case "username":
_UserName = Individual.InnerText;
break;
case "fullname":
FullName = Individual.InnerText;
break;
case "passwordhint":
PasswordHint = Individual.InnerText;
break;
case "altpassword":
AltPassword = Individual.InnerText;
break;
case "isadmin":
bool isadmin = false;
bool.TryParse(Individual.InnerText, out isadmin);
isAdmin = isadmin;
break;
case "password":
Passwords.Add(Individual.InnerText);
break;
case "project":
break;
}
}
}
}
public void Save()
{
Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.enbx"));
}
public void Save(string filename)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
XmlWriter writer = XmlWriter.Create(filename, settings);
//Now we write the file:
writer.WriteStartDocument();
writer.WriteStartElement("EduNetworkBuilder");
writer.WriteComment("This is a network file for EduNetworkBuilder.");
Save(writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
public void Save(XmlWriter writer)
{
//Save the language name
//save the number of items
//Save all the items
writer.WriteStartElement("User");
writer.WriteElementString("UserName", UserName);
writer.WriteElementString("FullName", FullName);
writer.WriteElementString("PasswordHint", PasswordHint);
writer.WriteElementString("AltPassword", AltPassword);
writer.WriteElementString("IsAdmin", isAdmin.ToString());
foreach(string One in Passwords)
writer.WriteElementString("Password", One);
//Save all the devices
foreach (SchoolworkClass One in Projects)
{
//One.Save(writer);
}
writer.WriteEndElement();
}
#endregion
}
}