mateamt/src/Util.hs

161 lines
4.3 KiB
Haskell
Raw Normal View History

{-# LANGUAGE FlexibleContexts #-}
2021-07-12 11:29:40 +00:00
{-# LANGUAGE OverloadedStrings #-}
2022-02-20 20:48:16 +00:00
{-# LANGUAGE RecordWildCards #-}
module Util where
2022-04-15 18:53:37 +00:00
import Servant
import Opaleye
import qualified Data.ByteString as BS
2021-07-12 11:29:40 +00:00
import Data.ByteString.Lazy (fromStrict)
2022-04-15 19:06:18 +00:00
import Data.Maybe (fromMaybe, fromJust)
import Data.Profunctor.Product.Default (Default)
2022-04-15 18:51:22 +00:00
import Data.String (fromString)
2021-07-12 11:29:40 +00:00
import qualified Data.Text as T
import qualified Data.Text.Encoding as E
2021-02-01 01:25:08 +00:00
import Database.PostgreSQL.Simple
2022-04-15 19:06:18 +00:00
import Control.Monad (void, filterM)
2021-07-12 11:29:40 +00:00
import Control.Monad.Reader (asks)
import Control.Monad.IO.Class (liftIO)
import Network.Mail.Mime
import System.Directory (doesFileExist)
2021-02-01 01:25:08 +00:00
import System.Random.Stateful
2021-02-01 01:25:08 +00:00
-- internal imports
import Model
2021-07-12 11:29:40 +00:00
import Types
2022-04-15 18:52:32 +00:00
-- | This is the internal function to check a users authorization to do certain
-- actions
checkCapability
:: Int -- ^ User Id to check
-> (Role -> Bool) -- ^ Predicate to check
-> MateHandler Bool -- ^ Result
checkCapability uid accessRule = do
conn <- asks rsConnection
assocs <- selectUserAssociations uid conn
let rids = map roleAssociationRole assocs
roles <- selectRoleList rids conn
return $ any accessRule roles
notImplemented :: MateHandler a
notImplemented = throwError $ err501
{ errBody = "Function has not yet been implemented!"
}
2022-02-20 20:48:16 +00:00
printSql :: Default Unpackspec a a => Select a -> IO ()
2022-02-20 20:03:05 +00:00
printSql = putStrLn . fromMaybe "Empty query" . showSql
2021-02-01 01:25:08 +00:00
initDB :: Connection -> IO ()
initDB conn = do
2022-02-20 20:03:05 +00:00
void $ execute_ conn initAvatar
void $ execute_ conn initUser
void $ execute_ conn initProduct
void $ execute_ conn initToken
void $ execute_ conn initAuthData
void $ execute_ conn initAmount
void $ execute_ conn initJournal
void $ execute_ conn initRole
void $ execute_ conn initUserToRole
2021-02-01 01:25:08 +00:00
void $ runInsertInitialRoles conn
2021-06-24 04:33:05 +00:00
-- This is only a dummy function.
-- TODO: Replace with proper translation function(s)!
__ = id
2021-07-12 11:29:40 +00:00
sendUserNotification
:: UserDetails -- ^ The recipient
-> T.Text -- ^ The mail subject
-> T.Text -- ^ The mail body
-> MateHandler ()
sendUserNotification recipient subject message =
case userDetailsEmail recipient of
Just email ->
sendNotification (Mail
{ mailFrom = Address Nothing "noreply"
, mailTo =
[ Address
(Just $ userDetailsIdent recipient)
2021-10-16 15:50:28 +00:00
email
2021-07-12 11:29:40 +00:00
]
, mailCc = []
, mailBcc = []
, mailHeaders = [("Subject", subject)]
, mailParts =
[[ Part
{ partType = "text/plain; charset=utf-8"
, partEncoding = None
, partDisposition = DefaultDisposition
, partHeaders = []
, partContent = PartContent (fromStrict $ E.encodeUtf8 message)
}
]]
}
)
Nothing ->
return ()
2021-07-14 01:08:58 +00:00
sendAdminNotification
:: T.Text -- The mail subject
-> T.Text -- The mail body
-> MateHandler ()
2022-02-20 20:48:16 +00:00
sendAdminNotification subject message = do
2022-04-15 18:52:46 +00:00
conn <- asks rsConnection
allUsers <- userDetailsSelectAll conn
2022-04-15 19:06:18 +00:00
admins <- filterM
((`checkCapability` roleCanManageSettings) . userDetailsId)
2022-02-20 20:48:16 +00:00
allUsers
mapM_
2022-04-15 19:06:18 +00:00
(\ UserDetails{..} ->
2022-02-20 20:48:16 +00:00
let mail = Mail
{ mailFrom = Address Nothing "noreply"
, mailTo =
[ Address
(Just userDetailsIdent)
2022-04-15 19:06:18 +00:00
(fromJust userDetailsEmail)
2022-02-20 20:48:16 +00:00
]
, mailCc = []
, mailBcc =[]
2022-04-15 18:52:46 +00:00
, mailHeaders = [("Subject", subject)]
, mailParts =
2022-02-20 20:48:16 +00:00
[[ Part
{ partType = "text/plain; charset=utf8"
, partEncoding = None
, partDisposition = DefaultDisposition
, partHeaders = []
2022-04-15 19:06:18 +00:00
, partContent = PartContent (fromString $ T.unpack message)
2022-02-20 20:48:16 +00:00
}
]]
}
in
sendNotification mail
)
2022-04-15 18:52:46 +00:00
admins
2021-07-14 01:08:58 +00:00
2021-10-16 15:50:28 +00:00
sendNotification :: Mail -> MateHandler ()
2021-07-12 11:29:40 +00:00
sendNotification mail = do
2021-10-16 15:50:28 +00:00
sendmailPath <- asks rsSendmailPath
2021-07-12 11:29:40 +00:00
liftIO $ do
2021-10-16 15:50:28 +00:00
existence <- doesFileExist sendmailPath
2021-07-12 11:29:40 +00:00
if existence
then
2021-10-16 15:50:28 +00:00
renderSendMailCustom sendmailPath [] mail
2021-07-12 11:29:40 +00:00
else
2021-10-16 15:50:28 +00:00
print ("Warning: sending notification failed: Sendmail not present!"
:: String)
randomString :: IO BS.ByteString
randomString = uniformByteStringM 32 =<< newIOGenM (mkStdGen 23)