-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a constraint for filtering access terms. (#4)
- Loading branch information
1 parent
e1c2e4e
commit 27ac2a2
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Drupal\idc_defaults\Plugin\Validation\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* Checks that the user has access to an entity. | ||
* | ||
* @Constraint( | ||
* id = "WorkbenchSections", | ||
* label = @Translation("Workbench Sections", context = "Validation"), | ||
* type = "string" | ||
* ) | ||
*/ | ||
class WorkbenchSections extends Constraint { | ||
|
||
/** | ||
* Entity passed in does not exist or isn't a taxonomy term. | ||
* | ||
* @var string | ||
*/ | ||
public $badType = 'The entity does not exist or is not a taxonomy term.'; | ||
|
||
/** | ||
* User does not have access to the term (collection) section via workbench. | ||
* | ||
* @var string | ||
*/ | ||
public $noAccess = 'The user does not have access to ingest into the collection: @collection.'; | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
src/Plugin/Validation/Constraint/WorkbenchSectionsValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Drupal\idc_defaults\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Session\AccountInterface; | ||
use Drupal\taxonomy\TermInterface; | ||
use Drupal\workbench_access\UserSectionStorageInterface; | ||
use Drupal\workbench_access\WorkbenchAccessManager; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
/** | ||
* Validates that the user has access to the entity. | ||
*/ | ||
class WorkbenchSectionsValidator extends ConstraintValidator implements ContainerInjectionInterface { | ||
|
||
/** | ||
* The entity type manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected $entityTypeManager; | ||
|
||
/** | ||
* Current user. | ||
* | ||
* @var \Drupal\Core\Session\AccountInterface | ||
*/ | ||
protected $currentUser; | ||
|
||
/** | ||
* User section storage. | ||
* | ||
* @var \Drupal\workbench_access\UserSectionStorageInterface | ||
*/ | ||
protected $userSectionStorage; | ||
|
||
/** | ||
* Validator construct. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | ||
* The entity type manager. | ||
* @param \Drupal\Core\Session\AccountInterface $currentUser | ||
* The current user making the request. | ||
* @param \Drupal\workbench_access\UserSectionStorageInterface $userSectionStorage | ||
* User section storage. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountInterface $currentUser, UserSectionStorageInterface $userSectionStorage) { | ||
$this->entityTypeManager = $entityTypeManager; | ||
$this->currentUser = $currentUser; | ||
$this->userSectionStorage = $userSectionStorage; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('entity_type.manager'), | ||
$container->get('current_user'), | ||
$container->get('workbench_access.user_section_storage') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($items, Constraint $constraint) { | ||
foreach ($items as $item) { | ||
if (!isset($item->entity) || !$item->entity instanceof TermInterface) { | ||
$this->context->addViolation($constraint->badType); | ||
} | ||
else { | ||
if (!$this->currentUser->hasPermission('bypass workbench access')) { | ||
// Ensure that an access scheme applies for this entity, bundle and | ||
// field. | ||
foreach ($this->entityTypeManager->getStorage('access_scheme')->loadMultiple() as $access_scheme) { | ||
$scheme = $access_scheme->getAccessScheme(); | ||
if (!$scheme->applies($item->getEntity()->getEntityTypeId(), $item->getEntity()->bundle())) { | ||
continue; | ||
} | ||
$fields = $scheme->getApplicableFields($item->getEntity()->getEntityTypeId(), $item->getEntity()->bundle()); | ||
foreach ($fields as $field) { | ||
if ($field['field'] !== $item->getFieldDefinition()->getName()) { | ||
continue; | ||
} | ||
// Ensure that the entity specified falls within the user's | ||
// allowed entities. | ||
if (!WorkbenchAccessManager::checkTree($access_scheme, [$item->entity->id()], $this->userSectionStorage->getUserSections($access_scheme))) { | ||
$this->context->addViolation($constraint->noAccess, ['@collection' => $item->entity->label()]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |