From f612bad5464c854b972eeab5dae8cc3cf38d4daa Mon Sep 17 00:00:00 2001 From: nek0 Date: Sun, 20 Feb 2022 21:48:16 +0100 Subject: [PATCH] make admin notification --- src/Control/User.hs | 11 +++++++++++ src/Model/User.hs | 12 ++++++++++++ src/Util.hs | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/Control/User.hs b/src/Control/User.hs index 8343331..6727244 100644 --- a/src/Control/User.hs +++ b/src/Control/User.hs @@ -48,6 +48,17 @@ userGet (Just (uid, _)) = do conn <- asks rsConnection userDetailsSelect uid conn +userGetAll + :: Maybe (Int, AuthMethod) + -> MateHandler [UserDetails] +userGetAll Nothing = + throwError $ err401 + { errBody = "No Authentication present." + } +userGetAll (Just (_, _)) = do + conn <- asks rsConnection + userDetailsSelectAll conn + userUpdate :: Maybe (Int, AuthMethod) -> UserDetailsSubmit diff --git a/src/Model/User.hs b/src/Model/User.hs index d7d2524..de377a7 100644 --- a/src/Model/User.hs +++ b/src/Model/User.hs @@ -85,6 +85,18 @@ userSelect ref conn = do ) users +userDetailsSelectAll + :: PGS.Connection + -> MateHandler [UserDetails] +userDetailsSelectAll conn = do + users <- liftIO (map fromDatabase <$> runSelect conn (selectTable userTable)) :: MateHandler [User] + return $ map + (\(User i1 i2 i3 _ i5 i6) -> + UserDetails i1 i2 i3 i5 i6 + ) + users + + userDetailsSelect :: Int -> PGS.Connection diff --git a/src/Util.hs b/src/Util.hs index 80c3e4a..5a4cd6f 100644 --- a/src/Util.hs +++ b/src/Util.hs @@ -1,5 +1,6 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} module Util where import Opaleye @@ -30,6 +31,9 @@ import Model import Types +import Control.User +import Types (UserDetails(userDetailsId)) + printSql :: Default Unpackspec a a => Select a -> IO () printSql = putStrLn . fromMaybe "Empty query" . showSql @@ -86,9 +90,36 @@ sendAdminNotification :: T.Text -- The mail subject -> T.Text -- The mail body -> MateHandler () -sendAdminNotification subject message = - -- TODO: Grab administrators from settings and actually send the mails to them - return () +sendAdminNotification subject message = do + allUsers <- userGetAll + admins <- mapM + (\uid -> checkCapability uid roleCanManageSettings. userDetailsId) + allUsers + mapM_ + (\ UserDetails {..} -> + let mail = Mail + { mailFrom = Address Nothing "noreply" + , mailTo = + [ Address + (Just userDetailsIdent) + userDetailsEmail + ] + , mailCc = [] + , mailBcc =[] + , mailHeaders = + [[ Part + { partType = "text/plain; charset=utf8" + , partEncoding = None + , partDisposition = DefaultDisposition + , partHeaders = [] + , partContent = PartContent (fromString $ E.encodeUtf8 message) + } + ]] + } + in + sendNotification mail + ) + allUsers sendNotification :: Mail -> MateHandler () sendNotification mail = do