User Guide

Table Of Contents
394 Chapter 16: Securing Applications
Application-based user security example
The example in this section shows how you might implement user security by authenticating
users and then allowing users to see or use only the resources that they are authorized to access.
This example has three ColdFusion pages:
The Application.cfc page contains the authentication logic that checks whether a user is logged
in, requests the login page if the user is not logged in, and authenticates the data from the login
page. If the user is authenticated, it logs the user in.
This page also includes the one-button form and logic for logging out a user, which appears at
the top of each page.
The loginform.cfm page displays the login form. The code on this page could also be included
in Application.cfc.
The securitytest.cfm page is a sample application page. It displays the logged-in users roles.
You can test the security behavior by adding your own pages to the same directory as the
Application.cfc page.
The example gets user information from the LoginInfo table of the cfdocexamples database that is
installed with ColdFusion. You can replace this database with any database containing UserID,
Password, and Roles fields. The sample database contains the following data:
Because spaces are meaningful in roles strings, you should not follow the comma separators in the
Roles fields with spaces.
Example: Application.cfc
The Application.cfc page consists of the following:
<cfcomponent>
<cfset This.name = "Orders">
<cfset This.Sessionmanagement="True">
<cfset This.loginstorage="session">
<cffunction name="OnRequestStart">
<cfargument name = "request" required="true"/>
<cfif IsDefined("Form.logout")>
<cflogout>
</cfif>
<cflogin>
<cfif NOT IsDefined("cflogin")>
<cfinclude template="loginform.cfm">
<cfabort>
<cfelse>
UserID Password Roles
BobZ Ads10 Employee,Sales
JaniceF Qwer12 Contractor,Documentation
RandalQ ImMe Employee,Human Resources,Manager