Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Combining old application.cfm & new Application.cfc

Posted on 2010-09-22
9
Medium Priority
?
751 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
[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
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 2000 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 53

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 53

Expert Comment

by:_agx_
ID: 33744046
Welcome :)
0

Featured Post

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.

Question has a verified solution.

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

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

578 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