Skip to content
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

Internal: Make a request to reset the current course #5485

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 1 addition & 5 deletions assets/vue/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ router.beforeResolve(async (to) => {
cid = parseInt(to.params?.id ?? 0)
}

if (cid) {
await cidReqStore.setCourseAndSessionById(cid, sid)
} else {
cidReqStore.resetCid()
}
await cidReqStore.setCourseAndSessionById(cid, sid)
})

export default router
9 changes: 9 additions & 0 deletions assets/vue/services/courseService.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import api from "../config/api"
import baseService from "./baseService"

/**
* @returns {Promise<void>}
*/
async function cidReset() {
await api.get("/course/cid_reset")
}

export default {
find: baseService.get,

cidReset,

/**
* @param {number} courseId
* @param {number=} sessionId
Expand Down
14 changes: 12 additions & 2 deletions assets/vue/store/cidReq.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export const useCidReqStore = defineStore("cidReq", () => {
}
})

const resetCid = () => {
const resetCid = async () => {
if (course.value) {
await courseService.cidReset()
}

course.value = null
session.value = null
group.value = null
Expand Down Expand Up @@ -92,9 +96,15 @@ export const useCidReqStore = defineStore("cidReq", () => {
}
}

/**
* @param {number|null} cId
* @param {number|null} sId
* @returns {Promise<Awaited<void>[]>|Promise<void>}
*/
const setCourseAndSessionById = (cId, sId = undefined, useBasic = true) => {

if (!cId) {
return Promise.resolve()
return resetCid()
}

const coursePromise = setCourseByIri(cId, sId)
Expand Down
15 changes: 15 additions & 0 deletions src/CoreBundle/Controller/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Chamilo\CoreBundle\Entity\Tag;
use Chamilo\CoreBundle\Entity\Tool;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\EventListener\CidReqListener;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Repository\CourseCategoryRepository;
use Chamilo\CoreBundle\Repository\ExtraFieldValuesRepository;
Expand Down Expand Up @@ -52,6 +53,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -69,6 +71,19 @@
private readonly UserHelper $userHelper,
) {}

#[Route('/cid_reset', methods: ['GET'])]

Check warning on line 74 in src/CoreBundle/Controller/CourseController.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/Controller/CourseController.php#L74

Added line #L74 was not covered by tests
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
public function cidReset(
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
Request $request,
TokenStorageInterface $tokenStorage,
): Response {
CidReqListener::cleanSessionHandler(
$request,
$tokenStorage->getToken()

Check warning on line 81 in src/CoreBundle/Controller/CourseController.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/Controller/CourseController.php#L79-L81

Added lines #L79 - L81 were not covered by tests
);

return new Response('', Response::HTTP_NO_CONTENT);

Check warning on line 84 in src/CoreBundle/Controller/CourseController.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/Controller/CourseController.php#L84

Added line #L84 was not covered by tests
}

#[Route('/{cid}/checkLegal.json', name: 'chamilo_core_course_check_legal_json')]
public function checkTermsAndConditionJson(
Request $request,
Expand Down
13 changes: 6 additions & 7 deletions src/CoreBundle/EventListener/CidReqListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -68,7 +69,10 @@
}

if (true === $cidReset) {
$this->cleanSessionHandler($request);
self::cleanSessionHandler(
$request,
$this->tokenStorage->getToken()

Check warning on line 74 in src/CoreBundle/EventListener/CidReqListener.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/EventListener/CidReqListener.php#L72-L74

Added lines #L72 - L74 were not covered by tests
);

return;
}
Expand Down Expand Up @@ -193,8 +197,6 @@
$courseParams = $this->generateCourseUrl($course, $sessionId, $groupId, $origin);
$sessionHandler->set('course_url_params', $courseParams);
$twig->addGlobal('course_url_params', $courseParams);
} else {
$this->cleanSessionHandler($request);
}
}

Expand Down Expand Up @@ -259,7 +261,7 @@
}
}

public function cleanSessionHandler(Request $request): void
public static function cleanSessionHandler(Request $request, ?TokenInterface $token): void

Check warning on line 264 in src/CoreBundle/EventListener/CidReqListener.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/EventListener/CidReqListener.php#L264

Added line #L264 was not covered by tests
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
{
$sessionHandler = $request->getSession();
$alreadyVisited = $sessionHandler->get('course_already_visited');
Expand All @@ -273,7 +275,7 @@
$sessionId = $sessionHandler->get('sid', 0);
$ip = $request->getClientIp();
if (0 !== $courseId) {
$token = $this->tokenStorage->getToken();

Check failure on line 278 in src/CoreBundle/EventListener/CidReqListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 Test on ubuntu-latest

InvalidScope

src/CoreBundle/EventListener/CidReqListener.php:278:22: InvalidScope: Invalid reference to $this in a static context (see https://psalm.dev/013)
if (null !== $token) {
/** @var User $user */
$user = $token->getUser();
Expand Down Expand Up @@ -312,7 +314,6 @@
ChamiloSession::erase('origin');

// Remove user temp roles
$token = $this->tokenStorage->getToken();
if (null !== $token) {
/** @var User $user */
$user = $token->getUser();
Expand All @@ -325,8 +326,6 @@
$user->removeRole('ROLE_CURRENT_COURSE_SESSION_TEACHER');
}
}

// $request->setLocale($request->getPreferredLanguage());
}

private function generateCourseUrl(?Course $course, int $sessionId, int $groupId, ?string $origin): string
Expand Down
Loading