• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

Combining old application.cfm & new Application.cfc

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
RedBullLee
Asked:
RedBullLee
  • 4
  • 3
  • 2
1 Solution
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
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
 
RedBullLeeAuthor Commented:
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
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Gurpreet Singh RandhawaWeb DeveloperCommented:
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
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
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
 
RedBullLeeAuthor Commented:
Thank you, myselfandhawa.

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

Much appreciated.
0
 
_agx_Commented:
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
 
RedBullLeeAuthor Commented:
Thanks, _agx_ - very useful, I'll keep that in mind.
0
 
_agx_Commented:
Welcome :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now