From f2796ec0a84fccb59074a385f96eeaa2f4d8ac57 Mon Sep 17 00:00:00 2001 From: "matheo.thierry" Date: Mon, 29 May 2023 15:16:16 +0200 Subject: [PATCH 1/3] persistance v1 --- notus/Biblioteque_de_Class/Database.cs | 2 +- notus/Biblioteque_de_Class/Note.cs | 15 ++++++++++++++- notus/Biblioteque_de_Class/User.cs | 4 ++-- notus/Notus_Persistence/ToJSON.cs | 16 +++------------- notus/Notus_Persistence/ToXML.cs | 23 ++++++++++++++++++++--- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/notus/Biblioteque_de_Class/Database.cs b/notus/Biblioteque_de_Class/Database.cs index 342abab..8aebfd7 100644 --- a/notus/Biblioteque_de_Class/Database.cs +++ b/notus/Biblioteque_de_Class/Database.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace Biblioteque_de_Class { - [DataContract] + [DataContract(IsReference = true)] public class Database { [DataMember] diff --git a/notus/Biblioteque_de_Class/Note.cs b/notus/Biblioteque_de_Class/Note.cs index 6bbad35..70f2893 100644 --- a/notus/Biblioteque_de_Class/Note.cs +++ b/notus/Biblioteque_de_Class/Note.cs @@ -1,34 +1,47 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace Biblioteque_de_Class { + [DataContract(IsReference = true)] public class Note { + [DataMember] private string name; + [DataMember] public string Name { get { return name; } private set { if (value == null) { name = "Unnamed Note"; } else { name = value; } } } + [DataMember] private string Text { get; set; } = ""; - + + [DataMember] private string logoPath; + [DataMember] public string LogoPath { get { return logoPath; } private set { if (value == null) { logoPath = "PATH TO DEFAULT LOGO"; } else { logoPath = value; } } } + [DataMember] private DateOnly CreationDate { get; } + [DataMember] private DateOnly ModificationDate { get; set; } + [DataMember] private readonly List ImageList; + [DataMember] private readonly List Collaborators; + [DataMember] private readonly List Editors; + [DataMember] private readonly User Owner; public Note(string name, string logoPath, User owner) diff --git a/notus/Biblioteque_de_Class/User.cs b/notus/Biblioteque_de_Class/User.cs index 1e3f52a..ddb793a 100644 --- a/notus/Biblioteque_de_Class/User.cs +++ b/notus/Biblioteque_de_Class/User.cs @@ -5,7 +5,7 @@ using System.Runtime.Serialization; namespace Biblioteque_de_Class { - [DataContract] + [DataContract(IsReference = true)] public class User { [DataMember] @@ -21,7 +21,7 @@ namespace Biblioteque_de_Class private List TagList; [DataMember] private List FavList; - [DataMember] + [DataMember(EmitDefaultValue = false)] private bool IsConnected { get; set; } [DataMember] private Dictionary> NoteTagged; diff --git a/notus/Notus_Persistence/ToJSON.cs b/notus/Notus_Persistence/ToJSON.cs index 814b095..a7dad3e 100644 --- a/notus/Notus_Persistence/ToJSON.cs +++ b/notus/Notus_Persistence/ToJSON.cs @@ -9,25 +9,15 @@ using System.Text; using System.Threading.Tasks; using System.IO; using System.Text.Json; +using System.Text.Json.Serialization; namespace Notus_Persistance { public class ToJSON : IManager { private const string DatabaseDataFilePath = "data.json"; - private const string UserDataFilePath = "userdata.json"; - private const string NoteDataFilePath = "notedata.json"; - private const string ThemeDataFilePath = "themedata.json"; - private const string LogoDataFilePath = "logodata.json"; - private const string TagsDataFilePath = "tagsdata.json"; - private const string NoteImageDataFilePath = "noteImagedata.json"; - private static DataContractJsonSerializer DatabasejsonSerializer = new DataContractJsonSerializer(typeof(Database)); - private static DataContractJsonSerializer UserjsonSerializer = new DataContractJsonSerializer(typeof(User)); - private static DataContractJsonSerializer NotejsonSerializer = new DataContractJsonSerializer(typeof(Note)); - private static DataContractJsonSerializer ThemejsonSerializer = new DataContractJsonSerializer(typeof(Theme)); - private static DataContractJsonSerializer LogojsonSerializer = new DataContractJsonSerializer(typeof(Logo)); - private static DataContractJsonSerializer TagsjsonSerializer = new DataContractJsonSerializer(typeof(Tags)); - private static DataContractJsonSerializer NoteImagejsonSerializer = new DataContractJsonSerializer(typeof(NoteImage)); + private static readonly DataContractJsonSerializer DatabasejsonSerializer = new(typeof(Database)); + public void SaveDatabaseData(Database database) { using (FileStream fileStream = File.Create(DatabaseDataFilePath)) diff --git a/notus/Notus_Persistence/ToXML.cs b/notus/Notus_Persistence/ToXML.cs index 64cc84d..70118c2 100644 --- a/notus/Notus_Persistence/ToXML.cs +++ b/notus/Notus_Persistence/ToXML.cs @@ -7,22 +7,39 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Serialization; namespace Notus_Persistance { public class ToXML : IManager { private const string DataFilePath = "data.xml"; - private const string XmlDataFilePath = "userdata.xml"; + private static readonly DataContractSerializer DatabaseXmlSerializer = new(typeof(Database)); public void SaveDatabaseData(Database database) { - throw new NotImplementedException(); + XmlWriterSettings settings = new XmlWriterSettings() { Indent = true }; + using TextWriter tw = File.CreateText(DataFilePath); + using XmlWriter writer = XmlWriter.Create(tw, settings); + DatabaseXmlSerializer.WriteObject(writer, database); } public Database LoadDatabaseData() { - throw new NotImplementedException(); + if (File.Exists(DataFilePath)) + { + using (FileStream fileStream = File.OpenRead(DataFilePath)) + { + return DatabaseXmlSerializer.ReadObject(fileStream) is not Database database + ? throw new FileException("Failed to load the database. The loaded object is null.") + : database; + } + } + else + { + throw new FileException("No data file found."); + } } + } } From 4129d5399f5378a10cbe65f840eb2a09fc0b10b0 Mon Sep 17 00:00:00 2001 From: Matheo THIERRY Date: Tue, 30 May 2023 16:39:32 +0200 Subject: [PATCH 2/3] Upgrade persistance (v1 -> v2) --- .../Biblioteque_de_Class.csproj | 4 ++ notus/Biblioteque_de_Class/Database.cs | 13 +++++ notus/Biblioteque_de_Class/IManager.cs | 5 +- .../PersistenceManager.cs | 9 +++- notus/Notus_Persistence/Stub.cs | 11 +++- notus/Notus_Persistence/ToJSON.cs | 52 ++++++++++++++++++- notus/Notus_Persistence/ToXML.cs | 43 +++++++++++++-- 7 files changed, 128 insertions(+), 9 deletions(-) diff --git a/notus/Biblioteque_de_Class/Biblioteque_de_Class.csproj b/notus/Biblioteque_de_Class/Biblioteque_de_Class.csproj index 4658cbf..469e6eb 100644 --- a/notus/Biblioteque_de_Class/Biblioteque_de_Class.csproj +++ b/notus/Biblioteque_de_Class/Biblioteque_de_Class.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/notus/Biblioteque_de_Class/Database.cs b/notus/Biblioteque_de_Class/Database.cs index 8aebfd7..fcf9fac 100644 --- a/notus/Biblioteque_de_Class/Database.cs +++ b/notus/Biblioteque_de_Class/Database.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.Design; +using System.Data; using System.Linq; using System.Runtime.Serialization; using System.Text; @@ -18,17 +19,24 @@ namespace Biblioteque_de_Class private List ThemeList; [DataMember] private List UserList; + [DataMember] + private Dictionary> AddedThemeList; public Database() { DefaultLogoList = new List(); ThemeList = new List(); UserList = new List(); + AddedThemeList = new Dictionary>(); } public List GetDefaultLogoList() { return DefaultLogoList; } public List GetThemeList() { return ThemeList; } public List GetUserList() { return UserList; } + public Dictionary> GetAddedThemeFromUser() { return AddedThemeList; } + + public void SetDefaultLogoList(List defaultLogoList) { DefaultLogoList = defaultLogoList; } + public void SetDefaultThemeList(List defaultThemeList) { ThemeList = defaultThemeList; } /// /// recherche un utilisateur dans la liste d'utilisateur @@ -204,5 +212,10 @@ namespace Biblioteque_de_Class } } } + + public List AddedThemeOfOneUser(User user) + { + return GetAddedThemeFromUser()[user]; + } } } \ No newline at end of file diff --git a/notus/Biblioteque_de_Class/IManager.cs b/notus/Biblioteque_de_Class/IManager.cs index 008e946..2b472a8 100644 --- a/notus/Biblioteque_de_Class/IManager.cs +++ b/notus/Biblioteque_de_Class/IManager.cs @@ -8,8 +8,11 @@ namespace Biblioteque_de_Class { public interface IManager { - public void SaveDatabaseData(Database database); + public void SaveDatabaseData(List UserList, Dictionary> AddedThemeFromUser); public Database LoadDatabaseData(); + + public List LoadDefaultTheme(); + public List LoadDefaultLogo(); } } diff --git a/notus/Biblioteque_de_Class/PersistenceManager.cs b/notus/Biblioteque_de_Class/PersistenceManager.cs index 0947896..bee3338 100644 --- a/notus/Biblioteque_de_Class/PersistenceManager.cs +++ b/notus/Biblioteque_de_Class/PersistenceManager.cs @@ -4,6 +4,8 @@ namespace Biblioteque_de_Class { public class PersistenceManager { + private Database db = new(); + private readonly IManager persistence; public PersistenceManager(IManager pers) @@ -13,12 +15,15 @@ namespace Biblioteque_de_Class public void SaveDatabaseData(Database database) { - persistence.SaveDatabaseData(database); + persistence.SaveDatabaseData(database.GetUserList(), database.GetAddedThemeFromUser()); } public Database LoadDatabaseData() { - return persistence.LoadDatabaseData(); + db = persistence.LoadDatabaseData(); + db.SetDefaultThemeList(persistence.LoadDefaultTheme()); + db.SetDefaultLogoList(persistence.LoadDefaultLogo()); + return db; } } } diff --git a/notus/Notus_Persistence/Stub.cs b/notus/Notus_Persistence/Stub.cs index 6e35b04..e32b77f 100644 --- a/notus/Notus_Persistence/Stub.cs +++ b/notus/Notus_Persistence/Stub.cs @@ -10,7 +10,7 @@ namespace Notus_Persistance { public class Stub : IManager { - public void SaveDatabaseData(Database database) + public void SaveDatabaseData(List UserList, Dictionary> AddedThemeFromUser) { throw new NotImplementedException(); } @@ -63,6 +63,15 @@ namespace Notus_Persistance return database; } + public List LoadDefaultTheme() + { + throw new NotImplementedException(); + } + public List LoadDefaultLogo() + { + throw new NotImplementedException(); + } + } } diff --git a/notus/Notus_Persistence/ToJSON.cs b/notus/Notus_Persistence/ToJSON.cs index a7dad3e..d38cf11 100644 --- a/notus/Notus_Persistence/ToJSON.cs +++ b/notus/Notus_Persistence/ToJSON.cs @@ -16,9 +16,11 @@ namespace Notus_Persistance public class ToJSON : IManager { private const string DatabaseDataFilePath = "data.json"; + private const string DefaultThemePath = ""; + private const string DefaultLogoPath = ""; private static readonly DataContractJsonSerializer DatabasejsonSerializer = new(typeof(Database)); - public void SaveDatabaseData(Database database) + public void SaveDatabaseData(List UserList, Dictionary> AddedThemeFromUser) { using (FileStream fileStream = File.Create(DatabaseDataFilePath)) { @@ -28,7 +30,8 @@ namespace Notus_Persistance false, true))//<- this boolean says that we sant indentation { - DatabasejsonSerializer.WriteObject(writer, database); + DatabasejsonSerializer.WriteObject(writer, UserList); + DatabasejsonSerializer.WriteObject(writer, AddedThemeFromUser); } } } @@ -55,5 +58,50 @@ namespace Notus_Persistance throw new FileException("No data file found."); } } + + public List LoadDefaultTheme() + { + if (File.Exists(DefaultThemePath)) + { + using (FileStream fileStream = File.OpenRead(DefaultThemePath)) + { + List? DefaultThemeList = (List?)DatabasejsonSerializer.ReadObject(fileStream); + if (DefaultThemeList == null) + { + throw new FileException("Failed to Default Theme. The loaded object is null."); + } + else + { + return DefaultThemeList; + } + } + } + else + { + throw new FileException("No data file found."); + } + } + public List LoadDefaultLogo() + { + if (File.Exists(DefaultLogoPath)) + { + using (FileStream fileStream = File.OpenRead(DefaultLogoPath)) + { + List? DefaultLogoList = (List?)DatabasejsonSerializer.ReadObject(fileStream); + if (DefaultLogoList == null) + { + throw new FileException("Failed to Default Logo. The loaded object is null."); + } + else + { + return DefaultLogoList; + } + } + } + else + { + throw new FileException("No data file found."); + } + } } } diff --git a/notus/Notus_Persistence/ToXML.cs b/notus/Notus_Persistence/ToXML.cs index 70118c2..b351f4a 100644 --- a/notus/Notus_Persistence/ToXML.cs +++ b/notus/Notus_Persistence/ToXML.cs @@ -14,14 +14,17 @@ namespace Notus_Persistance public class ToXML : IManager { private const string DataFilePath = "data.xml"; + private const string DefaultThemePath = ""; + private const string DefaultLogoPath = ""; private static readonly DataContractSerializer DatabaseXmlSerializer = new(typeof(Database)); - public void SaveDatabaseData(Database database) + public void SaveDatabaseData(List UserList, Dictionary> AddedThemeFromUser) { - XmlWriterSettings settings = new XmlWriterSettings() { Indent = true }; + XmlWriterSettings settings = new() { Indent = true }; using TextWriter tw = File.CreateText(DataFilePath); using XmlWriter writer = XmlWriter.Create(tw, settings); - DatabaseXmlSerializer.WriteObject(writer, database); + DatabaseXmlSerializer.WriteObject(writer, UserList); + DatabaseXmlSerializer.WriteObject(writer, AddedThemeFromUser); } public Database LoadDatabaseData() @@ -41,5 +44,39 @@ namespace Notus_Persistance } } + public List LoadDefaultTheme() + { + if (File.Exists(DefaultThemePath)) + { + using (FileStream fileStream = File.OpenRead(DefaultThemePath)) + { + return DatabaseXmlSerializer.ReadObject(fileStream) is not List DefaultThemeList + ? throw new FileException("Failed to load Default Theme. The loaded object is null.") + : DefaultThemeList; + } + } + else + { + throw new FileException("No data file found."); + } + } + + public List LoadDefaultLogo() + { + if (File.Exists(DefaultLogoPath)) + { + using (FileStream fileStream = File.OpenRead(DefaultLogoPath)) + { + return DatabaseXmlSerializer.ReadObject(fileStream) is not List DefaultLogoList + ? throw new FileException("Failed to load Default Logo. The loaded object is null.") + : DefaultLogoList; + } + } + else + { + throw new FileException("No data file found."); + } + } + } } From dbac7bb6b6ad5827ccc8d2726992ab34108041d7 Mon Sep 17 00:00:00 2001 From: Matheo THIERRY Date: Tue, 30 May 2023 17:15:03 +0200 Subject: [PATCH 3/3] Fix hashCode to password for stub and for the reste --- notus/Biblioteque_de_Class/User.cs | 6 ++++++ notus/Notus_Console/Program.cs | 29 +++-------------------------- notus/Notus_Persistence/Stub.cs | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/notus/Biblioteque_de_Class/User.cs b/notus/Biblioteque_de_Class/User.cs index ddb793a..20b6d5c 100644 --- a/notus/Biblioteque_de_Class/User.cs +++ b/notus/Biblioteque_de_Class/User.cs @@ -14,6 +14,9 @@ namespace Biblioteque_de_Class private string Email { get; set; } [DataMember] private string Password { get; set; } + [DataMember] + private string Picture { get; set; } + [DataMember] private Theme Theme; [DataMember] private List NoteList; @@ -31,6 +34,7 @@ namespace Biblioteque_de_Class Username = username; Email = email; Password = password; + Picture = "defaultpicture.png"; NoteList = new List(); TagList = new List(); FavList = new List(); @@ -40,6 +44,7 @@ namespace Biblioteque_de_Class public string GetUsername() { return Username; } public string GetEmail() { return Email; } public string GetPassword() { return Password; } + public string GetPicture() { return Picture;} public Theme GetTheme() { return Theme; } public List GetNoteList() { return NoteList; } public List GetTagList() { return TagList; } @@ -51,6 +56,7 @@ namespace Biblioteque_de_Class public void SetUsername(string username) { Username = username; } public void SetEmail(string email) { Email = email; } public void SetPassword(string password) { Password = password; } + public void SetPicture(string picture) { Picture = picture; } public void SetTheme(Theme theme) { Theme = theme; } public void SetIsConnected(bool isConnected) { IsConnected = isConnected; } diff --git a/notus/Notus_Console/Program.cs b/notus/Notus_Console/Program.cs index 81da245..bdc9bbe 100644 --- a/notus/Notus_Console/Program.cs +++ b/notus/Notus_Console/Program.cs @@ -7,10 +7,6 @@ using System.Text; // load database PersistenceManager manager = new(new Stub()); Database db = manager.LoadDatabaseData(); -foreach(User user in db.GetUserList()) -{ - user.SetPassword(GetSHA256Hash(user.GetPassword())); -} // initialization zone============================================================================== @@ -139,22 +135,6 @@ List? choix_couleur() return colorList; } -static string GetSHA256Hash(string input) -{ - using (SHA256 sha256Hash = SHA256.Create()) - { - byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); - - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < bytes.Length; i++) - { - builder.Append(bytes[i].ToString("x2")); - } - - return builder.ToString(); - } -} - while (menu) { Console.WriteLine("\n|--------------------------------------|"); @@ -190,7 +170,6 @@ while (menu) Console.WriteLine("\nEntrez un password :"); string? password = Console.ReadLine(); if (password == null) { continue; } - password = GetSHA256Hash(password); try { u = db.GetUser(nom); @@ -202,7 +181,7 @@ while (menu) } if (!connection) { - if (Database.ComparePassword(u, password)) + if (Database.ComparePassword(u, password.GetHashCode().ToString())) { u.SetIsConnected(true); Console.WriteLine("\nConnection réussie !\n"); @@ -234,8 +213,7 @@ while (menu) } catch (AlreadyUsedException) { - password = GetSHA256Hash(password); - u = new User(nom, "", password); + u = new User(nom, "", password.GetHashCode().ToString()); db.AddUser(u); db.GetUser(nom).SetIsConnected(true); Console.WriteLine("\nConnection réussie !\n"); @@ -548,8 +526,7 @@ while (u.GetIsConnected()) Console.WriteLine("\nLe mot de passe doit contenir au moins 8 caractères.\n"); break; } - wantedNewPassword = GetSHA256Hash(wantedNewPassword); - if(wantedNewPassword == u.GetPassword()){ + if(wantedNewPassword.GetHashCode().ToString() == u.GetPassword()){ Console.WriteLine("\nLe nouveau mot de passe doit être différent de l'ancien.\n"); break; } diff --git a/notus/Notus_Persistence/Stub.cs b/notus/Notus_Persistence/Stub.cs index e32b77f..71e32ff 100644 --- a/notus/Notus_Persistence/Stub.cs +++ b/notus/Notus_Persistence/Stub.cs @@ -4,8 +4,25 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization.Json; using System.Text; +using System.Security.Cryptography; using System.Threading.Tasks; +static string GetSHA256Hash(string input) +{ + using (SHA256 sha256Hash = SHA256.Create()) + { + byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); + + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < bytes.Length; i++) + { + builder.Append(bytes[i].ToString("x2")); + } + + return builder.ToString(); + } +} + namespace Notus_Persistance { public class Stub : IManager @@ -59,6 +76,10 @@ namespace Notus_Persistance colorListHexaCode = new("000000,FFFFFF,000000".Split(',')); database.AddTheme(new Theme("Theme_3", colorListHexaCode)); + foreach (User user in database.GetUserList()) + { + user.SetPassword(GetSHA256Hash(user.GetPassword())); + } return database; }