diff --git a/MCTG/ConsoleApp/Menu/AddRecipeMenu.cs b/MCTG/ConsoleApp/Menu/AddRecipeMenu.cs index 22d218c..c6ef6f2 100644 --- a/MCTG/ConsoleApp/Menu/AddRecipeMenu.cs +++ b/MCTG/ConsoleApp/Menu/AddRecipeMenu.cs @@ -45,8 +45,7 @@ namespace ConsoleApp.Menu preparationSteps: steps.ToArray() ); - - masterMgr.DataMgr.Data[nameof(Recipe)].Add(recipe); + masterMgr.AddRecipe(recipe); return null; } } diff --git a/MCTG/ConsoleApp/Menu/AddUserMenu.cs b/MCTG/ConsoleApp/Menu/AddUserMenu.cs new file mode 100644 index 0000000..57ca2aa --- /dev/null +++ b/MCTG/ConsoleApp/Menu/AddUserMenu.cs @@ -0,0 +1,44 @@ +using ConsoleApp.Menu.Core; +using Model; +using Model.Managers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal class AddUserMenu : Entry + { + MasterManager masterMgr; + + public AddUserMenu(MasterManager masterManager) + : base("Add Manager", + new Entry.EntryStep("Mail: ", typeof(string)), + new Entry.EntryStep("Name: ", typeof(string)), + new Entry.EntryStep("Surname: ", typeof(string)), + new Entry.EntryStep("Password: ", typeof(string), true)) + { + masterMgr = masterManager; + } + + public override IMenu? Return() + { + string mail = _selectList[0].Item.Input; + string name = _selectList[1].Item.Input; + string surname = _selectList[2].Item.Input; + string passwd = _selectList[3].Item.Input; + + User user = new User( + name: name, + surname: surname, + mail: mail, + password: passwd + ); + + masterMgr.Register(user); + return null; + } + } +} diff --git a/MCTG/ConsoleApp/Menu/ExportRecipeMenu.cs b/MCTG/ConsoleApp/Menu/ExportRecipeMenu.cs new file mode 100644 index 0000000..54c543f --- /dev/null +++ b/MCTG/ConsoleApp/Menu/ExportRecipeMenu.cs @@ -0,0 +1,40 @@ +using ConsoleApp.Menu.Core; +using Model.Managers; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal class ExportRecipeMenu : SearcherRecipe + { + public ExportRecipeMenu(MasterManager masterManager) + : base(masterManager) + { + } + + public override IMenu? Return() + { + if (CurrentSelected is null) + throw new ArgumentNullException(); + + Recipe recipe = CurrentSelected; + + try + { + _masterMgr.DataMgr.Export(recipe, ""); + } + catch (ArgumentNullException e) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("error: " + e.Message); + Console.ResetColor(); + return this; + } + return null; + } + } +} diff --git a/MCTG/ConsoleApp/Menu/ImportRecipeMenu.cs b/MCTG/ConsoleApp/Menu/ImportRecipeMenu.cs new file mode 100644 index 0000000..d1f402c --- /dev/null +++ b/MCTG/ConsoleApp/Menu/ImportRecipeMenu.cs @@ -0,0 +1,40 @@ +using ConsoleApp.Menu.Core; +using Model; +using Model.Managers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal class ImportRecipeMenu : Entry + { + MasterManager masterMgr; + + public ImportRecipeMenu(MasterManager masterManager) + : base("Import recipe", + new Entry.EntryStep("Path file: ", typeof(string))) + { + masterMgr = masterManager; + } + + public override IMenu? Return() + { + string path = _selectList[0].Item.Input; + try + { + masterMgr.DataMgr.Import(path); + } + catch(ArgumentNullException e) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("error: " + e.Message); + Console.ResetColor(); + return this; + } + return null; + } + } +} diff --git a/MCTG/ConsoleApp/Menu/MainMenu.cs b/MCTG/ConsoleApp/Menu/MainMenu.cs index bd7c103..9514c5d 100644 --- a/MCTG/ConsoleApp/Menu/MainMenu.cs +++ b/MCTG/ConsoleApp/Menu/MainMenu.cs @@ -30,6 +30,13 @@ namespace ConsoleApp.Menu _allSelectors.Add( new Selector(new AddRecipeMenu(masterManager), "Add recipe")); + _allSelectors.Add( + new Selector(new AddUserMenu(masterManager), "Add user")); + + _allSelectors.Add( + new Selector(new ImportRecipeMenu(masterManager), "Import recipe")); + _allSelectors.Add( + new Selector(new ExportRecipeMenu(masterManager), "Export recipe")); } protected override List> SearchInSelection() diff --git a/MCTG/DataPersistence/DataContractXML.cs b/MCTG/DataPersistence/DataContractXML.cs index 3cd4f58..3bd78f7 100644 --- a/MCTG/DataPersistence/DataContractXML.cs +++ b/MCTG/DataPersistence/DataContractXML.cs @@ -48,7 +48,8 @@ namespace DataPersistence typeof(Review), typeof(User), typeof(Ingredient), - typeof(Quantity) + typeof(Quantity), + typeof(PasswordManager) }, PreserveObjectReferences = true }; diff --git a/MCTG/Model/User/IPasswordManager.cs b/MCTG/Model/User/IPasswordManager.cs index a87d960..5ca0732 100644 --- a/MCTG/Model/User/IPasswordManager.cs +++ b/MCTG/Model/User/IPasswordManager.cs @@ -8,8 +8,8 @@ namespace Model { public interface IPasswordManager { - public int HashPassword(string password); - public bool VerifyPassword(int hashedPassword,string password); + public string HashPassword(string password); + public bool VerifyPassword(string hashedPassword,string password); } } diff --git a/MCTG/Model/User/PasswordManager.cs b/MCTG/Model/User/PasswordManager.cs index d762e42..2aad1a2 100644 --- a/MCTG/Model/User/PasswordManager.cs +++ b/MCTG/Model/User/PasswordManager.cs @@ -1,27 +1,34 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace Model { + [DataContract(Name = "passmgr")] public class PasswordManager : IPasswordManager { - - public int HashPassword(string password) + public string HashPassword(string password) { - int hashedPassword = password.GetHashCode(); - return hashedPassword; + byte[] data; + using (SHA256 sha256Hash = SHA256.Create()) + data = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(password)); + + var sb = new StringBuilder(); + foreach (byte b in data) sb.Append(b.ToString("x2")); + + return sb.ToString(); } - public bool VerifyPassword(int hashedPassword, string passwordEntered ) + public bool VerifyPassword(string hashedPassword, string passwordEntered) { - int passwordEnteredHashed = passwordEntered.GetHashCode(); - if ( passwordEnteredHashed == hashedPassword) - return true; - else return false; + string hashedInput = HashPassword(passwordEntered); + StringComparer strcmp = StringComparer.OrdinalIgnoreCase; + return strcmp.Compare(hashedPassword, hashedInput) == 0; } } } diff --git a/MCTG/Model/User/User.cs b/MCTG/Model/User/User.cs index 27243f9..57e13e6 100644 --- a/MCTG/Model/User/User.cs +++ b/MCTG/Model/User/User.cs @@ -25,7 +25,7 @@ namespace Model [DataMember] private string surname=""; [DataMember] private string mail = ""; [DataMember] private string picture = ""; - [DataMember] private int password ; + [DataMember] private string password = ""; [DataMember] private List priorities; public event PropertyChangedEventHandler? PropertyChanged; @@ -81,7 +81,7 @@ namespace Model } } - public int Password + public string Password { get => password; set => password = value; @@ -141,6 +141,7 @@ namespace Model return $"{Name} {Surname}"; } + [DataMember(Name = "passmgr")] public IPasswordManager psswMgr { get; private set; } @@ -156,7 +157,7 @@ namespace Model /// The user needs an email to login. /// The password of the new user. /// The password manager to manage the user password. - public User(string name, string surname, string mail, string password, IPasswordManager passwordManager ) + public User(string name, string surname, string mail, string password, IPasswordManager passwordManager) { Name = name; Surname = surname;