Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions classes/security/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PKP\site\SiteDAO;
use PKP\user\User;
use PKP\userGroup\UserGroup;
use PKP\userGroup\relationships\UserGroupStage;
use PKP\security\Role;
use PKP\core\PKPApplication;

Expand Down Expand Up @@ -114,12 +115,59 @@ public static function registerUserSession($user, &$reason)
$request = Application::get()->getRequest();
$request->getSessionGuard()->setUserDataToSession($user)->updateSession($user->getId());

// Ensure manager userGroups have all workflow stages assigned
static::ensureManagerUserGroupStages($user);

$user->setDateLastLogin(Core::getCurrentDate());
Repo::user()->edit($user);

return $user;
}

/**
* Ensure that all manager role userGroups for a user have all workflow stages assigned.
* This corrects any inconsistent database state where manager userGroups are missing stage assignments.
*/
protected static function ensureManagerUserGroupStages(User $user): void
{
// Get application-specific workflow stages (differs between OJS, OMP, OPS)
$allWorkflowStages = Application::getApplicationStages();

// Get manager userGroups for this user with their current stage assignments
$managerUserGroups = UserGroup::query()
->withRoleIds([Role::ROLE_ID_MANAGER])
->whereHas('userUserGroups', function ($query) use ($user) {
$query->withUserId($user->getId())->withActive();
})
->with('userGroupStages')
->get();

if ($managerUserGroups->isEmpty()) {
return;
}

// Collect all missing stage assignments for batch insert
$missingStages = [];
foreach ($managerUserGroups as $userGroup) {
$assignedStageIds = $userGroup->userGroupStages->pluck('stage_id')->toArray();

foreach ($allWorkflowStages as $stageId) {
if (!in_array($stageId, $assignedStageIds)) {
$missingStages[] = [
'context_id' => $userGroup->context_id,
'user_group_id' => $userGroup->user_group_id,
'stage_id' => $stageId,
];
}
}
}

// Batch insert missing stages
if (!empty($missingStages)) {
UserGroupStage::insert($missingStages);
}
}

/**
* Mark the user as logged out in the current session.
*
Expand Down