You are here

OpignoModuleAccessControlHandler.php in Opigno module 3.x

Same filename and directory in other branches
  1. 8 src/OpignoModuleAccessControlHandler.php

File

src/OpignoModuleAccessControlHandler.php
View source
<?php

namespace Drupal\opigno_module;

use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;

/**
 * Access controller for the Module entity.
 *
 * @see \Drupal\opigno_module\Entity\OpignoModule.
 */
class OpignoModuleAccessControlHandler extends EntityAccessControlHandler {

  /**
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
    if ($account
      ->hasPermission('manage group content in any group')) {
      return AccessResult::allowed();
    }

    /** @var \Drupal\opigno_module\Entity\OpignoModuleInterface $entity */
    switch ($operation) {
      case 'view':
        if (!$entity
          ->isPublished()) {
          return AccessResult::allowedIfHasPermission($account, 'view unpublished module entities');
        }
        return AccessResult::allowedIfHasPermission($account, 'view published module entities');
      case 'update':
        if ($entity
          ->getOwnerId() === $account
          ->id()) {

          // Allow users to edit its own content.
          return AccessResult::allowed();
        }
        return AccessResult::allowedIfHasPermission($account, 'edit module entities');
      case 'delete':
        if ($entity
          ->getOwnerId() === $account
          ->id()) {

          // Allow users to delete its own content.
          return AccessResult::allowed();
        }
        return AccessResult::allowedIfHasPermission($account, 'delete module entities');
    }

    // Unknown operation, no opinion.
    return AccessResult::neutral();
  }

  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
    if ($account
      ->hasPermission('manage group content in any group')) {
      return AccessResult::allowed();
    }
    return AccessResult::allowedIfHasPermission($account, 'add module entities');
  }

}

Classes

Namesort descending Description
OpignoModuleAccessControlHandler Access controller for the Module entity.