Commit 9bfe1611 authored by Gorodkov Denis's avatar Gorodkov Denis

commit

parent 1cc430a5
......@@ -37,6 +37,7 @@
"drupal/rabbit_hole": "^1.0@beta",
"drupal/recaptcha": "^3.0",
"drupal/redirect": "^1.7",
"drupal/rest_api_access_token": "^1.6",
"drupal/restui": "^1.20",
"drupal/search_api": "^1.23",
"drupal/search_api_autocomplete": "^1.6",
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "786aa69208b3efb27fa74ecad0d06e91",
"content-hash": "3f28a441b2b942ac67dc8e3433c26a0a",
"packages": [
{
"name": "asm89/stack-cors",
......@@ -4074,6 +4074,50 @@
"source": "https://git.drupalcode.org/project/redirect"
}
},
{
"name": "drupal/rest_api_access_token",
"version": "1.6.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/rest_api_access_token.git",
"reference": "8.x-1.6"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/rest_api_access_token-8.x-1.6.zip",
"reference": "8.x-1.6",
"shasum": "404dd271cba3cd969fc4eaa8035ccf92773ffc18"
},
"require": {
"drupal/core": "^8 || ^9"
},
"type": "drupal-module",
"extra": {
"drupal": {
"version": "8.x-1.6",
"datestamp": "1614004617",
"security-coverage": {
"status": "covered",
"message": "Covered by Drupal's security advisory policy"
}
}
},
"notification-url": "https://packages.drupal.org/8/downloads",
"license": [
"GPL-2.0-or-later"
],
"authors": [
{
"name": "marcinkazmierski",
"homepage": "https://www.drupal.org/user/3616253"
}
],
"description": "REST API Access Token module.",
"homepage": "https://www.drupal.org/project/rest_api_access_token",
"support": {
"source": "https://git.drupalcode.org/project/rest_api_access_token"
}
},
{
"name": "drupal/restui",
"version": "1.20.0",
......
......@@ -6,3 +6,8 @@ services:
class: \Drupal\mymodule\Commands\BatchCommands
tags:
- { name: drush.command }
mymodule.authentication:
class: Drupal\mymodule\Authentication\Provider\CustomAuth
arguments: ['@entity_type.manager']
tags:
- { name: authentication_provider, provider_id: 'x-csrf', priority: 100, global: TRUE }
<?php
namespace Drupal\mymodule\Authentication\Provider;
use Drupal\Core\Authentication\AuthenticationProviderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Access;
/**
* HTTP Basic authentication provider.
*/
class CustomAuth implements AuthenticationProviderInterface {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a HTTP basic authentication provider object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function applies(Request $request) {
$x_crsf = $request->headers->get('X-CSRF-Token');
return !empty($x_crsf);
}
/**
* {@inheritdoc}
*/
public function authenticate(Request $request) {
$x_crsf = $request->headers->get('X-CSRF-Token');
$valid = \Drupal::csrfToken()->validate($x_crsf);
$account = \Drupal\user\Entity\User::load(1);
return $account;
return NULL;
}
}
......@@ -2,7 +2,6 @@
namespace Drupal\mymodule\Plugin\rest\resource;
use Drupal\config_override_integration_test\CacheabilityMetadataConfigOverride;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
......@@ -10,6 +9,7 @@ use Symfony\Component\HttpFoundation\Response;
use \Drupal\node\Entity\Node;
use \Drupal\Core\Cache\CacheableMetadata;
/**
* Provides a resource to get people
*
......@@ -26,14 +26,14 @@ class GetPeople extends ResourceBase {
public function get() {
$query = \Drupal::request()->query->get('page');
if (empty($query)) {
$query = 1;
}
$cache = CacheableMetadata::createFromRenderArray([
'#cache' => [
'max-age' => 600,
'context' => ['url.query_args'],
'max-age' => 900,
],
]);
......@@ -44,22 +44,22 @@ class GetPeople extends ResourceBase {
]);
$nodes = array_values($nodes);
$count_nodes = count($nodes);
$number_last_page = (int)ceil($count_nodes/10);
$limit_page = (int)ceil($count_nodes/10);
$i = 0;
$response['count'] = $count_nodes;
$host = \Drupal::request()->getHost();
$host = 'http://' . $host . '/api/people?page=';
if ($query <= $number_last_page) {
if ($query <= $limit_page) {
if ($count_nodes > 10 and (empty($query) or $query == 1)) {
$response['next'] = $host . '2';
$response['previous'] = null;
} elseif ($query != $number_last_page) {
} elseif ($query != $limit_page) {
$response['next'] = $host . ($query + 1);
$response['previous'] = $host . ($query - 1);
} else {
$response['next'] = null;
$response['previous'] = $host . ($number_last_page - 1);
$response['previous'] = $host . ($limit_page - 1);
}
} else {
return new ResourceResponse('404 error');
......@@ -69,7 +69,7 @@ class GetPeople extends ResourceBase {
$i = ($query * 10) - 10;
}
if ($query == $number_last_page) {
if ($query == $limit_page) {
$last_index = count($nodes);
} else {
$last_index = $query * 10;
......@@ -93,54 +93,11 @@ class GetPeople extends ResourceBase {
$result['homeworld']['id'] = $node_homeworld->id();
$result['homeworld']['label'] = $node_homeworld->label();
$items = $node->field_films->referencedEntities();
foreach ($items as $item) {
$obj = [
'id' => $item->id(),
'label' => $item->label(),
];
$result['films'][] = $obj;
}
$items = $node->field_species->referencedEntities();
if (!empty($items)) {
foreach ($items as $item) {
$obj = [
'id' => $item->id(),
'label' => $item->label(),
];
$result['species'][] = $obj;
}
} else {
$result['species'][] = null;
}
$items = $node->field_vehicles->referencedEntities();
if (!empty($items)) {
foreach ($items as $item) {
$obj = [
'id' => $item->id(),
'label' => $item->label(),
];
$result['vehicles'][] = $obj;
}
} else {
$result['vehicles'][] = null;
}
$result['films'][] = $this->fillReference('films', $node);
$result['species'][] = $this->fillReference('species', $node);
$result['vehicles'][] = $this->fillReference('vehicles', $node);
$result['starships'][] = $this->fillReference('starships', $node);
$items = $node->field_starships->referencedEntities();
if (!empty($items)) {
foreach ($items as $item) {
$obj = [
'id' => $item->id(),
'label' => $item->label(),
];
$result['starships'][] = $obj;
}
} else {
$result['starships'][] = null;
}
$created = $node->get('created')->getValue();
$created = date("Y-m-d H:i:s", (int)$created[0]['value']);
$result['created'] = $created;
......@@ -148,22 +105,20 @@ class GetPeople extends ResourceBase {
$changed = $node->get('changed')->getValue();
$changed = date("Y-m-d H:i:s", (int)$changed[0]['value']);
$result['changed'] = $changed;
#$name = $node->label();
#$alias = Url::fromRoute('entity.node.canonical', ['node' => $node->id()])->toString();
$alias = $node->toUrl()->setAbsolute()->toString();
$result['url'] = $alias;
$url = $node->toUrl()->toString(TRUE);
$result['url'] = $url->getGeneratedUrl();
$response['results'][] = $result;
$cache->addCacheableDependency($node);
}
}
return new JsonResponse($response);
return (new ResourceResponse($response, 200))->addCacheableDependency($cache);
}
public function post($data) {
$head = \Drupal::request()->headers->get('X-CSRF-Token');
#aNNEGy331SxMmOsiGbCyBGUfQz7EXl7iIcXS-Vh8vSQ
if ($data['swapi_id']) {
if ($this->checkNode($data['swapi_id'])) {
return new ResourceResponse(['Нода с таким swapi_id уже существует!']);
......@@ -194,6 +149,23 @@ class GetPeople extends ResourceBase {
}
}
public function fillReference($nameAttr, $node) {
$field_name = "field_" . $nameAttr;
$items = $node->$field_name->referencedEntities();
if (!empty($items)) {
foreach ($items as $item) {
$obj = [
'id' => $item->id(),
'label' => $item->label(),
];
$result[$nameAttr][] = $obj;
}
} else {
$result[$nameAttr][] = null;
}
return $result[$nameAttr];
}
public function getFieldValue($field_name, $node) {
$value = $node->get($field_name)->getValue();
$value = $value[0]['value'];
......
......@@ -4,6 +4,7 @@ namespace Drupal\mymodule\Plugin\rest\resource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Drupal\Core\Cache\CacheableMetadata;
/**
* Provides a resource to get people
......@@ -12,10 +13,42 @@ use Drupal\rest\ResourceResponse;
* id = "post_people",
* label = @Translation("Post people"),
* uri_paths = {
* "/create" = "/post_test",
* "canonical" = "/post_test",
* }
* )
*/
class PostPeople extends ResourceBase {
/**
* {@inheritdoc}
*/
public function get() {
$cache = CacheableMetadata::createFromRenderArray([
'#cache' => [
'max-age' => 600,
],
]);
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => 'people',
'field_swapi_id' => 1,
]);
foreach ($nodes as $item) {
$node = $item;
}
$response = new ResourceResponse(
[
'title' => $node->label(),
'time' => time(),
]
);
#$cache->addCacheableDependency($node);
$response->addCacheableDependency($cache);
return $response;
}
}
......@@ -4,7 +4,7 @@
# 'example.settings.local.php' file, which sits next to this file.
parameters:
# Change to true if it really needed
http.response.debug_cacheability_headers: false
http.response.debug_cacheability_headers: true
twig.config:
debug: true
auto_reload: true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment