Grails Filters are very cool. Much better than action interceptors in my opinion. So much better that I will be surprised if I write another action interceptors.
There is just one small hiccup in the current version of Grails (1.0.1).
Lets say you create a User object and have your standard CRUD actions on it. What if you want to make sure that the only person allowed to update the user record is the actual user. You would neet to put guard logic on the UserController's Edit, Update, and Delete actions. You could do that or create a filter.
Give it a try using the filter below.
class UserFilters {
def filters =
userModificationCheck(controller:'user', action:'*') {
before = {
def currActionName = (actionName == null ? 'index' : actionName)
println "=================> BEFORE: The actionName is now ${currActionName}"
}
after = {
def currActionName = (actionName == null ? 'index' : actionName)
println "=================> AFTER: The actionName is now ${currActionName}"
}
}
}
}
This is a simple filter that prints the action name. After all you only want to do this for edit, update, and delete actions. So you want to make sure you can get the action names.
What you will find is that you use use a fully pathed URL such as http://localhost:8080/myApp/user/edit/1, everything works fine. But if you go to the User Listing > Select a User > and press the form's Edit button, . . . Ooops the action in the before filter doesn't say edit, is says index.
Probably not what you expected, I know I didn't. There is a bug in Grails 1.01, it is scheduled to be fixed in 1.0.2.
Luckly, there is a simple work around for this problem. In the filter, try printing out the params. You will discover a _action_**** entry in the params. Grails uses the entry to determine which action to call on the controller.
Try replacing the currActionName line with the following:
def currActionName = actionName ?: params.find {it.key ==~ "_action_.*"}?.value?.toLowerCase()
It uses a regular expression to find a key beginning with _action_ and graps the value.
This little work around should get you going again.
Wednesday, March 19, 2008
Grails Filters, Form Submission workaround
Subscribe to:
Post Comments (Atom)
7 comments:
Teilen Sie mir die Minute nicht zu? viagra preis levitra 10mg erfahrungen [url=http//t7-isis.org]levitra kaufen[/url]
This blog is really wonderful and very interesting. It caught my attention since the first time... I just enjoy the way like you redact the information.
I think they're excellent interceptors this is what I wanted to get, actually I did what you advice us here using the filters needed to achieve it.
Thanks mate... just dropped by. Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.
I liked this blog, i think is very interesting, most of all for the new ideas that this blog talk.
Thanks so much for this post. A combination of advice that I've heard before but always bears repeating; plus new tips that I really ought to consider
Thank you for the article, very worthwhile material.
Post a Comment