src/Platform/SecurityBundle/Listeners/OneRoster/OneRosterProcessOrgSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Listeners\OneRoster;
  3. use Cms\CoreBundle\Entity\AbstractOneRosterEntity;
  4. use Cms\CoreBundle\Entity\OneRoster\OneRosterOrg;
  5. use Cms\CoreBundle\Entity\OneRosterSync;
  6. use Cms\CoreBundle\Events\OneRosterProcessEvent;
  7. use Cms\CoreBundle\Model\Interfaces\OneRosterable\AbstractOneRosterableSubscriber;
  8. use Doctrine\Common\Util\ClassUtils;
  9. use Platform\SecurityBundle\Entity\Identity\Group;
  10. /**
  11.  * Class OneRosterProcessUserSubscriber
  12.  * @package Platform\SecurityBundle\Listeners\OneRoster
  13.  */
  14. final class OneRosterProcessOrgSubscriber extends AbstractOneRosterableSubscriber
  15. {
  16.     // NEW
  17.     const ALIASES = [
  18.         'oneroster.school.all' => [
  19.             AbstractOneRosterEntity::ENUMS__ROLE_TYPE__ADMINISTRATOR,
  20.             AbstractOneRosterEntity::ENUMS__ROLE_TYPE__AIDE,
  21.             AbstractOneRosterEntity::ENUMS__ROLE_TYPE__PROCTOR,
  22.             AbstractOneRosterEntity::ENUMS__ROLE_TYPE__TEACHER,
  23.             AbstractOneRosterEntity::ENUMS__ROLE_TYPE__STAFF,
  24.         ],
  25.         'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__ADMINISTRATOR => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__ADMINISTRATOR],
  26.         'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__AIDE => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__AIDE],
  27.         'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__PROCTOR => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__PROCTOR],
  28.         'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__TEACHER => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__TEACHER],
  29.         'oneroster.school.'.AbstractOneRosterEntity::ENUMS__ROLE_TYPE__STAFF => [AbstractOneRosterEntity::ENUMS__ROLE_TYPE__STAFF],
  30.     ];
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     static public function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             OneRosterProcessEvent::EVENT__ORG => [
  38.                 ['groupsSync'1],
  39.             ],
  40.         ];
  41.     }
  42.     /**
  43.      * @param OneRosterProcessEvent $event
  44.      */
  45.     public function groupsSync(OneRosterProcessEvent $event): void
  46.     {
  47.         // ensure we are meant to process this
  48.         if ( ! $this->checkTypes($event->getSync(), [
  49.             OneRosterSync::STRATEGIES__SSO,
  50.         ])) {
  51.             return;
  52.         }
  53.         // get the org
  54.         $org $event->getEntity();
  55.         if ( ! $org instanceof OneRosterOrg) {
  56.             throw new \Exception(sprintf(
  57.                 'SSO: Org is not of proper type, got "%s".',
  58.                 ClassUtils::getClass($org)
  59.             ));
  60.         }
  61.         // holder for groups to make
  62.         $groups = [];
  63.         // NEW: loop over all the groups we need to make
  64.         foreach (array_keys(self::ALIASES) as $base) {
  65.             // generate the exact alias
  66.             $alias sprintf(
  67.                 '%s.%s',
  68.                 $base,
  69.                 $org->getSourcedId()
  70.             );
  71.             // attempt to load the group by alias
  72.             $group $this->em->getRepository(Group::class)->findOneBy([
  73.                 'alias' => $alias,
  74.             ]);
  75.             // now if null, we need to make a new one
  76.             if (empty($group)) {
  77.                 $group = new Group();
  78.             }
  79.             // update fields on the account
  80.             $group
  81.                 ->setName($this->translator->trans(
  82.                     sprintf(
  83.                         'campussuite.platform.security.groups.aliases.%s',
  84.                         $base
  85.                     ),
  86.                     [
  87.                         '%org%' => $org->getName(),
  88.                     ]
  89.                 ))
  90.                 ->setAlias($alias)
  91.                 ->setFixed(true)
  92.                 ->setOneRosterId($org->getSourcedId())
  93.             ;
  94.             // attach to array of things to save
  95.             $groups[] = $group;
  96.         }
  97.         // cache the output
  98.         $output array_map(
  99.             function (Group $grp) {
  100.                 return sprintf(
  101.                     '    %s    %s    (%s | %s | %s)',
  102.                     ((empty($grp->getId())) ? 'Generating' 'Updating'),
  103.                     ClassUtils::getClass($grp),
  104.                     $grp->getName(),
  105.                     $grp->getOneRosterId(),
  106.                     $grp->getId() ?: '-'
  107.                 );
  108.             },
  109.             $groups
  110.         );
  111.         // let's save the groups
  112.         $this->em->saveAll($groups);
  113.         // DEBUGGING
  114.         $event->getOutput()->writeln($output);
  115.     }
  116. }