You are here

class CalendarEventAccess in Opigno calendar event 3.x

Same name and namespace in other branches
  1. 8 src/CalendarEventAccess.php \Drupal\opigno_calendar_event\CalendarEventAccess

Defines access control handler for the calendar event entity type.

Hierarchy

  • class \Drupal\opigno_calendar_event\CalendarEventAccess extends \Drupal\entity\EntityAccessControlHandler

Expanded class hierarchy of CalendarEventAccess

File

src/CalendarEventAccess.php, line 13

Namespace

Drupal\opigno_calendar_event
View source
class CalendarEventAccess extends EntityAccessControlHandler {

  /**
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
    if ($account
      ->hasPermission($operation . ' opigno_calendar_event')) {
      return AccessResult::allowed();
    }
    if ($entity
      ->get('uid')->target_id == $account
      ->id() && $account
      ->hasPermission($operation . ' own opigno calendar event')) {
      return AccessResult::allowed();
    }

    // Check if current user is a member.
    if ($operation == 'view') {
      $members = $entity
        ->get('field_calendar_event_members')
        ->getValue();
      $account_id = $account
        ->id();
      foreach ($members as $member) {
        if ($member['target_id'] == $account_id) {
          return AccessResult::allowed();
        }
      }
    }
    return AccessResult::forbidden();
  }

  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
    if ($account
      ->hasPermission('create opigno_calendar_event')) {
      return AccessResult::allowed();
    }
    return AccessResult::forbidden();
  }

}

Members