ILTAccessControlHandler.php in Opigno Instructor-led Trainings 8
File
src/ILTAccessControlHandler.php
View source
<?php
namespace Drupal\opigno_ilt;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
class ILTAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($account
->hasPermission('manage group content in any group')) {
return AccessResult::allowed();
}
switch ($operation) {
case 'view':
if ($account
->hasPermission('manage group members in any group')) {
return AccessResult::allowed();
}
$training = $entity
->getTraining();
if (isset($training) && $training
->hasPermission('score ilt entities', $account)) {
return AccessResult::allowed();
}
$members = $entity
->getMembersIds();
if (!empty($members)) {
if (!in_array($account
->id(), $members)) {
return AccessResult::forbidden();
}
}
else {
$training = $entity
->getTraining();
if (isset($training) && $training
->getMember($account) === FALSE) {
return AccessResult::forbidden();
}
}
return AccessResult::allowedIfHasPermission($account, 'view ilt entities');
case 'edit':
if ($entity
->getOwnerId() === $account
->id()) {
return AccessResult::allowed();
}
return AccessResult::allowedIfHasPermission($account, 'edit ilt entities');
case 'delete':
if ($entity
->getOwnerId() === $account
->id()) {
return AccessResult::allowed();
}
return AccessResult::allowedIfHasPermission($account, 'delete ilt entities');
case 'score':
$training = $entity
->getTraining();
if (isset($training) && $training
->hasPermission('score ilt entities', $account)) {
return AccessResult::allowed();
}
return AccessResult::allowedIfHasPermission($account, 'score ilt entities');
}
return AccessResult::neutral();
}
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 ilt entities');
}
}