Sumit Verma said the following on 01/06/2011 09:37 PM:
> Hi Guys,
>
> Just wanted to confirm that event args set in the xml config are not
> available in the preEvent plugin method. e.g.
No, event-args set in an event-handler are not available in the
preProcess plugin point. They are set when the event-handler is processed.
> <event-handler event="home" access="public">
>       <event-arg name="foo" value="bar" />
> </event-handler>
>
> So, in this case foo is not available in the preProcess or preEvent
> plugin method. I was setting up security and was hoping to just set
> <event-arg name="secureEvent" value="true" /> and then check for login
> in the preEvent.

Here's an example of using event-handler name matching.  It uses the
simple pattern matcher that is part of Mach-II 1.9 to do the wildcard *
type matching:

<plugin name="login" type="WebAssess.plugins.loginPlugin">
    <parameters>
        <parameter name="ignoreEvents">
            <array>
                <element value="home"/>
                <element value="login*"/>
                <element value="logout"/>
                <element value="preview.*"/>   
                <element value="help.*"/>
                <element value="sys.heartbeat"/>
                <element value="${properties.exceptionEvent}"/>
            </array>
        </parameter>
        <parameter name="notLoggedInEvent" value="login"/>
    </parameters>
</plugin>

Example code


<cfcomponent
    displayname="loginPlugin"
    extends="MachII.framework.Plugin"
    depends="sessionFacade"
    output="false"
    hint="Checks if a user is logged in.">

    <!---
    PROPERTIES
    --->
    <cfset variables.instance = StructNew() />
    <cfset variables.matcher = CreateObject("component",
"MachII.util.matching.SimplePatternMatcher").init() />
   
    <!---
    INITIALIZATION / CONFIGURATION
    --->
    <cffunction name="configure" access="public" returntype="void"
output="false"
        hint="Configures the plugin.">
       
        <cfif isParameterDefined("ignoreEvents") AND
IsArray(getParameter("ignoreEvents"))>
            <cfset
setIgnoreEvents(buildIgnoreEvents(getParameter("ignoreEvents"))) />
        <cfelse>
            <cfthrow type="LoginPlugin.invalidParameter"
                message="The parameter named 'ingoreEvents' is either
not defined in the plugin registration XML or not an array." />
        </cfif>
       
        <cfif isParameterDefined("notLoggedInEvent") AND
Len(getParameter("notLoggedInEvent"))>
            <cfset setNotLoggedInEvent(getParameter("notLoggedInEvent")) />
        <cfelse>
            <cfthrow type="LoginPlugin.invalidParameter"
                message="The parameter named 'notLoggedInEvent' is
either not defined in the plugin registration XML or does not have a
value." />
        </cfif>
    </cffunction>
   
    <!---
    PUBLIC FUNCTIONS
    --->
    <cffunction name="preProcess" access="public" returntype="void"
output="false"
        hint="Checks to see if the user is logged in during the
preProcess plugin point.">
        <cfargument name="eventContext"
type="MachII.framework.EventContext" required="true" />
       
        <cfset var event = arguments.eventContext.getNextEvent() />
        <cfset var eventName = event.getRequestName() />
        <cfset var ignoreEvents = getIgnoreEvents() / >
        <cfset var eH = "" />
        <cfset var persistData = StructNew() />
        <cfset var persistId = "" />
        <cfset var location = "" />
       
        <cfif NOT ignoreEvents.contains(eventName)
            AND NOT getSessionFacade().isLoggedIn()>

            <!--- Redirect the user to the login page --->
        </cfif>
    </cffunction>
   
    <!---
    PROTECTED FUNCTIONS
    --->
    <cffunction name="buildIgnoreEvents" access="private"
returntype="any" output="false"
        hint="Builds a HashSet of ignore events.">
        <cfargument name="rawIgnoreEvents" type="array" required="true" />
       
        <cfset var ignoreEvents = CreateObject("java",
"java.util.HashSet").init() />
        <cfset var pattern = "" />
        <cfset var eventHandlers =
getAppManager().getEventManager().getEventNames() />
        <cfset var i = 0 />
        <cfset var j = 0 />
                   
        <cfloop from="1" to="#ArrayLen(arguments.rawIgnoreEvents)#"
index="i">
            <cfif variables.matcher.isPattern(arguments.rawIgnoreEvents[i])>
                <cfloop from="1" to="#ArrayLen(eventHandlers)#" index="j">
                    <cfif
variables.matcher.match(arguments.rawIgnoreEvents[i], eventHandlers[j])>
                        <cfset ignoreEvents.add(eventHandlers[j]) />
                    </cfif>
                </cfloop>
            <cfelse>
                <cfset ignoreEvents.add(bindValue(i,
arguments.rawIgnoreEvents[i])) />
            </cfif>
        </cfloop>
       
        <cfreturn ignoreEvents />
    </cffunction>
   
    <!---
    ACCESSORS
    --->
    <cffunction name="setIgnoreEvents" access="private"
returntype="void" output="false">
        <cfargument name="ignoreEvents" type="any" required="true" />
        <cfset variables.instance.ignoreEvents = arguments.ignoreEvents />
    </cffunction>
    <cffunction name="getIgnoreEvents" access="private" returntype="any"
output="false">
        <cfreturn variables.instance.ignoreEvents />
    </cffunction>
   
    <cffunction name="setNotLoggedInEvent" access="private"
returntype="void" output="false">
        <cfargument name="notLoggedInEvent" type="string" required="true" />
        <cfset variables.instance.notLoggedInEvent =
arguments.notLoggedInEvent />
    </cffunction>
    <cffunction name="getNotLoggedInEvent" access="private"
returntype="string" output="false">
        <cfreturn variables.instance.notLoggedInEvent />
    </cffunction>

</cfcomponent>

-- 
You received this message because you are subscribed to Mach-II for CFML list.
To post to this group, send email to mach-ii-for-coldfusion@googlegroups.com
To unsubscribe from this group, send email to 
mach-ii-for-coldfusion-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/mach-ii-for-coldfusion?hl=en

SVN: http://svn.mach-ii.com/machii/
Wiki / Documentation / Tickets: http://trac.mach-ii.com/machii/

Reply via email to