Skip to content

Commit cf4b03c

Browse files
committed
Add event subscriber for Guard Event
1 parent 1318468 commit cf4b03c

File tree

8 files changed

+125
-3
lines changed

8 files changed

+125
-3
lines changed

src/LaravelWorkflowProcessServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Soap\LaravelWorkflowProcess;
44

5+
use Illuminate\Support\Facades\Event;
56
use Soap\LaravelWorkflowProcess\Commands\LaravelWorkflowProcessCommand;
7+
use Soap\LaravelWorkflowProcess\Listeners\WorkflowGuardSubscriber;
68
use Spatie\LaravelPackageTools\Package;
79
use Spatie\LaravelPackageTools\PackageServiceProvider;
810

@@ -19,7 +21,11 @@ public function configurePackage(Package $package): void
1921
->name('laravel-workflow-process')
2022
->hasConfigFile()
2123
->hasViews()
22-
->hasMigration('create_laravel_workflow_process_table')
2324
->hasCommand(LaravelWorkflowProcessCommand::class);
2425
}
26+
27+
public function packageBooted()
28+
{
29+
Event::subscribe(WorkflowGuardSubscriber::class);
30+
}
2531
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Soap\LaravelWorkflowProcess\Listeners;
4+
5+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
6+
use ZeroDaHero\LaravelWorkflow\Events\GuardEvent;
7+
8+
class WorkflowGuardSubscriber
9+
{
10+
/**
11+
* Handle the event.
12+
*/
13+
public function handleOnGuard(GuardEvent $event): void
14+
{
15+
// This is a call by using event proxy to Symfony GuardEvent
16+
$subject = $event->getSubject();
17+
$transition = $event->getTransition();
18+
$workflow = $event->getWorkflow();
19+
$metaData = $workflow->getMetadataStore()->getTransitionMetadata($transition);
20+
if (isset($metaData['guard'])) {
21+
$guard = $metaData['guard'];
22+
$expressionLanguage = new ExpressionLanguage;
23+
$result = $expressionLanguage->evaluate($guard, [
24+
'authenticated' => auth()->check(),
25+
'subject' => $subject,
26+
'user' => auth()->user(),
27+
]);
28+
if (! $result) {
29+
$event->setBlocked(true, 'Guard blocked');
30+
}
31+
}
32+
33+
}
34+
35+
public function subscribe($events): void
36+
{
37+
$events->listen(
38+
'workflow.guard',
39+
[WorkflowGuardSubscriber::class, 'handleOnGuard']
40+
);
41+
}
42+
}

workbench/app/Models/Post.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Workbench\App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Post extends Model
9+
{
10+
use HasFactory;
11+
}

workbench/app/Models/User.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
9-
use Laravel\Sanctum\HasApiTokens;
109

1110
class User extends Authenticatable
1211
{
13-
use HasApiTokens, HasFactory, Notifiable;
12+
use HasFactory, Notifiable;
1413

1514
/**
1615
* The attributes that are mass assignable.

workbench/config/workflow.php

Whitespace-only changes.

workbench/config/workflow_registry.php

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Workbench\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Workbench\App\Models\Post;
7+
8+
/**
9+
* @template TModel of \Workbench\App\Models\Post
10+
*
11+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
12+
*/
13+
class PostFactory extends Factory
14+
{
15+
/**
16+
* The name of the factory's corresponding model.
17+
*
18+
* @var class-string<TModel>
19+
*/
20+
protected $model = Post::class;
21+
22+
/**
23+
* Define the model's default state.
24+
*
25+
* @return array<string, mixed>
26+
*/
27+
public function definition(): array
28+
{
29+
return [
30+
//
31+
];
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('posts', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('title');
17+
$table->text('content');
18+
$table->string('state')->default('draft');
19+
$table->unsignedBigInteger('user_id');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('posts');
30+
}
31+
};

0 commit comments

Comments
 (0)