diff options
author | Joris | 2017-06-05 18:02:13 +0200 |
---|---|---|
committer | Joris | 2017-06-05 18:02:13 +0200 |
commit | 0b191f5c48edffc9da3e38c284e9640fd82e7cb1 (patch) | |
tree | c729e53822e7c41c1a854d82d25636e58ee65c9f /src/server/Controller/Index.hs | |
parent | 5c110716cfda6e616a795edd12f2012b132dca9f (diff) |
Replace persistent by sqlite-simple
Diffstat (limited to 'src/server/Controller/Index.hs')
-rw-r--r-- | src/server/Controller/Index.hs | 52 |
1 files changed, 23 insertions, 29 deletions
diff --git a/src/server/Controller/Index.hs b/src/server/Controller/Index.hs index 96d0a49..9fb2aa0 100644 --- a/src/server/Controller/Index.hs +++ b/src/server/Controller/Index.hs @@ -4,28 +4,22 @@ module Controller.Index ) where import Control.Monad.IO.Class (liftIO) - -import Web.Scotty hiding (get) - -import Network.HTTP.Types.Status (ok200) - import Data.Text (Text) import Data.Time.Clock (getCurrentTime, diffUTCTime) - -import Database.Persist hiding (Key, get) +import Network.HTTP.Types.Status (ok200) +import Web.Scotty hiding (get) import Conf (Conf(..)) +import Model.Init (getInit) +import Model.Json.Init (InitResult(..)) +import Model.Message.Key +import Model.User (User) import qualified LoginSession -import Secure (getUserFromToken) - -import Model.Database hiding (Key) import qualified Model.Json.Conf as M -import Model.User (getUser) -import Model.Message.Key -import Model.SignIn (getSignIn, signInTokenToUsed) -import Model.Json.Init (InitResult(..)) -import Model.Init (getInit) - +import qualified Model.Query as Query +import qualified Model.SignIn as SignIn +import qualified Model.User as User +import Secure (getUserFromToken) import View.Page (page) get :: Conf -> Maybe Text -> ActionM () @@ -37,54 +31,54 @@ get conf mbToken = do Left errorKey -> return . InitError $ errorKey Right user -> - liftIO . runDb . fmap InitSuccess . getInit $ user + liftIO . Query.run . fmap InitSuccess . getInit $ user Nothing -> do mbLoggedUser <- getLoggedUser case mbLoggedUser of Nothing -> return InitEmpty Just user -> - liftIO . runDb . fmap InitSuccess . getInit $ user + liftIO . Query.run . fmap InitSuccess . getInit $ user html $ page (M.Conf { M.currency = currency conf }) initResult -validateSignIn :: Conf -> Text -> ActionM (Either Key (Entity User)) +validateSignIn :: Conf -> Text -> ActionM (Either Key User) validateSignIn conf textToken = do mbLoggedUser <- getLoggedUser case mbLoggedUser of Just loggedUser -> return . Right $ loggedUser Nothing -> do - mbSignIn <- liftIO . runDb $ getSignIn textToken + mbSignIn <- liftIO . Query.run $ SignIn.getSignIn textToken now <- liftIO getCurrentTime case mbSignIn of Nothing -> return . Left $ SignInInvalid - Just signInValue -> - if signInIsUsed . entityVal $ signInValue + Just signIn -> + if SignIn.isUsed signIn then return . Left $ SignInUsed else - let diffTime = now `diffUTCTime` (signInCreation . entityVal $ signInValue) + let diffTime = now `diffUTCTime` (SignIn.creation signIn) in if diffTime > signInExpiration conf then return . Left $ SignInExpired else do - LoginSession.put conf (signInToken . entityVal $ signInValue) - mbUser <- liftIO . runDb $ do - signInTokenToUsed . entityKey $ signInValue - getUser . signInEmail . entityVal $ signInValue + LoginSession.put conf (SignIn.token signIn) + mbUser <- liftIO . Query.run $ do + SignIn.signInTokenToUsed . SignIn.id $ signIn + User.getUser . SignIn.email $ signIn return $ case mbUser of Nothing -> Left UnauthorizedSignIn Just user -> Right user -getLoggedUser :: ActionM (Maybe (Entity User)) +getLoggedUser :: ActionM (Maybe User) getLoggedUser = do mbToken <- LoginSession.get case mbToken of Nothing -> return Nothing Just token -> do - liftIO . runDb . getUserFromToken $ token + liftIO . Query.run . getUserFromToken $ token signOut :: Conf -> ActionM () signOut conf = LoginSession.delete conf >> status ok200 |