Solved

Combining old application.cfm & new Application.cfc

Posted on 2010-09-22
9
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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