Copy link to clipboard
Copied
Hi all, i need to store all the screen_code and APP_PRIVILEGES into session struct...but somehow when i dumped "session.AdminPrivStruct" I only get one key instead of getting 17 keys and values? Any reason for that?
---------
<cfquery datasource="#application.orc#" name="GetAdminPriv">
Select
p.APP_PRIVILEGES,
p.SCREEN_CODE,
sc.SCREEN_NAME
from
OPS$RMS.REF_LOGIN a,
OPS$RMS.LOGIN_PRIVILEGE p,
OPS$RMS.REF_SCREEN sc
where
a.EID = p.EID
And p.screen_code = sc.screen_code
And upper(a.EID) = upper('#GetAdminLogin.EID#')
</cfquery>
<cfoutput query="GetAdminPriv">
<cfscript>
session.AdminPrivStruct = structnew();
session.AdminPrivStruct["#SCREEN_Code#"] = SCREEN_code;
session.AdminPrivStruct["#SCREEN_code#_Priv"] = APP_PRIVILEGES;
</cfscript>
#SCREEN_code#
</cfoutput>
Copy link to clipboard
Copied
<cfoutput query="GetAdminPriv">
<cfscript>
session.AdminPrivStruct = structnew();....
</cfscript>
</cfoutput>
You are creating a new structure on each iteration, overwriting any previous results each time. The end result is only one key. You need to initialize the structure before the loop.
Copy link to clipboard
Copied
<cfoutput query="GetAdminPriv">
<cfscript>
session.AdminPrivStruct = structnew();
session.AdminPrivStruct["#SCREEN_Code#"] = SCREEN_code;
session.AdminPrivStruct["#SCREEN_code#_Priv"] = APP_PRIVILEGES;
</cfscript>
#SCREEN_code#
</cfoutput>
You have the session.AdminPrivStruct = structnew(); line inside the <cfoutput query=""> loop. That means each and very iteration through the loop will RESET session.AdminPrivstruct to a new, empty structure.
Try This.
<cfscript>
session.AdminPrivStruct = structnew();
</cfscript>
<cfoutput query="GetAdminPriv">
<cfscript>
session.AdminPrivStruct["#SCREEN_Code#"] = SCREEN_code;
session.AdminPrivStruct["#SCREEN_code#_Priv"] = APP_PRIVILEGES;
</cfscript>
#SCREEN_code#
</cfoutput>
Or maybe even just this.
<cfset session.AdminPrivQry = getAdminPriv>
Depends on if you really need a structure or just the record set data in a persistant scope.
Copy link to clipboard
Copied
How will i check for the key values of the struct? Please provide sample...I am getting error from this statement [ <cfif structkeyexists(session.AdminPrivStruct["Admin"],"Admin")>yes<cfelse>no</cfif>] ....
Copy link to clipboard
Copied
Unless AdminPrivStruct contains an element named "Admin" which is itself a structure ... and you are asking CF to see if that structure contains an element which is named "Admin" ... then what you wanted to say is simply:
<cfif StructKeyExists(Session, "AdminPrivStruct") >
This is asking: "Does the struct named Session ... which, by the way, you know does exist ... contain an element named 'AdminPrivStruct' ?"
You then might wish to use StructKeyExists a second time to see if that structure-element contains the key, "Admin." Only then is it safe to test its value.
Copy link to clipboard
Copied
Lets say, I need to get the value of Admin and Admin_Priv which is within session.AdminPrivStruct... How would I do that?
Copy link to clipboard
Copied
Do I really need to spoon-feed this?
Let's think about it together.
You can, if you try, see your way clear to do this without help from the community... and, you need to master this. We're here to "teach you to fish..."
Now for a few practical comments...
"Pardon me, I hate to bother you, but it seems that I have just determined that my right foot is missing. The gun in my left hand seems to have gone off at a most inconvenient moment and, well, what it boils down to is this: I am about to die. But before doing so I have carefully set-down the airplane in this somewhat out-of-the-way but safe landing field, and called a taxicab which will be shortl...."
Your code died (it couldn't exactly avoid it, since there's a bug somewhere...) but it did so under its own terms.
Copy link to clipboard
Copied
You can, of course, put (almost) any sort of struct into the Session.
The technique that I use is that the session is always set-up in the menu screen of the application (because I know that this will in all cases reliably convey the session-cookie to the client). And I always have: <cfset Session.exists = "Y">.
The other screens, then, do (within a <cflock>😞 <cfset SessionExists = StructKeyExists(Session, "exists")>. Then, outside of the lock, we test SessionExists, and if it is not True, <cflocation> to the menu screen.
The session-initialization logic in the menu-screen uses the same technique to check for the existence of a session. If the session-data needs to contain a structure, it's initialized (with StructNew()) at that time. (Not that you have to do that, but I do.)