Skip to content

Develop #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
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
12 changes: 1 addition & 11 deletions config/workflow-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
// 'evaluator' => function (array $variables) { return true; },
// ],

// 'authenicated' => \Soap\LaravelWorkflowProcess\GuardFunctions\Authenticated::class,

'authenticated' => [
'compiler' => function ($guard = 'web') {
return sprintf('authenticated("%s")', $guard);
},
'evaluator' => function (array $variables, $guard = 'web') {
// This allows checking a specific guard (e.g., 'web', 'api', etc.)
return auth()->guard($guard)->check();
},
],
// 'isAdmin' => \Soap\LaravelWorkflowProcess\GuardFunctions\AdminGuardFunction::class,
],
];
15 changes: 4 additions & 11 deletions src/Listeners/WorkflowGuardSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,13 @@ public function handleOnGuard(GuardEvent $event): void
$guardExpression = $metaData['guard'];
// Prepare variables to pass to the evaluator.
// You can pass the workflow subject, user, or any other required objects.

$workflowProcess = app('workflow-process');
$variables = [
'subject' => $event->getSubject(),
'authenticated' => $workflowProcess->getAuthenticated(),
'user' => $workflowProcess->getUser(),
];
$workflowProcess = app('workflow-process');

$authenticated = $workflowProcess->getAuthenticated();
$user = $workflowProcess->getUser();

$variables['authenticated'] = $authenticated;

// Optionally include the authenticated user.
if ($authenticated) {
$variables['user'] = $workflowProcess->getUser();
}

// Evaluate the guard expression using the GuardEvaluator.
$result = $this->guardEvaluator->evaluate($guardExpression, $variables);
Expand Down
25 changes: 5 additions & 20 deletions src/WorkflowProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,13 @@ public function getAuthenticated()
public function getUser()
{
$guards = config($this->configFile.'.authentication.guards', ['web']);
$logic = config($this->configFile.'.authentication.logic', 'or');

if ($logic === 'and') {
// The user is considered authenticated only if all guards return true.
$user = null;
$user = null;

foreach ($guards as $guard) {
if (auth()->guard($guard)->check()) {
$user = auth()->guard($guard)->user();
break;
}
foreach ($guards as $guard) {
if (auth()->guard($guard)->check()) {
$user = auth()->guard($guard)->user();
break;
}
} elseif ($logic === 'or') {
$user = null;
foreach ($guards as $guard) {
if (auth()->guard($guard)->check()) {
$user = auth()->guard($guard)->user();
break;
}
}
} else {
throw new \InvalidArgumentException('Invalid authentication logic. Use "and" or "or".');
}

return $user;
Expand Down
Loading