Coldfusion global user object

Viewed 47

I am trying to set a coldfusion user object for each person as they log in. Similar to how rails has devise and I can call current_user.id or current_user.username from wherever I am in the site. I have a users table that stores roles and other user information. I want to query that and assign all of the fields to a sort of global user object. I can then use that for components, page display etc.

I am trying to figure out where and how to do this. I have tried initializing a component like this in onsessionstart, onrequeststart etc, but when I try to reference globalUser.id in a component for instance it hits a 500 error because globalUser is not defined.

<cfset globalUser = CreateObject( "component",
"controllers.user" ).globalUser(empidname= '#getauthuser()#') />

Any recommended ways to do this? Any plugins that provide functionality like this?

1 Answers

You want to use session variables.

<cffunction name="onSessionStart">
   <cfset session.globalUser = CreateObject( "component",
"controllers.user" ).globalUser(empidname= '#getauthuser()#') />
...
</cffunction>

In order for sessions to be enabled, you have to alter application.cfc

component {
    this.name = "AppName";
    this.sessionManagement = true;
Related