Skip to content

Commit e472ee7

Browse files
committed
Merge branch 'feature-PRESIDECMS-3026_spoof-user-login-in-adhoc-task-bg-thread' into release-10.28.0
2 parents 14769fd + be2fa35 commit e472ee7

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-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 : {};

tests/unit/api/websiteUsers/WebsiteLoginServiceTest.cfc

+3
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ component output="false" extends="tests.resources.HelperObjects.PresideTestCase"
471471
mockUserDao = getMockbox().createStub();
472472
mockResetDao = getMockbox().createStub();
473473
mockUserLoginTokenDao = getMockbox().createStub();
474+
mockRequestContext = getMockbox().createStub();
474475
mockBCryptService = getMockBox().createEmptyMock( "preside.system.services.encryption.bcrypt.BCryptService" );
475476
mockSysConfigService = getMockBox().createEmptyMock( "preside.system.services.configuration.SystemConfigurationService" );
476477
mockEmailService = getMockBox().createEmptyMock( "preside.system.services.email.EmailService" );
@@ -490,10 +491,12 @@ component output="false" extends="tests.resources.HelperObjects.PresideTestCase"
490491
mockActionsService.$( "promoteVisitorActionsToUserActions", 1 );
491492
service.$( "$recordWebsiteUserAction" );
492493
service.$( "$announceInterception" );
494+
service.$( "$getRequestContext", mockRequestContext );
493495
mockSessionStorage.$( "rotate" );
494496
service.$( "$getPresideObject" ).$args( "website_user_reset_token" ).$results( mockResetDao );
495497
mockResetDao.$( "insertData", 1 );
496498
mockResetDao.$( "deleteData", 1 );
499+
mockRequestContext.$( "isBackgroundThread", false );
497500

498501
return service;
499502
}

0 commit comments

Comments
 (0)