Skip to content

Commit

Permalink
Merge pull request #86 from CUMGroup/account-timeout
Browse files Browse the repository at this point in the history
Account timeout
  • Loading branch information
jas20202 authored Mar 31, 2024
2 parents 4ee2af7 + 003b544 commit 4f12a77
Show file tree
Hide file tree
Showing 28 changed files with 356 additions and 89 deletions.
5 changes: 5 additions & 0 deletions PWManager.Application/Context/ICancelEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace PWManager.Application.Context;

public interface ICancelEnvironment {
bool CancelableState { get; set; }
}
2 changes: 2 additions & 0 deletions PWManager.Application/Context/IUserEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface IUserEnvironment {
public User? CurrentUser { get; set; }

public Group? CurrentGroup { get; set; }

public Settings? UserSettings { get; set; }
}
1 change: 1 addition & 0 deletions PWManager.Application/Exceptions/MessageStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class MessageStrings {

// GROUP Controller
public const string NO_GROUPS_FOUND = "There are no groups in your database. Something is really wrong!";
public const string NO_SETTINGS_IN_ENVIRONMENT = "There are not settings in your environment. Something is really wrong!";
// ----------------------------------------

// SETTINGS Controller
Expand Down
2 changes: 2 additions & 0 deletions PWManager.Application/Services/Interfaces/IClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public interface IClipboard {

event Action<string> OnClipboardUpdated;

void WriteClipboard(string val);

void ClearClipboard();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace PWManager.Application.Services.Interfaces;
public interface ISettingsService {

void ChangePasswordGenerationCriteria(PasswordGeneratorCriteria generatorCriteria);
void ChangeClipboardTimeoutSetting(ClipboardTimeoutSetting clipboardTimeoutSetting);
void ChangeClipboardTimeoutSetting(TimeSpan timeout);
void ChangeAccountTimeoutSetting(TimeSpan timeout);
void ChangeMainGroupSetting(MainGroupSetting mainGroupSetting);
Settings GetSettings();
}
24 changes: 16 additions & 8 deletions PWManager.Application/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,38 @@ namespace PWManager.Application.Services;
public class SettingsService : ISettingsService {

private readonly ISettingsRepository _settingsRepository;

public SettingsService(ISettingsRepository settingsRepository) {
private readonly IUserEnvironment _userEnv;

public SettingsService(ISettingsRepository settingsRepository, IUserEnvironment userEnv) {
_settingsRepository = settingsRepository;
_userEnv = userEnv;
}

public void ChangeClipboardTimeoutSetting(ClipboardTimeoutSetting clipboardTimeout) {
var settings = _settingsRepository.GetSettings();
settings.ClipboardTimeout = clipboardTimeout;
public void ChangeClipboardTimeoutSetting(TimeSpan timeout) {
var settings = GetSettings();
settings.Timeout = new TimeoutSettings(timeout, settings.Timeout.AccountTimeOutDuration);
_settingsRepository.UpdateSettings(settings);
}

public void ChangeAccountTimeoutSetting(TimeSpan timeout) {
var settings = GetSettings();
settings.Timeout = new TimeoutSettings(settings.Timeout.ClipboardTimeOutDuration, timeout);
_settingsRepository.UpdateSettings(settings);
}

public void ChangeMainGroupSetting(MainGroupSetting mainGroup) {
var settings = _settingsRepository.GetSettings();
var settings = GetSettings();
settings.MainGroup = mainGroup;
_settingsRepository.UpdateSettings(settings);
}

public void ChangePasswordGenerationCriteria(PasswordGeneratorCriteria generatorCriteria) {
var settings = _settingsRepository.GetSettings();
var settings = GetSettings();
settings.PwGenCriteria = generatorCriteria;
_settingsRepository.UpdateSettings(settings);
}

public Settings GetSettings() {
return _settingsRepository.GetSettings();
return _userEnv.UserSettings ??= _settingsRepository.GetSettings();
}
}
58 changes: 58 additions & 0 deletions PWManager.CLI/Abstractions/ConsoleInteraction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Sharprompt;

namespace PWManager.CLI.Abstractions;

public static class ConsoleInteraction {

public static event Action OnConsoleInput;

public static T Input<T>(string prompt) {
OnConsoleInput();
var res = Prompt.Input<T>(prompt);
OnConsoleInput();
return res;
}

public static bool Confirm(string prompt) {
OnConsoleInput();
var res = Prompt.Confirm(prompt);
OnConsoleInput();
return res;
}

public static T Select<T>(string message, IEnumerable<T>? items = null, object? defaultValue = null) where T : notnull {
OnConsoleInput();
var res = Prompt.Select<T>(message, items, defaultValue: defaultValue);
OnConsoleInput();
return res;
}

public static IEnumerable<T> MultiSelect<T>(string message, IEnumerable<T>? items = null, IEnumerable<T>? defaultValues = null)
where T : notnull {
OnConsoleInput();
var res = Prompt.MultiSelect<T>(message, items, defaultValues: defaultValues);
OnConsoleInput();
return res;
}

public static string Password(string message) {
OnConsoleInput();
var res = Prompt.Password(message);
OnConsoleInput();
return res;
}

public static string? ReadLine() {
OnConsoleInput();
var res = Console.ReadLine();
OnConsoleInput();
return res;
}

public static void ResetConsole() {
Console.CursorVisible = true;
Console.ResetColor();
Console.SetCursorPosition(0, Console.CursorTop);
Console.WriteLine();
}
}
36 changes: 22 additions & 14 deletions PWManager.CLI/Abstractions/PromptHelper.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
using PWManager.Application.Services.Interfaces;
using Sharprompt;

namespace PWManager.CLI.Abstractions;
namespace PWManager.CLI.Abstractions;

public static class PromptHelper {
public static string GetInput(string prompt) {
var input = Prompt.Input<string>(prompt);
var input = ConsoleInteraction.Input<string>(prompt);
while (string.IsNullOrWhiteSpace(input)) {
Console.WriteLine(UIstrings.EMPTY_INPUT);
input = Prompt.Input<string>(prompt);
input = ConsoleInteraction.Input<string>(prompt);
}

return input;
}

public static int GetInputGreaterThan(string message, int greaterThan) {
var val = ConsoleInteraction.Input<int>(message);

while(val <= greaterThan) {
PrintColoredText(ConsoleColor.Red, UIstrings.ValueCannotBeLessThan(greaterThan + 1));
val = ConsoleInteraction.Input<int>(message);
}

return val;
}

public static bool InputPassword(Func<string, bool> passwordValidator) {
var tryCount = 0;
var pass = Prompt.Password(UIstrings.ENTER_PASSWORD);
var pass = ConsoleInteraction.Password(UIstrings.ENTER_PASSWORD);
var succ = passwordValidator(pass);

while (!succ && tryCount < 3) {
++tryCount;
Console.WriteLine($"{UIstrings.INVALID_PASSWORD} {UIstrings.TRY_AGAIN} ({tryCount}/3)");
pass = Prompt.Password(UIstrings.ENTER_PASSWORD);
pass = ConsoleInteraction.Password(UIstrings.ENTER_PASSWORD);
succ = passwordValidator(pass);
}

Expand All @@ -40,18 +48,18 @@ public static string InputNewPassword() {
}

public static string? TryPasswordInput() {
var password = Prompt.Password(UIstrings.ENTER_PASSWORD);
var password = ConsoleInteraction.Password(UIstrings.ENTER_PASSWORD);
while (string.IsNullOrWhiteSpace(password) || password.Length < 8) {
Console.WriteLine(UIstrings.PASSWORD_TOO_SHORT);
password = Prompt.Password(UIstrings.ENTER_PASSWORD);
password = ConsoleInteraction.Password(UIstrings.ENTER_PASSWORD);
}

var repeat = Prompt.Password(UIstrings.REPEAT_PASSWORD);
var repeat = ConsoleInteraction.Password(UIstrings.REPEAT_PASSWORD);
var tryCount = 0;
while (!password.Equals(repeat) && tryCount < 3) {
++tryCount;
Console.WriteLine($"{UIstrings.REPEAT_PASSWORD_DOES_NOT_MATCH} {UIstrings.TRY_AGAIN} ({tryCount}/3)");
repeat = Prompt.Password(UIstrings.REPEAT_PASSWORD);
repeat = ConsoleInteraction.Password(UIstrings.REPEAT_PASSWORD);
}

return password.Equals(repeat) ? password : null;
Expand Down Expand Up @@ -95,12 +103,12 @@ public static void PrintColoredText(ConsoleColor color, string text) {
}

public static bool ConfirmDeletion(string identifier, Func<string, bool> passwordValidator) {
var areYouSure = Prompt.Confirm(UIstrings.DeletionOf(identifier));
var areYouSure = ConsoleInteraction.Confirm(UIstrings.DeletionOf(identifier));
if (!areYouSure) {
return false;
}

var passwordTest = Prompt.Password(UIstrings.ENTER_MASTER_PASSWORD);
var passwordTest = ConsoleInteraction.Password(UIstrings.ENTER_MASTER_PASSWORD);
var passwordCorrect = passwordValidator(passwordTest);

if (passwordCorrect) {
Expand Down
13 changes: 10 additions & 3 deletions PWManager.CLI/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ internal class ConsoleRunner : IRunner {
private readonly IServiceProvider _provider;
private readonly ICliEnvironment _environment;
private readonly IDebugEnvironment _debugInfo;
private readonly ICancelEnvironment _cancelEnvironment;
private readonly CommandParser _commandParser;

public ConsoleRunner(IServiceProvider provider, ICliEnvironment environment, IDebugEnvironment debugInfo) {
public ConsoleRunner(IServiceProvider provider, ICliEnvironment environment, IDebugEnvironment debugInfo, ICancelEnvironment cancelEnvironment) {
_provider = provider;
_environment = environment;
_debugInfo = debugInfo;
_cancelEnvironment = cancelEnvironment;
_commandParser = new CommandParser();
}

Expand All @@ -30,7 +32,7 @@ public void Run(string[] args) {
var exitCondition = ExecuteCommand(args);
while (exitCondition is ExitCondition.CONTINUE) {
_environment.WritePrompt();
var input = Console.ReadLine();
var input = ConsoleInteraction.ReadLine();
if (input is null) {
continue;
}
Expand Down Expand Up @@ -62,7 +64,9 @@ private ExitCondition ExecuteCommand(string input) {
return ExecuteCommand(_commandParser.ParseCommandWithArguments(input).ToArray());
}
private ExitCondition ExecuteCommand(string[] args) {
_cancelEnvironment.CancelableState = false;
if (args.Length <= 0 && _environment.RunningSession) {
_cancelEnvironment.CancelableState = true;
return ExitCondition.CONTINUE;
}
var cmd = args.Length <= 0 ? AvailableCommands.HELP : _commandParser.ParseCommand(args[0]);
Expand All @@ -73,7 +77,10 @@ private ExitCondition ExecuteCommand(string[] args) {

var controller = (IController) _provider.GetRequiredService(controllerType);
var commandArgs = args.Length <= 1 ? Array.Empty<string>() : args[1..];
return controller.Handle(commandArgs);

var exit = controller.Handle(commandArgs);
_cancelEnvironment.CancelableState = true;
return exit;
}

public void MapCommand<TCommand>(AvailableCommands command) {
Expand Down
8 changes: 3 additions & 5 deletions PWManager.CLI/Controllers/GetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using PWManager.CLI.Attributes;
using PWManager.CLI.Enums;
using PWManager.CLI.Interfaces;
using Sharprompt;
using System.IO;

namespace PWManager.CLI.Controllers;

Expand Down Expand Up @@ -86,17 +84,17 @@ private bool HandleDeletion(string identifier) {
}

private bool ConfirmRegeneration(string identifier) {
return Prompt.Confirm(UIstrings.ConfirmPwRegenerationOf(identifier));
return ConsoleInteraction.Confirm(UIstrings.ConfirmPwRegenerationOf(identifier));
}

private AccountAction GetAccountAction() {
return Prompt.Select<AccountAction>(UIstrings.SELECT_ACTION);
return ConsoleInteraction.Select<AccountAction>(UIstrings.SELECT_ACTION);
}

private string? GetAccountSelection() {
var names = _accountService.GetCurrentAccountNames();
if (names.Any()) {
return Prompt.Select(UIstrings.SEARCH_ACCOUNT, names);
return ConsoleInteraction.Select(UIstrings.SEARCH_ACCOUNT, names);
}
PromptHelper.PrintColoredText(ConsoleColor.Red, UIstrings.NO_ACCOUNTS_AVAILABLE);
return null;
Expand Down
14 changes: 8 additions & 6 deletions PWManager.CLI/Controllers/GroupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using PWManager.CLI.Enums;
using PWManager.CLI.Interfaces;
using PWManager.Domain.ValueObjects;
using Sharprompt;

namespace PWManager.CLI.Controllers;

Expand All @@ -27,7 +26,6 @@ public GroupController(IUserEnvironment userEnv, IGroupService groupService, ILo
}

public ExitCondition Handle(string[] args) {

GroupAction action;
var executed = false;
do {
Expand All @@ -39,7 +37,7 @@ public ExitCondition Handle(string[] args) {
}

private GroupAction GetGroupAction() {
return Prompt.Select<GroupAction>(UIstrings.SELECT_ACTION);
return ConsoleInteraction.Select<GroupAction>(UIstrings.SELECT_ACTION);
}

private bool ExecuteAction(GroupAction action) {
Expand All @@ -62,8 +60,8 @@ private bool HandleSwitchGroup(List<string> groups) {
if (!groups.Any()) {
throw new UserFeedbackException(MessageStrings.NO_GROUPS_FOUND);
}
var groupidentifier = Prompt.Select(UIstrings.SWITCH_GROUP_PROMPT, groups);
_groupService.SwitchGroup(groupidentifier);
var groupIdentifier = ConsoleInteraction.Select(UIstrings.SWITCH_GROUP_PROMPT, groups);
_groupService.SwitchGroup(groupIdentifier);

return true;
}
Expand All @@ -87,7 +85,11 @@ private bool HandleDeletion() {
return false;
}

var settings = _settingsService.GetSettings();
var settings = _userEnv.UserSettings;
if (settings is null) {
PromptHelper.PrintColoredText(ConsoleColor.Red, UIstrings.DELETE_ABORTED);
throw new UserFeedbackException(MessageStrings.NO_SETTINGS_IN_ENVIRONMENT);
}
var isMainGroup = settings.MainGroup.MainGroupIdentifier.Equals(identifier);
if (isMainGroup) {
PromptHelper.PrintColoredText(ConsoleColor.Yellow, UIstrings.DELETE_STANDARD_GROUP);
Expand Down
14 changes: 5 additions & 9 deletions PWManager.CLI/Controllers/InitController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System.ComponentModel;
using System.Text.RegularExpressions;
using PWManager.Application.Context;
using PWManager.Application.Exceptions;
using System.Text.RegularExpressions;
using PWManager.Application.Services.Interfaces;
using PWManager.CLI.Abstractions;
using PWManager.CLI.Attributes;
using PWManager.CLI.Enums;
using PWManager.CLI.Interfaces;
using Sharprompt;

namespace PWManager.CLI.Controllers;

Expand All @@ -22,18 +18,18 @@ public InitController(IDatabaseInitializerService dbInit) {
}

public ExitCondition Handle(string[] args) {
var path = Prompt.Input<string>(UIstrings.DESIRED_PATH);
var path = ConsoleInteraction.Input<string>(UIstrings.DESIRED_PATH);
while(!Path.Exists(path)) {
Console.WriteLine(UIstrings.PATH_DOES_NOT_EXIST);
path = Prompt.Input<string>(UIstrings.DESIRED_PATH);
path = ConsoleInteraction.Input<string>(UIstrings.DESIRED_PATH);
}

_dbInit.CheckIfDataBaseExistsOnPath(path);

var name = Prompt.Input<string>(UIstrings.DESIRED_NAME);
var name = ConsoleInteraction.Input<string>(UIstrings.DESIRED_NAME);
while (name.Length <= 1 || !Regex.IsMatch(name, @"^[a-zA-Z]+$")) {
Console.WriteLine(UIstrings.INVALID_NAME);
name = Prompt.Input<string>(UIstrings.DESIRED_NAME);
name = ConsoleInteraction.Input<string>(UIstrings.DESIRED_NAME);
}

var password = PromptHelper.InputNewPassword();
Expand Down
Loading

0 comments on commit 4f12a77

Please sign in to comment.