From 2b9f74a1b28b55f1a42ccbe2312cb6d4782677f1 Mon Sep 17 00:00:00 2001 From: "CMS4390\\marantalu" Date: Tue, 5 Dec 2017 17:21:10 +0100 Subject: [PATCH] * Added Logger and Exception manager * Added autocreate database if not exists --- Step.Client/Step.Client.csproj | 221 +++ Step.Config/StartupConfigController.cs | 10 +- Step.Database/App.config | 11 +- Step.Database/Controllers/UsersController.cs | 4 +- Step.Database/DatabaseContext.cs | 8 + Step.Database/Step.Database.csproj | 6 +- Step.Model/MessageModel.cs | 15 + Step.Model/RoleModel.cs | 1 - Step.Model/RoleModel.cs.d.ts | 1 - Step.Model/Step.Model.csproj | 1 + Step.Model/UserModel.cs | 6 + Step.UI/ServerControlWindow.Designer.cs | 2 +- Step.UI/ServerControlWindow.cs | 17 +- Step.UI/Step.UI.csproj | 4 + Step.Utils/Constants.cs | 14 +- Step.Utils/ExceptionManager.cs | 85 +- Step.Utils/NLog.config | 2 +- Step.Utils/Step.Utils.csproj | 30 +- Step.Utils/StepLogger.cs | 33 +- Step.Utils/app.config | 9 + Step.Utils/docs/MySqlCommand.xml | 934 ++++++++++++ Step.Utils/docs/MySqlCommandBuilder.xml | 321 +++++ Step.Utils/docs/MySqlConnection.xml | 1248 +++++++++++++++++ .../docs/MySqlConnectionStringBuilder.xml | 55 + Step.Utils/docs/MySqlDataAdapter.xml | 801 +++++++++++ Step.Utils/docs/MySqlDataReader.xml | 452 ++++++ Step.Utils/docs/MySqlException.xml | 53 + Step.Utils/docs/MySqlHelper.xml | 53 + Step.Utils/docs/MySqlParameter.xml | 45 + Step.Utils/docs/MySqlParameterCollection.xml | 45 + Step.Utils/docs/MySqlTransaction.xml | 329 +++++ Step.Utils/packages.config | 1 + Step/App.config | 10 +- Step/App_Start/WebApiConfig.cs | 49 +- Step/Attributes/SignalRAuthorizeAttribute.cs | 51 +- Step/Attributes/WebApiAuthorizeAttribute.cs | 51 +- Step/Controllers/LoginController.cs | 24 +- Step/program.cs | 5 +- Step/wwwroot/src/@types/RoleModel.cs.d.ts | 1 - 39 files changed, 4892 insertions(+), 116 deletions(-) create mode 100644 Step.Client/Step.Client.csproj create mode 100644 Step.Model/MessageModel.cs create mode 100644 Step.Utils/app.config create mode 100644 Step.Utils/docs/MySqlCommand.xml create mode 100644 Step.Utils/docs/MySqlCommandBuilder.xml create mode 100644 Step.Utils/docs/MySqlConnection.xml create mode 100644 Step.Utils/docs/MySqlConnectionStringBuilder.xml create mode 100644 Step.Utils/docs/MySqlDataAdapter.xml create mode 100644 Step.Utils/docs/MySqlDataReader.xml create mode 100644 Step.Utils/docs/MySqlException.xml create mode 100644 Step.Utils/docs/MySqlHelper.xml create mode 100644 Step.Utils/docs/MySqlParameter.xml create mode 100644 Step.Utils/docs/MySqlParameterCollection.xml create mode 100644 Step.Utils/docs/MySqlTransaction.xml diff --git a/Step.Client/Step.Client.csproj b/Step.Client/Step.Client.csproj new file mode 100644 index 00000000..778e4a92 --- /dev/null +++ b/Step.Client/Step.Client.csproj @@ -0,0 +1,221 @@ + + + + + + + Debug + AnyCPU + {66FA29DB-925A-402B-A4C7-D3D780FB1BC3} + WinExe + CMS_Client + CMS_Client + v4.6.2 + 512 + true + + + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + ..\Step\bin\Client\x86\ + DEBUG;TRACE + full + x86 + prompt + MinimumRecommendedRules.ruleset + true + true + + + bin\x86\Release\ + TRACE + true + pdbonly + x86 + prompt + MinimumRecommendedRules.ruleset + true + + + Resources\CMS_Icon.ico + + + true + ..\Step\bin\Client\x64\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + + + + + ..\packages\Microsoft.WindowsAPICodePack-Core.1.1.0.2\lib\Microsoft.WindowsAPICodePack.dll + + + ..\packages\Microsoft.WindowsAPICodePack-Shell.1.1.0.0\lib\Microsoft.WindowsAPICodePack.Shell.dll + + + ..\packages\Microsoft.WindowsAPICodePack-Shell.1.1.0.0\lib\Microsoft.WindowsAPICodePack.ShellExtensions.dll + + + + + + + + + + + + + + + + + + + + + + + + LoadingForm.cs + + + + MainForm.cs + + + + + + NcForm.cs + + + + LoadingForm.cs + + + MainForm.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + NcForm.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + Designer + + + + + + + + + + + + + + + + + False + Microsoft .NET Framework 4.6.2 %28x86 e x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + + + + + + Questo progetto fa riferimento a uno o più pacchetti NuGet che non sono presenti in questo computer. Usare lo strumento di ripristino dei pacchetti NuGet per scaricarli. Per altre informazioni, vedere http://go.microsoft.com/fwlink/?LinkID=322105. Il file mancante è {0}. + + + + + + + + + + + + \ No newline at end of file diff --git a/Step.Config/StartupConfigController.cs b/Step.Config/StartupConfigController.cs index b927f0a3..5942a00f 100644 --- a/Step.Config/StartupConfigController.cs +++ b/Step.Config/StartupConfigController.cs @@ -1,6 +1,4 @@ -using System.Xml; -using System.Collections.Generic; -using System; +using System; using System.Xml.Schema; using System.Xml.Linq; using System.Linq; @@ -104,13 +102,11 @@ namespace Step.Config { if (e.Severity == XmlSeverityType.Warning) { - Console.Write("WARNING: "); - Console.WriteLine(e.Message); + ExceptionManager.Manage(ERROR_LEVEL.WARNING, e.Message); } else if (e.Severity == XmlSeverityType.Error) { - Console.Write("ERROR: "); - Console.WriteLine(e.Message); + ExceptionManager.Manage(ERROR_LEVEL.FATAL, e.Message); } } diff --git a/Step.Database/App.config b/Step.Database/App.config index efb35769..37bd4d82 100644 --- a/Step.Database/App.config +++ b/Step.Database/App.config @@ -5,13 +5,8 @@ - - - - - - - + + @@ -22,8 +17,6 @@ - - diff --git a/Step.Database/Controllers/UsersController.cs b/Step.Database/Controllers/UsersController.cs index eb3bb5f1..c5edca1f 100644 --- a/Step.Database/Controllers/UsersController.cs +++ b/Step.Database/Controllers/UsersController.cs @@ -42,7 +42,7 @@ namespace Step.Database.Controllers public UserModel Find(int id) { // Find user by Id with Role object included - return dbCtx.Users.Include("Role").Where(u => u.UserId == id).First(); + return dbCtx.Users.Include("Role").Where(u => u.UserId == id).FirstOrDefault(); } public UserModel Find(string username) @@ -59,7 +59,7 @@ namespace Step.Database.Controllers if (user != null) { // Check if the passwords match - if (Crypto.VerifyHashedPassword(user.Password, password) != true) + if (Crypto.VerifyHashedPassword(user.GetPassword(), password) != true) { return null; } diff --git a/Step.Database/DatabaseContext.cs b/Step.Database/DatabaseContext.cs index 6ecafeb0..fc0b38f0 100644 --- a/Step.Database/DatabaseContext.cs +++ b/Step.Database/DatabaseContext.cs @@ -20,5 +20,13 @@ namespace Step.Database : base("mySQLDatabaseConnection") { } + + public void TestDatabaseConnection() + { + using (DatabaseContext dbContext = new DatabaseContext()) + { + dbContext.Database.Exists(); + } + } } } diff --git a/Step.Database/Step.Database.csproj b/Step.Database/Step.Database.csproj index 57808a44..7567b6e4 100644 --- a/Step.Database/Step.Database.csproj +++ b/Step.Database/Step.Database.csproj @@ -71,9 +71,6 @@ - - - {3f5c2483-fc87-43ef-92a8-66ff7d0e440f} @@ -97,5 +94,8 @@ + + + \ No newline at end of file diff --git a/Step.Model/MessageModel.cs b/Step.Model/MessageModel.cs new file mode 100644 index 00000000..a87d9929 --- /dev/null +++ b/Step.Model/MessageModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Step.Model +{ + public class ErrorMessageModel + { + public string Title { get; set; } + public string Message { get; set; } + public int errorLevel { get; set; } + } +} diff --git a/Step.Model/RoleModel.cs b/Step.Model/RoleModel.cs index b17b88c9..0cd94496 100644 --- a/Step.Model/RoleModel.cs +++ b/Step.Model/RoleModel.cs @@ -18,6 +18,5 @@ namespace Step.Model public string Name { get; set; } [Column("level")] public int Level { get; set; } - public List Users { get; set; } } } diff --git a/Step.Model/RoleModel.cs.d.ts b/Step.Model/RoleModel.cs.d.ts index 8082b3c7..6c9eab71 100644 --- a/Step.Model/RoleModel.cs.d.ts +++ b/Step.Model/RoleModel.cs.d.ts @@ -3,6 +3,5 @@ declare module server { roleId: number; name: string; level: number; - users: server.UserModel[]; } } diff --git a/Step.Model/Step.Model.csproj b/Step.Model/Step.Model.csproj index c6d01618..f0912709 100644 --- a/Step.Model/Step.Model.csproj +++ b/Step.Model/Step.Model.csproj @@ -57,6 +57,7 @@ + DtsGenerator RoleModel.cs.d.ts diff --git a/Step.Model/UserModel.cs b/Step.Model/UserModel.cs index 4081c158..b21feafc 100644 --- a/Step.Model/UserModel.cs +++ b/Step.Model/UserModel.cs @@ -29,5 +29,11 @@ namespace Step.Model public int RoleId { get; set; } [ForeignKey("RoleId")] public RoleModel Role { get; set; } + + + public string GetPassword() + { + return Password; + } } } diff --git a/Step.UI/ServerControlWindow.Designer.cs b/Step.UI/ServerControlWindow.Designer.cs index d39ba0c5..e7c4ee5c 100644 --- a/Step.UI/ServerControlWindow.Designer.cs +++ b/Step.UI/ServerControlWindow.Designer.cs @@ -71,7 +71,7 @@ this.StepNotifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("StepNotifyIcon.Icon"))); this.StepNotifyIcon.Text = "CMS Server"; this.StepNotifyIcon.Visible = true; - this.StepNotifyIcon.Click += new System.EventHandler(this.notifyIcon1_Click); + this.StepNotifyIcon.Click += new System.EventHandler(this.NotifyIcon_Click); // // NotifyIconMenu // diff --git a/Step.UI/ServerControlWindow.cs b/Step.UI/ServerControlWindow.cs index 1770de8e..7d4b7fe5 100644 --- a/Step.UI/ServerControlWindow.cs +++ b/Step.UI/ServerControlWindow.cs @@ -6,8 +6,10 @@ using System.Diagnostics; using System.IO; using System.Threading; using System.Windows.Forms; +using Step.Model; using TeamDev.SDK.MVVM; using static Step.Config.StartupConfig; +using static Step.Utils.Constants; namespace Step.UI { @@ -18,7 +20,18 @@ namespace Step.UI public ServerControlWindow() { InitializeComponent(); + HomePageUI = "http://localhost:" + ServerConfig.ServerPort.ToString() + "/index.html"; + + MessageServices.Current.Subscribe(SEND_MESSAGE, (a, b) => + { + // Cast object to ErrorMessageModel + ErrorMessageModel message = (ErrorMessageModel)a; + // If error has a fatal level, icon must be error + message.errorLevel = message.errorLevel > (int)ERROR_LEVEL.ERROR ? (int)ERROR_LEVEL.ERROR : message.errorLevel; + // Open BalloonTip with data + StepNotifyIcon.ShowBalloonTip(1000, message.Title, message.Message, (ToolTipIcon)message.errorLevel); + }); } private static ServerControlWindow ctrlwindow = null; @@ -59,7 +72,7 @@ namespace Step.UI if (!_closing) { e.Cancel = true; - this.Hide(); + Hide(); } } @@ -75,7 +88,7 @@ namespace Step.UI MessageServices.Current.Publish("StopServer"); } - private void notifyIcon1_Click(object sender, EventArgs e) + private void NotifyIcon_Click(object sender, EventArgs e) { this.Show(); this.TopMost = true; diff --git a/Step.UI/Step.UI.csproj b/Step.UI/Step.UI.csproj index 29e525de..0354322c 100644 --- a/Step.UI/Step.UI.csproj +++ b/Step.UI/Step.UI.csproj @@ -86,6 +86,10 @@ {631375DD-06D3-49BB-8130-D9DDB34C429D} Step.Model + + {cbeb631b-abfa-4042-9779-c0060b0dfefe} + Step.Utils + \ No newline at end of file diff --git a/Step.Utils/Constants.cs b/Step.Utils/Constants.cs index f39d735d..7cf4f6b4 100644 --- a/Step.Utils/Constants.cs +++ b/Step.Utils/Constants.cs @@ -8,6 +8,14 @@ WRITE } + public enum ERROR_LEVEL + { + INFO = 1, + WARNING = 2, + ERROR = 3, + FATAL = 4 + } + // Token fields Keys public const string ROLE_LEVEL_KEY = "roleLevel"; public const string USERNAME_KEY = "username"; @@ -19,7 +27,7 @@ public const string AREAS_CONFIG_KEY = "areasConfig"; // Step Areas - public static class AREAS + public class AREAS { public const string PRODUCTION_KEY = "production"; public const string TOOLING_KEY = "tooling"; @@ -33,5 +41,9 @@ // Filenames public const string STARTUP_CONFIG_SCHEMA_PATH = "startupValidator.xsd"; public const string STARTUP_CONFIG_PATH = "startupConfig.xml"; + + // Message Headers + public const string STOP_SERVER = "StopServer"; + public const string SEND_MESSAGE = "SendMessage"; } } diff --git a/Step.Utils/ExceptionManager.cs b/Step.Utils/ExceptionManager.cs index d8cb78c9..0d4426c3 100644 --- a/Step.Utils/ExceptionManager.cs +++ b/Step.Utils/ExceptionManager.cs @@ -1,4 +1,7 @@ using System; +using Step.Model; +using TeamDev.SDK.MVVM; +using static Step.Utils.Constants; namespace Step.Utils { @@ -7,16 +10,86 @@ namespace Step.Utils public static void Manage(Exception ex) { if (ex is System.IO.FileNotFoundException) - { + { // File not found - StepLogger.FatalException(ex); - Environment.Exit(0); + LogAndSendMessage(CreateMessageModel("File not found!", ex.Message, ERROR_LEVEL.FATAL)); + // Close the application + Environment.Exit(1); + } + else if(ex is MySql.Data.MySqlClient.MySqlException) + { + // MySql Error // TODO CHECK NUMBERS + // Log + LogAndSendMessage(CreateMessageModel("Database Error!", ex.Message, ERROR_LEVEL.FATAL)); + // Close the application } else - { - // Default case - StepLogger.FatalException(ex); + { + if (ex.InnerException != null) + { + Manage(ex.InnerException); // TODO test + } + else + { + // Default case + LogAndSendMessage(CreateMessageModel("Generic Error!", ex.Message, ERROR_LEVEL.FATAL)); + // Close the application + Environment.Exit(1); + } } } + + public static void Manage(ERROR_LEVEL errorLevel, string message) + { + switch (errorLevel) + { + case ERROR_LEVEL.INFO: + { + LogAndSendMessage(CreateMessageModel("Info", message, ERROR_LEVEL.INFO)); + } + break; + case ERROR_LEVEL.WARNING: + { + LogAndSendMessage(CreateMessageModel("Warning", message, ERROR_LEVEL.WARNING)); + } + break; + case ERROR_LEVEL.ERROR: + { + LogAndSendMessage(CreateMessageModel("Error", message, ERROR_LEVEL.ERROR)); + } + break; + case ERROR_LEVEL.FATAL: + { + LogAndSendMessage(CreateMessageModel("Fatal Error", message, ERROR_LEVEL.FATAL)); + // Close the application + Environment.Exit(1); + }break; + default: + { + LogAndSendMessage(CreateMessageModel("Generic Error", message, ERROR_LEVEL.FATAL)); + // Close the application + Environment.Exit(1); + } + break; + } + } + + private static void LogAndSendMessage(ErrorMessageModel error) + { + // Log + StepLogger.LogMessage(error.Message, (ERROR_LEVEL)error.errorLevel); + // Notify user + MessageServices.Current.Publish(SEND_MESSAGE, null, error); + } + + private static ErrorMessageModel CreateMessageModel(string title, string message, ERROR_LEVEL level) + { + return new ErrorMessageModel() + { + Title = title, + Message = message, + errorLevel = (int)level + }; + } } } diff --git a/Step.Utils/NLog.config b/Step.Utils/NLog.config index d4f2a058..ee84cba7 100644 --- a/Step.Utils/NLog.config +++ b/Step.Utils/NLog.config @@ -21,7 +21,7 @@ See https://github.com/nlog/NLog/wiki/Targets for possible targets. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers. --> - +