Charles Robertson
03/10/2024, 11:42 AMsession
scope inside the controller, if the controller is a singleton?Charles Robertson
03/10/2024, 11:44 AMcomponent {
function init( fw ) {
variables.fw = fw;
}
function session( rc ) {
// set up the user's session
session.auth = {};
session.auth.isLoggedIn = false;
session.auth.fullname = 'Guest';
}
function authorize( rc ) {
// check to make sure the user is logged on
if ( not ( structKeyExists( session, "auth" ) && session.auth.isLoggedIn ) &&
!listfindnocase( 'login', variables.fw.getSection() ) &&
!listfindnocase( 'main.error', variables.fw.getFullyQualifiedAction() ) ) {
variables.fw.redirect('login');
}
}
}
It would seem we can? But wouldn't this increase the risk of user data, getting mixed up?aliaspooryorik
session
scope in your controllers. You can easily create a service which hides how that information is stored from the controller.Charles Robertson
03/10/2024, 11:52 AMaliaspooryorik
Charles Robertson
03/10/2024, 11:54 AMsecurity.cfc
controller? Isn't this dangerous? I didn't write this code, by the way. This is an FW1 example website.Charles Robertson
03/10/2024, 11:55 AMsecurity.cfc
instance? And there would be no issue?aliaspooryorik
session
as MySessionServiceSingleton
then the when you do session.auth.isLoggedIn
it's a bit like MySessionServiceSingleton.getAuthIsLoggedIn()
aliaspooryorik
session
scope is not like the variables
scopeCharles Robertson
03/10/2024, 11:57 AMsession.auth.isLoggedIn
, inside the same cfc
instance?Charles Robertson
03/10/2024, 11:58 AMaliaspooryorik
session
is tied to each user (created in onSessionStart)aliaspooryorik
fw1
btw - it's just how the session scope works in CFMLaliaspooryorik
Charles Robertson
03/10/2024, 11:59 AMCharles Robertson
03/10/2024, 12:02 PMvar lckisloggedin = false;
lock timeout="10" scope="session" type="read" {
lckisloggedin = structKeyExists( session, "auth" ) ? session.auth.isLoggedIn : false;
}
if ( lckisloggedin ) &&
!listfindnocase( 'login', variables.fw.getSection() ) &&
!listfindnocase( 'main.error', variables.fw.getFullyQualifiedAction() ) ) {
variables.fw.redirect('login');
}
aliaspooryorik
Charles Robertson
03/10/2024, 12:03 PMaliaspooryorik
aliaspooryorik
Charles Robertson
03/10/2024, 12:05 PMaliaspooryorik
aliaspooryorik
Charles Robertson
03/10/2024, 12:06 PMCharles Robertson
03/10/2024, 12:07 PMCharles Robertson
03/10/2024, 12:09 PMaliaspooryorik
session
scope and change it. If you'd started off with a SecurityService
then the session
scope is only referenced in that one place so you can switch out how you track who is logged in by changing very little code as your controllers don't care how it is stored.Charles Robertson
03/10/2024, 12:09 PMCharles Robertson
03/10/2024, 12:10 PMCharles Robertson
03/10/2024, 12:11 PMaliaspooryorik
Charles Robertson
03/10/2024, 12:12 PMaliaspooryorik
Charles Robertson
03/10/2024, 12:13 PMCharles Robertson
03/10/2024, 12:17 PMsingleton
that everyone could read and write to, it would turn into a veritable variables bun fight πaliaspooryorik
var
scoping is crucialCharles Robertson
03/10/2024, 12:20 PMlocal
scope.
But I actually tend to write at the top of each method:
var local = {};
Because this is compatible with ACF9, onwards...aliaspooryorik
aliaspooryorik
local
prefix but I understand people like it as they can clearly see what is scoped.aliaspooryorik
var
scoped then your code is way too long πCharles Robertson
03/10/2024, 12:23 PMlocal
prefix, for ease.
Sometimes I also write var
scoped variables at the top of each method, if these variables are extra important, and I want to emphasize them.Charles Robertson
03/10/2024, 12:24 PMvar
scope anywhere, in a method. But I am pretty sure this use to throw an error in ACF10 <Charles Robertson
03/10/2024, 12:25 PMvar
scoped variables, at the top of my method. I guess this is just habit?aliaspooryorik
Charles Robertson
03/10/2024, 12:36 PMCharles Robertson
03/10/2024, 12:37 PMCharles Robertson
03/10/2024, 12:38 PMaliaspooryorik