Skip to content

Commit

Permalink
[Training] fixes organization filter for data source
Browse files Browse the repository at this point in the history
  • Loading branch information
Elorfin committed Mar 1, 2023
1 parent 74e411f commit dfa886a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 92 deletions.
34 changes: 14 additions & 20 deletions src/plugin/cursus/Listener/DataSource/AllCoursesSource.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* 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;
Expand Down Expand Up @@ -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(
Expand All @@ -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 [];
}
}
37 changes: 14 additions & 23 deletions src/plugin/cursus/Listener/DataSource/EventsSource.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* 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;
Expand Down Expand Up @@ -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(
Expand All @@ -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 [];
}
}
43 changes: 14 additions & 29 deletions src/plugin/cursus/Listener/DataSource/PublicSessionSource.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* 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;
Expand Down Expand Up @@ -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(
Expand All @@ -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 [];
}
}
34 changes: 14 additions & 20 deletions src/plugin/cursus/Listener/DataSource/SessionSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 [];
}
}
8 changes: 8 additions & 0 deletions src/plugin/cursus/Resources/modules/data/sources/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/plugin/cursus/Resources/modules/data/sources/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/plugin/cursus/Resources/modules/data/sources/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dfa886a

Please sign in to comment.