module Model.Identity
( module Model.Identity.Types
, determineIdentity
, maybeIdentity
, identityJSON
) where
import Data.Monoid ((<>))
import Ops
import Has
import qualified JSON
import Model.Id
import Model.Token
import HTTP.Request
import Service.Types
import Service.DB
import HTTP.Cookie
import Model.Party
import Model.Permission
import Model.Identity.Types
determineIdentity :: (MonadHas Secret c m, MonadHasRequest c m, MonadDB c m) => m Identity
determineIdentity =
maybe NotLoggedIn Identified <$> (flatMapM lookupSession =<< getSignedCookie "session")
maybeIdentity
:: (MonadHasIdentity c m)
=> m a
-> (Session -> m a)
-> m a
maybeIdentity z f = extractFromIdentifiedSessOrDefault z f =<< peek
identityJSON :: JSON.ToObject o => Identity -> JSON.Record (Id Party) o
identityJSON i = partyJSON (view i) `JSON.foldObjectIntoRec`
( "authorization" JSON..= accessSite i
<> "csverf" `JSON.kvObjectOrEmpty` identityVerf i
<> "superuser" `JSON.kvObjectOrEmpty` (True `useWhen` identityAdmin i))