Commit ec73ef16 authored by gbyte.co's avatar gbyte.co

Adding access checking to every generated path. If no access, path will not be...

Adding access checking to every generated path. If no access, path will not be included in the sitemap.
parent 15a3818c
......@@ -13,42 +13,11 @@ use Drupal\simplesitemap\SitemapGenerator;
* Adds sitemap settings to entity types that are supported via plugins.
*/
function simplesitemap_form_alter(&$form, $form_state, $form_id) {
// Get all simplesitemap plugins.
$manager = \Drupal::service('plugin.manager.simplesitemap');
$plugins = $manager->getDefinitions();
// Go through simplesitemap plugins and check if one of them declares usage
// of this particular form. If that's the case, get entity type id of the
// plugin definition and assume the bundle to be of the same name as the
// entity type id.
foreach($plugins as $plugin) {
if (isset($plugin['form_id']) && $plugin['form_id'] === $form_id) {
$entity_type_id = $plugin['id'];
$bundle_name = $entity_type_id;
}
}
// Else get entity type id and bundle name from the form if available and only
// if a simplesitemap plugin of the same entity type exists.
if (empty($entity_type_id)) {
$form_entity = Simplesitemap::get_form_entity($form_state);
if ($form_entity !== FALSE) {
$form_entity_type_id = $form_entity->getEntityTypeId();
if (isset($plugins[$form_entity_type_id])) {
if (!isset($plugins[$form_entity_type_id]['form_id'])
|| $plugins[$form_entity_type_id]['form_id'] === $form_id) {
$entity_type_id = $form_entity_type_id;
$bundle_name = $form_entity->Id();
}
}
}
}
// If both methods of getting simplesitemap configuration for this form
// failed, return.
if (empty($entity_type_id))
$sitemap_form_entity_data = Simplesitemap::get_sitemap_form_entity_data($form_state, $form_id);
if (empty($sitemap_form_entity_data))
return;
$entity_type_id = $sitemap_form_entity_data['entity_type_id'];
$bundle_name = $sitemap_form_entity_data['bundle_name'];
$sitemap = new Simplesitemap;
......
......@@ -8,10 +8,14 @@
namespace Drupal\simplesitemap;
use Drupal\Core\Url;
use \Drupal\user\Entity\User;
/**
* CustomLinkGenerator class.
*/
class CustomLinkGenerator {
const ANONYMOUS_USER_ID = 0;
/**
* Returns an array of all urls of the custom paths.
......@@ -22,13 +26,17 @@ class CustomLinkGenerator {
*
*/
public function get_custom_paths($custom_paths) {
$anonymous_account = User::load(self::ANONYMOUS_USER_ID);
$paths = array();
foreach($custom_paths as $i => $custom_path) {
if (!isset($custom_path['index']) || $custom_path['index']) {
$paths[$i]['path'] = substr($custom_path['path'], 1);
$paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
$paths[$i]['lastmod'] = NULL; //todo: implement lastmod
//todo: get url parameters and page fragment into $paths[$i]['settings'] so that future hook implementations can alter the array instead of having to do string processing.
$url_object = Url::fromUserInput($custom_path['path'], array());
if ($url_object->access($anonymous_account)) {
$paths[$i]['path'] = $url_object->getInternalPath();
$paths[$i]['options'] = $url_object->getOptions();
$paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
$paths[$i]['lastmod'] = NULL; //todo: implement lastmod
}
}
}
return $paths;
......
......@@ -7,18 +7,24 @@
namespace Drupal\simplesitemap;
use Drupal\Component\Plugin\PluginBase;
use \Drupal\user\Entity\User;
abstract class LinkGeneratorBase extends PluginBase implements LinkGeneratorInterface {
private $entity_paths = array();
private $current_entity_type;
private $anonymous_account;
const PLUGIN_ERROR_MESSAGE = "The simplesitemap @plugin plugin has been omitted, as it does not return the required numeric array of path data sets. Each data sets must contain the required path element and optionally other elements, like lastmod.";
const ANONYMOUS_USER_ID = 0;
/**
* {@inheritdoc}
*/
public function get_entity_paths($entity_type, $bundles) {
$this->current_entity_type = $entity_type;
$this->anonymous_account = User::load(self::ANONYMOUS_USER_ID);
$i = 0;
foreach($bundles as $bundle => $bundle_settings) {
if (!$bundle_settings['index'])
......@@ -73,4 +79,8 @@ abstract class LinkGeneratorBase extends PluginBase implements LinkGeneratorInte
* @abstract
*/
abstract function get_entity_bundle_paths($bundle);
protected function access($url_object) {
return $url_object->access($this->anonymous_account); //todo: Add error checking.
}
}
......@@ -33,14 +33,14 @@ class Menu extends LinkGeneratorBase {
if (empty($entity->route_name))
continue;
$paths[$id]['options'] = !empty($options = unserialize($entity->options)) ? $options : array();
//todo: Use Url::getRouteParameters()?
$route_parameters = !empty($route_parameters = unserialize($entity->route_parameters))
$options = !empty($options = unserialize($entity->options)) ? $options : array(); //todo: Use Url::getOptions()
$route_parameters = !empty($route_parameters = unserialize($entity->route_parameters)) //todo: Use Url::getRouteParameters()
? array(key($route_parameters) => $route_parameters[key($route_parameters)]) : array();
$paths[$id]['path'] = Url::fromRoute($entity->route_name, $route_parameters, $options)->getInternalPath();
//todo: Implement lastmod for menu items.
if (parent::access($url_object = Url::fromRoute($entity->route_name, $route_parameters, $options))) {
$paths[$id]['path'] = $url_object->getInternalPath();
$paths[$id]['options'] = $url_object->getOptions();
//todo: Implement lastmod for menu items.
}
}
return $paths;
}
......
......@@ -30,8 +30,10 @@ class NodeType extends LinkGeneratorBase {
$paths = array();
foreach ($results as $id => $data) {
$paths[$id]['path'] = Url::fromRoute("entity.node.canonical", array('node' => $id), array())->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
if (parent::access($url_object = Url::fromRoute("entity.node.canonical", array('node' => $id), array()))) {
$paths[$id]['path'] = $url_object->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
}
}
return $paths;
}
......
......@@ -30,8 +30,10 @@ class TaxonomyVocabulary extends LinkGeneratorBase {
$paths = array();
foreach ($results as $id => $data) {
$paths[$id]['path'] = Url::fromRoute("entity.taxonomy_term.canonical", array('taxonomy_term' => $id), array())->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
if (parent::access($url_obj = Url::fromRoute("entity.taxonomy_term.canonical", array('taxonomy_term' => $id), array()))) {
$paths[$id]['path'] = $url_obj->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
}
}
return $paths;
}
......
......@@ -31,8 +31,10 @@ class User extends LinkGeneratorBase {
$paths = array();
foreach ($results as $id => $data) {
$paths[$id]['path'] = Url::fromRoute("entity.user.canonical", array('user' => $id), array())->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
if (parent::access($url_obj = Url::fromRoute("entity.user.canonical", array('user' => $id), array()))) {
$paths[$id]['path'] = $url_obj->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
}
}
return $paths;
}
......
......@@ -26,15 +26,68 @@ class Simplesitemap {
}
/**
* Returns an the form entity object.
* Gets the entity_type_id and bundle_name of the form object if available and only
* if the sitemap supports this entity type through an existing plugin.
*
* @param object $form_state
* @param string $form_id
*
* @return object $entity or FALSE if non-existent.
* @return array containing the entity_type_id and the bundle_name of the
* form object or FALSE if none found or not supported by an existing plugin.
*/
public static function get_form_entity($form_state) {
if (!is_null($form_state->getFormObject())
&& method_exists($form_state->getFormObject(), 'getEntity')) {
public static function get_sitemap_form_entity_data($form_state, $form_id) {
// Get all simplesitemap plugins.
$manager = \Drupal::service('plugin.manager.simplesitemap');
$plugins = $manager->getDefinitions();
// Go through simplesitemap plugins and check if one of them declares usage
// of this particular form. If that's the case, get entity type id of the
// plugin definition and assume the bundle to be of the same name as the
// entity type id.
foreach($plugins as $plugin) {
if (isset($plugin['form_id']) && $plugin['form_id'] === $form_id) {
return array(
'entity_type_id' => $plugin['id'],
'bundle_name' => $plugin['id'],
);
}
}
// Else get entity type id and bundle name from the form if available and only
// if a simplesitemap plugin of the same entity type exists.
$form_entity = self::get_form_entity($form_state);
if ($form_entity !== FALSE) {
$form_entity_type_id = $form_entity->getEntityTypeId();
if (isset($plugins[$form_entity_type_id])) {
if (!isset($plugins[$form_entity_type_id]['form_id'])
|| $plugins[$form_entity_type_id]['form_id'] === $form_id) {
return array(
'entity_type_id' => $form_entity_type_id,
'bundle_name' => $form_entity->Id(),
);
}
}
}
// If both methods of getting simplesitemap entity data for this form
// failed, return FALSE.
return FALSE;
}
/**
* Gets the object entity of the form if available.
*
* @param object $form_state
*
* @return object $entity or FALSE if non-existent or if form operation is
* 'delete'.
*/
private static function get_form_entity($form_state) {
$form_object = $form_state->getFormObject();
if (!is_null($form_object)
&& method_exists($form_state->getFormObject(), 'getEntity')
&& $form_object->getOperation() !== 'delete') {
$entity = $form_state->getFormObject()->getEntity();
return $entity;
}
......@@ -57,6 +110,9 @@ class Simplesitemap {
/**
* Gets a specific sitemap configuration from the configuration storage.
*
* @return mixed
* The requested configuration.
*/
public function get_config($key) {
return $this->config->get($key);
......@@ -187,7 +243,7 @@ class Simplesitemap {
/**
* Returns a 'time ago' string of last timestamp generation.
*
*
* @return mixed
* Formatted timestamp of last sitemap generation, otherwise FALSE.
*/
......
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