maybeAuthPair, requireAuthPair

`maybeAuth` and `requireAuth` have been reverted to their original
signatures, which assume a Persistent database.  `maybeAuthPair` and
`requireAuthPair` are introduced, which do the same but without that
assumption.
This commit is contained in:
Manuel Gómez 2014-09-12 11:47:18 +00:00
parent 086837686a
commit 4fd20299c1

View File

@ -28,8 +28,10 @@ module Yesod.Auth
, loginErrorMessageI , loginErrorMessageI
-- * User functions -- * User functions
, defaultMaybeAuthId , defaultMaybeAuthId
, maybeAuthPair
, maybeAuth , maybeAuth
, requireAuthId , requireAuthId
, requireAuthPair
, requireAuth , requireAuth
-- * Exception -- * Exception
, AuthException (..) , AuthException (..)
@ -342,13 +344,27 @@ handlePluginR plugin pieces = do
ap:_ -> apDispatch ap method pieces ap:_ -> apDispatch ap method pieces
-- | Similar to 'maybeAuthId', but additionally look up the value associated -- | Similar to 'maybeAuthId', but additionally look up the value associated
-- with the user\'s database identifier to get the value in the database. -- with the user\'s database identifier to get the value in the database. This
-- assumes that you are using a Persistent database.
-- --
-- Since 1.1.0 -- Since 1.1.0
maybeAuth maybeAuth :: ( YesodAuthPersist master
:: (YesodAuthPersist master, Typeable (AuthEntity master)) , val ~ AuthEntity master
=> HandlerT master IO (Maybe (AuthId master, AuthEntity master)) , Key val ~ AuthId master
, PersistEntity val
, Typeable val
) => HandlerT master IO (Maybe (Entity val))
maybeAuth = runMaybeT $ do maybeAuth = runMaybeT $ do
(aid, ae) <- MaybeT maybeAuthPair
return $ Entity aid ae
-- | Similar to 'maybeAuth', but doesnt assume that you are using a
-- Persistent database.
--
-- Since 1.4.0
maybeAuthPair :: (YesodAuthPersist master, Typeable (AuthEntity master))
=> HandlerT master IO (Maybe (AuthId master, AuthEntity master))
maybeAuthPair = runMaybeT $ do
aid <- MaybeT maybeAuthId aid <- MaybeT maybeAuthId
ae <- MaybeT $ cachedAuth aid ae <- MaybeT $ cachedAuth aid
return (aid, ae) return (aid, ae)
@ -404,10 +420,22 @@ requireAuthId = maybeAuthId >>= maybe handleAuthLack return
-- authenticated or responds with error 401 if this is an API client (expecting JSON). -- authenticated or responds with error 401 if this is an API client (expecting JSON).
-- --
-- Since 1.1.0 -- Since 1.1.0
requireAuth :: (YesodAuthPersist master, Typeable (AuthEntity master)) requireAuth :: ( YesodAuthPersist master
=> HandlerT master IO (AuthId master, AuthEntity master) , val ~ AuthEntity master
, Key val ~ AuthId master
, PersistEntity val
, Typeable val
) => HandlerT master IO (Entity val)
requireAuth = maybeAuth >>= maybe handleAuthLack return requireAuth = maybeAuth >>= maybe handleAuthLack return
-- | Similar to 'requireAuth', but not tied to Persistent's 'Entity' type.
-- Instead, the 'AuthId' and 'AuthEntity' are returned in a tuple.
--
-- Since 1.4.0
requireAuthPair :: (YesodAuthPersist master, Typeable (AuthEntity master))
=> HandlerT master IO (AuthId master, AuthEntity master)
requireAuthPair = maybeAuthPair >>= maybe handleAuthLack return
handleAuthLack :: Yesod master => HandlerT master IO a handleAuthLack :: Yesod master => HandlerT master IO a
handleAuthLack = do handleAuthLack = do
aj <- acceptsJson aj <- acceptsJson