Skip to content

Course: Filter courses by access URL if multiple URLs enabled #5694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion public/main/admin/course_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
$visibility = $course['visibility'];

if (isset($course['duration'])) {
$course['duration'] = $course['duration'] * 60;
$course['duration'] = (int) $course['duration'] * 60;
}

// @todo should be check in the CidReqListener
Expand Down
40 changes: 28 additions & 12 deletions src/CoreBundle/DataProvider/Extension/CourseRelUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

// use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;

final class CourseRelUserExtension implements QueryCollectionExtensionInterface // , QueryItemExtensionInterface
final class CourseRelUserExtension implements QueryCollectionExtensionInterface

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing class doc comment

{
public function __construct(
private readonly Security $security
private readonly Security $security,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line indented incorrectly; expected 4 spaces, found 8

private readonly AccessUrlHelper $accessUrlHelper

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line indented incorrectly; expected 4 spaces, found 8

) {}

public function applyToCollection(
Expand All @@ -30,13 +31,34 @@
?Operation $operation = null,
array $context = []
): void {
if ($this->accessUrlHelper->isMultiple()) {
$accessUrl = $this->accessUrlHelper->getCurrent();
$rootAlias = $queryBuilder->getRootAliases()[0];
if (isset($context['filters']['sticky']) && $context['filters']['sticky']) {
$queryBuilder
->innerJoin(
AccessUrlRelCourse::class,
'url_rel',
'WITH',
'url_rel.course = ' . $rootAlias
)
->andWhere('url_rel.url = :access_url_id')
->setParameter('access_url_id', $accessUrl->getId());

Check warning on line 46 in src/CoreBundle/DataProvider/Extension/CourseRelUserExtension.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/DataProvider/Extension/CourseRelUserExtension.php#L35-L46

Added lines #L35 - L46 were not covered by tests
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method applyToCollection uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.

$queryBuilder
->innerJoin("$rootAlias.course", 'c')
->innerJoin('c.urls', 'url_rel')
->andWhere('url_rel.url = :access_url_id')
->setParameter('access_url_id', $accessUrl->getId());

Check warning on line 52 in src/CoreBundle/DataProvider/Extension/CourseRelUserExtension.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/DataProvider/Extension/CourseRelUserExtension.php#L48-L52

Added lines #L48 - L52 were not covered by tests
}
}

if ($this->security->isGranted('ROLE_ADMIN')) {
return;
}

if (CourseRelUser::class === $resourceClass) {
// Blocks a ROLE_USER to access CourseRelUsers from another User.
if ('collection_query' === $operation->getName()) {
if ('collection_query' === $operation?->getName()) {

Check warning on line 61 in src/CoreBundle/DataProvider/Extension/CourseRelUserExtension.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/DataProvider/Extension/CourseRelUserExtension.php#L61

Added line #L61 was not covered by tests
/** @var User|null $user */
if (null === $user = $this->security->getUser()) {
throw new AccessDeniedException('Access Denied.');
Expand All @@ -51,12 +73,6 @@
$this->addWhere($queryBuilder, $resourceClass);
}

/*public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void
{
error_log('applyToItem');
$this->addWhere($queryBuilder, $resourceClass);
}*/

private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void
{
if (CourseRelUser::class !== $resourceClass) {
Expand Down
Loading