Solved

Combining old application.cfm & new Application.cfc

Posted on 2010-09-22
9
736 Views
Last Modified: 2012-05-10
Hi Folks,

I'm working on an application at the moment and looking to switch to using Coldfusion components, still new to me.

I have an existing application.cfm that I'm using for authentication.  I've also got the monkehTweets cfc (http://monkehtweet.riaforge.org/) for Twitter integration.  I'm looking to combine both Application files so that if the user has a Twitter account enabled (according to values in the database) I can set up the monkehTweets cfc ready for use.

At the moment I'm struggling to figure out how to combine these two - moreso when it comes to the 'if' part.

I've posted my existing application.cfm file below, together with logincheck.cfm (which sets the session variables) and Application.cfc.

I've also included a snippet of code used to interact with the monkeTweets API - this is partly where I'm struggling - at the moment I seem to be unable to add session values for accessToken, accessSecret and userAccountName.  Setting those values in the same way I currently do in logincheck.cfm doesn't seem to work.

Any help would be much appreciated!
<!--- Application.cfm --->

<cfset Datasource="mydatasource">
<cfapplication name="myapp" sessionmanagement="yes">
<CFIF NOT IsDefined("SESSION.Auth.IsLoggedIn")>
<CFINCLUDE TEMPLATE="login.cfm">
<CFABORT>
</CFIF>

Open in new window

<!--- only included on login page - not standalone page --->
<!--- ensure username and password are present --->
<CFPARAM NAME="Form.UserLogin" TYPE="string">
<CFPARAM NAME="Form.UserPassword" TYPE="string">
<!--- check the database for username and password --->
<CFQUERY NAME="GetUser" DATASOURCE="#datasource#">
SELECT *
FROM users
WHERE username='#Form.UserLogin#'
AND password='#Form.UserPassword#'
</CFQUERY>
<!--- if details are correct, then continue --->
<CFIF GetUser.RecordCount EQ 1>
<!--- remember logged-in status, plus details in structure --->
	<cflock type="exclusive" scope="session" timeout="30">
	<CFSET SESSION.Auth = StructNew()>
	<CFSET SESSION.Auth.IsLoggedIn="Yes">
	<CFSET SESSION.Auth.firstname="#GetUser.firstname#">
    <CFSET SESSION.Auth.surname="#GetUser.surname#">
    <cfset SESSION.Auth.wUserID="#GetUser.wUserID#">
	</cflock>
    <!--- Now look for a Twitter Account --->
    <cfquery name="getTwitter" datasource="#datasource#">
    SELECT *
    FROM useraccounts
    WHERE wUserID="#GetUser.wUserID#"
    </cfquery>
    <cfif getTwitter.recordcount EQ 1>
    <CFSET SESSION.Auth.accessToken="#getTwitter.accessToken#">
	<CFSET SESSION.Auth.accessSecret="#getTwitter.accessSecret#">
	<CFSET SESSION.Auth.screen_name="#getTwitter.screen_name#">
    </cfif>
<!--- user is now logged in, so send to start page --->
</CFIF>
<CFLOCATION URL="#CGI.SCRIPT_NAME#">

Open in new window

<!--- Application.cfc - part of monkehTweets --->

<cfcomponent output="true">
 
 
	<!--- Set up the application. --->
	<cfscript>
		this.Name 				= "monkehTweet_V1.2";
		this.ApplicationTimeout = CreateTimeSpan( 0, 0, 1, 0 );
		this.SessionManagement 	= true;
		this.SetClientCookies 	= true;
		//do this for CF7
		this.mappings 			= structnew();
		//do this for CF 8+
		this.mappings['/com'] 	= GetDirectoryFromPath(GetCurrentTemplatePath()) & "/com";
	</cfscript>	
  
 
	<cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false">
 		<cfscript>
 			
			application.objMonkehTweet = createObject('component',
		        'com.coldfumonkeh.monkehTweet')
				.init(
					consumerKey			=	'mykey',
					consumerSecret		=	'mysecret',
					parseResults		=	true
				);
			return true;
		</cfscript>
	</cffunction>
	
	<cffunction name="onrequestStart">
		<cfscript>
		if(structKeyExists(url, 'reinit')) {
			onApplicationStart();
		}
		</cfscript>
	</cffunction>
 
</cfcomponent>

Open in new window

<!--- Snippet - example of code used to interact with the monkehTweet CFC --->

<cfscript>
	// We also need to set the values into the authentication class inside monkehTweets
	application.objMonkehTweet.setFinalAccessDetails(
									oauthToken			= 	session['accessToken'],
									oauthTokenSecret	=	session['accessSecret'],
									userAccountName		=	session['screen_name']
								);
	returnData = application.objMonkehTweet.getHomeTimeline(format='json');
</cfscript>

Open in new window

0
Comment
Question by:RedBullLee
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
My Suggestion is remove Application.cfm and use only Application.cfc File, if your website has some pages which can be accessed using the member section, you can call the login include page in the onRequestStart Method!

if ur website has some static pages which must be avaliable to all users irrespective of login system, you can use methods:

1. Either create a template and include the templates which you want to exclude from bypassing the Coldfusion login Process

2. You can move the Login.cfm file from application.cfc and define in the other file and lock members area using the IsUserInRole or getAuth() User values.

Both ways work fine and you can avoid confusion in your application

0
 

Author Comment

by:RedBullLee
Comment Utility
Thanks for the reply, myselfandhawa.

My intention is indeed to get rid of the application.cfm file.

I'm basically trying to create a whole new Application.cfc that brings together the parts I need from the 2 existing files (plus the login handling).  

You do raise a point that only some pages will need to be protected, so perhaps I need to move that out anyway.

My main confusion is figuring out how to set the session values using a .cfc (all the code examples I can find use a totally different structure to the way I've been doing it in application.cfm), and how to bring in the parts from the monkehTweets Application.cfc that I need *if* there is a valid twitter account for the user logging in.
0
 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
just setup ur sessions in the onsessionstart method as:

<cfset session.user = "">
<cfset session.email = "">

etc etc

and then u can use the same in ur authencation
0
 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
like this

<cffunction name="OnSessionStart">
<CFSET SESSION.Auth = StructNew()>
      <CFSET SESSION.Auth.IsLoggedIn="No">
      <CFSET SESSION.Auth.firstname="">
    <CFSET SESSION.Auth.surname="">
    <cfset SESSION.Auth.wUserID="">

</cffunction>
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 15

Accepted Solution

by:
myselfrandhawa earned 500 total points
Comment Utility
u can also check this link for furthur reference:

best

http://www.strikefish.com/index.cfm?mode=entry&entry=585F0E1E-E19B-31A5-4357446E2AF47653

or correcting my above OnsessionStart as:

<cffunction name="OnSessionStart" access="public" returntype="void" output="false" hint="Fires when the session is first created.">
 cffunction
name="OnSessionStart"
access="public"
returntype="void"
output="false"
hint="Fires when the session is first created.">
 
<CFSET SESSION.Auth = StructNew()>
      <CFSET SESSION.Auth.IsLoggedIn="No">
      <CFSET SESSION.Auth.firstname="">
    <CFSET SESSION.Auth.surname="">
    <cfset SESSION.Auth.wUserID="">

<!--- Return out. --->
<cfreturn />
</cffunction>


also do onSessionEnd as:

<cffunction name="OnSessionEnd" access="public" returntype="void" output="false" hint="Fires when the session is terminated.">
 <!--- Define arguments. --->
<cfargument name="SessionScope" type="struct" required="true">
 <cfreturn />
</cffunction>
0
 

Author Comment

by:RedBullLee
Comment Utility
Thank you, myselfandhawa.

That's been a huge help - I think I can get my head around it now.

Much appreciated.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Two small comments

>> <CFIF NOT IsDefined("SESSION.Auth.IsLoggedIn")>

1) If you're setting a default for IsLoggedIn in OnSessionStart, then your current login test will always be true. So don't forget to change the logic

>> <cflock type="exclusive" scope="session" timeout="30">

2)   Unless you're concerned about race conditions, you don't need to lock. At least not in versions MX6+
0
 

Author Comment

by:RedBullLee
Comment Utility
Thanks, _agx_ - very useful, I'll keep that in mind.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Welcome :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now