Solved

Combining old application.cfm & new Application.cfc

Posted on 2010-09-22
9
743 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 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33736587
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
ID: 33736695
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 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33737539
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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33741718
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
 
LVL 16

Accepted Solution

by:
Gurpreet Singh Randhawa earned 500 total points
ID: 33741738
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
ID: 33742649
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_
ID: 33743916
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
ID: 33743986
Thanks, _agx_ - very useful, I'll keep that in mind.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33744046
Welcome :)
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Coldfusion CFMESSAGEBOX Passing Variables 6 138
CFIf Syntax and Logic 27 65
SQL script to confirm data has been entered into a table. 2 22
Application.cfm not found issue 2 34
This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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