/// <summary> /// Opens the settings file and populates the allUsers field with the data found there. /// Also restores the last user to access the system to current user status. /// </summary> public static void ReadSettings() { try { using (FileStream fs = new FileStream(GetSettingFilePath(), FileMode.OpenOrCreate, FileAccess.Read)) { using (BinaryReader reader = new BinaryReader(fs)) { if (new FileInfo(GetSettingFilePath()).Length != 0) // If the file wasn't just created { int n = reader.ReadInt32(); for (int i = 0; i < n; i++) { allUsers.Add(new MPAiUser(reader.ReadString(), reader.ReadString(), VoiceType.getVoiceTypeFromString(reader.ReadString()), reader.ReadBoolean(), reader.ReadBoolean())); } // restore the last used user, if there was one. int index = reader.ReadInt32(); if (index >= 0 && index < allUsers.Count) { currentUser = allUsers[index]; } } } } } catch (Exception exp) { Console.WriteLine(exp); } }
/// <summary> /// Checks if a user already exists with the same username as the user input, and adds the user to the database if not. /// </summary> /// <param name="candidate">The user to add to the system, as an MPAiUser.</param> /// <returns>True if the creation was successful, false if not.</returns> public static bool CreateNewUser(MPAiUser candidate) { if (allUsers.Contains(candidate)) { return(false); } else { allUsers.Add(candidate); return(true); } }
/// <summary> /// Checks if the input user is a valid, existing user, with a correct password, and sets them to the current user. /// Also adds the stored voicetype to the user object that is authenticated. /// </summary> /// <param name="tUser">The user to authenticate, as an MPAiUser object. This is passed by reference.</param> /// <returns>True if the user exists already, false otherwise.</returns> public static bool AuthenticateUser(ref MPAiUser tUser) { // This changes the field, as the property's setter is designed to be used from outside the class, and would cause this to break. if (allUsers.Contains(tUser) && GetUser(tUser.getName()).codeCorrect(tUser.getCode())) { MPAiUser user = GetUser(tUser.getName()); currentUser = user; // Set the user as the current user. return(true); } else { return(false); } }
/// <summary> /// Override for equals(). /// Two users with the same username are considered the same user. /// </summary> /// <param name="obj">The object to be compared to the current user.</param> /// <returns>True if the user and the object are the same thing, false otherwise.</returns> public override bool Equals(System.Object obj) { if (obj is MPAiUser) { MPAiUser otherUser = (MPAiUser)obj; if (userName == null || passWord == null) { return(false); } return(getName() == otherUser.getName()); } else { return(false); } }
/// <summary> /// Checks whether the input user already exists in the system. /// </summary> /// <param name="candidate">The user to check, as an MPAiUser.</param> /// <returns>True if the user is in the system, false if not.</returns> public static bool ContainsUser(MPAiUser candidate) { return(ContainsUser(candidate.getName())); }
/// <summary> /// Removes the specified user from the system. /// </summary> /// <param name="userToRemove">The user to remove, as an MPAiUser object.</param> public static void RemoveUser(MPAiUser userToRemove) { allUsers.Remove(userToRemove); }