[Coldbox 4.3.0] CF2016 - Layouts, Viewlets and session data

48 views
Skip to first unread message

Philippe Sambor

unread,
Mar 16, 2018, 3:24:18 AM3/16/18
to ColdBox Platform
1 - I created a Viewlets controller with two actions: sessionheader and sessionwrapper and two corresponding views. 
2 - The two views are called in a Layout that takes all the needed javascript and CSS and renders what I want, which is a UI template containing data passed by the Viewlets controller. 
3 - The data passed by the Viewlets controller is consumed as a structure that must be put in the session scope, because it changes with every logged-in user.

So far everything works fine from an event and presentation standpoint, as long as I place my session structure within each action of the controller. The problem with this approach is that I need a way for the structure being put in the session scope to be available to all actions within the controller. I tried to put the code under the onSessionStart() function within the Viewlets controller but it does not help, meaning the data is not shared among all the actions within the controller. Any suggestions? 

Here is the Viewlets controller:

/**
* I am the Viewlets handler - I load all the necessary user session parameters to build the
* session header and the session wrapper to customise menu, options and languages for the user.
* The viewlets are then included in the Foundation layout.
*/

component{
// Dependency Injection

property name="userSVC" inject="rrm.UserSVC";
property name="localeSVC" inject="glob.LocaleSVC";

function index(event,rc,prc){

event.noRender();
}

function onSessionStart(event,rc,prc){

// Should I place my session data here instead? But this prc data cannot be shared by "sessionheader" and "sessionwrapper"

}

function sessionheader( event, rc, prc ){

       // sessionheader is a top banner whose contents depend on the user session

// I set a user ID for testing purpose.
// Ultimately, the user ID will be passed by a login/authentication process

prc.userinfo_qData = userSVC.read(13);

// retrieve basic locale parameters for the user locale and load a structure in the session scope

var session.structULocale = localeSVC.LocaleParameters(prc.userinfo_qData.getUserLocaleCD());
prc.user_localeData = session.structULocale;

// retrieve universal date format parameters for the user locale and load another structure in the session scope

var session.structUDatetime = localeSVC.DisplayDatetimeFormat(prc.userinfo_qData.getUserLocaleCD());
prc.user_localeDatetimeFormat = session.structUDatetime;
 

// render out content
return renderView( "viewlets/sessionheader" );
}


function sessionwrapper( event, rc, prc ){

               // sessionwarpper is a dynamic menu whose contents depend on the user session  

prc.userinfo_qData = userSVC.read(13); 

// render out content
return renderView( "viewlets/sessionwrapper" );
}

}

Ancient Programmer

unread,
Mar 16, 2018, 12:05:05 PM3/16/18
to ColdBox Platform
To make prc available to all handlers, I would implement an Interceptor.

// /myapp/interceptors/UserNav.cfc
component {

function preProcess(event, interceptData, buffer, rc, prc) localmode="modern" {
prc.userNavHeader = "This is user header";
return;
}

}

Then, register it in ColdBox.cfc

interceptors = [
{ class="interceptors.UserNav" }
];

prc.userNavHeader will be available to all handlers. Don't forget to fwreinit once.

Reference:


A better approach to user authentication, personalized user nav, and user session, I would use ColdBox SecurityService. It's convenient. I can check user permissions, load user data into session, etc.

Reference: 

Ancient Programmer

unread,
Mar 16, 2018, 12:12:13 PM3/16/18
to ColdBox Platform
One more thing I forgot to mention. Use sessionStorage from ColdBox cbStorages (https://github.com/coldbox-modules/cbox-security/wiki) to handle all your session scope.

Philippe Sambor

unread,
Mar 16, 2018, 10:14:34 PM3/16/18
to ColdBox Platform
Thanks for the feedback. I had the cbstorages module loaded but was not sure about what it did and how to make use of it. The links will definitely help and as for the cbsecurity module, yes I am definitely going to use it to implement the user login process.
Reply all
Reply to author
Forward
0 new messages