Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Please help: Losing Request Scope Variable Values

Question posted by: chromis (Newbie) on April 10th, 2008 08:39 AM
Hi there,

I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request scope. Application variables are then accessed in this manner Request.App.<Var>.

To begin with I had a simple functioning login system inside a subdirectory named admin, this subdirectory had it's own application.cfm, I wasn't sure whether to duplicate the method i used in the root application.cfm so i removed the admin/application.cfm and declared the vars i wanted in the root application.cfm.

Everything worked fine and I was able to log into the admin section as before, only when i tryed to navigate to a page in admin section my security component which deals with logging in etc, lost it's variable values.

I'm not sure why it is losing the information, and was hoping someone could point me in the right direction please?

Here is the relevant code:

/Application.cfm:

Code: ( text )
  1. <cfapplication name="myApp" sessionTimeout="#CreateTimeSpan(0, 0, 20, 0)#" sessionmanagement="yes" clientmanagement="yes">
  2.  
  3. <!--- initialise objects --->
  4. <cfsetting showdebugoutput="no">
  5. <cfimport taglib="taglib" prefix="func">
  6.    
  7. <!--- Create an exclusive lock for the Application scope and
  8.    set a default value for Application.Initialized. --->
  9. <cflock scope="Application" timeout="10" type="exclusive">
  10.   <cfparam name="Application.Initialized" default="false">
  11.   <!--- Create a Request variable to hold the value so you don't
  12.     need a read lock to get it in the next block of code. --->
  13.   <cfset Request.Initialized = Application.Initialized>
  14. </cflock>
  15.  
  16. <!--- If Request.Initialized is false, then the Application scope
  17.    has been reset and the variables need to be set. --->
  18. <cfif NOT Request.Initialized>
  19.   <!--- Create an exclusive Application scope lock and set the values. --->
  20.   <cflock scope="Application" timeout="10" type="exclusive">
  21.     <cfscript>
  22.       Application.Initialized = true;
  23.           <!--- define app level vars --->
  24.     </cfscript>
  25.   </cflock> 
  26. </cfif>
  27.  
  28. <!--- Create a read-only lock for the Application scope
  29.    and duplicate it into Request.App. --->
  30. <cflock scope="Application" timeout="10" type="readonly">
  31.   <cfset Request.App = Duplicate(Application)>
  32. </cflock>
  33.  
  34. <!--- Create a read-only lock for the Session scope
  35.    and duplicate it into Request.Ses. --->
  36. <cflock scope="Session" timeout="10" type="readonly">
  37.   <cfset Request.Ses = Duplicate(Session)>
  38. </cflock>
  39.  
  40. <func:initSecurity> <!--- Instantiates security component --->


/taglib/initSecurity.cfm

Code: ( text )
  1. <!--- init registration session --->
  2. <cflock scope="Session" timeout="10" type="readonly">
  3.     <cfscript>
  4.         if(NOT isDefined("Request.Ses.security")) {
  5.             Request.Ses.security = createObject("component","com.security");
  6.         }
  7.     </cfscript>
  8. </cflock>



/com/security.cfc


<!--- security component --->
Code: ( text )
  1. <cfcomponent >
  2.     <!--- define vars --->
  3.     <cfscript>
  4.         // datasource
  5.         This.dsn = Request.App.dsn;
  6.         // member details
  7.         This.memberid = "";
  8.         This.loggedIn = 0;
  9.         This.password = "";
  10.         This.username = "";
  11.         This.roles = "";
  12.         // referal info
  13.         This.returnURL = "";
  14.     </cfscript>
  15.     <cffunction name="validate" access="public" output="true" hint="validates username and password">
  16.         <cftry>
  17.             <cfquery name="validate" datasource="#This.dsn#">
  18.                 SELECT id,username,password,roles
  19.                 FROM cms_security
  20.                 WHERE username = '#This.username#'
  21.                 AND password = '#hash(This.password)#'
  22.             </cfquery>
  23.         <cfcatch type="database">
  24.             <p>Sorry, cannot validate user.</p>
  25.             <cfabort>
  26.         </cfcatch>
  27.         </cftry>
  28.         <cfscript>
  29.             if (validate.recordCount EQ 1) {
  30.                 This.roles = validate.roles;
  31.                 login();
  32.             }
  33.         </cfscript>
  34.         <!---
  35.         <cfif This.returnURL NEQ "">
  36.             <cfset tmpURL = This.returnURL>
  37.             <cfset This.returnURL = "">
  38.             <cflocation url="#tmpURL#" addtoken="no">
  39.         </cfif>
  40.         --->
  41.     </cffunction>
  42.     <cffunction name="login" access="private" output="false" hint="sets values for user session">
  43.         <cfscript>
  44.             This.loggedIn = 1;
  45.             This.password = "";
  46.         </cfscript>
  47.     </cffunction>
  48.     <cffunction name="logout" access="public" output="false" hint="clears security session, logging user out">
  49.         <cfscript>
  50.             This.memberid = "";
  51.             This.loggedIn = 0;
  52.             This.password = "";
  53.             This.username = "";
  54.             This.name = "";
  55.             This.roles = "";
  56.             // referal info
  57.             This.returnURL = "";
  58.         </cfscript>
  59.     </cffunction>
  60.     <cffunction name="protect" access="public" output="true" hint="checks user list of roles against one allowed role">
  61.         <cfargument name="roles">
  62.         <cfargument name="redirectURL" default="../index.cfm">
  63.         <cfif NOT isDefined("Request.Ses.security") OR Request.Ses.security.loggedIn EQ 0>
  64.        
  65.         <cflocation url="#redirectURL#" addtoken="no">
  66.             <cfdump var="#Request#"> <!--- Has lost security object at this point --->
  67.         </cfif>
  68.         <cfset roleFound = false>
  69.         <cfdump var="#Request.Ses#">
  70.         <cfloop list="#Request.Ses.security.roles#" index="sessionRole">
  71.             <cfif listFindNoCase(roles,sessionRole) GT 0><cfset roleFound = true></cfif>
  72.         </cfloop>
  73.         <cfif roleFound EQ false><cfdump var="#This.roles#"><!---<cflocation url="#redirectURL#" addtoken="no">---></cfif>
  74.        
  75.     </cffunction>
  76.     <cffunction name="protectGeneral" hint="protects page from un-logged-in users">
  77.         <cfargument name="redirectURL" default="../index.cfm">
  78.         <cfif NOT isDefined("Request.Ses.security")>
  79.             <cflocation url="#redirectURL#" addtoken="no">
  80.         </cfif>
  81.         <cfif Request.Ses.security.loggedIn NEQ 1>
  82.             <cflocation url="#redirectURL#" addtoken="no">
  83.         </cfif> 
  84.     </cffunction>
  85.     <cffunction name="findUsername" access="public" output="false" hint="checks to see if username is in db">
  86.         <cfargument name="dsn" required="yes">
  87.         <cfargument name="theUsername" required="yes">
  88.  
  89.         <cftry>
  90.             <cfquery name="get" datasource="#dsn#">
  91.                 SELECT username
  92.                 FROM cms_security
  93.                 WHERE username = '#theUsername#'
  94.             </cfquery>
  95.             <cfreturn get.username>
  96.          <cfcatch type="database">
  97.             <p>Sorry, cannot find username.</p>
  98.             <cfabort>
  99.         </cfcatch>
  100.         </cftry>
  101.     </cffunction>
  102.     <cffunction name="setNewPassword" access="public" output="false" hint="sets new password">
  103.     <cftry>
  104.         <cfquery name="set" datasource="#This.dsn#">
  105.             UPDATE cms_security
  106.             SET
  107.             password = '#hash(This.password)#'
  108.             WHERE id = #This.id#
  109.         </cfquery>
  110.         <cfset This.password="">
  111.     <cfcatch type="database">
  112.         <p>Sorry, setting of new password failed.</p>
  113.         <cfabort>
  114.     </cfcatch>
  115.     </cftry>
  116.     </cffunction>
  117. </cfcomponent>



The admin section has the following structure:

/admin/index.cfm
/admin/display/login.cfm - Login form
/admin/display/dologin.cfm - Shown on login
/admin/news/index.cfm
/admin/news/display/add.cfm
/admin/news/display/edit.cfm
/admin/news/display/view.cfm

I wasn't sure where to put the code to test the user so i have tryed in the index.cfm and the display/* files.

This code checks for the logged in user:
[CODE"]<cfset Request.Ses.security.protect("staff")>[/CODE]

The security object and it's variables exist when the /admin/index.cfm page is run I have checked with cfdump.

It's just when i try to access one of the function pages

/admin/news/index.cfm?action=add

the values stored in the security object get reset somehow.

Has anyone any ideas?

Thanks,

Chromis
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
acoder's Avatar
acoder
Site Moderator
9,896 Posts
April 10th, 2008
03:11 PM
#2

Re: Please help: Losing Request Scope Variable Values
If you're calling <cflocation>, then wouldn't the page have been redirected before you got to cfdump:
Code: ( text )
  1. <cflocation url="#redirectURL#" addtoken="no">
  2. <cfdump var="#Request#"> <!--- Has lost security object at this point --->

Reply
chromis's Avatar
chromis
Newbie
30 Posts
June 11th, 2008
09:53 AM
#3

Re: Please help: Losing Request Scope Variable Values
Quote:
Originally Posted by acoder
If you're calling <cflocation>, then wouldn't the page have been redirected before you got to cfdump:
Code: ( text )
  1. <cflocation url="#redirectURL#" addtoken="no">
  2. <cfdump var="#Request#"> <!--- Has lost security object at this point --->


Ah heheh, thanks acoder a rather silly mistake!

Reply
acoder's Avatar
acoder
Site Moderator
9,896 Posts
June 11th, 2008
09:59 AM
#4

Re: Please help: Losing Request Scope Variable Values
No problem! Is it April 11th or June 11th? :p

Reply
chromis's Avatar
chromis
Newbie
30 Posts
June 11th, 2008
10:25 AM
#5

Re: Please help: Losing Request Scope Variable Values
Quote:
Originally Posted by acoder
No problem! Is it April 11th or June 11th? :p


Heheh definately June, something not quite right on this forum!

Reply
Reply
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Coldfusion Forum Contributors