Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controllers in Grails

I'm writing a small webapp in Grails and I have the following question regarding best practices for controller design and using GORM:

I'm storing the user object in session.user. Currently all my action methods start with the following code to make sure a valid user is logged in and that the user object is fresh:

class FooController {
  def actionMethodThatRequiresAValidUser = {
    if (!session?.user) {
      redirect(controller: "authentication", action: "login")
    }
    session.user.refresh()
    ...
    /* do stuff */
    ...
  }
}

Is that best practice? Can it be done in a better and/or more concise way?

like image 676
knorv Avatar asked Jan 27 '09 21:01

knorv


1 Answers

Use a filter, that way you can put that same repeated code in the filter and keep your controllers focussed on the real action.

like image 176
Bart Schuller Avatar answered Sep 29 '22 03:09

Bart Schuller