Skip to content

Commit 3e0b84c

Browse files
Merge pull request #579 from components-web-app/feat/569-admin-only-content-writes
Require the publishable permission to write CWA content (#569)
2 parents b17d7fa + acee1f4 commit 3e0b84c

24 files changed

Lines changed: 694 additions & 170 deletions

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Every change that reaches `main` adds a line under **Unreleased**. When a versio
44

55
## Unreleased
66

7+
### Breaking
8+
- Writing CWA content (pages, layouts, routes, component groups and their order, component positions, every component including `/move`, and every page data class, project classes included) requires `publishable.permission` (or the class's `#[Publishable(isGranted:)]`), ANDed with any `security` the operation already has; opt an operation or resource out with `extraProperties: ['publishable_permission' => false]`. Form `/submit` stays public ([#579](https://github.com/components-web-app/api-components-bundle/pull/579))
9+
710
### Added
811
- `emails.logo` (a Twig path, default the bundle's logo) and `emails.logo_width` (pixels, optional) set the logo at the top of every email the bundle sends, whose alt text is now the `website_name` instead of `Logo` ([#576](https://github.com/components-web-app/api-components-bundle/pull/576))
912

‎CLAUDE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Resources are fetched and cached **individually**. Never embed related data outs
294294

295295
### Security and users
296296

297+
- **Writing CWA content needs the publishable permission, whatever the application's access rules** (#569). `ContentWriteSecurityResourceMetadataCollectionFactory`, the outermost bundle metadata decorator (decoration priority -1, so it sees every operation the other bundle factories add, `/move` and uploads included), gives every `POST`/`PUT`/`PATCH`/`DELETE` operation of `Page`, `Layout`, `Route`, `ComponentGroup`, `ComponentGroupOrder`, `ComponentPosition` and every `AbstractComponent` and `AbstractPageData` subclass, project classes included, the `security` `publishable.permission`, or the class's own `#[Publishable(isGranted:)]` when it sets one (the permission `PublishableStatusChecker` resolves for the class). It goes in `security`, not `securityPostDenormalize`: the expression needs no object, and `security` runs before deserialization, so a refused write never reaches the processors. **An operation's own `security` is ANDed with it, never replaced and never an opt-out**: `Route`'s write operations carry `read_route` and `RoutableResourceMetadataCollectionFactory` gives page and page data writes `read_routable` (and `routable_security` on `POST`), which are read checks that granted any reader the write. **The opt-out is `extraProperties: [ContentWriteSecurityResourceMetadataCollectionFactory::OPT_OUT_EXTRA_PROPERTY => false]`** on an operation, or on `#[ApiResource]` for every operation of the resource; then the operation's own `security` (or none) is all that applies. Form `/submit` (`POST` and the validating `PATCH`) opts out, and the test bundle's `DummyPublishableComponent` does, so the publishable tests can still write as a user and anonymously. Reads are untouched. `ROLE_USER` never implies editing: a test that writes content logs in as an admin. `Security\ContentWriteSecurityTest` is the authority.
297298
- **A `PUT` on a user merges, never replaces** (#426). API Platform's `standard_put` defaults to `true` on 4.x and 5.x: the body is denormalized into a new object and every property is copied onto the stored one, including those nobody can write (password hash, tokens), so they took the constructor's defaults. `UserResourceMetadataCollectionFactory` sets `standard_put: false` on every `Put` of an `AbstractUser`, over any application default. `User\UserPutTest` pins it.
298299
- **`UserNormalizer` handles only a user payload (an array)** (#572). A user referenced by IRI from another resource's write reaches the serializer as a string; running `processChanges()` on it treated the stored user as a new registration and reset `emailAddressVerified`. `User\UserReferenceTest` pins it.
299300
- **A plain password is hashed only after validation, and never verifies the email address** (#570). `UserDataProcessor::processChanges()` does not hash: the API hashes in `UserPasswordStateProcessor` (on `state_processor.locator`, before the persist) and the form listener after `processChanges()`, so the `plainPassword` `Length` (in `Default` as well as `User:password:create`) sees the value on API writes; hashed during denormalization, it was already erased. Only a password reset, which proves the inbox, sets `emailAddressVerified`. `User\UserPasswordTest` pins it.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Silverback API Components Bundle Project
5+
*
6+
* (c) Daniel West <daniel@silverback.is>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource;
13+
14+
use ApiPlatform\Metadata\HttpOperation;
15+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
16+
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
17+
use Silverback\ApiComponentsBundle\ApiResource\ComponentGroupOrder;
18+
use Silverback\ApiComponentsBundle\AttributeReader\PublishableAttributeReader;
19+
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
20+
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
21+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentGroup;
22+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
23+
use Silverback\ApiComponentsBundle\Entity\Core\Layout;
24+
use Silverback\ApiComponentsBundle\Entity\Core\Page;
25+
use Silverback\ApiComponentsBundle\Entity\Core\Route;
26+
27+
/**
28+
* @author Daniel West <daniel@silverback.is>
29+
*/
30+
final readonly class ContentWriteSecurityResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
31+
{
32+
public const string OPT_OUT_EXTRA_PROPERTY = 'publishable_permission';
33+
34+
private const array CONTENT_CLASSES = [
35+
Page::class,
36+
Layout::class,
37+
Route::class,
38+
ComponentGroup::class,
39+
ComponentGroupOrder::class,
40+
ComponentPosition::class,
41+
AbstractComponent::class,
42+
AbstractPageData::class,
43+
];
44+
45+
private const array WRITE_METHODS = [
46+
HttpOperation::METHOD_POST,
47+
HttpOperation::METHOD_PUT,
48+
HttpOperation::METHOD_PATCH,
49+
HttpOperation::METHOD_DELETE,
50+
];
51+
52+
public function __construct(
53+
private ResourceMetadataCollectionFactoryInterface $decorated,
54+
private PublishableAttributeReader $publishableAttributeReader,
55+
private string $permission,
56+
) {
57+
}
58+
59+
public function create(string $resourceClass): ResourceMetadataCollection
60+
{
61+
$resourceMetadataCollection = $this->decorated->create($resourceClass);
62+
63+
if (!$this->isContent($resourceClass)) {
64+
return $resourceMetadataCollection;
65+
}
66+
67+
$permission = $this->permission($resourceClass);
68+
foreach ($resourceMetadataCollection as $i => $resource) {
69+
$operations = $resource->getOperations();
70+
if (null === $operations) {
71+
continue;
72+
}
73+
foreach ($operations as $key => $operation) {
74+
if (!\in_array($operation->getMethod(), self::WRITE_METHODS, true) || false === ($operation->getExtraProperties()[self::OPT_OUT_EXTRA_PROPERTY] ?? true)) {
75+
continue;
76+
}
77+
$operations->add($key, $operation->withSecurity(self::security($permission, $operation->getSecurity())));
78+
}
79+
$resourceMetadataCollection[$i] = $resource->withOperations($operations);
80+
}
81+
82+
return $resourceMetadataCollection;
83+
}
84+
85+
private function isContent(string $resourceClass): bool
86+
{
87+
foreach (self::CONTENT_CLASSES as $contentClass) {
88+
if (is_a($resourceClass, $contentClass, true)) {
89+
return true;
90+
}
91+
}
92+
93+
return false;
94+
}
95+
96+
private function permission(string $resourceClass): string
97+
{
98+
if (!$this->publishableAttributeReader->isConfigured($resourceClass)) {
99+
return $this->permission;
100+
}
101+
102+
return $this->publishableAttributeReader->getConfiguration($resourceClass)->isGranted ?? $this->permission;
103+
}
104+
105+
private static function security(string $permission, ?string $security): string
106+
{
107+
if (null === $security || '' === $security) {
108+
return $permission;
109+
}
110+
111+
return \sprintf('(%s) and (%s)', $permission, $security);
112+
}
113+
}

‎src/DependencyInjection/SilverbackApiComponentsExtension.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Persistence\ManagerRegistry;
1515
use Ramsey\Uuid\Doctrine\UuidType;
16+
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\ContentWriteSecurityResourceMetadataCollectionFactory;
1617
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\RoutableResourceMetadataCollectionFactory;
1718
use Silverback\ApiComponentsBundle\AttributeReader\UploadableAttributeReader;
1819
use Silverback\ApiComponentsBundle\Command\ImagineGenerateCommand;
@@ -277,6 +278,9 @@ public function load(array $configs, ContainerBuilder $container): void
277278
$definition = $container->findDefinition(RoutableResourceMetadataCollectionFactory::class);
278279
$definition->setArgument('$securityStr', $config['routable_security']);
279280

281+
$definition = $container->findDefinition(ContentWriteSecurityResourceMetadataCollectionFactory::class);
282+
$definition->setArgument('$permission', $config['publishable']['permission']);
283+
280284
$definition = $container->findDefinition(MercureAuthorization::class);
281285
$definition->setArgument('$cookieSameSite', $config['mercure']['cookie']['samesite']);
282286
$definition->setArgument('$hubName', $config['mercure']['hub_name']);

‎src/Entity/Component/Form.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Metadata\Put;
2222
use Doctrine\ORM\Mapping as ORM;
2323
use Silverback\ApiComponentsBundle\Annotation as Silverback;
24+
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\ContentWriteSecurityResourceMetadataCollectionFactory;
2425
use Silverback\ApiComponentsBundle\DataProvider\StateProvider\FormStateProvider;
2526
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
2627
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
@@ -52,6 +53,7 @@
5253
write: false,
5354
serialize: true,
5455
requirements: ['id' => '[^/]+'],
56+
extraProperties: [ContentWriteSecurityResourceMetadataCollectionFactory::OPT_OUT_EXTRA_PROPERTY => false],
5557
),
5658
new Post(
5759
name: '_api_/forms/{id}/submit{._format}_post',
@@ -62,6 +64,7 @@
6264
write: false,
6365
serialize: true,
6466
requirements: ['id' => '[^/]+'],
67+
extraProperties: [ContentWriteSecurityResourceMetadataCollectionFactory::OPT_OUT_EXTRA_PROPERTY => false],
6568
),
6669
]
6770
)]

‎src/Resources/config/services.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Property\ComponentPropertyMetadataFactory;
3535
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Property\ImagineFiltersPropertyMetadataFactory;
3636
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\ComponentResourceMetadataFactory;
37+
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\ContentWriteSecurityResourceMetadataCollectionFactory;
3738
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\RoutableResourceMetadataCollectionFactory;
3839
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\RoutingPrefixResourceMetadataCollectionFactory;
3940
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\UploadableResourceMetadataCollectionFactory;
@@ -2051,6 +2052,18 @@
20512052
);
20522053
$services->alias(UserResourceMetadataCollectionFactory::class, 'silverback.api_components.api_platform.metadata.resource.user_factory');
20532054

2055+
$services
2056+
->set('silverback.api_components.api_platform.metadata.resource.content_write_security_factory')
2057+
->class(ContentWriteSecurityResourceMetadataCollectionFactory::class)
2058+
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, -1)
2059+
->args(
2060+
[
2061+
new Reference('silverback.api_components.api_platform.metadata.resource.content_write_security_factory.inner'),
2062+
new Reference(PublishableAttributeReader::class),
2063+
]
2064+
);
2065+
$services->alias(ContentWriteSecurityResourceMetadataCollectionFactory::class, 'silverback.api_components.api_platform.metadata.resource.content_write_security_factory');
2066+
20542067
$services
20552068
->set(VerifyEmailAddressAction::class)
20562069
->public()

0 commit comments

Comments
 (0)