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

8.0.x
catch 2013-02-06 12:35:42 +00:00
parent f0656a7c38
commit 8215bf998a
8 changed files with 137 additions and 51 deletions

View File

@ -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');
}

View File

@ -22,6 +22,7 @@ use Drupal\Core\Annotation\Translation;
* 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"
* },

View File

@ -19,6 +19,7 @@ use Drupal\Core\Annotation\Translation;
* label = @Translation("Taxonomy vocabulary"),
* module = "taxonomy",
* controller_class = "Drupal\taxonomy\VocabularyStorageController",
* access_controller_class = "Drupal\taxonomy\VocabularyAccessController",
* form_controller_class = {
* "default" = "Drupal\taxonomy\VocabularyFormController"
* },

View File

@ -65,7 +65,7 @@ class LinkEdit extends FieldPluginBase {
$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()));
}

View File

@ -0,0 +1,49 @@
<?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);
}
}

View File

@ -15,20 +15,6 @@ use Drupal\translation_entity\EntityTranslationController;
*/
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 @@ class TermTranslationController extends EntityTranslationController {
$form_state['redirect'] = $this->getEditPath($entity);
}
}
}

View File

@ -0,0 +1,49 @@
<?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);
}
}

View File

@ -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.
*