Pages

Thursday, January 29, 2015

Beginning Grails, Groovy and Griffon, error #1

Just found an error in the book. When you create a filter, the author suggests you to copy this code block to your app:

if (session?.user?.id != params?.id) {
flash.message = "You can only modify yourself!"
redirect(controller:"user", action: "index")
return false

}

But actually the params.id is a String and the session.user.id is a Long type, so this condition turns out to be executed every time, even when both values are 2, for example.

The working code would be like this:

if (session?.user?.id?.toInteger() != params?.id?.toInteger()) {
flash.message = "You can only modify yourself!"
redirect(controller:"user"action"index")
return false

}

No comments:

Post a Comment