<?php
namespace Platform\SecurityBundle\Listeners\OneRoster;
use Cms\CoreBundle\Entity\AbstractOneRosterEntity;
use Cms\CoreBundle\Entity\OneRoster\OneRosterOrg;
use Cms\CoreBundle\Entity\OneRosterSync;
use Cms\CoreBundle\Events\OneRosterProcessEvent;
use Cms\CoreBundle\Model\Interfaces\OneRosterable\AbstractOneRosterableSubscriber;
use Doctrine\Common\Util\ClassUtils;
use Platform\SecurityBundle\Entity\Identity\Group;
/**
* Class OneRosterProcessUserSubscriber
* @package Platform\SecurityBundle\Listeners\OneRoster
*/
final class OneRosterProcessOrgSubscriber extends AbstractOneRosterableSubscriber
{
// NEW
const ALIASES = [
'oneroster.school.all' => [
AbstractOneRosterEntity::ENUMS__ROLE_TYPE__ADMINISTRATOR,
AbstractOneRosterEntity::ENUMS__ROLE_TYPE__AIDE,
AbstractOneRosterEntity::ENUMS__ROLE_TYPE__PROCTOR,
AbstractOneRosterEntity::ENUMS__ROLE_TYPE__TEACHER,
AbstractOneRosterEntity::ENUMS__ROLE_TYPE__STAFF,
],
'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__ADMINISTRATOR => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__ADMINISTRATOR],
'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__AIDE => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__AIDE],
'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__PROCTOR => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__PROCTOR],
'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__TEACHER => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__TEACHER],
'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__STAFF => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__STAFF],
];
/**
* {@inheritdoc}
*/
static public function getSubscribedEvents(): array
{
return [
OneRosterProcessEvent::EVENT__ORG => [
['groupsSync', 1],
],
];
}
/**
* @param OneRosterProcessEvent $event
*/
public function groupsSync(OneRosterProcessEvent $event): void
{
// ensure we are meant to process this
if ( ! $this->checkTypes($event->getSync(), [
OneRosterSync::STRATEGIES__SSO,
])) {
return;
}
// get the org
$org = $event->getEntity();
if ( ! $org instanceof OneRosterOrg) {
throw new \Exception(sprintf(
'SSO: Org is not of proper type, got "%s".',
ClassUtils::getClass($org)
));
}
// holder for groups to make
$groups = [];
// NEW: loop over all the groups we need to make
foreach (array_keys(self::ALIASES) as $base) {
// generate the exact alias
$alias = sprintf(
'%s.%s',
$base,
$org->getSourcedId()
);
// attempt to load the group by alias
$group = $this->em->getRepository(Group::class)->findOneBy([
'alias' => $alias,
]);
// now if null, we need to make a new one
if (empty($group)) {
$group = new Group();
}
// update fields on the account
$group
->setName($this->translator->trans(
sprintf(
'campussuite.platform.security.groups.aliases.%s',
$base
),
[
'%org%' => $org->getName(),
]
))
->setAlias($alias)
->setFixed(true)
->setOneRosterId($org->getSourcedId())
;
// attach to array of things to save
$groups[] = $group;
}
// cache the output
$output = array_map(
function (Group $grp) {
return sprintf(
' %s %s (%s | %s | %s)',
((empty($grp->getId())) ? 'Generating' : 'Updating'),
ClassUtils::getClass($grp),
$grp->getName(),
$grp->getOneRosterId(),
$grp->getId() ?: '-'
);
},
$groups
);
// let's save the groups
$this->em->saveAll($groups);
// DEBUGGING
$event->getOutput()->writeln($output);
}
}