From a9e12867d9226a506e053651a722128b97740e55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luiz=20Machado?= Date: Fri, 20 Oct 2023 16:30:44 +0200 Subject: [PATCH] [FIX][SOLAR-387] Add fallback when no registration available or FF TAO_AS_A_TOOL is disabled (#392) * fix: verify if feature is enabled and if platform config is available * chore: fix CS * chore(fix): typo on phpdoc * fix: removed wrong inspection * refactor: variable name * refactor: increased the log level to warning * chore: removed phpdoc --- controller/AuthoringTool.php | 68 +++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/controller/AuthoringTool.php b/controller/AuthoringTool.php index 98252a2e..f4cd62db 100644 --- a/controller/AuthoringTool.php +++ b/controller/AuthoringTool.php @@ -28,16 +28,22 @@ use helpers_Random; use InterruptedActionException; use OAT\Library\Lti1p3Core\Message\Payload\LtiMessagePayloadInterface; +use oat\tao\model\featureFlag\FeatureFlagChecker; +use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; use oat\taoLti\models\classes\LtiException; use oat\taoLti\models\classes\LtiMessages\LtiErrorMessage; use oat\taoLti\models\classes\LtiService; use oat\taoLti\models\classes\Tool\Validation\Lti1p3Validator; use oat\taoLti\models\classes\user\UserService; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use tao_actions_Main; use tao_models_classes_UserService; class AuthoringTool extends ToolModule { + private const LTI_NO_MATCHING_REGISTRATION_FOUND_MESSAGE = 'No matching registration found tool side'; + /** * @throws LtiException * @throws InterruptedActionException @@ -55,6 +61,9 @@ public function run(): void } } + /** + * @throws LtiException + */ protected function getValidatedLtiMessagePayload(): LtiMessagePayloadInterface { return $this->getServiceLocator() @@ -64,27 +73,76 @@ protected function getValidatedLtiMessagePayload(): LtiMessagePayloadInterface } /** - * @throws common_exception_Error * @throws ActionEnforcingException * @throws InterruptedActionException + * @throws LtiException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws common_exception_Error */ public function launch(): void { - $message = $this->getValidatedLtiMessagePayload(); + $ltiMessage = $this->getLtiMessageOrRedirectToLogin(); $user = $this->getServiceLocator() ->getContainer() ->get(tao_models_classes_UserService::class) ->addUser( - $message->getUserIdentity()->getIdentifier(), + $ltiMessage->getUserIdentity()->getIdentifier(), helpers_Random::generateString(UserService::PASSWORD_LENGTH), - new core_kernel_classes_Resource(current($message->getRoles())) + new core_kernel_classes_Resource(current($ltiMessage->getRoles())) ); $this->getServiceLocator() ->getContainer() ->get(LtiService::class) - ->startLti1p3Session($message, $user); + ->startLti1p3Session($ltiMessage, $user); $this->forward('run', null, null, $_GET); } + + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + private function isFeatureTaoAsToolEnabled(): bool + { + return $this->getServiceManager() + ->getContainer() + ->get(FeatureFlagChecker::class) + ->isEnabled(FeatureFlagCheckerInterface::FEATURE_FLAG_TAO_AS_A_TOOL); + } + + /** + * @throws ContainerExceptionInterface + * @throws InterruptedActionException + * @throws LtiException + * @throws NotFoundExceptionInterface + */ + private function getLtiMessageOrRedirectToLogin(): LtiMessagePayloadInterface + { + if (!$this->isFeatureTaoAsToolEnabled()) { + $this->getLogger()->info( + 'TAO as tool feature is disabled. The user will be redirected to the login page.' + ); + $this->redirect(_url('login', 'Main', 'tao')); + } + + try { + $message = $this->getValidatedLtiMessagePayload(); + } catch (LtiException $exception) { + if ($exception->getMessage() !== self::LTI_NO_MATCHING_REGISTRATION_FOUND_MESSAGE) { + throw $exception; + } + + $this->getLogger()->warning( + sprintf( + 'Missing registration for current audience. Redirecting to the login page. Exception: %s', + $exception + ) + ); + $this->redirect(_url('login', 'Main', 'tao')); + } + + return $message; + } }