Skip to content

Commit

Permalink
LDT-80: Capture delete operation and log in Drupal database.
Browse files Browse the repository at this point in the history
  • Loading branch information
axelabhay committed May 1, 2024
1 parent 7c80ee2 commit 8a33623
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Drupal/modules/custom/api_response_modifier/README.md
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
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
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 }
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(),
]);
}
}
}
}
}

0 comments on commit 8a33623

Please sign in to comment.