Skip to content

ISSUE-345: privileges #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
{
"name": "Xheni Myrtaj",
"email": "[email protected]",
"role": "Maintainer"
"role": "Former developer"
},
{
"name": "Oliver Klee",
"email": "[email protected]",
"role": "Former developer"
},
{
"name": "Tatevik Grigoryan",
"email": "[email protected]",
"role": "Maintainer"
}
],
"support": {
Expand All @@ -31,7 +36,7 @@
},
"require": {
"php": "^8.1",
"phplist/core": "v5.0.0-alpha7",
"phplist/core": "dev-ISSUE-345",
"friendsofsymfony/rest-bundle": "*",
"symfony/test-pack": "^1.0",
"symfony/process": "^6.4",
Expand Down Expand Up @@ -93,7 +98,6 @@
"symfony-tests-dir": "tests",
"phplist/core": {
"bundles": [
"FOS\\RestBundle\\FOSRestBundle",
"PhpList\\RestBundle\\PhpListRestBundle"
],
"routes": {
Expand All @@ -111,6 +115,11 @@
"resource": "@PhpListRestBundle/Messaging/Controller/",
"type": "attribute",
"prefix": "/api/v2"
},
"rest-api-analitics": {
"resource": "@PhpListRestBundle/Statistics/Controller/",
"type": "attribute",
"prefix": "/api/v2"
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions config/services/controllers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ services:
autowire: true
autoconfigure: true
public: true

PhpList\RestBundle\Statistics\Controller\:
resource: '../src/Statistics/Controller'
tags: [ 'controller.service_arguments' ]
autowire: true
autoconfigure: true
public: true
13 changes: 13 additions & 0 deletions config/services/managers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ services:
PhpList\Core\Domain\Subscription\Service\SubscriberCsvExporter:
autowire: true
autoconfigure: true

PhpList\Core\Domain\Analytics\Service\Manager\LinkTrackManager:
autowire: true
autoconfigure: true

PhpList\Core\Domain\Analytics\Service\Manager\UserMessageViewManager:
autowire: true
autoconfigure: true

PhpList\Core\Domain\Analytics\Service\AnalyticsService:
autowire: true
autoconfigure: true

16 changes: 16 additions & 0 deletions config/services/normalizers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ services:
PhpList\RestBundle\Subscription\Serializer\SubscribersExportRequestNormalizer:
tags: [ 'serializer.normalizer' ]
autowire: true

PhpList\RestBundle\Statistics\Serializer\CampaignStatisticsNormalizer:
tags: [ 'serializer.normalizer' ]
autowire: true

PhpList\RestBundle\Statistics\Serializer\ViewOpensStatisticsNormalizer:
tags: [ 'serializer.normalizer' ]
autowire: true

PhpList\RestBundle\Statistics\Serializer\TopDomainsNormalizer:
tags: [ 'serializer.normalizer' ]
autowire: true

PhpList\RestBundle\Statistics\Serializer\TopLocalPartsNormalizer:
tags: [ 'serializer.normalizer' ]
autowire: true
4 changes: 4 additions & 0 deletions config/services/providers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ services:
PhpList\RestBundle\Common\Service\Provider\PaginatedDataProvider:
autowire: true
autoconfigure: true

PhpList\RestBundle\Messaging\Service\CampaignService:
autowire: true
autoconfigure: true
1 change: 1 addition & 0 deletions src/Common/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/** @SuppressWarnings(PHPMD.NumberOfChildren) */
abstract class BaseController extends AbstractController
{
protected Authentication $authentication;
Expand Down
18 changes: 18 additions & 0 deletions src/Common/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace PhpList\RestBundle\Common\EventListener;

use Exception;
use PhpList\Core\Domain\Identity\Exception\AdminAttributeCreationException;
use PhpList\Core\Domain\Subscription\Exception\SubscriptionCreationException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Validator\Exception\ValidatorException;

class ExceptionListener
{
Expand All @@ -34,6 +37,21 @@ public function onKernelException(ExceptionEvent $event): void
'message' => $exception->getMessage(),
], $exception->getStatusCode());
$event->setResponse($response);
} elseif ($exception instanceof AdminAttributeCreationException) {
$response = new JsonResponse([
'message' => $exception->getMessage(),
], $exception->getStatusCode());
$event->setResponse($response);
} elseif ($exception instanceof ValidatorException) {
$response = new JsonResponse([
'message' => $exception->getMessage(),
], 400);
$event->setResponse($response);
} elseif ($exception instanceof AccessDeniedException) {
$response = new JsonResponse([
'message' => $exception->getMessage(),
], 403);
$event->setResponse($response);
} elseif ($exception instanceof Exception) {
$response = new JsonResponse([
'message' => $exception->getMessage(),
Expand Down
24 changes: 24 additions & 0 deletions src/Identity/OpenApi/SwaggerSchemasRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
type: 'boolean',
example: false
),
new OA\Property(
property: 'privileges',
description: 'Array of privileges where keys are privilege names and values are booleans',
properties: [
new OA\Property(property: 'subscribers', type: 'boolean', example: true),
new OA\Property(property: 'campaigns', type: 'boolean', example: false),
new OA\Property(property: 'statistics', type: 'boolean', example: true),
new OA\Property(property: 'settings', type: 'boolean', example: false),
],
type: 'object',
example: ['subscribers' => true, 'campaigns' => false, 'statistics' => true, 'settings' => false]
),
],
type: 'object'
)]
Expand Down Expand Up @@ -68,6 +80,18 @@
type: 'boolean',
example: false
),
new OA\Property(
property: 'privileges',
description: 'Array of privileges where keys are privilege names and values are booleans',
properties: [
new OA\Property(property: 'subscribers', type: 'boolean', example: true),
new OA\Property(property: 'campaigns', type: 'boolean', example: false),
new OA\Property(property: 'statistics', type: 'boolean', example: true),
new OA\Property(property: 'settings', type: 'boolean', example: false),
],
type: 'object',
example: ['subscribers' => true, 'campaigns' => false, 'statistics' => true, 'settings' => false]
),
],
type: 'object'
)]
Expand Down
22 changes: 18 additions & 4 deletions src/Identity/Request/CreateAdministratorRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpList\Core\Domain\Identity\Model\Administrator;
use PhpList\Core\Domain\Identity\Model\Dto\CreateAdministratorDto;
use PhpList\Core\Domain\Identity\Model\PrivilegeFlag;
use PhpList\RestBundle\Common\Request\RequestInterface;
use PhpList\RestBundle\Identity\Validator\Constraint\UniqueEmail;
use PhpList\RestBundle\Identity\Validator\Constraint\UniqueLoginName;
Expand All @@ -31,13 +32,26 @@ class CreateAdministratorRequest implements RequestInterface
#[Assert\Type('bool')]
public bool $superUser = false;

/**
* Array of privileges where keys are privilege names (from PrivilegeFlag enum) and values are booleans.
* Example: ['subscribers' => true, 'campaigns' => false, 'statistics' => true, 'settings' => false]
*/
#[Assert\Type('array')]
#[Assert\All([
'constraints' => [
new Assert\Type(['type' => 'bool']),
],
])]
public array $privileges = [];

public function getDto(): CreateAdministratorDto
{
return new CreateAdministratorDto(
$this->loginName,
$this->password,
$this->email,
$this->superUser
loginName: $this->loginName,
password: $this->password,
email: $this->email,
isSuperUser: $this->superUser,
privileges: $this->privileges
);
}
}
14 changes: 14 additions & 0 deletions src/Identity/Request/UpdateAdministratorRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpList\Core\Domain\Identity\Model\Administrator;
use PhpList\Core\Domain\Identity\Model\Dto\UpdateAdministratorDto;
use PhpList\Core\Domain\Identity\Model\PrivilegeFlag;
use PhpList\RestBundle\Common\Request\RequestInterface;
use PhpList\RestBundle\Identity\Validator\Constraint\UniqueEmail;
use PhpList\RestBundle\Identity\Validator\Constraint\UniqueLoginName;
Expand All @@ -29,6 +30,18 @@ class UpdateAdministratorRequest implements RequestInterface
#[Assert\Type('bool')]
public ?bool $superAdmin = null;

/**
* Array of privileges where keys are privilege names (from PrivilegeFlag enum) and values are booleans.
* Example: ['subscribers' => true, 'campaigns' => false, 'statistics' => true, 'settings' => false]
*/
#[Assert\Type('array')]
#[Assert\All([
'constraints' => [
new Assert\Type(['type' => 'bool']),
],
])]
public array $privileges = [];

public function getDto(): UpdateAdministratorDto
{
return new UpdateAdministratorDto(
Expand All @@ -37,6 +50,7 @@ public function getDto(): UpdateAdministratorDto
password: $this->password,
email: $this->email,
superAdmin: $this->superAdmin,
privileges: $this->privileges
);
}
}
1 change: 1 addition & 0 deletions src/Identity/Serializer/AdministratorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function normalize($object, string $format = null, array $context = []):
'login_name' => $object->getLoginName(),
'email' => $object->getEmail(),
'super_admin' => $object->isSuperUser(),
'privileges' => $object->getPrivileges()->all(),
'created_at' => $object->getCreatedAt()?->format(DateTimeInterface::ATOM),
];
}
Expand Down
54 changes: 18 additions & 36 deletions src/Messaging/Controller/CampaignController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
namespace PhpList\RestBundle\Messaging\Controller;

use OpenApi\Attributes as OA;
use PhpList\Core\Domain\Messaging\Model\Filter\MessageFilter;
use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Messaging\Service\MessageManager;
use PhpList\Core\Security\Authentication;
use PhpList\RestBundle\Common\Controller\BaseController;
use PhpList\RestBundle\Common\Service\Provider\PaginatedDataProvider;
use PhpList\RestBundle\Common\Validator\RequestValidator;
use PhpList\RestBundle\Messaging\Request\CreateMessageRequest;
use PhpList\RestBundle\Messaging\Request\UpdateMessageRequest;
use PhpList\RestBundle\Messaging\Serializer\MessageNormalizer;
use PhpList\RestBundle\Messaging\Service\CampaignService;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -29,21 +26,15 @@
#[Route('/campaigns', name: 'campaign_')]
class CampaignController extends BaseController
{
private MessageNormalizer $normalizer;
private MessageManager $messageManager;
private PaginatedDataProvider $paginatedProvider;
private CampaignService $campaignService;

public function __construct(
Authentication $authentication,
RequestValidator $validator,
MessageNormalizer $normalizer,
MessageManager $messageManager,
PaginatedDataProvider $paginatedProvider,
CampaignService $campaignService,
) {
parent::__construct($authentication, $validator);
$this->normalizer = $normalizer;
$this->messageManager = $messageManager;
$this->paginatedProvider = $paginatedProvider;
$this->campaignService = $campaignService;
}

#[Route('', name: 'get_list', methods: ['GET'])]
Expand Down Expand Up @@ -103,12 +94,10 @@ public function __construct(
)]
public function getMessages(Request $request): JsonResponse
{
$authUer = $this->requireAuthentication($request);

$filter = (new MessageFilter())->setOwner($authUer);
$authUser = $this->requireAuthentication($request);

return $this->json(
$this->paginatedProvider->getPaginatedList($request, $this->normalizer, Message::class, $filter),
$this->campaignService->getMessages($request, $authUser),
Response::HTTP_OK
);
}
Expand Down Expand Up @@ -157,11 +146,7 @@ public function getMessage(
): JsonResponse {
$this->requireAuthentication($request);

if (!$message) {
throw $this->createNotFoundException('Campaign not found.');
}

return $this->json($this->normalizer->normalize($message), Response::HTTP_OK);
return $this->json($this->campaignService->getMessage($message), Response::HTTP_OK);
}

#[Route('', name: 'create', methods: ['POST'])]
Expand Down Expand Up @@ -216,15 +201,17 @@ public function getMessage(
),
]
)]
public function createMessage(Request $request, MessageNormalizer $normalizer): JsonResponse
public function createMessage(Request $request): JsonResponse
{
$authUser = $this->requireAuthentication($request);

/** @var CreateMessageRequest $createMessageRequest */
$createMessageRequest = $this->validator->validate($request, CreateMessageRequest::class);
$data = $this->messageManager->createMessage($createMessageRequest->getDto(), $authUser);

return $this->json($normalizer->normalize($data), Response::HTTP_CREATED);
return $this->json(
$this->campaignService->createMessage($createMessageRequest, $authUser),
Response::HTTP_CREATED
);
}

#[Route('/{messageId}', name: 'update', requirements: ['messageId' => '\d+'], methods: ['PUT'])]
Expand Down Expand Up @@ -291,14 +278,13 @@ public function updateMessage(
): JsonResponse {
$authUser = $this->requireAuthentication($request);

if (!$message) {
throw $this->createNotFoundException('Campaign not found.');
}
/** @var UpdateMessageRequest $updateMessageRequest */
$updateMessageRequest = $this->validator->validate($request, UpdateMessageRequest::class);
$data = $this->messageManager->updateMessage($updateMessageRequest->getDto(), $message, $authUser);

return $this->json($this->normalizer->normalize($data), Response::HTTP_OK);
return $this->json(
$this->campaignService->updateMessage($updateMessageRequest, $authUser, $message),
Response::HTTP_OK
);
}

#[Route('/{messageId}', name: 'delete', requirements: ['messageId' => '\d+'], methods: ['DELETE'])]
Expand Down Expand Up @@ -348,13 +334,9 @@ public function deleteMessage(
Request $request,
#[MapEntity(mapping: ['messageId' => 'id'])] ?Message $message = null
): JsonResponse {
$this->requireAuthentication($request);

if (!$message) {
throw $this->createNotFoundException('Campaign not found.');
}
$authUser = $this->requireAuthentication($request);

$this->messageManager->delete($message);
$this->campaignService->deleteMessage($authUser, $message);

return $this->json(null, Response::HTTP_NO_CONTENT);
}
Expand Down
Loading
Loading