File tree Expand file tree Collapse file tree 3 files changed +87
-0
lines changed
contexts/ArticlePublishing/Domain/Policies Expand file tree Collapse file tree 3 files changed +87
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Contexts \ArticlePublishing \Domain \Policies ;
6+
7+ use App \Exceptions \BizException ;
8+ use Contexts \ArticlePublishing \Domain \Models \ArticleId ;
9+ use Contexts \ArticlePublishing \Domain \Models \AuthorId ;
10+ use Contexts \ArticlePublishing \Infrastructure \Repositories \ArticleRepository ;
11+ use Contexts \Shared \Contracts \BaseAuthorizationPolicy ;
12+
13+ class ArticleOwnershipPolicy implements BaseAuthorizationPolicy
14+ {
15+ public function __construct (
16+ private ArticleId $ articleId ,
17+ private AuthorId $ authorId
18+ ) {}
19+
20+ public function check (): void
21+ {
22+ $ repository = app (ArticleRepository::class);
23+ $ article = $ repository ->getById ($ this ->articleId );
24+
25+ if (! $ article ->isOwnedBy ($ this ->authorId )) {
26+ throw BizException::make ('You are not the owner of this article ' )->code (403 );
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Contexts \ArticlePublishing \Domain \Policies ;
6+
7+ use App \Exceptions \BizException ;
8+ use Contexts \ArticlePublishing \Domain \Models \ArticleId ;
9+ use Contexts \ArticlePublishing \Domain \Models \ArticleStatus ;
10+ use Contexts \ArticlePublishing \Infrastructure \Repositories \ArticleRepository ;
11+ use Contexts \Shared \Contracts \BaseAuthorizationPolicy ;
12+
13+ class ArticleStatusPolicy implements BaseAuthorizationPolicy
14+ {
15+ public function __construct (
16+ private ArticleId $ articleId ,
17+ private ArticleRepository $ repository ,
18+ private ArticleStatus $ requiredStatus
19+ ) {}
20+
21+ public function check (): void
22+ {
23+ $ article = $ this ->repository ->getById ($ this ->articleId );
24+
25+ if (! $ article ->getStatus ()->equals ($ this ->requiredStatus )) {
26+ throw BizException::make ('Invalid article status: :status ' )
27+ ->with ('status ' , $ article ->getStatus ()->getValue ());
28+ }
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Contexts \ArticlePublishing \Domain \Policies ;
6+
7+ use App \Exceptions \BizException ;
8+ use Contexts \ArticlePublishing \Domain \Gateway \AuthorizationGateway ;
9+ use Contexts \Shared \Contracts \BaseAuthorizationPolicy ;
10+
11+ class GlobalPermissionPolicy implements BaseAuthorizationPolicy
12+ {
13+ public function __construct (private string $ action ) {}
14+
15+ public static function canPerform (string $ action )
16+ {
17+ return new self ($ action );
18+ }
19+
20+ public function check (): void
21+ {
22+ $ authorizationGateway = app (AuthorizationGateway::class);
23+
24+ if (! $ authorizationGateway ->canPerformAction ($ this ->action )) {
25+ throw BizException::make ('You are not authorized to perform this action ' )->code (403 );
26+ }
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments