-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from davewalker5/add-user-management
Added user management app
- Loading branch information
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters