Commit 8215bf99 authored by catch's avatar catch

Issue #1862758 by dawehner, rootatwc: Implement entity access API for terms and vocabularies.

parent f0656a7c
......@@ -743,3 +743,22 @@ function entity_query($entity_type, $conjunction = 'AND') {
function entity_page_access(EntityInterface $entity, $operation = 'view') {
return $entity->access($operation);
}
/**
* Generic access callback for create entity pages.
*
* Some entity types might have create access per bundle or something else.
* In that case you have to create a custom access callback.
*
* @param string $entity_type
* The entity type.
*
* @return bool
* TRUE if the access is granted. FALSE if access is denied.
*/
function entity_page_create_access($entity_type) {
$entity = drupal_container()->get('plugin.manager.entity')
->getStorageController($entity_type)
->create(array());
return $entity->access('create');
}
......@@ -22,6 +22,7 @@
* module = "taxonomy",
* controller_class = "Drupal\taxonomy\TermStorageController",
* render_controller_class = "Drupal\taxonomy\TermRenderController",
* access_controller_class = "Drupal\taxonomy\TermAccessController",
* form_controller_class = {
* "default" = "Drupal\taxonomy\TermFormController"
* },
......
......@@ -19,6 +19,7 @@
* label = @Translation("Taxonomy vocabulary"),
* module = "taxonomy",
* controller_class = "Drupal\taxonomy\VocabularyStorageController",
* access_controller_class = "Drupal\taxonomy\VocabularyAccessController",
* form_controller_class = {
* "default" = "Drupal\taxonomy\VocabularyFormController"
* },
......
......@@ -65,7 +65,7 @@ function render($values) {
$term = entity_create('taxonomy_term', array(
'vid' => $values->{$this->aliases['vid']},
));
if (taxonomy_term_access('edit', $term)) {
if ($term->access('update')) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('edit');
return l($text, 'taxonomy/term/'. $tid . '/edit', array('query' => drupal_get_destination()));
}
......
<?php
/**
* @file
* Contains \Drupal\taxonomy\TermAccessController.
*/
namespace Drupal\taxonomy;
use Drupal\Core\Entity\EntityAccessController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\Plugin\Core\Entity\User;
/**
* Defines an access controller for the taxonomy term entity.
*
* @see \Drupal\taxonomy\Plugin\Core\Entity\Term
*/
class TermAccessController extends EntityAccessController {
/**
* Implements \Drupal\Core\Entity\EntityAccessControllerInterface::viewAccess().
*/
public function viewAccess(EntityInterface $entity, $langcode = LANGUAGE_DEFAULT, User $account = NULL) {
return user_access('access content', $account);
}
/**
* Implements \Drupal\Core\Entity\EntityAccessControllerInterface::createAccess().
*/
public function createAccess(EntityInterface $entity, $langcode = LANGUAGE_DEFAULT, User $account = NULL) {
return user_access('administer taxonomy', $account);
}
/**
* Implements \Drupal\Core\Entity\EntityAccessControllerInterface::updateAccess().
*/
public function updateAccess(EntityInterface $entity, $langcode = LANGUAGE_DEFAULT, User $account = NULL) {
return user_access("update terms in {$entity->bundle()}", $account) || user_access('administer taxonomy', $account);
}
/**
* Implements \Drupal\Core\Entity\EntityAccessControllerInterface::deleteAccess().
*/
public function deleteAccess(EntityInterface $entity, $langcode = LANGUAGE_DEFAULT, User $account = NULL) {
return user_access("delete terms in {$entity->bundle()}", $account) || user_access('administer taxonomy', $account);
}
}
......@@ -15,20 +15,6 @@
*/
class TermTranslationController extends EntityTranslationController {
/**
* Overrides EntityTranslationController::getAccess().
*/
public function getAccess(EntityInterface $entity, $op) {
switch ($op) {
case 'create':
case 'update':
return taxonomy_term_access('edit', $entity);
case 'delete':
return taxonomy_term_access('delete', $entity);
}
return parent::getAccess($entity, $op);
}
/**
* Overrides EntityTranslationController::entityFormAlter().
*/
......@@ -53,4 +39,5 @@ function entityFormSave(array $form, array &$form_state) {
$form_state['redirect'] = $this->getEditPath($entity);
}
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\VocabularyAccessController.
*/
namespace Drupal\taxonomy;
use Drupal\Core\Entity\EntityAccessController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\Plugin\Core\Entity\User;
/**
* Defines an access controller for the vocabulary entity.
*
* @see \Drupal\taxonomy\Plugin\Core\Entity\Vocabulary.
*/
class VocabularyAccessController extends EntityAccessController {
/**
* Implements \Drupal\Core\Entity\EntityAccessControllerInterface::viewAccess().
*/
public function viewAccess(EntityInterface $entity, $langcode = LANGUAGE_DEFAULT, User $account = NULL) {
return user_access('administer taxonomy', $account);
}
/**
* Implements \Drupal\Core\Entity\EntityAccessControllerInterface::createAccess().
*/
public function createAccess(EntityInterface $entity, $langcode = LANGUAGE_DEFAULT, User $account = NULL) {
return user_access('administer taxonomy', $account);
}
/**
* Implements \Drupal\Core\Entity\EntityAccessControllerInterface::updateAccess().
*/
public function updateAccess(EntityInterface $entity, $langcode = LANGUAGE_DEFAULT, User $account = NULL) {
return user_access('administer taxonomy', $account);
}
/**
* Implements \Drupal\Core\Entity\EntityAccessControllerInterface::deleteAccess().
*/
public function deleteAccess(EntityInterface $entity, $langcode = LANGUAGE_DEFAULT, User $account = NULL) {
return user_access('administer taxonomy', $account);
}
}
......@@ -271,7 +271,8 @@ function taxonomy_menu() {
$items['admin/structure/taxonomy/add'] = array(
'title' => 'Add vocabulary',
'page callback' => 'taxonomy_vocabulary_add',
'access arguments' => array('administer taxonomy'),
'access callback' => 'entity_page_create_access',
'access arguments' => array('taxonomy_vocabulary'),
'type' => MENU_LOCAL_ACTION,
'file' => 'taxonomy.admin.inc',
);
......@@ -282,7 +283,8 @@ function taxonomy_menu() {
'title arguments' => array(2),
'page callback' => 'taxonomy_term_page',
'page arguments' => array(2),
'access arguments' => array('access content'),
'access callback' => 'entity_page_access',
'access arguments' => array(2, 'view'),
'file' => 'taxonomy.pages.inc',
);
$items['taxonomy/term/%taxonomy_term/view'] = array(
......@@ -295,8 +297,8 @@ function taxonomy_menu() {
// Pass a NULL argument to ensure that additional path components are not
// passed to taxonomy_term_form() as the vocabulary machine name argument.
'page arguments' => array(2),
'access callback' => 'taxonomy_term_access',
'access arguments' => array('edit', 2),
'access callback' => 'entity_page_access',
'access arguments' => array(2, 'update'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
'file' => 'taxonomy.admin.inc',
......@@ -305,8 +307,8 @@ function taxonomy_menu() {
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_term_confirm_delete', 2),
'access callback' => 'taxonomy_term_access',
'access arguments' => array('delete', 2),
'access callback' => 'entity_page_access',
'access arguments' => array(2, 'delete'),
'type' => MENU_LOCAL_TASK,
'weight' => 11,
'file' => 'taxonomy.admin.inc',
......@@ -317,7 +319,8 @@ function taxonomy_menu() {
'title arguments' => array(2),
'page callback' => 'taxonomy_term_feed',
'page arguments' => array(2),
'access arguments' => array('access content'),
'access callback' => 'entity_page_access',
'access arguments' => array(2, 'view'),
'type' => MENU_CALLBACK,
'file' => 'taxonomy.pages.inc',
);
......@@ -335,7 +338,8 @@ function taxonomy_menu() {
'title arguments' => array(3),
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_overview_terms', 3),
'access arguments' => array('administer taxonomy'),
'access callback' => 'entity_page_access',
'access arguments' => array(3, 'view'),
'file' => 'taxonomy.admin.inc',
);
$items['admin/structure/taxonomy/%taxonomy_vocabulary/list'] = array(
......@@ -347,7 +351,8 @@ function taxonomy_menu() {
'title' => 'Edit',
'page callback' => 'entity_get_form',
'page arguments' => array(3),
'access arguments' => array('administer taxonomy'),
'access callback' => 'entity_page_access',
'access arguments' => array(3, 'update'),
'type' => MENU_LOCAL_TASK,
'weight' => -10,
'file' => 'taxonomy.admin.inc',
......@@ -357,7 +362,8 @@ function taxonomy_menu() {
'title' => 'Add term',
'page callback' => 'taxonomy_term_add',
'page arguments' => array(3),
'access arguments' => array('administer taxonomy'),
'access callback' => 'entity_page_create_access',
'access arguments' => array('taxonomy_term'),
'type' => MENU_LOCAL_ACTION,
'file' => 'taxonomy.admin.inc',
);
......@@ -378,32 +384,6 @@ function taxonomy_admin_paths() {
return $paths;
}
/**
* Access callback: Checks a user's permission for performing a taxonomy term
* operation.
*
* @param $op
* The operation to be performed on the taxonomy term. Possible values are:
* - "edit"
* - "delete"
* @param $term
* The $term object on which the operation is to be performed.
*
* @return
* TRUE if the operation may be performed, FALSE otherwise.
*
* @see taxonomy_menu()
*/
function taxonomy_term_access($op, $term) {
if (!$term || !in_array($op, array('edit', 'delete'), TRUE)) {
// If there was no term to check against, or the $op was not one of the
// supported ones, we return access denied.
return FALSE;
}
return user_access("$op terms in {$term->bundle()}") || user_access('administer taxonomy');
}
/**
* Saves a vocabulary.
*
......
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