My current project needs a very dynamic and fast pages without refreshs, or using the buzz word ajax, to accomplish this requirement I’m using ZK Direct RIA. As ZK heavily uses ajax requests with unpredictable url I need another way to protect the controller’s actions (composer’s method) instead of url. Using @Secured and groovy’s invokeMethod I was able to protect each method of any class (please read the post to get a better picture of what I’ll discuss) which works fines if the method call is from a groovy code, ZK is fully implemented in java though.
When a user clicks on a button, or does anything that generates events, zk process it and call the corresponding method in the composer class. ZK events processing executes this code when it find the method to call:
1
| |
This uses Java Reflection API and completely bypass groovy’s invokeMethod, what we need to do is use groovy’s InvokerHelper class instead of direct calling the method using reflection.
1
| |
Now that we know how to do it for any kind of java code let’s dig into the problem with ZK event and find out where we should use InvokerHelper.
Implementing InvokerHelper in GrailsComposer
The GrailsComposer in ZKGrails plugin is a subclass of
GenericForwardComposer that changes the default char separator from ‘$’
to ‘_’, because groovy already uses the ‘$’ with another meaning, and
allows the use of a closure as the doAfterCompose method).
So when you create a new composer using grails create-composer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
The idea here is that only some users have access to edit the text. ZK is fully AJAX based and every event cames from client through javascript request. Because of this even if the btnEditText is invisible a malicious users could forge a request to call the method - not easy, but possible - the use of @Secured is to assure that an attempt to call this method from an unauthorized user will fail.
If you run your app using ZKGrails 0.7.5, the current version for Grails 1.1.1, and test if an unauthorized user fails when call the method - just make the button always visible to test - you will see that nothing is blocked no AccessDeniedException is thrown, and of course this isn’t good. Humm, and how could we solve this? How to make zk event handling aware of the dynamic methods?
My first thought was “ZK is open source, what about reading it’s code to understand the event system?” and there I go, downloaded the source and start reading how ZK maps the methods name to components event, in other words how it does this magic:
1
| |
The above method is the same as the not so nice:
1
| |
Reading the source code I find out that this kind of event is a ForwardEvent but I couldn’t find where the original method is called until I look the class diagram for GrailsComposer:
And there was the solution GenericEventListener onEvent method, I’ve finally found where the original method (onClick_componentId) is invoked and now we just need to override onEvent in GrailsComposer, this is the implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
Just replacing method.invoke to InvokerHelper.invokeMethod (lines 13, 19, 24 and 27) is enough to make the methods actually secured. Open your GrailsComposer class and add this method to the class, this is all you need to do. If you don’t understand how the @Secured is now working, read the previous post which is about the implementation of @Secured.
I’ve submitted this as a patch to Chanwit, the plugin’s maintainer, and ZKGrails 1.0 will have this overridin onEvent out-of-the-box. No need to change the plugin source code anymore, great news.
