This screen shot shows the CFO object as a key within
the variables structure. You can see each function (or
method) available within the CFO object. You can also
see the property, “DSN" with its value, “companyInfo" in
the right column.
Building a CFC with the cfscript function Let’s
go one step further and add a new method to the CFC.
Because I’m a cfscript
fan, I won’t use the cffunction tags. This time, I’ll
build the CFC method with the cfscript function.
At the bottom of your CFC file, (just above
</CFCOMPONENT> )
add the following script: <CFSCRIPT>
function authenticateUser(username, password) {
var SQLString = "SELECT UserID FROM LoginInfo
WHERE UserID='#arguments.username#'
AND Password = '#arguments.password#'";
/* perform the Query, remember, the DSN is already set! */
var checkUser=this.QUERY(SQLString);
/* Trace the SQL Statement to the Browser */
this.OUTPUT("Trace: " & SQLString, true);
/* Return a value of true or false to the user */
if (checkUser.recordCount neq 0) return true;
else return false;
}
</CFSCRIPT>
This cfscript function will call some of
the functions you declared within the CFC. (Note, that’s
where the “this" comes in). The “this" is not required,
but it’s always preferable to scope variables so that
ColdFusion doesn’t search all the server to find it.
Now I’ll step through the
function line by line. First, the function,
authenticateUser, was defined with two arguments,
“username" and “password". Next, a SQL string was
developed to select the UserID from the LoginInfo table,
where the supplied username and password arguments are
passed.
The value of SQLString is then
sent to the “QUERY" function, defined earlier within the
CFC. The data returned will be stored in a local
function variable called "checkUser." The data source is
not required, because it is set as a property of the
ColdFusion (CFO) object. Following the query, a simple
cfoutput tag lets you
see what was sent to the query.
Finally, a recordCount challenge for the query, named
checkUser checks for a value of zero. If it isn’t zero,
it means a match was found, and a Boolean value of
“true" is sent back to the caller.
Tip: You should be aware that the
SQL mentioned in the above code has the potential of an
injection hack (someone putting some malicious code
within the login and password fields). To compensate for
that, you can (optionally) escape the single quotes
using a replace command: var SQLString = "Select UserID From LoginInfo where
UserID='#replace(arguments.username,"'","''","ALL")#' and
Password = '#replace(arguments.password,"'","''","ALL")#'";
Accessing the authenticateUser()
function Now the authentication function can
now be accessed from any ColdFusion page by simply
calling the authenticate method, passing the username
and password. <CFSCRIPT>
IF (CFO.authenticateUser("BobZ", "Ads10")) {
/* User Authenticated*/
CFO.Output("Success!");
}
ELSE {
/* User Login Failed */
CFO.Output("Failed!");
}
</CFSCRIPT>
In most cases, you wouldn’t hardcode the username and
password. I only hardcoded the values for demonstration
purposes. Typically, you’d use parameters sent from a
form post method, as follows: CFO.authenticateUser(FORM.username, FORM.password)
Clearly you can do a lot with this concept. I hope
I've helped you see what you can achieve with this
approach. Certainly, there are other ways to achieve
what I've set out to do in this column. This article was
intended to start you thinking about objects, methods
and properties. Object-oriented ColdFusion development
is powerful. If you want to excel in ActionScript with
Macromedia Flash MX or the Macromedia Flash
Communication Server, I strongly recommend you read more
about them in the application developer centers and in
books.
You can find a lot of functions on the Common
Function Library Project. This open-source
repository of ColdFusion functions is managed by Ray
Camden and Rob Brook-Bilson. Check it out, there are
already a number of ColdFusion Tag Libraries converted
to cfscript
functions.
|