ColdFusion 9.0 Resources |
Application-based user security exampleThe following example shows how to 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:
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, do not follow the comma separators in the Roles fields with spaces. Example: Application.cfcThe 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> <cfif cflogin.name IS "" OR cflogin.password IS ""> <cfoutput> <h2>You must enter text in both the User Name and Password fields. </h2> </cfoutput> <cfinclude template="loginform.cfm"> <cfabort> <cfelse> <cfquery name="loginQuery" dataSource="cfdocexamples"> SELECT UserID, Roles FROM LoginInfo WHERE UserID = '#cflogin.name#' AND Password = '#cflogin.password#' </cfquery> <cfif loginQuery.Roles NEQ ""> <cfloginuser name="#cflogin.name#" Password = "#cflogin.password#" roles="#loginQuery.Roles#"> <cfelse> <cfoutput> <H2>Your login information is not valid.<br> Please Try again</H2> </cfoutput> <cfinclude template="loginform.cfm"> <cfabort> </cfif> </cfif> </cfif> </cflogin> <cfif GetAuthUser() NEQ ""> <cfoutput> <form action="securitytest.cfm" method="Post"> <input type="submit" Name="Logout" value="Logout"> </form> </cfoutput> </cfif> </cffunction> </cfcomponent> Reviewing the codeThe Application.cfc page 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:
Example: loginform.cfmThe loginform.cfm page consists of the following: <H2>Please Log In</H2> <cfoutput> <form action="#CGI.script_name#?#CGI.query_string#" method="Post"> <table> <tr> <td>user name:</td> <td><input type="text" name="j_username"></td> </tr> <tr> <td>password:</td> <td><input type="password" name="j_password"></td> </tr> </table> <br> <input type="submit" value="Log In"> </form> </cfoutput> Reviewing the codeThe following table describes the loginform.cfm page CFML code and its function:
Example: securitytest.cfmThe securitytest.cfm page shows how any application page can use ColdFusion user authorization features. Application.cfc ensures the existence of an authenticated user before the page content appears. The securitytest.cfm page uses the IsUserInAnyRole and GetAuthUser functions to control the information that is displayed. The securitytest.cfm page consists of the following: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Security test page</title> </head> <body> <cfoutput> <h2>Welcome #GetAuthUser()#!</h2> </cfoutput> ALL Logged-in Users see this message.<br> <br> <cfscript> if (IsUserInRole("Human Resources")) WriteOutput("Human Resources members see this message.<br><br>"); if (IsUserInRole("Documentation")) WriteOutput("Documentation members see this message.<br><br>"); if (IsUserInRole("Sales")) WriteOutput("Sales members see this message.<br><br>"); if (IsUserInRole("Manager")) WriteOutput("Managers see this message.<br><br>"); if (IsUserInRole("Employee")) WriteOutput("Employees see this message.<br><br>"); if (IsUserInRole("Contractor")) WriteOutput("Contractors see this message.<br><br>"); </cfscript> </body> </html> Reviewing the codeThe following table describes the securitytest.cfm page CFML code and its function:
|