Adobe ColdFusion 8

Example: Application.cfc

The Application.cfc page consists of the following:

<cfcomponent>
<cfset This.name = "Orders">
<cffunction name="OnRequestStart">
    <cfargument name = "request" required="true"/>
    <cflogin>
        <cfif IsDefined("cflogin")>
            <cfif cflogin.name eq "admin">
                <cfset roles = "user,admin">
            <cfelse>
                <cfset roles = "user">
            </cfif>
            <cfloginuser name = "#cflogin.name#" password = "#cflogin.password#"
                roles = "#roles#" />
        <cfelse>
            <!--- This should never happen. --->
            <h4>Authentication data is missing.</h4>
            Try to reload the page or contact the site administrator.
        <cfabort>
        </cfif>
    </cflogin>
</cffunction>
</cfcomponent>

Reviewing the code

The Application.cfc onRequestStart method executes before the code in each ColdFusion page in an application. For more information on the Application.cfc page and when it is executed, see Designing and Optimizing a ColdFusion Application.

The following table describes the CFML code in Application.cfc and its function:

Code

Description

<cfcomponent> <cfset This.name = "Orders"> <cffunction name="OnRequestStart"> <cfargument name = "request" required="true"/>

Identifies the application and starts the onRequestStart method that runs at the starts of each request. The login information on this page only applies to this application.

<cflogin> <cfif IsDefined("cflogin")> <cfif cflogin.name eq "admin"> <cfset roles = "user,admin"> <cfelse> <cfset roles = "user"> </cfif>

Executes if there is no logged-in user.

Makes sure the user is correctly logged in by the web server. (Otherwise, there would be no cflogin variable.)

Sets a roles variable based on the user's ID. Assigns users named "admin" to the admin role. Assigns all other users to the users role.

<cfloginuser name = "#cflogin.name#" password = "#cflogin.password#" roles = "#roles#" />

Logs the user into the ColdFusion security system and specifies the user's password, name, and roles. Gets the password and name directly from the cflogin structure.

<cfelse> <!--- This should never happen. ---> <h4>Authentication data is missing.</h4> Try to reload the page or contact the site administrator. <cfabort>

This code should never run, but if the user somehow got to this page without logging in to the web server, this message would display and ColdFusion would stop processing the request.

</cfif> </cflogin> </cffunction> </cfcomponent>

Ends the if/else block.

Ends the cflogin tag body.

Ends the onRequestStart method.

Ends the Application component.