{-# LANGUAGE DeriveDataTypeable, FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, OverloadedStrings, RecordWildCards, TemplateHaskell, TypeFamilies, TypeSynonymInstances, OverloadedStrings #-} {-# OPTIONS_GHC -F -pgmFhsx2hs #-} module TinyAuth.Template where import qualified Data.Text.Lazy as Lazy (pack, Text) import Happstack.Auth (AuthProfileURL(AuthURL), AuthURL(A_AddAuth, A_CreateAccount, A_Login, A_Logout), getUserId, UserId(unUserId)) import Happstack.Foundation (Attr(..), EmbedAsAttr(..), EmbedAsChild(..), fromStringLit, Happstack, liftM, Response, ToMessage(toResponse), XMLGen(..), XMLGenerator, XMLGenT(XMLGenT)) import qualified Happstack.Server.HSP.HTML as HTML (defaultTemplate) import TinyAuth.Acid (Acid(..)) import TinyAuth.Route (CtrlV, CtrlV', Route(CSS, NewPaste, U_AuthProfile, ViewRecent)) ------------------------------------------------------------------------------ -- appTemplate ------------------------------------------------------------------------------ -- | page template function -- -- There are two forms here because we need to make it work for both -- our usual happstack-foundation/HSP stuff, and for happstack-auth, -- which is Blaze rather than HSP based. So we make the base -- template, and then make two versions that give it each of the two -- contexts it needs. baseAppTemplate :: ( XMLGenerator m , Happstack m , EmbedAsAttr m (Attr Lazy.Text Route) , EmbedAsChild m headers , EmbedAsChild m body , StringType m ~ Lazy.Text ) => Acid -> Lazy.Text -- ^ page title -> headers -- ^ extra headers to add to \ tag -> body -- ^ contents of \ tag -> m (XMLType m) baseAppTemplate acid@Acid{..} ttl moreHdrs bdy = HTML.defaultTemplate ttl <%><% moreHdrs %> $ <%> <% do mUserId <- getUserId acidAuth acidProfile -- Debugging -- authState <- query' acidAuth AskAuthState -- let authDump = traceMsg "authState: " $ ppDoc authState case mUserId of Nothing -> do (Just uid) -> do %> <% bdy %> -- This is the baseAppTemplate wrapped so that it can be used in the -- normal happstack-foundation stuff. appTemplate :: ( EmbedAsChild CtrlV' headers , EmbedAsChild CtrlV' body ) => Acid -> Lazy.Text -- ^ page title -> headers -- ^ extra headers to add to \ tag -> body -- ^ contents of \ tag -> CtrlV Response appTemplate acid ttl moreHdrs bdy = liftM toResponse (XMLGenT $ baseAppTemplate acid ttl moreHdrs bdy)