Skip to content

Commit

Permalink
currentAdmin, currentAdminOrNothing, currentAdminId, ensureIsAdmin no…
Browse files Browse the repository at this point in the history
…w works without explicit type passed
  • Loading branch information
mpscholten committed Sep 14, 2022
1 parent 131a1e3 commit ba44265
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions IHP/LoginSupport/Helper/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ ensureIsUser =
Nothing -> redirectToLoginWithMessage (newSessionUrl (Proxy :: Proxy user))

{-# INLINABLE currentAdmin #-}
currentAdmin :: forall admin. (?context :: ControllerContext, HasNewSessionUrl admin, Typeable admin) => admin
currentAdmin :: forall admin. (?context :: ControllerContext, HasNewSessionUrl admin, Typeable admin, admin ~ CurrentAdminRecord) => admin
currentAdmin = fromMaybe (redirectToLogin (newSessionUrl (Proxy @admin))) currentAdminOrNothing

{-# INLINABLE currentAdminOrNothing #-}
currentAdminOrNothing :: forall admin. (?context :: ControllerContext, HasNewSessionUrl admin, Typeable admin) => (Maybe admin)
currentAdminOrNothing :: forall admin. (?context :: ControllerContext, HasNewSessionUrl admin, Typeable admin, admin ~ CurrentAdminRecord) => (Maybe admin)
currentAdminOrNothing = case unsafePerformIO (maybeFromContext @(Maybe admin)) of
Just admin -> admin
Nothing -> error "currentAdminOrNothing: initAuthentication @Admin has not been called in initContext inside FrontController of this application"

{-# INLINABLE currentAdminId #-}
currentAdminId :: forall admin adminId. (?context :: ControllerContext, HasNewSessionUrl admin, HasField "id" admin adminId, Typeable admin) => adminId
currentAdminId :: forall admin adminId. (?context :: ControllerContext, HasNewSessionUrl admin, HasField "id" admin adminId, Typeable admin, admin ~ CurrentAdminRecord) => adminId
currentAdminId = currentAdmin @admin |> get #id

{-# INLINABLE ensureIsAdmin #-}
ensureIsAdmin :: forall (admin :: Type) adminId. (?context :: ControllerContext, HasNewSessionUrl admin, Typeable admin) => IO ()
ensureIsAdmin :: forall (admin :: Type) adminId. (?context :: ControllerContext, HasNewSessionUrl admin, Typeable admin, admin ~ CurrentAdminRecord) => IO ()
ensureIsAdmin =
case currentAdminOrNothing @admin of
Just _ -> pure ()
Expand Down

0 comments on commit ba44265

Please sign in to comment.