public
Created

how to fail inside for comprehension of actionmonad

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for{
u <- UserStore.getFull(requesterRef)
// .map{usr=> if(false) notAuthorizedC("x") else usr}
.map{usr=>UserRefs.ById(usr.id)}
// _ <- if (u.status == Statuses.Active) success
// else notAuthorizedC("Unable to retrieve the permissions for post: [%s] and requester: [%s]".format(postRef, requesterRef))
r <- postRef match {
case ref:PostRefs.ById =>
getPermitedOperationsForPostsById(Set(ref), u)
.map[ReaderCtx, Set[Operation]]{r=> r(ref)}
case ref:PostRefs.ByPermalink =>
getPermitedOperationsForPostsByPermalink(Set(ref), u)
.map[ReaderCtx, Set[Operation]]{r=> r(ref)}
}
} yield (r)

I need to test the user status in the middle of the for comprehension for the actionmonad. line 3 or 5,6
not sure how to do it.

i think what we should do is test the user's status in the SQL WHERE clause. this will work because you are retrieving all posts based upon the user's permissions. thus the following conditions will help

u.id = {user_id} AND u.status = 'active'::user_statuses

You several options available to you:

// Technique 1 - simple if

for{
  u <- UserStore.getFull(requesterRef)
  _ <- if (u.status == Statuses.Active) unitWriteResult
       else notAuthorizedC("Unable to retrieve the permissions for post: [%s] and requester: [%s]".format(postRef, requesterRef))
  r <- postRef match {
        case ref:PostRefs.ById => 
          getPermitedOperationsForPostsById(Set(ref), u)
            .map[ReaderCtx, Set[Operation]]{r=> r(ref)}
        case ref:PostRefs.ByPermalink => 
          getPermitedOperationsForPostsByPermalink(Set(ref), u)
            .map[ReaderCtx, Set[Operation]]{r=> r(ref)}
      }
} yield (r)

// Technique 2 - "monadic if"

for{
  u <- UserStore.getFull(requesterRef) ifM(_.status == Statuses.Active) onTrueMap(_.successPoint(Private)) onFalse(notAuthorizedC("Unable to retrieve the permissions for post: [%s] and requester: [%s]".format(postRef, requesterRef)))
  r <- postRef match {
        case ref:PostRefs.ById => 
          getPermitedOperationsForPostsById(Set(ref), u)
            .map[ReaderCtx, Set[Operation]]{r=> r(ref)}
        case ref:PostRefs.ByPermalink => 
          getPermitedOperationsForPostsByPermalink(Set(ref), u)
            .map[ReaderCtx, Set[Operation]]{r=> r(ref)}
      }
} yield (r)

// Technique 3 - "monadic 'when'"

for{
  u <- UserStore.getFull(requesterRef) when(_.status != Statuses.Active) apply notAuthorizedC("Unable to retrieve the permissions for post: [%s] and requester: [%s]".format(postRef, requesterRef))
  r <- postRef match {
        case ref:PostRefs.ById => 
          getPermitedOperationsForPostsById(Set(ref), u)
            .map[ReaderCtx, Set[Operation]]{r=> r(ref)}
        case ref:PostRefs.ByPermalink => 
          getPermitedOperationsForPostsByPermalink(Set(ref), u)
            .map[ReaderCtx, Set[Operation]]{r=> r(ref)}
      }
} yield (r)

@peterjanovsky is probably more correct: better to handle this in SQL first rather than bring it back to the client

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.