From f686fb741291c224e04d683bb2b7aef6ac8a192c Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Mon, 31 Dec 2018 15:27:36 -0500 Subject: [PATCH] implemented service renaming --- app/UI.hs | 38 +++++++++++++++++++++++++++++++------- app/Util.hs | 12 ++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/app/UI.hs b/app/UI.hs index 96dea0d..2aa86de 100644 --- a/app/UI.hs +++ b/app/UI.hs @@ -70,20 +70,19 @@ mainMenu = addPassword :: S.StateT Status IO () addPassword = do svc <- req $ prompt "service name: " reqResp - db <- S.gets $ view database - if pwHasService svc db - then do + ifServExists svc + (do edit <- req (confirm $ "The service already exists in the database.\n" ++ "Would you like to edit it?") if edit then servMenu svc - else mainMenu - else do + else mainMenu) + (do d <- buildData setService svc d showPass svc - servMenu svc + servMenu svc) viewEditMenu :: S.StateT Status IO () viewEditMenu = menu "View/Edit Password" @@ -145,6 +144,7 @@ servMenu x = menu x [ ( "show password", showPass x >> servMenu x ) , ( "edit password", editPassMenu x ) , ( "remove service", removeServ x ) + , ( "rename service", renameServ x ) , ( "back", mainMenu ) ] @@ -161,10 +161,34 @@ removeServ x = do "Are you sure you want to delete the password for " ++ x ++ "?" if go then do - S.modify $ over database $ pwRemoveService x + removeServ' x mainMenu else servMenu x +removeServ' :: String -> S.StateT Status IO () +removeServ' = S.modify . over database . pwRemoveService + +renameServ :: String -> S.StateT Status IO () +renameServ x = do + y <- req $ prompt "new service name: " reqResp + if x == y + then servMenu x + else ifServExists y + (do + overwrite <- req $ confirm $ + y ++ " already exists.\n" ++ + "Would you like to overwrite it?" + if overwrite + then renameServ' x y + else servMenu x) + (renameServ' x y) + +renameServ' :: String -> String -> S.StateT Status IO () +renameServ' x y = withService x mainMenu $ \d -> do + removeServ' x + setService y d + servMenu y + changeSalt :: String -> S.StateT Status IO () changeSalt x = withService x mainMenu $ \d -> do salt <- run newPWSalt diff --git a/app/Util.hs b/app/Util.hs index 482a70d..602021e 100644 --- a/app/Util.hs +++ b/app/Util.hs @@ -24,6 +24,7 @@ module Util ( menu , run , withService + , ifServExists , setService , req , tryReq @@ -82,6 +83,17 @@ withService srv fb act = do Nothing -> fb Just x -> act x +ifServExists + :: String + -> S.StateT Status IO a + -> S.StateT Status IO a + -> S.StateT Status IO a +ifServExists s x y = do + db <- S.gets $ view database + if pwHasService s db + then x + else y + setService :: String -> PWData -> S.StateT Status IO () setService k = S.modify . over database . pwSetService k