diff --git a/src/FlightRecorder.Users/CommandParser.cs b/src/FlightRecorder.Users/CommandParser.cs new file mode 100644 index 0000000..1d25e11 --- /dev/null +++ b/src/FlightRecorder.Users/CommandParser.cs @@ -0,0 +1,51 @@ +using System; + +namespace FlightRecorder.Users +{ + public class CommandParser + { + // The index into this array is one of the values from the OperationType + // enumeration, mapping the operation to the required argument count + private readonly int[] _requiredArgumentCount = { 3, 3, 2 }; + + public OperationType Operation { get; set; } + public string UserName { get; set; } + public string Password { get; set; } + + /// + /// Validate the command line, extracting the operaiton to be performed + /// and its arguments + /// + /// + /// + public bool ValidateCommandLine(string[] args) + { + bool validationResult = false; + + if (args.Length > 0) + { + // Attempt to parse out the operation type from the first argument + if (Enum.TryParse(args[0], out OperationType result)) + { + // Check there are sufficient arguments for this operation + Operation = result; + int requiredArgumentCount = _requiredArgumentCount[(int)result]; + + // All is OK at this point if the argument count is correct + validationResult = (args.Length == requiredArgumentCount); + if (validationResult) + { + // Extract the arguments + UserName = args[1]; + if (result != OperationType.delete) + { + Password = args[2]; + } + } + } + } + + return validationResult; + } + } +} diff --git a/src/FlightRecorder.Users/FlightRecorder.Users.csproj b/src/FlightRecorder.Users/FlightRecorder.Users.csproj new file mode 100644 index 0000000..2828ad0 --- /dev/null +++ b/src/FlightRecorder.Users/FlightRecorder.Users.csproj @@ -0,0 +1,21 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + + + + + PreserveNewest + + + diff --git a/src/FlightRecorder.Users/OperationType.cs b/src/FlightRecorder.Users/OperationType.cs new file mode 100644 index 0000000..d463c11 --- /dev/null +++ b/src/FlightRecorder.Users/OperationType.cs @@ -0,0 +1,9 @@ +namespace FlightRecorder.Users +{ + public enum OperationType + { + add = 0, + setpassword = 1, + delete = 2 + } +} diff --git a/src/FlightRecorder.Users/Program.cs b/src/FlightRecorder.Users/Program.cs new file mode 100644 index 0000000..d2b450e --- /dev/null +++ b/src/FlightRecorder.Users/Program.cs @@ -0,0 +1,53 @@ +using System; +using FlightRecorder.BusinessLogic.Factory; +using FlightRecorder.Data; + +namespace FlightRecorder.Users +{ + class Program + { + static void Main(string[] args) + { + string version = typeof(Program).Assembly.GetName().Version.ToString(); + Console.WriteLine($"Flight Recorder User Management {version}"); + + CommandParser parser = new CommandParser(); + if (parser.ValidateCommandLine(args)) + { + FlightRecorderDbContext context = new FlightRecorderDbContextFactory().CreateDbContext(null); + FlightRecorderFactory factory = new FlightRecorderFactory(context); + + try + { + switch (parser.Operation) + { + case OperationType.add: + factory.Users.AddUser(parser.UserName, parser.Password); + Console.WriteLine($"Added user {parser.UserName}"); + break; + case OperationType.setpassword: + factory.Users.SetPassword(parser.UserName, parser.Password); + Console.WriteLine($"Set password for user {parser.UserName}"); + break; + case OperationType.delete: + factory.Users.DeleteUser(parser.UserName); + Console.WriteLine($"Deleted user {parser.UserName}"); + break; + default: + break; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error : {ex.Message}"); + } + } + else + { + string executable = AppDomain.CurrentDomain.FriendlyName; + string[] commands = Enum.GetNames(typeof(OperationType)); + Console.WriteLine($"Usage : {executable} {string.Join('|', commands)} username [password]"); + } + } + } +} diff --git a/src/FlightRecorder.Users/appsettings.json b/src/FlightRecorder.Users/appsettings.json new file mode 100644 index 0000000..bbecee7 --- /dev/null +++ b/src/FlightRecorder.Users/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "FlightRecorderDB": "Data Source=/Users/dave/Dropbox/Documents/Aviation/FlightRecorder/Data/flightrecorder.db" + } +} diff --git a/src/FlightRecorderDb.sln b/src/FlightRecorderDb.sln index 6911ff6..40a3af2 100644 --- a/src/FlightRecorderDb.sln +++ b/src/FlightRecorderDb.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlightRecorder.DataExchange EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlightRecorder.Migrations", "FlightRecorder.Migrations\FlightRecorder.Migrations.csproj", "{C900A267-3486-430A-ABB5-1D25292BE450}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlightRecorder.Users", "FlightRecorder.Users\FlightRecorder.Users.csproj", "{B17F7264-1B0D-48AA-829F-83E0E0A13670}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -43,5 +45,9 @@ Global {C900A267-3486-430A-ABB5-1D25292BE450}.Debug|Any CPU.Build.0 = Debug|Any CPU {C900A267-3486-430A-ABB5-1D25292BE450}.Release|Any CPU.ActiveCfg = Release|Any CPU {C900A267-3486-430A-ABB5-1D25292BE450}.Release|Any CPU.Build.0 = Release|Any CPU + {B17F7264-1B0D-48AA-829F-83E0E0A13670}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B17F7264-1B0D-48AA-829F-83E0E0A13670}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B17F7264-1B0D-48AA-829F-83E0E0A13670}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B17F7264-1B0D-48AA-829F-83E0E0A13670}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal