Skip to content

Commit 0405190

Browse files
committed
add anonymous users support
1 parent eed7f16 commit 0405190

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/Action/RegisterSubscriptionAction.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace BenTools\WebPushBundle\Action;
44

55
use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionManagerRegistry;
6+
use BenTools\WebPushBundle\Model\User\AnonymousUser;
67
use Symfony\Component\HttpFoundation\Request;
78
use Symfony\Component\HttpFoundation\Response;
8-
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
99
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1010
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1111
use Symfony\Component\Security\Core\User\UserInterface;
@@ -55,17 +55,14 @@ private function unsubscribe(UserInterface $user, string $subscriptionHash)
5555

5656
public function __invoke(Request $request, UserInterface $user = null): Response
5757
{
58-
if (null === $user) {
59-
throw new AccessDeniedHttpException('Not authenticated.');
60-
}
61-
6258
if (!in_array($request->getMethod(), ['POST', 'DELETE'])) {
6359
throw new MethodNotAllowedHttpException(['POST', 'DELETE']);
6460
}
6561

6662
$data = json_decode($request->getContent(), true);
6763
$subscription = $data['subscription'] ?? [];
6864
$options = $data['options'] ?? [];
65+
$user = $user ?? new AnonymousUser();
6966

7067
if (JSON_ERROR_NONE !== json_last_error()) {
7168
throw new BadRequestHttpException(json_last_error_msg());

src/Model/User/AnonymousUser.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
4+
namespace BenTools\WebPushBundle\Model\User;
5+
6+
use Symfony\Component\Security\Core\User\UserInterface;
7+
8+
/**
9+
* This class is used to support anonymous user subscription
10+
*
11+
* @internal
12+
*/
13+
class AnonymousUser implements UserInterface
14+
{
15+
public function getRoles()
16+
{
17+
}
18+
19+
public function getPassword()
20+
{
21+
}
22+
23+
public function getSalt()
24+
{
25+
}
26+
27+
public function getUsername()
28+
{
29+
}
30+
31+
public function eraseCredentials()
32+
{
33+
}
34+
}

0 commit comments

Comments
 (0)