Skip to content

Commit 7f2d144

Browse files
committed
PRESIDECMS-3026 simple, non-intrusive logic to assume a logged in user in background thread
This allows threaded code to set a spoofed logged in user ID that might be used by various logic run in the thread to give context. In particular, if a developer creates an adhoc task with a web user owner, that owner ID should and will now be used as the spoofed logged in user for the task thread.
1 parent 4095634 commit 7f2d144

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

system/services/taskmanager/AdHocTaskManagerService.cfc

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ component displayName="Ad-hoc Task Manager Service" {
136136
return true;
137137
}
138138

139+
if ( Len( task.web_owner ?: "" ) ) {
140+
$getWebsiteLoginService().spoofUserLoginInBgThread( task.web_owner );
141+
}
142+
139143
if ( task.status == "running" || !markTaskAsRunning( taskId=arguments.taskId ) ) {
140144
$raiseError( error={
141145
type = "AdHoTaskManagerService.task.already.running"

system/services/websiteUsers/WebsiteLoginService.cfc

+18
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ component displayName="Website login service" {
8989
return false;
9090
}
9191

92+
/**
93+
* For background threads, allows the thread to assume that a specific user is logged in
94+
*
95+
*/
96+
public function spoofUserLoginInBgThread( required string userId ) {
97+
var user = _getUserDao().selectData(
98+
filter = { id=arguments.userId, active=true }
99+
, useCache = false
100+
);
101+
102+
if ( user.recordCount ) {
103+
request._bgThreadUser = $helpers.queryRowToStruct( user );
104+
}
105+
}
106+
92107
/**
93108
* Impersonates a login
94109
*
@@ -205,6 +220,9 @@ component displayName="Website login service" {
205220
* If no user is logged in, an empty structure will be returned.
206221
*/
207222
public struct function getLoggedInUserDetails() autodoc=true {
223+
if ( $getRequestContext().isBackgroundThread() ) {
224+
return request._bgThreadUser ?: {};
225+
}
208226
var userDetails = _getSessionStorage().getVar( name=_getSessionKey(), default={} );
209227

210228
return !IsNull( local.userDetails ) && IsStruct( userDetails ) ? userDetails : {};

0 commit comments

Comments
 (0)