WeatherAccessControlHandler.php in Weather 2.0.x
File
src/WeatherAccessControlHandler.php
View source
<?php
namespace Drupal\weather;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\weather\Entity\WeatherDisplayInterface;
class WeatherAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
return $this
->commonAccessCheck($account, $entity);
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return $this
->commonAccessCheck($account);
}
protected function commonAccessCheck(AccountInterface $account, EntityInterface $entity = NULL) {
if ($account
->hasPermission('administer site configuration')) {
return AccessResult::allowed();
}
$entityTypesAllowed = [
'weather_display',
'weather_display_place',
];
if ($account
->hasPermission('administer custom weather block') && in_array($this->entityTypeId, $entityTypesAllowed)) {
if ($entity instanceof EntityInterface) {
$typeFieldName = $this->entityTypeId == 'weather_display' ? 'type' : 'display_type';
$ownerFieldName = $this->entityTypeId == 'weather_display' ? 'number' : 'display_number';
$type = $entity->{$typeFieldName}->value;
$owner = $entity->{$ownerFieldName}->value;
return AccessResult::allowedIf($type == WeatherDisplayInterface::USER_TYPE && $owner == $account
->id());
}
return AccessResult::allowed();
}
return AccessResult::neutral();
}
}