Skip to content

Commit

Permalink
Merge branch 'main' of github.com:nysenate/NYSenate.gov-Website-D9
Browse files Browse the repository at this point in the history
  • Loading branch information
aheaphy committed Sep 12, 2024
2 parents 8e0a1d3 + 882bce1 commit af42822
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"drupal/diff": "^1.0",
"drupal/eck": "^2.0",
"drupal/email_registration": "^2.0@RC",
"drupal/email_tfa": "^2.0",
"drupal/email_tfa": "^1.0",
"drupal/entity_print": "^2.13",
"drupal/entity_reference_revisions": "^1.10",
"drupal/entityqueue": "^1.2",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions config/sync/user.role.microsite_content_producer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ dependencies:
- node.type.video
- node.type.webform
module:
- block
- captcha
- contact
- content_moderation
Expand Down Expand Up @@ -48,7 +47,6 @@ permissions:
- 'access toolbar'
- 'access user contact forms'
- 'access webform overview'
- 'administer blocks'
- 'create article content'
- 'create event content'
- 'create field_featured'
Expand Down
9 changes: 9 additions & 0 deletions web/modules/custom/nys_senators/nys_senators.module
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ function nys_senators_entity_access(EntityInterface $entity, $operation, Account
return AccessResult::neutral();
}

/**
* Implements hook_entity_type_build().
*/
function nys_senators_entity_type_build(array &$entity_types) {
if (isset($entity_types['block_content'])) {
$entity_types['block_content']->setHandlerClass('access', '\Drupal\nys_senators\Access\McpBlockContentAccessControlHandler');
}
}

/**
* Implements hook_entity_presave().
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Drupal\nys_senators\Access;

use Drupal\block_content\BlockContentAccessControlHandler;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\nys_users\UsersHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* Custom content block access control handler for MCPs.
*/
class McpBlockContentAccessControlHandler extends BlockContentAccessControlHandler {

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* McpBlockContentAccessControlHandler constructor.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
* @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $dispatcher
* The event dispatcher.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
*/
public function __construct(
EntityTypeInterface $entity_type,
EventDispatcherInterface $dispatcher,
EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($entity_type, $dispatcher);
$this->entityTypeManager = $entityTypeManager;
}

/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('event_dispatcher'),
$container->get('entity_type.manager')
);
}

/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
// Grant MCPs access to edit content blocks tied to their senator.
$current_user = UsersHelper::resolveUser();
if (UsersHelper::isMcp($current_user)) {
$managed_senator_tids = UsersHelper::getManagedSenators($current_user);
try {
$node_storage = $this->entityTypeManager
->getStorage('node');
}
catch (\Exception) {
}
if (isset($node_storage)) {
$is_block_linked_to_managed_senator = $node_storage
->getQuery()
->accessCheck(FALSE)
->condition('field_block', $entity->id(), 'CONTAINS')
->condition('field_senator_multiref', $managed_senator_tids, 'IN')
->count()
->execute();
if ($is_block_linked_to_managed_senator) {
return AccessResult::allowed();
}
}
}

// Otherwise, fallback on core access rules.
return parent::checkAccess($entity, $operation, $account);
}

/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
// Grant MCPs access to create new content blocks.
$current_user = UsersHelper::resolveUser();
if (UsersHelper::isMcp($current_user)) {
return AccessResult::allowed();
}

// Otherwise, fallback on core access rules.
return parent::checkCreateAccess($account, $context, $entity_bundle);
}

}

0 comments on commit af42822

Please sign in to comment.