By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,069 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,069 IT Pros & Developers. It's quick & easy.

Please help: Losing Request Scope Variable Values

100+
P: 113
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:

Expand|Select|Wrap|Line Numbers
  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 --->
  41.  
/taglib/initSecurity.cfm

Expand|Select|Wrap|Line Numbers
  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 --->
Expand|Select|Wrap|Line Numbers
  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
Apr 10 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
If you're calling <cflocation>, then wouldn't the page have been redirected before you got to cfdump:
Expand|Select|Wrap|Line Numbers
  1. <cflocation url="#redirectURL#" addtoken="no">
  2. <cfdump var="#Request#"> <!--- Has lost security object at this point --->
Apr 10 '08 #2

100+
P: 113
If you're calling <cflocation>, then wouldn't the page have been redirected before you got to cfdump:
Expand|Select|Wrap|Line Numbers
  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!
Jun 11 '08 #3

acoder
Expert Mod 15k+
P: 16,027
No problem! Is it April 11th or June 11th? :p
Jun 11 '08 #4

100+
P: 113
No problem! Is it April 11th or June 11th? :p
Heheh definately June, something not quite right on this forum!
Jun 11 '08 #5

P: 1
May I ask a very basic question to you? Why are you setting request variables when you already have the variables set either in application or session? These two are global for either the period of time the application is running and the user session is active. I strongly suggest refraining from using request variables except when it is specifically for that page and only that page. Do not reinvent the wheel they say when it has already been done ie: do not push global variables to local variable status. Think about how the coldfusion server handles a datasource at the application level vs. at a local variable level. If you guessed it, so much caching going on for even one application. Cannot reuse cached queries for other users of same application. Request and local variables die once you leave that page and that will even generate more caching especially at the datasource, unique caching for every page that runs a query. Just some thoughts for you to think about.
Aug 11 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.