Skip to content

Commit

Permalink
Merge pull request #4 from davewalker5/add-user-management
Browse files Browse the repository at this point in the history
Added user management app
  • Loading branch information
davewalker5 committed Apr 13, 2020
2 parents fb9544f + f74ba1a commit d9bf31b
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/FlightRecorder.Users/CommandParser.cs
Original file line number Diff line number Diff line change
@@ -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; }

/// <summary>
/// Validate the command line, extracting the operaiton to be performed
/// and its arguments
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
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<OperationType>(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;
}
}
}
21 changes: 21 additions & 0 deletions src/FlightRecorder.Users/FlightRecorder.Users.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\FlightRecorder.BusinessLogic\FlightRecorder.BusinessLogic.csproj" />
<ProjectReference Include="..\FlightRecorder.Entities\FlightRecorder.Entities.csproj" />
<ProjectReference Include="..\FlightRecorder.Data\FlightRecorder.Data.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions src/FlightRecorder.Users/OperationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FlightRecorder.Users
{
public enum OperationType
{
add = 0,
setpassword = 1,
delete = 2
}
}
53 changes: 53 additions & 0 deletions src/FlightRecorder.Users/Program.cs
Original file line number Diff line number Diff line change
@@ -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]");
}
}
}
}
5 changes: 5 additions & 0 deletions src/FlightRecorder.Users/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"FlightRecorderDB": "Data Source=/Users/dave/Dropbox/Documents/Aviation/FlightRecorder/Data/flightrecorder.db"
}
}
6 changes: 6 additions & 0 deletions src/FlightRecorderDb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit d9bf31b

Please sign in to comment.