There was talk at one point of making a supertype decorator to allow the addition of methods to the supertype, I was wondering if anyone had done something like this yet?
I have a plugin called roleCheck.cfc with a method checkRole() that returns a Boolean result representing whether or not a user belongs to a particular role. I want to call that plugin’s method from my view, and I know I can do #getMyPlugin.checkRole(‘someRole’,session.User)# but I’d prefer to just do something sleek like #security(‘someRole’)# and have it call my plugin / method for me.
I need a method that I can call from views and layouts simply with:
<cfif #security(‘admin’)#>show something
I am using this to display partial content in a view depending on the roles the user has.
For handlers, etc I can just inject the plugin via wirebox and access it as an instance variable, and I suppose I could put the plugin into rc and call rc.myPlugin.checkRole() from my view, but I really would like to do it as I have illustrated above with the simple syntax.
To accomplish this, I added a method to the FrameworkSupertype.cfc:
<cfset local.args[‘role’] = arguments.role>
<cfif isDefined(“arguments.user”)>
<cfset local.args[‘user’] = arguments.user>
<cfreturn controller.getPlugin(argumentCollection=arguments).check(argumentCollection=local.args)>
This method is hard-coded to get my specific plugin (roleCheck) and then pass along the role and user arguments. The check() method of the plugin accepts two arguments, a role and a user to check the role on. If the user argument isn’t provided then the plugin tries to check session.User, and if session.User doesn’t exist, then the plugin returns false.
So this works fine when I add the above method to my FrameworkSupertype.cfc:
<cfif #security(‘admin’)#>show something
It calls the security() method, which gets the roleCheck plugin and runs the check() method of the plugin using the ‘admin’ role variable. The plugin defaults to session.User since I didn’t pass in a user argument and then returns true or false depending on if the session user has the provided role.
This:
<cfif #security(‘admin’,session.User)#>show something
Does the same thing, except since I provide the user argument, it is passed along to the plugin. (I do this so I can use non-session user objects to check roles for administrative purposes if I pass in the argument and just session user objects for actual permissions during a session if I don’t pass it in.)
Anyway, I have what I want working, but the part I need to think about is how to extend the supertype… if I update ColdBox, I’ll lose my new security() method, so I need this to be part of my application.
A FrameworkSupertype decorator seems to be the best fit, do we have a way to do this right now?