LPResultAccessControlHandler.php in Opigno Learning path 3.x
File
src/LPResultAccessControlHandler.php
View source
<?php
namespace Drupal\opigno_learning_path;
use Drupal\Core\Access\AccessException;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
class LPResultAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$group = $entity
->getLearningPath();
$is_owner = $entity
->getUserId() == $account
->id();
if (empty($group) || !is_object($group)) {
return AccessResult::neutral();
}
if ($group
->getGroupType()
->id() !== 'learning_path') {
throw new AccessException('LPResult associated with wrong group type!');
}
switch ($operation) {
case 'view':
return AccessResult::allowedIf($is_owner && $group
->hasPermission('view own results', $account) || $group
->hasPermission('view all results', $account));
case 'edit':
return AccessResult::allowedIf($is_owner && $group
->hasPermission('edit own results', $account) || $group
->hasPermission('edit all results', $account));
case 'delete':
return AccessResult::allowedIf($is_owner && $group
->hasPermission('delete own results', $account) || $group
->hasPermission('delete all results', $account));
}
return AccessResult::neutral();
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::neutral();
}
}