Skip to content

Commit

Permalink
Add accessDeniedWhen
Browse files Browse the repository at this point in the history
My brain can't work with the double negative of `accessDeniedUnless`. It's much easier for me to say when to  access deny.
  • Loading branch information
amitaibu authored Jun 30, 2023
1 parent 88164dc commit 1b4a3d3
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions IHP/AuthSupport/Authorization.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,31 @@ import IHP.Prelude
class CanView user model where
canView :: (?modelContext :: ModelContext) => model -> user -> IO Bool

-- | Stops the action execution with an error message when the access condition is false.
-- | Stops the action execution with an error message when the access condition is True.
--
-- __Example:__ Checking a user is author of a blog post.
-- __Example:__ Checking a user is the author of a blog post.
--
-- > action EditPostAction { postId } = do
-- > post <- fetch postId
-- > accessDeniedWhen (post.authorId /= currentUserId)
-- >
-- > renderHtml EditView { .. }
--
-- This will throw an error and prevent the view from being rendered when the current user is not the author of the post.
accessDeniedWhen :: Bool -> IO ()
accessDeniedWhen condition = if condition then fail "Access denied" else pure ()

-- | Stops the action execution with an error message when the access condition is False.
--
-- __Example:__ Checking a user is the author of a blog post.
--
-- > action EditPostAction { postId } = do
-- > post <- fetch postId
-- > accessDeniedUnless (post.authorId == currentUserId)
-- >
-- > renderHtml EditView { .. }
--
-- This will throw an error and prevent the view from being rendered when the current user is not author of the post.
-- This will throw an error and prevent the view from being rendered when the current user is not the author of the post.
accessDeniedUnless :: Bool -> IO ()
accessDeniedUnless condition = if condition then pure () else fail "Access denied"
accessDeniedUnless condition = if condition then pure () else fail "Access denied"

0 comments on commit 1b4a3d3

Please sign in to comment.