Skip to content

Commit 798c69f

Browse files
authored
Merge pull request #9 from soap/develop
authenticated is core variable now, with some customization
2 parents 6021e8d + 3f19216 commit 798c69f

13 files changed

+168
-13
lines changed

config/workflow-process.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
// config for Soap/LaravelWorkflowProcess
44
return [
5+
'authentication' => [
6+
'guards' => ['web'],
7+
'logic' => 'or', // 'and' or 'or'
8+
],
59
'custom_functions' => [
610
// 'function_name' => [
711
// 'compiler' => function () { return 'true'; },
812
// 'evaluator' => function (array $variables) { return true; },
913
// ],
10-
// 'function_name' => 'App\CustomGuardFunction',
14+
1115
// 'authenicated' => \Soap\LaravelWorkflowProcess\GuardFunctions\Authenticated::class,
1216

1317
'authenticated' => [

src/Facades/LaravelWorkflowProcess.php renamed to src/Facades/WorkflowProcess.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/**
8-
* @see \Soap\LaravelWorkflowProcess\LaravelWorkflowProcess
8+
* @see \Soap\LaravelWorkflowProcess\WorkflowProcess
99
*/
1010
class LaravelWorkflowProcess extends Facade
1111
{
1212
protected static function getFacadeAccessor(): string
1313
{
14-
return \Soap\LaravelWorkflowProcess\LaravelWorkflowProcess::class;
14+
return \Soap\LaravelWorkflowProcess\WorkflowProcess::class;
1515
}
1616
}

src/LaravelWorkflowProcess.php

-5
This file was deleted.

src/LaravelWorkflowProcessServiceProvider.php

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public function registeringPackage()
2929
$this->app->singleton(GuardEvaluator::class, function ($app) {
3030
return new GuardEvaluator(new \Symfony\Component\ExpressionLanguage\ExpressionLanguage);
3131
});
32+
33+
$this->app->singleton(WorkflowProcess::class, function ($app) {
34+
return new WorkflowProcess;
35+
});
36+
37+
$this->app->alias(WorkflowProcess::class, 'workflow-process');
3238
}
3339

3440
public function packageBooted()

src/Listeners/WorkflowGuardSubscriber.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ public function handleOnGuard(GuardEvent $event): void
4141
$variables = [
4242
'subject' => $event->getSubject(),
4343
];
44+
$workflowProcess = app('workflow-process');
45+
46+
$authenticated = $workflowProcess->getAuthenticated();
47+
$user = $workflowProcess->getUser();
48+
49+
$variables['authenticated'] = $authenticated;
4450

4551
// Optionally include the authenticated user.
46-
if (auth()->check()) {
47-
$variables['user'] = auth()->user();
52+
if ($authenticated) {
53+
$variables['user'] = $workflowProcess->getUser();
4854
}
4955

5056
// Evaluate the guard expression using the GuardEvaluator.

src/WorkflowProcess.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Soap\LaravelWorkflowProcess;
4+
5+
class WorkflowProcess
6+
{
7+
private $configFile = 'workflow-process';
8+
9+
public function __construct() {}
10+
11+
public function getAuthenticated()
12+
{
13+
$guards = config($this->configFile.'.authentication.guards', ['web']);
14+
$logic = config($this->configFile.'.authentication.logic', 'or');
15+
16+
if ($logic === 'and') {
17+
// The user is considered authenticated only if all guards return true.
18+
$authenticated = true;
19+
20+
foreach ($guards as $guard) {
21+
if (! auth()->guard($guard)->check()) {
22+
$authenticated = false;
23+
break;
24+
}
25+
}
26+
} elseif ($logic === 'or') {
27+
$authenticated = false;
28+
foreach ($guards as $guard) {
29+
if (auth()->guard($guard)->check()) {
30+
$authenticated = true;
31+
break;
32+
}
33+
}
34+
} else {
35+
throw new \InvalidArgumentException('Invalid authentication logic. Use "and" or "or".');
36+
}
37+
38+
return $authenticated;
39+
}
40+
41+
public function getUser()
42+
{
43+
$guards = config($this->configFile.'.authentication.guards', ['web']);
44+
$logic = config($this->configFile.'.authentication.logic', 'or');
45+
46+
if ($logic === 'and') {
47+
// The user is considered authenticated only if all guards return true.
48+
$user = null;
49+
50+
foreach ($guards as $guard) {
51+
if (auth()->guard($guard)->check()) {
52+
$user = auth()->guard($guard)->user();
53+
break;
54+
}
55+
}
56+
} elseif ($logic === 'or') {
57+
$user = null;
58+
foreach ($guards as $guard) {
59+
if (auth()->guard($guard)->check()) {
60+
$user = auth()->guard($guard)->user();
61+
break;
62+
}
63+
}
64+
} else {
65+
throw new \InvalidArgumentException('Invalid authentication logic. Use "and" or "or".');
66+
}
67+
68+
return $user;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

tests/Feature/GuardFunctions/AuthenticatedTest.php renamed to tests/Feature/GuardFunctions/AuthenticatedFunctionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

33
use Illuminate\Support\Facades\Auth;
4-
use Mockery;
4+
// use Mockery;
55
use Soap\LaravelWorkflowProcess\GuardFunctions\Authenticated;
66

77
afterEach(function () {
88
// Ensure that all mock expectations are met and clean up
9-
Mockery::close();
9+
\Mockery::close();
1010
});
1111

1212
test('compile returns authenticated("web") when no guard argument is provided', function () {

workbench/app/Models/Post.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
7+
use ZeroDaHero\LaravelWorkflow\Traits\WorkflowTrait;
78

89
class Post extends Model
910
{
1011
use HasFactory;
12+
use WorkflowTrait;
1113
}

workbench/config/workflow-process.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
return [
4+
'authentication' => [
5+
'guards' => ['web'],
6+
'logic' => 'or', // 'and' or 'or'
7+
],
8+
'custom_functions' => [
9+
10+
],
11+
];

workbench/config/workflow.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
return [
4+
'post' => [
5+
'type' => 'workflow',
6+
'supports' => ['Workbench\App\Models\Post'],
7+
'marking_store' => [
8+
'property' => 'status',
9+
],
10+
'places' => [
11+
'draft',
12+
'review',
13+
'published',
14+
'archived',
15+
],
16+
'transitions' => [
17+
'submit' => [
18+
'from' => ['draft'],
19+
'to' => 'review',
20+
'metadata' => [
21+
'guard' => 'authenticated && subject.user_id == user.id',
22+
],
23+
],
24+
'approve' => [
25+
'from' => ['review'],
26+
'to' => 'published',
27+
],
28+
'reject' => [
29+
'from' => ['review'],
30+
'to' => 'draft',
31+
],
32+
'archive' => [
33+
'from' => ['published'],
34+
'to' => 'archived',
35+
],
36+
],
37+
],
38+
];
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
return [
4+
/**
5+
* When set to true, the registry will track the workflows that have been loaded.
6+
* This is useful when you're loading from a DB, or just loading outside of the
7+
* main config files.
8+
*/
9+
'track_loaded' => false,
10+
11+
/**
12+
* Only used when track_loaded = true
13+
*
14+
* When set to true, a registering a duplicate workflow will be ignored (will not load the new definition)
15+
* When set to false, a duplicate workflow will throw a DuplicateWorkflowException
16+
*/
17+
'ignore_duplicates' => false,
18+
];

workbench/database/factories/PostFactory.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class PostFactory extends Factory
2727
public function definition(): array
2828
{
2929
return [
30-
//
30+
'title' => $this->faker->sentence,
31+
'content' => $this->faker->paragraph,
32+
'status' => 'draft',
33+
'created_at' => now(),
34+
'updated_at' => now(),
3135
];
3236
}
3337
}

0 commit comments

Comments
 (0)