about summary refs log tree commit diff
path: root/src/LoginAttempts.hs
blob: f5a5bde6a004be4366632b27e697fd6bdce87713 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
--------------------------------------------------------------------------------
module LoginAttempts where
--------------------------------------------------------------------------------
import Database.SQLite.Simple

import qualified Types as T
--------------------------------------------------------------------------------

reset :: FilePath -> T.Username -> IO ()
reset dbFile username = withConnection dbFile $ \conn ->
  execute conn "UPDATE LoginAttempts SET numAttempts = 0 WHERE username = ?"
    (Only username)

-- | Attempt to return the number of failed login attempts for
-- `username`. Returns a Maybe in case `username` doesn't exist.
forUsername :: FilePath -> T.Username -> IO (Maybe Integer)
forUsername dbFile username = withConnection dbFile $ \conn -> do
  res <- query conn "SELECT username,numAttempts FROM LoginAttempts WHERE username = ?"
    (Only username)
  case res of
    [T.LoginAttempt{..}] -> pure (Just loginAttemptNumAttempts)
    _  -> pure Nothing

increment :: FilePath -> T.Username -> IO ()
increment dbFile username = withConnection dbFile $ \conn ->
  execute conn "UPDATE LoginAttempts SET numAttempts = numAttempts + 1 WHERE username = ?"
    (Only username)