From dfa886aaf15255083840422bbebf5103b54f9ea0 Mon Sep 17 00:00:00 2001 From: Elorfin Date: Wed, 1 Mar 2023 08:24:01 +0100 Subject: [PATCH] [Training] fixes organization filter for data source --- .../Listener/DataSource/AllCoursesSource.php | 34 ++++++--------- .../Listener/DataSource/EventsSource.php | 37 ++++++---------- .../DataSource/PublicSessionSource.php | 43 ++++++------------- .../Listener/DataSource/SessionSource.php | 34 ++++++--------- .../Resources/modules/data/sources/courses.js | 8 ++++ .../Resources/modules/data/sources/events.js | 8 ++++ .../modules/data/sources/sessions.js | 8 ++++ 7 files changed, 80 insertions(+), 92 deletions(-) diff --git a/src/plugin/cursus/Listener/DataSource/AllCoursesSource.php b/src/plugin/cursus/Listener/DataSource/AllCoursesSource.php index 80702ef385d..4008cd13c59 100644 --- a/src/plugin/cursus/Listener/DataSource/AllCoursesSource.php +++ b/src/plugin/cursus/Listener/DataSource/AllCoursesSource.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace Claroline\CursusBundle\Listener\DataSource; use Claroline\AppBundle\API\FinderProvider; @@ -51,17 +42,8 @@ public function getData(GetDataEvent $event) $options = $event->getOptions(); $options['hiddenFilters']['hidden'] = false; - if (!$this->authorization->isGranted('ROLE_ADMIN')) { - $user = $this->tokenStorage->getToken()->getUser(); - if ($user instanceof User) { - $organizations = $user->getOrganizations(); - } else { - $organizations = $this->om->getRepository(Organization::class)->findBy(['default' => true]); - } - - $options['hiddenFilters']['organizations'] = array_map(function (Organization $organization) { - return $organization->getUuid(); - }, $organizations); + if (!$this->authorization->isGranted('ROLE_ADMIN') && (empty($options['filters']) || empty($options['filters']['organizations']))) { + $options['hiddenFilters']['organizations'] = $this->getOrganizations(); } $event->setData( @@ -70,4 +52,16 @@ public function getData(GetDataEvent $event) $event->stopPropagation(); } + + private function getOrganizations(): array + { + $user = $this->tokenStorage->getToken()->getUser(); + if ($user instanceof User) { + return array_map(function (Organization $organization) { + return $organization->getUuid(); + }, $user->getOrganizations()); + } + + return []; + } } diff --git a/src/plugin/cursus/Listener/DataSource/EventsSource.php b/src/plugin/cursus/Listener/DataSource/EventsSource.php index 1c825180055..af84f5c5aea 100644 --- a/src/plugin/cursus/Listener/DataSource/EventsSource.php +++ b/src/plugin/cursus/Listener/DataSource/EventsSource.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace Claroline\CursusBundle\Listener\DataSource; use Claroline\AppBundle\API\FinderProvider; @@ -59,20 +50,8 @@ public function getData(GetDataEvent $event) $options['hiddenFilters']['session'] = array_map(function (Session $session) { return $session->getUuid(); }, $this->sessionRepo->findByWorkspace($event->getWorkspace())); - } else { - // filter by organization for desktop - if (!$this->authorization->isGranted('ROLE_ADMIN')) { - $user = $this->tokenStorage->getToken()->getUser(); - if ($user instanceof User) { - $organizations = $user->getOrganizations(); - } else { - $organizations = $this->om->getRepository(Organization::class)->findBy(['default' => true]); - } - - $options['hiddenFilters']['organizations'] = array_map(function (Organization $organization) { - return $organization->getUuid(); - }, $organizations); - } + } elseif (!$this->authorization->isGranted('ROLE_ADMIN') && (empty($options['filters']) || empty($options['filters']['organizations']))) { + $options['hiddenFilters']['organizations'] = $this->getOrganizations(); } $event->setData( @@ -81,4 +60,16 @@ public function getData(GetDataEvent $event) $event->stopPropagation(); } + + private function getOrganizations(): array + { + $user = $this->tokenStorage->getToken()->getUser(); + if ($user instanceof User) { + return array_map(function (Organization $organization) { + return $organization->getUuid(); + }, $user->getOrganizations()); + } + + return []; + } } diff --git a/src/plugin/cursus/Listener/DataSource/PublicSessionSource.php b/src/plugin/cursus/Listener/DataSource/PublicSessionSource.php index 49fbddb51ca..afce816803a 100644 --- a/src/plugin/cursus/Listener/DataSource/PublicSessionSource.php +++ b/src/plugin/cursus/Listener/DataSource/PublicSessionSource.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace Claroline\CursusBundle\Listener\DataSource; use Claroline\AppBundle\API\FinderProvider; @@ -53,26 +44,8 @@ public function getData(GetDataEvent $event) if (DataSource::CONTEXT_WORKSPACE === $event->getContext() && (empty($options['filters'] || empty($options['filters']['workspace'])))) { $options['hiddenFilters']['workspace'] = $event->getWorkspace()->getUuid(); - } elseif (DataSource::CONTEXT_HOME === $event->getContext()) { - // only display sessions of the default organization on home - $organizations = $this->om->getRepository(Organization::class)->findBy(['default' => true]); - $options['hiddenFilters']['organizations'] = array_map(function (Organization $organization) { - return $organization->getUuid(); - }, $organizations); - } else { - // filter by organization for desktop - if (!$this->authorization->isGranted('ROLE_ADMIN')) { - $user = $this->tokenStorage->getToken()->getUser(); - if ($user instanceof User) { - $organizations = $user->getOrganizations(); - } else { - $organizations = $this->om->getRepository(Organization::class)->findBy(['default' => true]); - } - - $options['hiddenFilters']['organizations'] = array_map(function (Organization $organization) { - return $organization->getUuid(); - }, $organizations); - } + } elseif (!$this->authorization->isGranted('ROLE_ADMIN') && (empty($options['filters']) || empty($options['filters']['organizations']))) { + $options['hiddenFilters']['organizations'] = $this->getOrganizations(); } $event->setData( @@ -81,4 +54,16 @@ public function getData(GetDataEvent $event) $event->stopPropagation(); } + + private function getOrganizations(): array + { + $user = $this->tokenStorage->getToken()->getUser(); + if ($user instanceof User) { + return array_map(function (Organization $organization) { + return $organization->getUuid(); + }, $user->getOrganizations()); + } + + return []; + } } diff --git a/src/plugin/cursus/Listener/DataSource/SessionSource.php b/src/plugin/cursus/Listener/DataSource/SessionSource.php index 0d13aa54663..f43778fae3f 100644 --- a/src/plugin/cursus/Listener/DataSource/SessionSource.php +++ b/src/plugin/cursus/Listener/DataSource/SessionSource.php @@ -54,26 +54,8 @@ public function getData(GetDataEvent $event) if (DataSource::CONTEXT_WORKSPACE === $event->getContext() && (empty($options['filters'] || empty($options['filters']['workspace'])))) { // we allow users to display sessions from another workspace (this is a little weird) $options['hiddenFilters']['workspace'] = $event->getWorkspace()->getUuid(); - } elseif (DataSource::CONTEXT_HOME === $event->getContext()) { - // only display sessions of the default organization on home - $organizations = $this->om->getRepository(Organization::class)->findBy(['default' => true]); - $options['hiddenFilters']['organizations'] = array_map(function (Organization $organization) { - return $organization->getUuid(); - }, $organizations); - } else { - // filter by organization for desktop - if (!$this->authorization->isGranted('ROLE_ADMIN')) { - $user = $this->tokenStorage->getToken()->getUser(); - if ($user instanceof User) { - $organizations = $user->getOrganizations(); - } else { - $organizations = $this->om->getRepository(Organization::class)->findBy(['default' => true]); - } - - $options['hiddenFilters']['organizations'] = array_map(function (Organization $organization) { - return $organization->getUuid(); - }, $organizations); - } + } elseif (!$this->authorization->isGranted('ROLE_ADMIN') && (empty($options['filters']) || empty($options['filters']['organizations']))) { + $options['hiddenFilters']['organizations'] = $this->getOrganizations(); } $event->setData( @@ -82,4 +64,16 @@ public function getData(GetDataEvent $event) $event->stopPropagation(); } + + private function getOrganizations(): array + { + $user = $this->tokenStorage->getToken()->getUser(); + if ($user instanceof User) { + return array_map(function (Organization $organization) { + return $organization->getUuid(); + }, $user->getOrganizations()); + } + + return []; + } } diff --git a/src/plugin/cursus/Resources/modules/data/sources/courses.js b/src/plugin/cursus/Resources/modules/data/sources/courses.js index d0794b7c38a..2193545e71f 100644 --- a/src/plugin/cursus/Resources/modules/data/sources/courses.js +++ b/src/plugin/cursus/Resources/modules/data/sources/courses.js @@ -67,6 +67,14 @@ export default { label: trans('order'), displayable: false, filterable: false + }, { + name: 'organizations', + type: 'organizations', + label: trans('organizations'), + displayable: false, + displayed: false, + sortable: false, + filterable: true } ], card: CourseCard diff --git a/src/plugin/cursus/Resources/modules/data/sources/events.js b/src/plugin/cursus/Resources/modules/data/sources/events.js index a7d30793589..f51fc61e462 100644 --- a/src/plugin/cursus/Resources/modules/data/sources/events.js +++ b/src/plugin/cursus/Resources/modules/data/sources/events.js @@ -67,6 +67,14 @@ export default { label: trans('workspace'), displayable: false, sortable: false + }, { + name: 'organizations', + type: 'organizations', + label: trans('organizations'), + displayable: false, + displayed: false, + sortable: false, + filterable: true } ], card: EventCard diff --git a/src/plugin/cursus/Resources/modules/data/sources/sessions.js b/src/plugin/cursus/Resources/modules/data/sources/sessions.js index da5e71bc397..e1c4ce2c53a 100644 --- a/src/plugin/cursus/Resources/modules/data/sources/sessions.js +++ b/src/plugin/cursus/Resources/modules/data/sources/sessions.js @@ -87,6 +87,14 @@ export default { options: { objectClass: 'Claroline\\CursusBundle\\Entity\\Course' } + }, { + name: 'organizations', + type: 'organizations', + label: trans('organizations'), + displayable: false, + displayed: false, + sortable: false, + filterable: true } ], card: SessionCard