-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LDT-80: Capture delete operation and log in Drupal database.
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Secure Deletion of User-Created Articles | ||
|
||
## Description: | ||
Enhance the mobile application functionality to now allow users to securely review and delete their own articles. This feature requires the development or enhancement of an OAuth-secured API endpoint that supports deletion operations while ensuring robust security. | ||
|
||
## Acceptance Criteria: | ||
- Users must be able to review and delete only the articles that they have created | ||
- The system uses OAuth to authenticate users | ||
- The system verifies that the authenticated user has the appropriate role and permissions to delete the article | ||
- All deletion operations must be logged with sufficient details | ||
|
||
## Solution | ||
Course Link: | ||
Troubleshoot: | ||
Raise Issue: https://github.com/axelerant-trainings/project-usecases/issues/new |
5 changes: 5 additions & 0 deletions
5
Drupal/modules/custom/api_response_modifier/api_response_modifier.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: 'API Response Modifier' | ||
type: module | ||
description: 'Alters JSON:API response to include custom messages for delete operations.' | ||
package: Custom | ||
core_version_requirement: ^10 |
6 changes: 6 additions & 0 deletions
6
Drupal/modules/custom/api_response_modifier/api_response_modifier.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
api_response_mod.response_subscriber: | ||
class: Drupal\api_response_modifier\EventSubscriber\ApiResponseSubscriber | ||
arguments: ['@logger.factory'] | ||
tags: | ||
- { name: event_subscriber } |
59 changes: 59 additions & 0 deletions
59
Drupal/modules/custom/api_response_modifier/src/EventSubscriber/ApiResponseSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Drupal\api_response_modifier\EventSubscriber; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Drupal\Core\Logger\LoggerChannelFactoryInterface; | ||
|
||
/** | ||
* Class ApiResponseSubscriber. | ||
*/ | ||
class ApiResponseSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* Logger service. | ||
* | ||
* @var \Psr\Log\LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* Constructs a new ApiResponseSubscriber. | ||
* | ||
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory | ||
*/ | ||
public function __construct(LoggerChannelFactoryInterface $logger_factory) { | ||
$this->logger = $logger_factory->get('api_response_modifier'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
$events[KernelEvents::RESPONSE][] = ['onResponse', 0]; | ||
return $events; | ||
} | ||
|
||
/** | ||
* Responds to kernel response events. | ||
*/ | ||
public function onResponse(ResponseEvent $event) { | ||
$request = $event->getRequest(); | ||
if ($request->attributes->get('_route') === 'jsonapi.node--article.individual.delete') { | ||
if ($request->getMethod() === 'DELETE') { | ||
$response = $event->getResponse(); | ||
// Check if the response status code indicates a successful deletion. | ||
if ($response->getStatusCode() === Response::HTTP_NO_CONTENT) { | ||
$node = $request->attributes->get('entity'); | ||
$this->logger->info('Article \'@label\' (@nid) deleted via API.', [ | ||
'@label' => $node->label(), | ||
'@nid' => $node->id(), | ||
]); | ||
} | ||
} | ||
} | ||
} | ||
} |