Skip to content

Instantly share code, notes, and snippets.

@mhenke
Created August 14, 2010 19:09
Show Gist options
  • Save mhenke/524601 to your computer and use it in GitHub Desktop.
Save mhenke/524601 to your computer and use it in GitHub Desktop.
<cffunction name="filters" returntype="void" access="public" output="false"
hint="Tells Wheels to run a function before an action is run or after an action has been run. You can also specify multiple functions and actions."
examples=
'
<!--- Always execute restrictAccess before all actions in this controller --->
<cfset filters("restrictAccess")>
<!--- Always execute isLoggedIn and checkIPAddress (in that order) before all actions in this controller except the home and login action --->
<cfset filters(through="isLoggedIn,checkIPAddress", except="home,login")>
'
categories="controller-initialization" chapters="filters-and-verification" functions="verifies">
<cfargument name="through" type="string" required="true" hint="Function(s) to execute before or after the action(s)">
<cfargument name="type" type="string" required="false" default="before" hint="Whether to run the function(s) before or after the action(s)">
<cfargument name="only" type="string" required="false" default="" hint="Pass in a list of action names (or one action name) to tell Wheels that the filter function(s) should only be run on these actions">
<cfargument name="except" type="string" required="false" default="" hint="Pass in a list of action names (or one action name) to tell Wheels that the filter function(s) should be run on all actions except the specified ones">
<cfargument name="cookie" type="string" required="false" default="" hint="Verify that the passed in variable name exists in the cookie">
<cfargument name="session" type="string" required="false" default="" hint="Verify that the passed in variable name exists in the session">
<cfargument name="params" type="string" required="false" default="" hint="Verify that the passed in variable name exists in the params">
<cfargument name="onfailure" type="string" required="false" default="" hint="Action to take if verification failed">
<cfscript>
var loc = {};
loc.iEnd = ListLen(arguments.through);
loc.verifications = arguments.controller.$getVerifications();
loc.iEnd = ArrayLen(loc.verifications);
loc.item = Trim(ListGetAt(arguments.through, loc.i));
for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
{
loc.jEnd = ListLen(loc.verification.params);
for (loc.j=1; loc.j <= loc.jEnd; loc.j++)
{
if (!StructKeyExists(arguments.params, ListGetAt(loc.verification.params, loc.j)))
loc.item = arguments.onfailure;
}
loc.jEnd = ListLen(loc.verification.session);
for (loc.j=1; loc.j <= loc.jEnd; loc.j++)
{
if (!StructKeyExists(session, ListGetAt(loc.verification.session, loc.j)))
loc.item = arguments.onfailure;
}
loc.jEnd = ListLen(loc.verification.cookie);
for (loc.j=1; loc.j <= loc.jEnd; loc.j++)
{
if (!StructKeyExists(cookie, ListGetAt(loc.verification.cookie, loc.j)))
loc.item = arguments.onfailure;
}
}
loc.item = Trim(ListGetAt(arguments.through, loc.i));
loc.thisFilter = {};
loc.thisFilter.through = loc.item;
loc.thisFilter.only = Replace(arguments.only, ", ", ",", "all");
loc.thisFilter.except = Replace(arguments.except, ", ", ",", "all");
if (arguments.type == "before")
ArrayAppend(variables.wheels.beforeFilters, loc.thisFilter);
else
ArrayAppend(variables.wheels.afterFilters, loc.thisFilter);
</cfscript>
</cffunction>
@mhenke
Copy link
Author

mhenke commented Aug 14, 2010

@mhenke
Copy link
Author

mhenke commented Aug 14, 2010

Actually, I should call $runVerifications and alter that to hand filters

@mhenke
Copy link
Author

mhenke commented Aug 14, 2010

tried to get it to run but am lost :-)

@raulriera
Copy link

What I would do is create a plugin, overwrite the filters method, add more arguments (the one you are trying to implement) and then write to logic to "use those" arguments and if everything works out ok call core.filters to actually filter the request...

You don't need to mess around with the code to make it work (I always wrap around in a plugin before adding stuff)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment