<?php
namespace App\Service\Content;
use App\Entity\Feed\AbstractEntry;
use App\Entity\System\School;
use App\Util\Permissions;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ContentSchoolProvider
{
private EntityManager $em;
private AuthorizationCheckerInterface $authorizationChecker;
/**
* @param EntityManager $em
* @param AuthorizationCheckerInterface $authorizationChecker
*/
public function __construct(
EntityManager $em,
AuthorizationCheckerInterface $authorizationChecker
)
{
$this->em = $em;
$this->authorizationChecker = $authorizationChecker;
}
/**
* @return School[]
*/
public function getSchools(): array
{
static $perms = [
'campussuite.cms.container.generic.manage',// schools only use generic departments right now...
'campussuite.cms.module.manage',
'campussuite.cms.modules.calendar.manage',
'campussuite.cms.modules.gallery.manage',
'campussuite.cms.modules.news.manage',
];
$schools = [];
foreach ($this->em->getRepository(School::class)->findAll() as $school) {
if ($this->authorizationChecker->isGranted(Permissions::attributesExpression($perms), $school)) {
$schools[] = $school;
}
}
return $schools;
}
/**
* @param School $school
* @return array
*/
public function getSchoolAllowedEntryTypes(School $school): array
{
// NOTE: since the input is a school, we should only ever have a "generic" department tied to it...
static $mappings = [
AbstractEntry::TYPES__POST => [
'campussuite.cms.container.generic.manage',
'campussuite.cms.module.manage',
'campussuite.cms.modules.news.manage',
],
AbstractEntry::TYPES__GALLERY => [
'campussuite.cms.container.generic.manage',
'campussuite.cms.module.manage',
'campussuite.cms.modules.gallery.manage',
],
AbstractEntry::TYPES__EVENT => [
'campussuite.cms.container.generic.manage',
'campussuite.cms.module.manage',
'campussuite.cms.modules.calendar.manage',
],
AbstractEntry::TYPES__VIDEO => [
'campussuite.cms.container.generic.manage',
'campussuite.cms.module.manage',
'campussuite.cms.modules.gallery.manage',
],
];
$types = [];
foreach ($mappings as $type => $perms) {
if ($this->authorizationChecker->isGranted(Permissions::attributesExpression($perms), [null, $school->getDepartment()])) {
$types[] = $type;
}
}
return $types;
}
}