BackgroundImageAccessControlHandler.php in Background Image 2.0.x
File
src/BackgroundImageAccessControlHandler.php
View source
<?php
namespace Drupal\background_image;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
class BackgroundImageAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
switch ($operation) {
case 'view':
return AccessResult::allowedIfHasPermission($account, 'view background image');
case 'edit':
case 'update':
if ($entity
->getOwnerId() === $account
->id()) {
return AccessResult::allowedIfHasPermission($account, 'edit own background image')
->orIf(AccessResult::allowedIfHasPermission($account, 'edit background image'));
}
return AccessResult::allowedIfHasPermission($account, 'edit background image');
case 'delete':
if ($entity
->getOwnerId() === $account
->id()) {
return AccessResult::allowedIfHasPermission($account, 'delete own background image')
->orIf(AccessResult::allowedIfHasPermission($account, 'delete background image'));
}
return AccessResult::allowedIfHasPermission($account, 'delete background image');
}
return AccessResult::neutral();
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add background image');
}
}