Search code examples
grailscontrollergrails-orm

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?


Solution

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