Skip to content

Instantly share code, notes, and snippets.

@zefer
Created December 17, 2010 15:05
Show Gist options
  • Save zefer/745069 to your computer and use it in GitHub Desktop.
Save zefer/745069 to your computer and use it in GitHub Desktop.
An example DAO using cfmongodb. The model object is an API consumer.
<cfcomponent extends="beans.model.AbstractServiceObject" output="false" hint="">
<cffunction name="init" access="public" output="false" returntype="beans.model.ConsumerDAO">
<cfscript>
// something random to append to passwords to prevent reverse md5 lookups
variables.salt = "_8sh3m9cc"
super.init( argumentcollection: arguments );
variables.mongo = getFactory().getBean( "mongoFactory" ).getMongo( "model" );
// index on apikey, with unique constraint
mongo.dropIndexes(collectionName="consumers");
mongo.ensureIndex( fields=["apiKey"], collectionName="consumers", unique=true );
return this;
</cfscript>
</cffunction>
<cffunction name="create" access="public" output="false" returntype="void">
<cfargument name="consumer" type="beans.model.Consumer" required="true" />
<cfscript>
var stConsumer = {
"name": arguments.consumer.getName(),
"apiKey": arguments.consumer.getApiKey(),
"secret": hashSecret(arguments.consumer.getSecret()),
"dateCreated": now(),
"dateLastAuthenticated": javacast("null", ""),
"storeId": arguments.consumer.getStoreId()
};
mongo.save( stConsumer, "consumers" );
// get the mongodb id as a string
stConsumer.id = stConsumer._id.toString();
arguments.consumer.setID( stConsumer.id );
</cfscript>
</cffunction>
<cffunction name="read" access="public" output="false" returntype="void">
<cfargument name="consumer" type="beans.model.Consumer" required="true" />
<cfscript>
var stConsumer = 0
if( len(arguments.consumer.getId()) )
{
// get by id
try
{
stConsumer = mongo.findById( consumer.getId(), "consumers" );
}
catch(any e)
{
throw( type="dao.NullResult", message="No records matching 'consumer' id: #arguments.consumer.getId()#" );
}
}
else
{
// get by apikey
try
{
stConsumer = mongo.query( "consumers" ).$eq( "apiKey", arguments.consumer.getApiKey() ).search(limit=1).asArray()[1];
}
catch(any e)
{
throw( type="dao.NullResult", message="No records matching 'consumer' apiKey: #arguments.consumer.getApiKey()#" );
}
}
// get the mongodb id as a string
stConsumer.id = stConsumer._id.toString();
arguments.consumer.setId( stConsumer.id );
arguments.consumer.setName( stConsumer.name );
arguments.consumer.setApiKey( stConsumer.apiKey );
arguments.consumer.setSecret( stConsumer.secret );
arguments.consumer.setDateCreated( stConsumer.dateCreated );
arguments.consumer.setDateLastAuthenticated( stConsumer.dateLastAuthenticated );
arguments.consumer.setStoreId( stConsumer.storeId );
</cfscript>
</cffunction>
<cffunction name="authenticate" access="public" output="false" returntype="void">
<cfargument name="consumer" type="beans.model.Consumer" required="true" />
<cfscript>
var stAuthenticatedConsumer = 0;
try
{
stAuthenticatedConsumer = mongo.query( "consumers" ).$eq( "apiKey", arguments.consumer.getApiKey() ).$eq( "secret", hashSecret(arguments.consumer.getSecret()) ).search().asArray()[1];
}
catch(any e)
{
throw( type="dao.authenticationFailure", message="No consumer records with this apikey/secret combination" );
}
// get the mongodb id as a string
stAuthenticatedConsumer.id = stAuthenticatedConsumer._id.toString();
arguments.consumer.setId( stAuthenticatedConsumer.id );
arguments.consumer.setName( stAuthenticatedConsumer.name );
arguments.consumer.setDateCreated( stAuthenticatedConsumer.dateCreated );
arguments.consumer.setStoreId( stAuthenticatedConsumer.storeId );
if( structKeyExists(stAuthenticatedConsumer, "dateLastAuthenticated") )
{
arguments.consumer.setDateLastAuthenticated( stAuthenticatedConsumer.dateLastAuthenticated );
}
// time-stamp this successfull authentication
stAuthenticatedConsumer.dateLastAuthenticated = now();
mongo.save( stAuthenticatedConsumer, "consumers" );
</cfscript>
</cffunction>
<cffunction name="update" access="public" output="false" returntype="void">
<cfargument name="consumer" type="beans.model.Consumer" required="true" />
<cfscript>
var stConsumer = 0;
try
{
stConsumer = mongo.findById( arguments.consumer.getId(), "consumers" );
}
catch(any e)
{
throw( type="dao.NullResult", message="No records matching 'consumer' id: #arguments.consumer.getId()#" );
}
stConsumer.name = arguments.consumer.getName();
stConsumer.apiKey = arguments.consumer.getApiKey();
stConsumer.dateCreated = arguments.consumer.getDateCreated();
stConsumer.dateLastAuthenticated = arguments.consumer.getDateLastAuthenticated();
stConsumer.storeId = arguments.consumer.getStoreId();
mongo.update( stConsumer, "consumers" );
</cfscript>
</cffunction>
<cffunction name="delete" access="public" output="false" returntype="void">
<cfargument name="consumer" type="beans.model.Consumer" required="true" />
<cfscript>
mongo.removeById( arguments.consumer.getId(), "consumers" );
</cfscript>
</cffunction>
<cffunction name="exists" access="public" output="false" returntype="boolean">
<cfargument name="consumer" type="beans.model.Consumer" required="true" />
<cfscript>
try
{
stConsumer = mongo.findById( arguments.consumer.getId(), "consumers" );
return true;
}
catch(any e)
{
return false;
}
</cfscript>
</cffunction>
<cffunction name="hashSecret" access="private" output="false" returntype="string" hint="makes a non-reversible (encrypted) version of the password">
<cfargument name="secret" type="string" required="true" />
<cfreturn hash( arguments.secret & variables.salt ) />
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment