Skip to content

Commit 9b15a57

Browse files
authored
Merge pull request #403 from dotkernel/issue-390-2
Issue #390: Implemented `ResourceProviderMiddleware` and added `ResourceGuardInterface`
2 parents 668a665 + 8159d77 commit 9b15a57

File tree

46 files changed

+469
-308
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+469
-308
lines changed

.github/workflows/qodana_code_quality.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request:
55
push:
66
branches:
7-
- 5.0
7+
- '6.0'
88
- 'releases/*'
99

1010
jobs:

config/pipeline.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
declare(strict_types=1);
44

5-
use Api\App\Handler\GetNotFoundViewHandler;
5+
use Api\App\Handler\GetNotFoundResourceHandler;
66
use Api\App\Middleware\AuthenticationMiddleware;
77
use Api\App\Middleware\AuthorizationMiddleware;
88
use Api\App\Middleware\ContentNegotiationMiddleware;
99
use Api\App\Middleware\DeprecationMiddleware;
10+
use Api\App\Middleware\ResourceProviderMiddleware;
1011
use Dot\ResponseHeader\Middleware\ResponseHeaderMiddleware;
1112
use Mezzio\Application;
1213
use Mezzio\Cors\Middleware\CorsMiddleware;
@@ -80,11 +81,13 @@
8081
// - route-based validation
8182
// - etc.
8283

84+
$app->pipe(ResourceProviderMiddleware::class);
85+
8386
// Register the dispatch middleware in the middleware pipeline
8487
$app->pipe(DispatchMiddleware::class);
8588
// At this point, if no Response is returned by any middleware, the
8689
// NotFoundHandler kicks in; alternately, you can provide other fallback
8790
// middleware to execute.
8891
$app->pipe(ProblemDetailsNotFoundHandler::class);
89-
$app->pipe(GetNotFoundViewHandler::class);
92+
$app->pipe(GetNotFoundResourceHandler::class);
9093
};

src/Admin/src/Handler/Account/GetAdminAccountResourceHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
namespace Api\Admin\Handler\Account;
66

77
use Api\App\Handler\AbstractHandler;
8-
use Core\Admin\Entity\Admin;
8+
use Api\App\IdentityInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use Psr\Http\Message\ServerRequestInterface;
1111

1212
class GetAdminAccountResourceHandler extends AbstractHandler
1313
{
1414
public function handle(ServerRequestInterface $request): ResponseInterface
1515
{
16-
return $this->createResponse($request, $request->getAttribute(Admin::class));
16+
return $this->createResponse($request, $request->getAttribute(IdentityInterface::class));
1717
}
1818
}

src/Admin/src/Handler/Account/PatchAdminAccountResourceHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Api\App\Exception\ConflictException;
1111
use Api\App\Exception\NotFoundException;
1212
use Api\App\Handler\AbstractHandler;
13-
use Core\Admin\Entity\Admin;
13+
use Api\App\IdentityInterface;
1414
use Core\App\Message;
1515
use Dot\DependencyInjection\Attribute\Inject;
1616
use Psr\Http\Message\ResponseInterface;
@@ -47,7 +47,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
4747
$request,
4848
$this->adminService->saveAdmin(
4949
(array) $this->inputFilter->getValues(),
50-
$request->getAttribute(Admin::class)
50+
$request->getAttribute(IdentityInterface::class)
5151
)
5252
);
5353
}

src/Admin/src/Handler/Admin/DeleteAdminResourceHandler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
namespace Api\Admin\Handler\Admin;
66

77
use Api\Admin\Service\AdminServiceInterface;
8-
use Api\App\Exception\NotFoundException;
8+
use Api\App\Attribute\Resource;
99
use Api\App\Handler\AbstractHandler;
10+
use Core\Admin\Entity\Admin;
1011
use Dot\DependencyInjection\Attribute\Inject;
1112
use Psr\Http\Message\ResponseInterface;
1213
use Psr\Http\Message\ServerRequestInterface;
@@ -21,12 +22,12 @@ public function __construct(
2122
) {
2223
}
2324

24-
/**
25-
* @throws NotFoundException
26-
*/
25+
#[Resource(entity: Admin::class)]
2726
public function handle(ServerRequestInterface $request): ResponseInterface
2827
{
29-
$this->adminService->deleteAdmin($this->adminService->findAdmin($request->getAttribute('uuid')));
28+
$this->adminService->deleteAdmin(
29+
$request->getAttribute(Admin::class)
30+
);
3031

3132
return $this->noContentResponse();
3233
}

src/Admin/src/Handler/Admin/GetAdminResourceHandler.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,20 @@
44

55
namespace Api\Admin\Handler\Admin;
66

7-
use Api\Admin\Service\AdminServiceInterface;
8-
use Api\App\Exception\NotFoundException;
7+
use Api\App\Attribute\Resource;
98
use Api\App\Handler\AbstractHandler;
10-
use Dot\DependencyInjection\Attribute\Inject;
9+
use Core\Admin\Entity\Admin;
1110
use Psr\Http\Message\ResponseInterface;
1211
use Psr\Http\Message\ServerRequestInterface;
1312

1413
class GetAdminResourceHandler extends AbstractHandler
1514
{
16-
#[Inject(
17-
AdminServiceInterface::class,
18-
)]
19-
public function __construct(
20-
protected AdminServiceInterface $adminService,
21-
) {
22-
}
23-
24-
/**
25-
* @throws NotFoundException
26-
*/
15+
#[Resource(entity: Admin::class)]
2716
public function handle(ServerRequestInterface $request): ResponseInterface
2817
{
2918
return $this->createResponse(
3019
$request,
31-
$this->adminService->findAdmin($request->getAttribute('uuid'))
20+
$request->getAttribute(Admin::class)
3221
);
3322
}
3423
}

src/Admin/src/Handler/Admin/PatchAdminResourceHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use Api\Admin\InputFilter\UpdateAdminInputFilter;
88
use Api\Admin\Service\AdminServiceInterface;
9+
use Api\App\Attribute\Resource;
910
use Api\App\Exception\BadRequestException;
1011
use Api\App\Exception\ConflictException;
1112
use Api\App\Exception\NotFoundException;
1213
use Api\App\Handler\AbstractHandler;
14+
use Core\Admin\Entity\Admin;
1315
use Core\App\Message;
1416
use Dot\DependencyInjection\Attribute\Inject;
1517
use Psr\Http\Message\ResponseInterface;
@@ -32,6 +34,7 @@ public function __construct(
3234
* @throws ConflictException
3335
* @throws NotFoundException
3436
*/
37+
#[Resource(entity: Admin::class)]
3538
public function handle(ServerRequestInterface $request): ResponseInterface
3639
{
3740
$this->inputFilter->setData((array) $request->getParsedBody());
@@ -46,7 +49,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
4649
$request,
4750
$this->adminService->saveAdmin(
4851
(array) $this->inputFilter->getValues(),
49-
$this->adminService->findAdmin($request->getAttribute('uuid'))
52+
$request->getAttribute(Admin::class)
5053
)
5154
);
5255
}

src/Admin/src/Handler/Admin/Role/GetAdminRoleResourceHandler.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,20 @@
44

55
namespace Api\Admin\Handler\Admin\Role;
66

7-
use Api\Admin\Service\AdminRoleServiceInterface;
8-
use Api\App\Exception\NotFoundException;
7+
use Api\App\Attribute\Resource;
98
use Api\App\Handler\AbstractHandler;
10-
use Dot\DependencyInjection\Attribute\Inject;
9+
use Core\Admin\Entity\AdminRole;
1110
use Psr\Http\Message\ResponseInterface;
1211
use Psr\Http\Message\ServerRequestInterface;
1312

1413
class GetAdminRoleResourceHandler extends AbstractHandler
1514
{
16-
#[Inject(
17-
AdminRoleServiceInterface::class,
18-
)]
19-
public function __construct(
20-
protected AdminRoleServiceInterface $adminRoleService,
21-
) {
22-
}
23-
24-
/**
25-
* @throws NotFoundException
26-
*/
15+
#[Resource(entity: AdminRole::class)]
2716
public function handle(ServerRequestInterface $request): ResponseInterface
2817
{
2918
return $this->createResponse(
3019
$request,
31-
$this->adminRoleService->findAdminRole($request->getAttribute('uuid'))
20+
$request->getAttribute(AdminRole::class)
3221
);
3322
}
3423
}

src/App/src/Attribute/Resource.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Api\App\Attribute;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_METHOD)]
10+
readonly class Resource implements ResourceInterface
11+
{
12+
/**
13+
* @param class-string $entity The target entity to be found
14+
* @param string $identifier The class property used to find the entity by
15+
* @param string $placeholder The route parameter containing the identifier value
16+
* @param class-string|null $guard An invokable class implementing Api\App\Guard\ResourceGuardInterface,
17+
* which will determine if the current user is allowed to access the entity
18+
*/
19+
public function __construct(
20+
public string $entity,
21+
public string $identifier = 'uuid',
22+
public string $placeholder = 'uuid',
23+
public ?string $guard = null,
24+
) {
25+
}
26+
27+
public function hasGuard(): bool
28+
{
29+
return $this->guard !== null;
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Api\App\Attribute;
6+
7+
interface ResourceInterface
8+
{
9+
public function hasGuard(): bool;
10+
}

0 commit comments

Comments
 (0)