How to retrieve the current logged in user?

13 views
Skip to first unread message

Oscar Picasso

unread,
Nov 18, 2007, 9:06:29 PM11/18/07
to liftweb
I have created a User class and User object like in the wiki example.

I can sign-up and login.

But how can I get the current logged user in the code, for example if
I want to display its name?

Steve Jenson

unread,
Nov 18, 2007, 9:25:51 PM11/18/07
to lif...@googlegroups.com
User.currentUser works from within the request cycle (so not in
CometActors). It's in the User object.

HTH,
Steve

David Pollak

unread,
Nov 19, 2007, 12:27:37 AM11/19/07
to lif...@googlegroups.com
On Nov 18, 2007 6:25 PM, Steve Jenson <ste...@gmail.com> wrote:

User.currentUser works from within the request cycle (so not in
CometActors)

This limitation is removed in version 381 (just posted.)

 
. It's in the User object.

HTH,
Steve

On Nov 18, 2007 6:06 PM, Oscar Picasso <oscarp...@gmail.com > wrote:
>
> I have created a User class and User object like in the wiki example.
>
> I can sign-up and login.
>
> But how can I get the current logged user in the code, for example if
> I want to display its name?
> >
>






--
lift, the secure, simple, powerful web framework
http://liftweb.net
Collaborative Task Management http://much4.us

Oscar Picasso

unread,
Nov 20, 2007, 5:45:27 PM11/20/07
to liftweb
Funny it returns a Can.

I want to print the user name and "You are not logged" if there is no
current user.

I guess I have to open the can to get the actual user. I was thinking
of using openOr but not sure how.

What is the idiomatic way to do what I want?

On Nov 19, 12:27 am, "David Pollak" <feeder.of.the.be...@gmail.com>
wrote:
> On Nov 18, 2007 6:25 PM, Steve Jenson <ste...@gmail.com> wrote:
>
>
>
> > User.currentUser works from within the request cycle (so not in
> > CometActors)
>
> This limitation is removed in version 381 (just posted.)
>
>
>
> > . It's in the User object.
>
> > HTH,
> > Steve
>

Viktor Klang

unread,
Nov 20, 2007, 5:49:56 PM11/20/07
to lif...@googlegroups.com
On Nov 20, 2007 11:45 PM, Oscar Picasso <oscarp...@gmail.com> wrote:

Funny it returns a Can.

I want to print the user name and "You are not logged" if there is no
current user.

I guess I have to open the can to get the actual user. I was thinking
of using openOr but not sure how.

You mean User.currentUser openOr "You are not logged in!" ?

Excuse me if i didn't understand you correctly, it's really late here and I should be sleeping. :(

Best regards
-Viktor
 

David Pollak

unread,
Nov 20, 2007, 5:50:01 PM11/20/07
to lif...@googlegroups.com

On Nov 20, 2007, at 2:45 PM, Oscar Picasso wrote:

>
> Funny it returns a Can.

Yes, a Can[User]

>
> I want to print the user name and "You are not logged" if there is no
> current user.
>
> I guess I have to open the can to get the actual user. I was thinking
> of using openOr but not sure how.

User.currentUser.map(_.lastName).openOr("Not Logged In")

>
> What is the idiomatic way to do what I want?
>
> On Nov 19, 12:27 am, "David Pollak" <feeder.of.the.be...@gmail.com>
> wrote:
>> On Nov 18, 2007 6:25 PM, Steve Jenson <ste...@gmail.com> wrote:
>>
>>
>>
>>> User.currentUser works from within the request cycle (so not in
>>> CometActors)
>>
>> This limitation is removed in version 381 (just posted.)
>>
>>
>>
>>> . It's in the User object.
>>
>>> HTH,
>>> Steve
>>
>>> On Nov 18, 2007 6:06 PM, Oscar Picasso <oscarpica...@gmail.com>
>>> wrote:
>>
>>>> I have created a User class and User object like in the wiki
>>>> example.
>>
>>>> I can sign-up and login.
>>
>>>> But how can I get the current logged user in the code, for
>>>> example if
>>>> I want to display its name?
>>
>> --
>> lift, the secure, simple, powerful web frameworkhttp://liftweb.net
>> Collaborative Task Managementhttp://much4.us
> >

--
David Pollak
http://blog.lostlake.org

Oscar Picasso

unread,
Nov 20, 2007, 6:05:54 PM11/20/07
to liftweb
Let me rephrase it:

If a user is logged, I want to display its name
otherwise I want to display the message "You are not logged"

David's answer if very close to what I want.

I am trying now to understand the implications of his code fragment.

On Nov 20, 5:49 pm, "Viktor Klang" <viktor.kl...@gmail.com> wrote:
> On Nov 20, 2007 11:45 PM, Oscar Picasso <oscarpica...@gmail.com> wrote:
>
>
>
> > Funny it returns a Can.
>
> > I want to print the user name and "You are not logged" if there is no
> > current user.
>
> > I guess I have to open the can to get the actual user. I was thinking
> > of using openOr but not sure how.
>
> You mean *User.currentUser openOr "You are not logged in!" *?

David Pollak

unread,
Nov 20, 2007, 6:17:35 PM11/20/07
to lif...@googlegroups.com
Are you a java guy?

On Nov 20, 2007, at 3:05 PM, Oscar Picasso <oscarp...@gmail.com>
wrote:

>

Jorge Eugenio Ortiz Hinojosa

unread,
Nov 20, 2007, 6:21:45 PM11/20/07
to lif...@googlegroups.com
There are three kinds of Cans: Full cans, Empty cans, and Failure cans
(which are essentially just Empty cans with some attached info about
why the operation failed).

The "map" and "openOr" methods are defined on all Cans, but have
different behavior depending on the type of Can that they are applied
to. When "map" is called on a Full can, it will apply the given
function to the contents of the Can, returning a Can with the result.
When "map" is applied on an Empty can, it returns the Empty can. When
"openOr" is called on a Full can, it returns the contents of the Can.
When "openOr" is called on an Empty can, it returns the argument to
"openOr".

So let's look at David's code:

User.currentUser.map(_.lastName).openOr("Not Logged In")

First, let's desugar the syntax. The "_.lastName" is Scala shorthand
for defining a function. In this case, it's shorthand for something
like:

(u: User) => u.lastName

That is to say, it's a function that takes a User and returns a String
of the user's last name.

So: User.currentUser returns a Can. The Can is Full (and contains the
current logged in user) if there is a current logged in user. The Can
is Empty if no user is logged in.

Then map(_.lastName) will return a Full can (with a string containing
the current logged in user's last name) if there is a current logged
in user. It will return an Empty can if no user is logged in.

Finally, openOr("You are not logged in") will return the contents of
the Can if the Can is Full (in this case, the content is the current
logged in user's last name), or "You are not logged in" if the Can is
Empty.

The Can is just a more advanced version of Scala's Option class. You
might want to read David's blog post about how lift used to use the
Option class. (Now lift uses Can, but the concepts are the same. Just
mentally replace "Some" with "Full", "None" with "Empty", and
"getOrElse" with "openOr".)

http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html

--j

Oscar Picasso

unread,
Nov 20, 2007, 6:28:58 PM11/20/07
to liftweb
So if I understand correctly:
1-
map(_.lastName) is just a shortcut for map(u => u.lastName)

2-
if there is a logged user:
currentUser returns a Full[User] Can "representing" the user
we can then call its map method which returns a Full[???] Can
"representing" the lastname
which we can open, because it is full, to get the actual lastname

if there is no user logged:
currentUser return an Empty[User] Can "representing" a none (?) user
we can then call its map method which returns an Empty[???] Can
"representing" a none(?) lastname
we call then openOr on it. As this last Can is empty it returns the
message "You are not logged"

Is this more or less what happens?

What if I want to display something like :
"First name: " David
"Last name: " Pollack

Oscar Picasso

unread,
Nov 20, 2007, 6:38:36 PM11/20/07
to liftweb
I do mostly java for a living.

I also coded in python and tried ruby and lisp and some other
languages.

I really like lisp but didn't code anything serious with it.

On Nov 20, 6:17 pm, David Pollak <d...@athena.com> wrote:
> Are you a java guy?
>
> On Nov 20, 2007, at 3:05 PM, Oscar Picasso <oscarpica...@gmail.com>

Jorge Eugenio Ortiz Hinojosa

unread,
Nov 20, 2007, 6:43:33 PM11/20/07
to lif...@googlegroups.com
On 11/20/07, Oscar Picasso <oscarp...@gmail.com> wrote:
>
> So if I understand correctly:
> 1-
> map(_.lastName) is just a shortcut for map(u => u.lastName)

Yes.

> 2-
> if there is a logged user:
> currentUser returns a Full[User] Can "representing" the user
> we can then call its map method which returns a Full[???] Can
> "representing" the lastname
> which we can open, because it is full, to get the actual lastname

Yes. Here ??? == String. Scala's type inferencer figures out that
since _.lastName returns a String, then
User.currentUser.map(_.lastName) must be a Can[String] (either Full or
Empty).

> if there is no user logged:
> currentUser return an Empty[User] Can "representing" a none (?) user
> we can then call its map method which returns an Empty[???] Can
> "representing" a none(?) lastname
> we call then openOr on it. As this last Can is empty it returns the
> message "You are not logged"

Yes. It'd be a little more accurate to say that currentUser returns an
Empty Can[User], and map returns an Empty Can[String] (technically,
User is parametrizing Can, not Empty, but you have the right idea).

> Is this more or less what happens?
>
> What if I want to display something like :
> "First name: " David
> "Last name: " Pollack

Then you could change the map function. For example:

User.currentUser.map(u => "First: " + u.firstName + " Last: " +
u.lastName).openOr("You are not logged in")

Hope this helps,

--j

Oscar Picasso

unread,
Nov 20, 2007, 6:45:13 PM11/20/07
to liftweb
Thanks for the explanation.

You just wrote what I was more or less trying to grasp in the
meantime.

I started to read the David's blog about option but didn't fully
understand its implications even if I felt there was probably some
powerful concept behind.

I'll read it again.

On Nov 20, 6:21 pm, "Jorge Eugenio Ortiz Hinojosa"
> http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-clas...
>
> --j

David Pollak

unread,
Nov 20, 2007, 6:46:40 PM11/20/07
to lif...@googlegroups.com
Oscar,

I was going to do a "when you don't have a valid value in Java, you
return a null" rant, but it looks like Jorge beat me to the punch and
wrote an excellent explanation of the code fragment.

Like Lisp, the optimal Scala constructs are transformative rather
than flow of control. map is a great transformative tool. Whenever
I have to solve a problem in Scala, I think "how will map, flatMap,
and filter help me with this?" They are transformative.

Thanks,

David

>>>>>> lift, the secure, simple, powerful web frameworkhttp://
>>>>>> liftweb.net

Jorge Eugenio Ortiz Hinojosa

unread,
Nov 20, 2007, 6:54:15 PM11/20/07
to lif...@googlegroups.com
> Like Lisp, the optimal Scala constructs are transformative rather
> than flow of control. map is a great transformative tool. Whenever
> I have to solve a problem in Scala, I think "how will map, flatMap,
> and filter help me with this?" They are transformative.

Sage advice...

As I was replying to this thread, a few lightbulbs went off in my
head, and I went back to my own code and sprinkled it with more uses
of map, flatMap, and fliter. I shaved off some lines of code and made
it even prettier :)

--j

Oscar Picasso

unread,
Nov 20, 2007, 7:00:43 PM11/20/07
to liftweb
I am just trying to understand both scala and lift.

I have to say that working with lift and trying to understand what you
have done is also a very good exercise to a better understanding of
scala.

I love the "lipsy"/functional flavor of what I have seen.

Oscar Picasso

unread,
Nov 20, 2007, 9:37:41 PM11/20/07
to liftweb
There is still something I don't understand.

I have tried using the script/console:

Full("Hello").map(_.length)
returns Full(5)

but
Empty.map(_.length)
gives me
<console>:6: error: value length is not a member of Nothing
val res7 = Empty.map(_.length)

In the previous example, when no user was logged, the returned Can by
User.currentUser was Empty but I didn't get an error with
map(_.lastname) even if empty has no lastname method.

Why Empty.map(_.length) gives me an errror?

Jorge Eugenio Ortiz Hinojosa

unread,
Nov 21, 2007, 12:17:54 AM11/21/07
to lif...@googlegroups.com
The Scala interpreter doesn't have enough information to correctly
infer that you want an Empty Can of type String. Right now it's
inferring you have an Empty Can of type Nothing. Your code is
equivalent to the following:

val e: Can[Nothing] = Empty
e.map(_.length)

Even though "e" is an Empty Can, if it wasn't Empty, it's contents
would be a Nothing, and Nothings don't have a "length" method defined.

Try the following instead:

val e: Can[String] = Empty
e.map(_.length)

This explicitly declares e to be a Can of type String (as opposed to
Nothing). Since Strings have a "length" method, then we can call
"length" on whatever is inside (if there were something inside).

Hope this makes sense,

--j

Eric Willigers

unread,
Nov 21, 2007, 1:44:07 AM11/21/07
to lif...@googlegroups.com
On Nov 21, 2007 1:37 PM, Oscar Picasso <oscarp...@gmail.com> wrote:
>
> There is still something I don't understand.
>
> I have tried using the script/console:
>
> Full("Hello").map(_.length)
> returns Full(5)
>
> but
> Empty.map(_.length)
> gives me
> <console>:6: error: value length is not a member of Nothing
> val res7 = Empty.map(_.length)
>
> In the previous example, when no user was logged, the returned Can by
> User.currentUser was Empty but I didn't get an error with
> map(_.lastname) even if empty has no lastname method.
>
> Why Empty.map(_.length) gives me an errror?

The problem is with the declared type.

Use
(Empty: Can[String]).map(_.length)
or


val e: Can[String] = Empty
e.map(_.length)

or
(None: Option[String]).map(_.length)

When you just have "Empty", with no context to indicate the type of
Can, Scala treats it as an empty Can of Nothing.

David Bernard

unread,
Nov 21, 2007, 4:33:13 AM11/21/07
to lif...@googlegroups.com
Very interesting thread, I copy the response from Jorge Eugenio Ortiz into the wiki
http://liftweb.net/index.php/FAQ

David Pollak

unread,
Nov 21, 2007, 10:32:10 AM11/21/07
to lif...@googlegroups.com
On Nov 21, 2007 1:33 AM, David Bernard <dwa...@free.fr> wrote:

Very interesting thread, I copy the response from Jorge Eugenio Ortiz into the wiki
http://liftweb.net/index.php/FAQ

Thanks!
 
Reply all
Reply to author
Forward
0 new messages