Created
March 11, 2011 16:44
-
-
Save mightybyte/866161 to your computer and use it in GitHub Desktop.
Useful Heist Splices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------------------------------------------------------------------------ | |
-- | Renders the child nodes only if the request comes from an authenticated | |
-- user. | |
ifLoggedIn :: Splice Application | |
ifLoggedIn = do | |
node <- getParamNode | |
res <- lift $ requireUser (return []) (return $ childNodes node) | |
return res | |
------------------------------------------------------------------------------ | |
-- | Renders the child nodes only if the request comes from a user that is not | |
-- logged in. | |
ifGuest :: Splice Application | |
ifGuest = do | |
node <- getParamNode | |
res <- lift $ requireUser (return $ childNodes node) (return []) | |
return res | |
------------------------------------------------------------------------------ | |
-- | Returns the current username as a text node. | |
currentUser :: Splice Application | |
currentUser = do | |
mu <- lift currentAuthUser | |
return $ maybe [] (\u -> [TextNode $ uUsername u]) $ (cast' . Doc . snd) =<< mu | |
------------------------------------------------------------------------------ | |
-- | Prevents the whole page from rendering if the user isn't logged in. | |
requireAuth :: Splice Application | |
requireAuth = do | |
lift $ requireUser pass (return []) | |
appSplices :: [(T.Text, Splice Application)] | |
appSplices = | |
[("ifLoggedIn", ifLoggedIn) | |
,("ifGuest", ifGuest) | |
,("requireAuth", requireAuth) | |
,("currentUser", currentUser) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment