-
Notifications
You must be signed in to change notification settings - Fork 512
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
public function __construct( | ||
private readonly Security $security | ||
private readonly Security $security, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line indented incorrectly; expected 4 spaces, found 8 |
||
private readonly AccessUrlHelper $accessUrlHelper | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line indented incorrectly; expected 4 spaces, found 8 |
||
) {} | ||
|
||
public function applyToCollection( | ||
|
@@ -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()); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} | ||
|
||
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()) { | ||
/** @var User|null $user */ | ||
if (null === $user = $this->security->getUser()) { | ||
throw new AccessDeniedException('Access Denied.'); | ||
|
@@ -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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing class doc comment