aboutsummaryrefslogtreecommitdiff
path: root/src/server/Secure.hs
blob: 1fef71371c746f8958e007ae225022966a00f1c4 (plain)
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
30
31
32
33
34
35
36
37
38
{-# LANGUAGE OverloadedStrings #-}

module Secure
  ( loggedAction
  ) where

import Web.Scotty

import Network.HTTP.Types.Status (forbidden403)

import Database.Persist (Entity)

import Model.Database (User, runDb)
import Model.User (getUser)

import Control.Monad.IO.Class (liftIO)

import qualified Data.Text as T
import qualified Data.Text.IO as TIO

import qualified LoginSession

loggedAction :: (Entity User -> ActionM ()) -> ActionM ()
loggedAction action = do
  maybeLogin <- LoginSession.get
  case maybeLogin of
    Just login -> do
      maybeUser <- liftIO . runDb $ getUser login
      case maybeUser of
        Just user ->
          action user
        Nothing -> do
          status forbidden403
          liftIO . TIO.putStrLn . T.concat $ ["Could not find the user which login is ", login]
          html "Could not find a user from your login"
    Nothing -> do
      status forbidden403
      html "You need to be logged in to perform this action"