Skip to content

Commit 5ad7c58

Browse files
authored
Merge pull request #20 from jsdrupal/support-module
Move the support module into the main repo
2 parents 3e9343e + 951aaa1 commit 5ad7c58

File tree

5 files changed

+293
-0
lines changed

5 files changed

+293
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ yarn build
1414
cd ../drupal
1515
ln -s ../drupal-admin-ui/build vfancy
1616
17+
## Link the support module to Drupal
18+
cd ../drupal
19+
ln -s ../drupal-admin-ui/admin_ui_support modules
20+
1721
# Install Drupal and start webserver
1822
composer install
1923
php -S localhost:8000
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: 'Admin UI support'
2+
type: module
3+
description: 'Provides endpoints for the drupal-admin-ui'
4+
version: VERSION
5+
core: 8.x
6+
dependencies:
7+
- drupal:rest
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- admin_ui_support
6+
- serialization
7+
- user
8+
id: permissions_collection
9+
plugin_id: permissions_collection
10+
granularity: resource
11+
configuration:
12+
methods:
13+
- GET
14+
formats:
15+
- json
16+
authentication:
17+
- cookie
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Drupal\admin_ui_support\Plugin\rest\resource;
4+
5+
use Drupal\Component\Render\PlainTextOutput;
6+
use Drupal\Core\Extension\ExtensionList;
7+
use Drupal\Core\Extension\ModuleExtensionList;
8+
use Drupal\Core\Extension\ModuleHandlerInterface;
9+
use Drupal\Core\Render\RenderContext;
10+
use Drupal\Core\Render\RendererInterface;
11+
use Drupal\rest\Plugin\ResourceBase;
12+
use Drupal\rest\ResourceResponse;
13+
use Drupal\user\PermissionHandlerInterface;
14+
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
16+
17+
/**
18+
* Provides a rest resource for listing available permissions.
19+
*
20+
* @RestResource(
21+
* id = "permissions_collection",
22+
* label = @Translation("Available permissions"),
23+
* uri_paths = {
24+
* "canonical" = "/admin-api/permissions"
25+
* }
26+
* )
27+
*/
28+
class PermissionsCollectionResource extends ResourceBase {
29+
30+
/**
31+
* The permission handler.
32+
*
33+
* @var \Drupal\user\PermissionHandlerInterface
34+
*/
35+
protected $permissionHandler;
36+
37+
/**
38+
* The renderer.
39+
*
40+
* @var \Drupal\Core\Render\RendererInterface
41+
*/
42+
protected $renderer;
43+
44+
/**
45+
* The module extension list.
46+
*
47+
* @var \Drupal\Core\Extension\ExtensionList
48+
*/
49+
protected $moduleExtensionList;
50+
51+
/**
52+
* Constructs a PermissionResource object.
53+
*
54+
* @param array $configuration
55+
* A configuration array containing information about the plugin instance.
56+
* @param string $plugin_id
57+
* The plugin_id for the plugin instance.
58+
* @param mixed $plugin_definition
59+
* The plugin implementation definition.
60+
* @param array $serializer_formats
61+
* The available serialization formats.
62+
* @param \Psr\Log\LoggerInterface $logger
63+
* A logger instance.
64+
* @param \Drupal\user\PermissionHandlerInterface $permission_handler
65+
* The permission handler
66+
* @param \Drupal\Core\Render\RendererInterface $renderer
67+
* The renderer.
68+
* @param \Drupal\Core\Extension\ExtensionList $module_extension_list
69+
* The module extension list.
70+
*/
71+
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, PermissionHandlerInterface $permission_handler, RendererInterface $renderer, ExtensionList $module_extension_list) {
72+
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
73+
74+
$this->permissionHandler = $permission_handler;
75+
$this->renderer = $renderer;
76+
$this->moduleExtensionList = $module_extension_list;
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
83+
return new static(
84+
$configuration,
85+
$plugin_id,
86+
$plugin_definition,
87+
$container->getParameter('serializer.formats'),
88+
$container->get('logger.factory')->get('rest'),
89+
$container->get('user.permissions'),
90+
$container->get('renderer'),
91+
$container->get('extension.list.module')
92+
);
93+
}
94+
95+
/**
96+
* Responds to GET requests.
97+
*
98+
* @return \Drupal\rest\ResourceResponse
99+
* The response containing the list of available permissions.
100+
*/
101+
public function get() {
102+
$context = new RenderContext();
103+
104+
$permissions = $this->renderer->executeInRenderContext($context, function () {
105+
$permissions = $this->permissionHandler->getPermissions();
106+
foreach ($permissions as $id => $permission) {
107+
$permissions[$id]['id'] = $id;
108+
$permissions[$id]['provider_label'] = $this->moduleExtensionList->getName($permissions[$id]['provider']);
109+
};
110+
return array_values($permissions);
111+
});
112+
113+
$response = new ResourceResponse($permissions);
114+
$response->addCacheableDependency($context);
115+
return $response;
116+
}
117+
118+
/**
119+
* {@inheritdoc}
120+
*/
121+
public function permissions() {
122+
return [];
123+
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
protected function getBaseRoute($canonical_path, $method) {
129+
$route = parent::getBaseRoute($canonical_path, $method);
130+
$route->addRequirements(['_permission' => 'administer permissions']);
131+
return $route;
132+
}
133+
134+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Drupal\Tests\admin_ui_support\Functional\rest;
4+
5+
use Drupal\Component\Serialization\Json;
6+
use Drupal\Core\Url;
7+
use Drupal\rest\RestResourceConfigInterface;
8+
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
9+
use Drupal\Tests\rest\Functional\ResourceTestBase;
10+
11+
/**
12+
* Tests the user permissions resource.
13+
*
14+
* @group user
15+
*/
16+
class UserPermissionsResourceTest extends ResourceTestBase {
17+
18+
use CookieResourceTestTrait;
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
protected static $format = 'hal_json';
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected static $mimeType = 'application/hal+json';
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected static $auth = 'cookie';
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
protected static $resourceConfigId = 'permissions_collection';
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public static $modules = ['hal', 'user', 'rest', 'admin_ui_support'];
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function setUp() {
49+
parent::setUp();
50+
51+
$auth = isset(static::$auth) ? [static::$auth] : [];
52+
$this->provisionResource([static::$format], $auth);
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
protected function provisionResource($formats = [], $authentication = []) {
59+
$this->resourceConfigStorage->create([
60+
'id' => static::$resourceConfigId,
61+
'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
62+
'configuration' => [
63+
'methods' => ['GET'],
64+
'formats' => $formats,
65+
'authentication' => $authentication,
66+
],
67+
'status' => TRUE,
68+
])->save();
69+
$this->refreshTestStateAfterRestConfigChange();
70+
}
71+
72+
/**
73+
* Writes a log messages and retrieves it via the REST API.
74+
*/
75+
public function testPermissionsCollection() {
76+
$this->initAuthentication();
77+
$url = Url::fromRoute('rest.permissions_collection.GET', ['_format' => static::$format]);
78+
$request_options = $this->getAuthenticationRequestOptions('GET');
79+
80+
$response = $this->request('GET', $url, $request_options);
81+
$this->assertResourceResponse(403, '{"message":"The \u0027administer permissions\u0027 permission is required."}', $response);
82+
83+
// create a user account that has the required permissions to read
84+
// the watchdog resource via the rest api.
85+
$this->setUpAuthorization('GET');
86+
87+
$response = $this->request('GET', $url, $request_options);
88+
$this->assertResourceResponse(200, false, $response, ['config:rest.resource.permissions_collection', 'config:rest.settings', 'http_response'], ['user.permissions'], FALSE, 'MISS');
89+
$permissions = json::decode((string) $response->getBody());
90+
91+
$permission_handler = \drupal::service('user.permissions')->getPermissions();
92+
$this->assertSame(array_keys($permission_handler), array_keys($permissions));
93+
$this->assertSame([
94+
'title' => 'Administer permissions',
95+
'restrict access' => TRUE,
96+
'description' => NULL,
97+
'provider' => 'user',
98+
'id' => 'administer permissions',
99+
], $permissions['administer permissions']);
100+
}
101+
102+
/**
103+
* {@inheritdoc}
104+
*/
105+
protected function setUpAuthorization($method) {
106+
switch ($method) {
107+
case 'GET':
108+
$this->grantPermissionsToTestedRole(['administer permissions']);
109+
break;
110+
111+
default:
112+
throw new \UnexpectedValueException();
113+
}
114+
}
115+
116+
/**
117+
* {@inheritdoc}
118+
*/
119+
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {}
120+
121+
/**
122+
* {@inheritdoc}
123+
*/
124+
protected function getExpectedUnauthorizedAccessMessage($method) {}
125+
126+
/**
127+
* {@inheritdoc}
128+
*/
129+
protected function getExpectedUnauthorizedAccessCacheability() {}
130+
131+
}

0 commit comments

Comments
 (0)