Problem concatenating function with string in client scope variable in Application.cfc

110 Views Asked by At

I want to set a client scope variable which is a combination of text and the output from a function. No matter how I format the cfset I can't get the result I need.

I am using session management and the users are authenticated against database tables. So I use the GetAuthUser() function to get the user name.

In my application.cfc I want to prefix the username with a pathname string. But I can't seem to get the syntax correct. I always end up with only the string. I start and stop the Coldfusion sever to make sure the client scope is cleared but it makes no difference.

I would prefer to use cfset for this and not cfscript.

This is the function in my application.cfc:

<cffunction name="OnSessionStart" >

  <cfset client.customerpath="C:\Dropbox\CustomerArea\" & #GetAuthUser()#>

</cffunction>

A cfdump shows only C:\Dropbox\CustomerArea\

The GetAuthUser() on it's own works fine. I just can't seem to prefix it.

3

There are 3 best solutions below

2
Shawn On

First, verify that your Login is working correctly. I believe that your code above is functioning correctly, but the login is breaking down somewhere, so your getAuthUser() is blank.

After that is verified, your above concatenation should work as expected.

Or, there are a couple of other ways to concatenate a string. Off the top of my head:

Your method:

<cfset client.s1="C:\Dropbox\CustomerArea\" & GetAuthUser()>

Direct concatenation: (NOTE: In modern versions of CF, this one should be pretty quick for multiple calls.)

<cfset client.s2="C:\Dropbox\CustomerArea\#GetAuthUser()#">

The shorthand of your method:

<cfset client.s3="C:\Dropbox\CustomerArea\"> 
<cfset client.s3&=GetAuthUser()>

And the Java StringBuffer:

<cfset client.s4 = createObject("java",  "java.lang.StringBuffer").init("C:\Dropbox\CustomerArea\")>
<cfset client.s4 = client.s4.append(getAuthUser()).toString()>

Each method will have different performance, and you will have to test in your system to see which is most performant.

https://trycf.com/gist/22fc36dcbb8653d0b32ceb22987bf2d5/acf?theme=monokai

NOTE: You might also want to provide a default directory to prevent accidental assignment to a higher directory. (If CF11+, you can use the elvis operator to make this easy. https://trycf.com/gist/0b0d513b3e45af427f4813099e84c9c9/acf11?theme=monokai)


And on a personal opinion note, I don't know why you would prefer <cfset...> over using cfscript. This is one of the things that cfscript makes exceptionally easy and clear. Plus changing from cftags to cfscript will open up a lot of benefits to ColdFusion.

2
James A Mohler On

Are you sure you have getAuthUser() just as you are starting the session? Consider this instead:

<cffunction name="OnRequestStart" >

   <cfset request.customerpath = "C:\Dropbox\CustomerArea\#GetAuthUser()#">

</cffunction>
1
DeltaSol On

Thanks to all who replied. You helped me realise that in Applicaiton.cfc I had the user authentication in OnRequestStart but I was trying to use the username in OnSessionStart. I moved the variable assignment to OnRequestStart and I used the session scope for the variable.

<cffunction name="OnRequestStart">

<!--- code here including cflogin to authenticate user --->

        <cfif GetAuthUser() NEQ "">
            <cfset session.customerpath="C:\Dropbox\CustomerArea\#GetAuthUser()#">
        <cfelse>
            <cfset session.customerpath="User not authenticated">
        </cfif>
</cffunction>

I hope that using the session scope within OnRequestStart is not bad practice. I assume it just means the variable will get reset more often - which I hope is slightly more secure anyway.