Srivi
Srivi

Reputation: 417

current sessions in Grails

I am a beginner in Grails 2.0 framework and I am trying to follow this tutorial http://grails.org/Simple+Avatar+Uploader . I implemented this code but I am getting an error in UserController.groovy at this line 'def user = User.current(session)' as ' No such property: User for class: grailtwitter.PersonController' I assume that this line takes the user's current session. I am looking for an explanation of how this works ?

Upvotes: 1

Views: 557

Answers (2)

This code is incomplete. The controller presupposes that you have a way of identifying the currently logged in user. Implicitly the line def user = User.current(session) assumes that you have defined a current() method on the user class itself that takes in a session and presumably uses some field that you've set in it to retrieve a User. That would be kind of dumb.

A common way to do this would be to build your own authentication mechanism. Note that this is a naive practice and you are far better off using Spring Security Core unless you want to leave your application open for gaping security holes. But, for practice, something like:

def login = {
    //if you're stupid enough to store your passwords in plain text and not sanitize user inputs, you deserve to be hacked
    def user = User.findByPassword(params.password) 
    if(user){
       session.user = user
    }
}

You could then replace the offending line in the tutorial (--def user = User.current(session)--) with

def user = session.user ?: new User(userid:"I'm a little teapot") 

Before you go much further, you probably want to walk through this free eBook on Grails before you get much further. Also highly recommend Grails in Action.

Upvotes: 1

chrislatimer
chrislatimer

Reputation: 3560

When I have run into this in the past it's because I didn't import the class and Grails/Groovy thinks I'm trying to access a variable called User rather than a method on the class.

Upvotes: 0

Related Questions