From ceaf92996b8c16bed3b794603efdcd1450dcb2da Mon Sep 17 00:00:00 2001 From: SJost Date: Wed, 27 Jun 2018 13:15:17 +0200 Subject: [PATCH] Favourites updated immediately now; Links for Lecturers are shown --- src/Foundation.hs | 14 ++++++++++---- src/Utils.hs | 3 +++ src/Utils/DB.hs | 4 ++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Foundation.hs b/src/Foundation.hs index 34153e3b5..8a45bed2c 100644 --- a/src/Foundation.hs +++ b/src/Foundation.hs @@ -395,9 +395,9 @@ instance Yesod UniWorX where -- To add it, chain it together with the defaultMiddleware: yesodMiddleware = defaultYesodMiddleware . defaultCsrfMiddleware -- For details, see the CSRF documentation in the Yesod.Core.Handler module of the yesod-core package. yesodMiddleware handler = do - res <- defaultYesodMiddleware handler void . runMaybeT $ do route <- MaybeT getCurrentRoute + guardM . lift $ (== Authorized) <$> isAuthorized route False case route of -- update Course Favourites here CourseR tid csh _ -> do uid <- MaybeT maybeAuthId @@ -420,6 +420,7 @@ instance Yesod UniWorX where lift $ mapM_ delete oldFavs _other -> return () + res <- defaultYesodMiddleware handler -- handler is executed before Favourites are update return res defaultLayout widget = do @@ -600,10 +601,15 @@ pageActions (CourseR tid csh CShowR) = , menuItemRoute = CourseR tid csh SheetListR , menuItemAccessCallback' = do --TODO always show for lecturer let sheetRouteAccess shn = (== Authorized) <$> (isAuthorized (CSheetR tid csh shn SShowR) False) - sheets <-runDB $ do + muid <- maybeAuthId + (sheets,lecturer) <- runDB $ do cid <- getKeyBy404 $ CourseTermShort tid csh - map (sheetName.entityVal) <$> selectList [SheetCourse ==. cid] [Desc SheetActiveFrom] - anyM sheets sheetRouteAccess + sheets <- map (sheetName.entityVal) <$> selectList [SheetCourse ==. cid] [Desc SheetActiveFrom] + lecturer <- case muid of + Nothing -> return False + (Just uid) -> existsBy $ UniqueLecturer uid cid + return (sheets,lecturer) + or2M (return lecturer) $ anyM sheets sheetRouteAccess } , PageActionSecondary $ MenuItem { menuItemLabel = "Neues Übungsblatt anlegen" diff --git a/src/Utils.hs b/src/Utils.hs index 6ad5aca4c..989e3cda4 100644 --- a/src/Utils.hs +++ b/src/Utils.hs @@ -210,6 +210,9 @@ shortCircuitM sc mx my op = do False -> op <$> pure x <*> my +guardM :: MonadPlus m => m Bool -> m () +guardM f = guard =<< f + -- Some Utility Functions from Agda.Utils.Monad -- | Monadic if-then-else. ifM :: Monad m => m Bool -> m a -> m a -> m a diff --git a/src/Utils/DB.hs b/src/Utils/DB.hs index 6c149e668..e547e34eb 100644 --- a/src/Utils/DB.hs +++ b/src/Utils/DB.hs @@ -25,6 +25,10 @@ getKeyBy404 :: (PersistEntityBackend record ~ BaseBackend backend, PersistEntity => Unique record -> ReaderT backend m (Key record) getKeyBy404 = (fmap entityKey) . getBy404 -- TODO optimize this, so that DB does not deliver entire record! +existsBy :: (PersistEntityBackend record ~ BaseBackend backend, PersistEntity record, PersistUniqueRead backend, MonadIO m) + => Unique record -> ReaderT backend m Bool +existsBy = fmap isJust . getBy + myReplaceUnique :: (MonadIO m