Ah, the compiler error made me actually look at my code (doh), and here's
what I really use:

def isLoggedIn_? = currentUserIdVar.is.isDefined

I actually have a few things defined around this:

  object currentUserIdVar extends SessionVar[Can[Long]](Empty)
  def currentUserId = currentUserIdVar.is openOr -1

  object currentUserObjVar extends
RequestVar[Can[Member]](currentUserIdVar.map(Model.getReference(classOf[Member],
_)))
  def currentUserObj = currentUserObjVar.is

  def currentUsername = currentUserObj map {_.userId} openOr "Not logged in"

  def login (member : Member) = currentUserIdVar(Full(member.id))

  def logout = currentUserIdVar.remove

  def isLoggedIn_? = currentUserIdVar.is.isDefined

  def isSuperuser_? = currentUserObj map {_.superuser} openOr false

I was thinking of my isSuperuser_? method. Hope this helps!

Derek

On Thu, Oct 16, 2008 at 8:33 PM, Charles F. Munat <[EMAIL PROTECTED]> wrote:

>
> The compiler doesn't like that at all (even after replacing : with =)
>
> error: type mismatch;
> [WARNING]  found   : Boolean(true)
> [WARNING]  required: (Long) => ?
> [WARNING]   def isAuthenticated_? = CurrentUserId.map(true) openOr false
> [WARNING]                                             ^
>
> Also, it needs the () after the ?. This works:
>
> def isAuthenticated_?() = CurrentUserId.is.map((Long) => true) openOr false
>
> Thanks!
>
> Chas.
>
> Derek Chen-Becker wrote:
> > Everything there looks kosher at first glance to me. SessionVars should
> > stay in the session no matter what. One thing I can think of is if
> > somehow you're losing the session. Is the session ID being stored in a
> > cookie, or as part of the URI? If it's part of the URI you may be
> > dropping it somewhere else in your code if you process the URI at all.
> > That's just a guess. Also, I would probably write the isAuthenticated
> > method as:
> >
> > def isAuthenticated_? : CurrentUserId.map(true) openOr false
> >
> > Derek
> >
> > On Thu, Oct 16, 2008 at 5:15 PM, Charles F. Munat <[EMAIL PROTECTED]
> > <mailto:[EMAIL PROTECTED]>> wrote:
> >
> >
> >     I've got a JPA Lift site and have set up a login in which the User's
> id
> >     is stored in a session variable thus, as shown below.
> >
> >     This works just fine. The user can log in and go from page to page.
> But
> >     after some random number of clicks, whoops! I'm suddenly no longer
> >     logged in.
> >
> >     Hmmm. I must be doing something wrong. I just love intermittent
> >     failures.
> >
> >     (My user object is a Member.)
> >
> >     object CurrentUserId extends SessionVar[Can[Long]](Empty)
> >     object RequestedURL extends SessionVar[Can[String]](Empty)
> >     object CurrentUser extends RequestVar[Can[Member]](Empty)
> >
> >     object AccessControl {
> >
> >       def login(): Can[LiftResponse] = {
> >         if (S.post_?) {
> >           try {
> >             val member: Member = Model.createNamedQuery[Member](
> >               "findMemberByEmailAddress",
> >               "emailAddress" ->
> >                 S.param("emailAddress").openOr("")).getSingleResult()
> >
> >             if (member.authenticate(S.param("password").openOr(""))) {
> >                 CurrentUser(Full(member))
> >                 CurrentUserId(Full(member.id <http://member.id>))
> >             }
> >             else
> >               S.error("Go away!")
> >
> >           } catch {
> >             case x: NoResultException =>
> >               S.error("This thing don't work.")
> >             case _ => S.error("Done broke. Ouch.")
> >           }
> >         }
> >
> >         val uri = RequestedURL.openOr("/")
> >         RequestedURL(Empty)
> >         Full(RedirectResponse(uri))
> >       }
> >
> >       def logout(): Can[LiftResponse] = {
> >         CurrentUser(Empty)
> >         CurrentUserId(Empty)
> >         Full(RedirectResponse(S.param("path").openOr("/")))
> >       }
> >
> >       def isAuthenticated_?() : Boolean = CurrentUserId.is match {
> >         case Empty => false
> >         case Full(_) => true
> >         case _ => false
> >       }
> >
> >     Then:
> >
> >     Menu(Loc("forum", List("forum", "index"), "Forum",
> LocGroup("global"),
> >       If(isAuthenticated_?, "Please log in to see this page.")))
> >
> >
> >
> >
> > >
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to