You are here

class GroupContentCreateAccessCheck in Group 2.0.x

Same name and namespace in other branches
  1. 8 src/Access/GroupContentCreateAccessCheck.php \Drupal\group\Access\GroupContentCreateAccessCheck

Determines access for group content creation.

Hierarchy

Expanded class hierarchy of GroupContentCreateAccessCheck

1 string reference to 'GroupContentCreateAccessCheck'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses GroupContentCreateAccessCheck
access_check.group_content.create in ./group.services.yml
Drupal\group\Access\GroupContentCreateAccessCheck

File

src/Access/GroupContentCreateAccessCheck.php, line 15

Namespace

Drupal\group\Access
View source
class GroupContentCreateAccessCheck implements AccessInterface {

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

  /**
   * Constructs a EntityCreateAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Checks access for group content creation routes.
   *
   * All routes using this access check should have a group and plugin_id
   * parameter and have the _group_content_create_access requirement set to
   * either 'TRUE' or 'FALSE'.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group in which the content should be created.
   * @param string $plugin_id
   *   The group relation ID to use for the group content entity.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(Route $route, AccountInterface $account, GroupInterface $group, $plugin_id) {
    $needs_access = $route
      ->getRequirement('_group_content_create_access') === 'TRUE';

    // We can only get the group content type ID if the plugin is installed.
    if (!$group
      ->getGroupType()
      ->hasContentPlugin($plugin_id)) {
      return AccessResult::neutral();
    }

    // Determine whether the user can create group content using the plugin.
    $group_content_type_id = $group
      ->getGroupType()
      ->getContentPlugin($plugin_id)
      ->getContentTypeConfigId();
    $access_control_handler = $this->entityTypeManager
      ->getAccessControlHandler('group_content');
    $access = $access_control_handler
      ->createAccess($group_content_type_id, $account, [
      'group' => $group,
    ]);

    // Only allow access if the user can create group content using the
    // provided plugin or if he doesn't need access to do so.
    return AccessResult::allowedIf($access xor !$needs_access);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupContentCreateAccessCheck::$entityTypeManager protected property The entity type manager.
GroupContentCreateAccessCheck::access public function Checks access for group content creation routes.
GroupContentCreateAccessCheck::__construct public function Constructs a EntityCreateAccessCheck object.