Example of User Authentication and AuthorizationThe following sample pages illustrate 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 use. In this example, a user requests a page in an application named Orders, which is part of a security context, also named Orders, that governs pages and resources for an order-tracking application. User security is generally handled in two steps:
Authenticating users in Application.cfmThe following example code for an Application.cfm page checks first to see whether the current user is authenticated by checking to see whether a login form was submitted. If the username and password can be authenticated for the current security context, the user passes through and the requested page is served. If the Application.cfm page does not receive the user's login information from the previous page, it prompts the user to provide a username and password. The user's response is checked against the list of valid users defined for the current security context.
If the user passes the authentication step, the requested page appears. The application uses the CGI variables All pages governed by this Application.cfm page - those in the same directory as Application.cfm and in its subtree - automatically invoke this authentication test.
Example: Application.cfm
Checking for authentication and authorization
Inside application pages, you can use the
The following sample page appears to users who pass the authentication test in the previous Application.cfm page. It uses the Example: orders.cfm<!--- First, check whether a form button was submitted ---> <cfif IsDefined("Form.btnUpdate")>
<!--- Is user is authorized to update or select
information from the Orders data source? --->
<cfif IsAuthorized("DataSource", "Orders", "update")>
<cfquery name="AddItem" datasource="Orders">
INSERT INTO Orders (Customer, OrderID)
VALUES #Customer#, #OrderID#
</cfquery>
<cfoutput query="AddItem">
Authorization Succeeded. Order information added:
#Customer# - #OrderID#<br>
</cfoutput>
<cfelse>
<cfabort showerror="You are not allowed to update order information.">
</cfif>
</cfif>
<cfif IsAuthorized("DataSource", "Orders", "select")>
<cfquery name="GetList" datasource="Orders">
SELECT *
FROM Orders
</cfquery>
Authorization Succeeded. Order information follows:
<cfoutput query="GetList">
#Customer# - #BalanceDue#<br>
</cfoutput>
<cfelse>
<cfabort showerror="You cannot view order information.">
</cfif>
|