/** * PRE_SET_DATA event handler * * @param FormEvent $event */ public function preSetData(FormEvent $event) { $form = $event->getForm(); if ($this->calendarConfig->isPublicCalendarEnabled() && $this->calendarConfig->isSystemCalendarEnabled()) { $options = ['required' => false, 'label' => 'oro.calendar.systemcalendar.public.label', 'empty_value' => false, 'choices' => [false => 'oro.calendar.systemcalendar.scope.organization', true => 'oro.calendar.systemcalendar.scope.system']]; /** @var SystemCalendar|null $data */ $data = $event->getData(); if ($data) { $isPublicGranted = $this->securityFacade->isGranted('oro_public_calendar_management'); $isSystemGranted = $this->securityFacade->isGranted($data->getId() ? 'oro_system_calendar_update' : 'oro_system_calendar_create'); if (!$isPublicGranted || !$isSystemGranted) { $options['read_only'] = true; if (!$data->getId() && !$isSystemGranted) { $options['data'] = true; } unset($options['choices'][$isSystemGranted]); } } $form->add('public', 'choice', $options); } elseif ($this->calendarConfig->isPublicCalendarEnabled()) { $form->add('public', 'hidden', ['data' => true]); } elseif ($this->calendarConfig->isSystemCalendarEnabled()) { $form->add('public', 'hidden', ['data' => false]); } }
/** * {@inheritdoc} */ public function getCalendarEvents($organizationId, $userId, $calendarId, $start, $end, $connections, $extraFields = []) { if (!$this->calendarConfig->isSystemCalendarEnabled() || !$this->securityFacade->isGranted('oro_system_calendar_view')) { return []; } //@TODO: temporary return all system calendars until BAP-6566 implemented ///** @var CalendarEventRepository $repo */ //$repo = $this->doctrineHelper->getEntityRepository('OroCalendarBundle:CalendarEvent'); //$qb = $repo->getSystemEventListByTimeIntervalQueryBuilder( // $calendarId, // $start, // $end, // [] //); $extraFields = $this->filterSupportedFields($extraFields, 'Oro\\Bundle\\CalendarBundle\\Entity\\CalendarEvent'); /** @var CalendarEventRepository $repo */ $repo = $this->doctrineHelper->getEntityRepository('OroCalendarBundle:CalendarEvent'); $qb = $repo->getSystemEventListByTimeIntervalQueryBuilder($start, $end, [], $extraFields)->andWhere('c.organization = :organizationId')->setParameter('organizationId', $organizationId); $invisibleIds = []; foreach ($connections as $id => $visible) { if (!$visible) { $invisibleIds[] = $id; } } if ($invisibleIds) { $qb->andWhere('c.id NOT IN (:invisibleIds)')->setParameter('invisibleIds', $invisibleIds); } return $this->calendarEventNormalizer->getCalendarEvents($calendarId, $qb->getQuery()); }
/** * {@inheritdoc} */ protected function checkPermissions($entity, ObjectManager $em) { if ($entity->isPublic()) { if (!$this->calendarConfig->isPublicCalendarEnabled()) { throw new ForbiddenException('Public calendars are disabled.'); } elseif (!$this->securityFacade->isGranted('oro_public_calendar_management')) { throw new ForbiddenException('Access denied.'); } } else { if (!$this->calendarConfig->isSystemCalendarEnabled()) { throw new ForbiddenException('System calendars are disabled.'); } elseif (!$this->securityFacade->isGranted('DELETE', $entity)) { throw new ForbiddenException('Access denied.'); } } }
/** * @param BuildAfter $event */ public function onBuildAfter(BuildAfter $event) { $datagrid = $event->getDatagrid(); $datasource = $datagrid->getDatasource(); if ($datasource instanceof OrmDatasource) { $isPublicGranted = $this->calendarConfig->isPublicCalendarEnabled(); $isSystemGranted = $this->calendarConfig->isSystemCalendarEnabled() && $this->securityFacade->isGranted('oro_system_calendar_view'); if ($isPublicGranted && $isSystemGranted) { $datasource->getQueryBuilder()->andWhere('(sc.public = :public OR sc.organization = :organizationId)')->setParameter('public', true)->setParameter('organizationId', $this->securityFacade->getOrganizationId()); } elseif ($isPublicGranted) { $datasource->getQueryBuilder()->andWhere('sc.public = :public')->setParameter('public', true); } elseif ($isSystemGranted) { $datasource->getQueryBuilder()->andWhere('sc.organization = :organizationId')->setParameter('organizationId', $this->securityFacade->getOrganizationId()); } else { // it is denied to view both public and system calendars $datasource->getQueryBuilder()->andWhere('1 = 0'); } } }
/** * {@inheritdoc} */ public function getCalendarEvents($organizationId, $userId, $calendarId, $start, $end, $connections, $extraFields = []) { if (!$this->calendarConfig->isPublicCalendarEnabled()) { return []; } /** @var CalendarEventRepository $repo */ $repo = $this->doctrineHelper->getEntityRepository('OroCalendarBundle:CalendarEvent'); $qb = $repo->getPublicEventListByTimeIntervalQueryBuilder($start, $end, [], $extraFields); $invisibleIds = []; foreach ($connections as $id => $visible) { if (!$visible) { $invisibleIds[] = $id; } } if (!empty($invisibleIds)) { $qb->andWhere('c.id NOT IN (:invisibleIds)')->setParameter('invisibleIds', $invisibleIds); } return $this->calendarEventNormalizer->getCalendarEvents($calendarId, $qb->getQuery()); }
/** * {@inheritdoc} */ protected function checkPermissions($entity, ObjectManager $em) { /** @var SystemCalendar|null $calendar */ $calendar = $entity->getSystemCalendar(); if ($calendar) { if ($calendar->isPublic()) { if (!$this->calendarConfig->isPublicCalendarEnabled()) { throw new ForbiddenException('Public calendars are disabled.'); } elseif (!$this->securityFacade->isGranted('oro_public_calendar_event_management')) { throw new ForbiddenException('Access denied.'); } } else { if (!$this->calendarConfig->isSystemCalendarEnabled()) { throw new ForbiddenException('System calendars are disabled.'); } elseif (!$this->securityFacade->isGranted('oro_system_calendar_event_management')) { throw new ForbiddenException('Access denied.'); } } } else { parent::checkPermissions($entity, $em); } }
/** * Links an event with a calendar by its alias and id * * @param CalendarEvent $event * @param string $calendarAlias * @param int $calendarId * * @throws \LogicException * @throws ForbiddenException */ public function setCalendar(CalendarEvent $event, $calendarAlias, $calendarId) { if ($calendarAlias === Calendar::CALENDAR_ALIAS) { $calendar = $event->getCalendar(); if (!$calendar || $calendar->getId() !== $calendarId) { $event->setCalendar($this->findCalendar($calendarId)); } } elseif (in_array($calendarAlias, [SystemCalendar::CALENDAR_ALIAS, SystemCalendar::PUBLIC_CALENDAR_ALIAS])) { $systemCalendar = $this->findSystemCalendar($calendarId); //@TODO: Added permission verification if ($systemCalendar->isPublic() && !$this->calendarConfig->isPublicCalendarEnabled()) { throw new ForbiddenException('Public calendars are disabled.'); } if (!$systemCalendar->isPublic() && !$this->calendarConfig->isSystemCalendarEnabled()) { throw new ForbiddenException('System calendars are disabled.'); } $event->setSystemCalendar($systemCalendar); } else { throw new \LogicException(sprintf('Unexpected calendar alias: "%s". CalendarId: %d.', $calendarAlias, $calendarId)); } }
/** * @param ConfigureMenuEvent $event */ public function onNavigationConfigure(ConfigureMenuEvent $event) { if (!$this->calendarConfig->isPublicCalendarEnabled() && !$this->calendarConfig->isSystemCalendarEnabled()) { $event->getMenu()->getChild('system_tab')->getChild('oro_system_calendar_list')->setDisplay(false); } }
/** * @dataProvider configProvider */ public function testConfig($enabledSystemCalendar, $expectedIsPublicCalendarEnabled, $expectedIsSystemCalendarEnabled) { $config = new SystemCalendarConfig($enabledSystemCalendar); $this->assertSame($expectedIsPublicCalendarEnabled, $config->isPublicCalendarEnabled()); $this->assertSame($expectedIsSystemCalendarEnabled, $config->isSystemCalendarEnabled()); }