Commit 79e688d0 authored by salvis's avatar salvis

Issue 2215439 by lussoluca, salvis: Move DNA to its own project for D8.

parent d96f9a7d
......@@ -27,10 +27,6 @@ Devel Generate
=================
Bulk creates nodes, users, comment, terms for development. Has Drush integration.
Devel Node Access
=================
Prints the node_access records for a given node. Also offers hook_node_access_explain for all node access modules to implement.
Devel Generate Extensions
=========================
Devel Images Provider [http://drupal.org/project/devel_image_provider] allows to configure external providers for images.
......
README
======
This module contains tools for developers using access control modules
to restrict access to some nodes. It is intended to help catch some
common mistakes and provide feedback to confirm that restricted nodes
are in fact visible only to the intended users.
Provides a summary page which queries the node_access table and
reports common mistakes such as the presence of Drupal's default entry
which grants all users read access to all nodes. Also reports the
presence of nodes not represented in node_access table. This may
occur when an access control module is installed after nodes have
already been created.
Provides a block which shows all node_access entries for the nodes
shown on a given page. This gives developers a quick check to see
that grants are provided as they should be. This block auto-enables
to the footer region. You may move it as desired.
If Views module is installed, allows browsing of nodes by realm,
including those nodes not in the node_access table (NULL realm).
WISHLIST
========
Things I'd like to see but haven't had time to do:
* Automatically solve common problems. I.e. delete the "all" realm
entry, and automatically save all nodes not in the node_access table.
* Nicer feedback indicating whether nodes are visible to the public or
not. I.e. use color coding or icons.
* Summary does not differentiate between view grants and other types
of grants. I personally use node_access only for view grants so I'm
not sure exactly what else it should show.
AUTHOR
======
Dave Cohen AKA yogadex on drupal.org
debug_mode: FALSE
by_user_mode: FALSE
user_ajax: FALSE
# Schema for the configuration files of the Devel Node Access module.
devel_node_access.settings:
type: mapping
label: 'Devel Node Access settings'
mapping:
debug_mode:
type: boolean
label: 'Debug Mode'
by_user_mode:
type: boolean
label: 'By-User Analysis'
user_ajax:
type: boolean
label: 'User AJAX'
div.devel-node-access-inline > div {
display: inline-block;
}
<?php
/**
* @file
* Hook provided by the Devel Node Access module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Explain your records in the {node_access} table.
*
* In order to help developers and administrators understand the forces
* that control access to any given node, the DNA module provides the
* Devel Node Access block, which lists all the grant records in the
* {node_access} table for that node.
*
* However, every Node Access module is free in how it defines and uses the
* 'realm' and 'gid' fields in its records in the {node_access} table, and
* it's often difficult to interpret them. This hook passes each record
* that DNA wants to display, and the owning module is expected to return
* an explanation of that record.
*
* The explanation should not be localized (not be passed through t()), so
* that administrators seeking help can present English explanations.
*
* @param $row
* The record from the {node_access} table, as object. The member fields are:
* nid, gid, realm, grant_view, grant_update, grant_delete.
*
* @return string|null
* A string with a (short!) explanation of the given {node_access} row,
* to be displayed in DNA's 'Devel Node Access' block. It will be displayed
* as HTML; any variable parts must already be sanitized.
*
* @see hook_node_access_records()
* @see devel_node_access_node_access_explain()
*
* @ingroup node_access
*/
function hook_node_access_explain($row) {
if ($row->realm == 'mymodule_myrealm') {
if ($row->grant_view) {
/** @var \Drupal\user\RoleInterface $role */
$role = \Drupal\user\Entity\Role::load($row->gid);
return t('Role %role may view this node.', array('%role' => $role->get('name')));
}
else {
return 'No access.';
}
}
return null;
}
/**
* Acknowledge ownership of 'alien' grant records.
*
* Some node access modules store grant records directly into the {node_access}
* table rather than returning them through hook_node_access_records(). This
* practice is not recommended and DNA will flag all such records as 'alien'.
*
* If this is unavoidable, a module can confess to being the owner of these
* grant records, so that DNA can properly attribute them.
*
* @see hook_node_access_records()
*
* @ingroup node_access
*/
function hook_node_access_acknowledge($grant) {
if ($grant['realm'] == 'mymodule_all' && $grant['nid'] == 0) {
return TRUE;
}
return NULL;
}
/**
* @} End of "addtogroup hooks".
*/
type: module
name: 'Devel Node Access'
description: 'Developer blocks and page illustrating relevant node_access records.'
package: Development
core: 8.x
dependencies:
- devel
- menu_ui
tags:
- developer
<?php
/**
* @file
* Install, update and uninstall functions for the Devel Node Access module.
*/
/**
* @file devel_node_access.js.
*/
(function ($) {
/**
* Perform the access by user ajax request.
*/
function devel_node_access_user_ajax(context, settings) {
// Get the cell ID for the first .dna-permission that isn't processed.
var cell = $('td.dna-permission', context)
.not('.ajax-processed', context)
.attr('id');
if (cell !== undefined) {
// Generate the URI from the basePath, path, data type, cell ID, and a
// random token to bypass caching.
var url = settings.path.basePath
+ "?q="
+ 'devel/node_access/by_user/json/'
+ cell
+ '/'
+ Math.floor((1000000000 * Math.random())).toString(16);
// Execute Ajax callback and handle the response.
$.getJSON(url, function(data) {
$('#' + cell, context).html(data).addClass('ajax-processed');
// Call this function again.
devel_node_access_user_ajax(context);
});
// Ajax fails silently on error, mark bad requests with an error message.
// If the request is just slow this will update when the request succeeds.
setTimeout(
function() {
if ($('#' + cell, context).hasClass('ajax-processed') == false) {
$('#' + cell, context)
.html(
'<span class="error">'
+ '<a href="' + url.replace('/json/', '/html/') + '">'
+ Drupal.t('Error: could not explain access')
+ '</a>'
+ '</span>'
)
.addClass('ajax-processed');
// Call this function again.
devel_node_access_user_ajax(context);
}
},
3000
);
}
}
/**
* Attach the access by user behavior which initiates ajax.
*/
Drupal.behaviors.develNodeAccessUserAjax = {
attach: function(context, settings) {
// Start the ajax.
devel_node_access_user_ajax(context, settings);
}
};
})(jQuery);
devel_node_access:
version: 0
js:
devel_node_access.js: {}
css:
component:
css/devel_node_access.module.css: {}
This diff is collapsed.
view devel_node_access information:
title: 'Access DNA information'
description: 'View the node_access information block on node pages and the summary page.'
restrict access: TRUE
<?php
namespace Drupal\devel_node_access\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\devel_node_access\Plugin\Block\DnaBlock;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the form to change the DNA settings.
*/
class DnaForm extends FormBase {
/**
* Constructs a new DnaForm object.
*/
public function __construct() {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static();
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'devel_dna_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attached'] = array(
'library' => array(
'devel_node_access/devel_node_access'
),
);
$debug_mode = \Drupal::config('devel_node_access.settings')->get('debug_mode');
$by_user_mode = \Drupal::config('devel_node_access.settings')->get('by_user_mode');
$form['node_content'] = array(
'#prefix' => '<div id="devel-node-access-node-content-div">',
'#suffix' => '</div>',
);
$form['node_content'][0] = DnaBlock::buildNodeInfo($debug_mode);
$form['user_content'] = array(
'#prefix' => '<div id="devel-node-access-by-user-content-div">',
'#suffix' => '</div>',
);
if ($by_user_mode) {
$form['user_content'][0] = DnaBlock::buildByUserInfo();
}
$form['setup'] = array(
'#markup' => t('Enable:'),
'#prefix' => '<div class="devel-node-access-inline">',
'#suffix' => '</div>',
);
$form['setup']['debug_mode'] = array(
'#type' => 'checkbox',
'#value' => $debug_mode,
'#prefix' => ' &nbsp; &nbsp; ',
'#title' => t('Debug Mode'),
'#ajax' => array(
'callback' => '::toggleDebugMode',
'wrapper' => 'devel-node-access-node-content-div',
),
'#disabled' => TRUE,
);
$form['setup']['by_user_mode'] = array(
'#type' => 'checkbox',
'#value' => $by_user_mode,
'#prefix' => ' &nbsp; &nbsp; ',
'#title' => t('By-User Analysis (slow!)'),
'#ajax' => array(
'callback' => '::toggleByUserMode',
'wrapper' => 'devel-node-access-by-user-content-div',
),
'#disabled' => TRUE,
);
return $form;
}
/**
* AJAX handler for form_state.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
*/
public function toggleDebugMode($form, FormStateInterface $form_state) {
$debug_mode = $form_state->getUserInput();
$debug_mode = !empty($debug_mode['debug_mode']);
\Drupal::configFactory()->getEditable('devel_node_access.settings')->set('debug_mode', $debug_mode)->save(TRUE);
$form['node_content'][0] = DnaBlock::buildNodeInfo($debug_mode);
return $form['node_content'];
}
/**
* AJAX handler for by_user_state.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
*/
public function toggleByUserMode($form, FormStateInterface $form_state) {
$by_user_mode = $form_state->getUserInput();
$by_user_mode = !empty($by_user_mode['by_user_mode']);
\Drupal::configFactory()->getEditable('devel_node_access.settings')->set('by_user_mode', $by_user_mode)->save(TRUE);
$form['user_content'][0] = ($by_user_mode ? DnaBlock::buildByUserInfo() : []);
return $form['user_content'];
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
This diff is collapsed.
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