Skip to content

Commit

Permalink
Merge pull request #8962 from surlabs/ilias10_LTI_provider13
Browse files Browse the repository at this point in the history
LTI Provider Functionality Fixes and Adaptations
  • Loading branch information
ZallaxDev authored Feb 4, 2025
2 parents 578abef + 9f3b025 commit 4b79db7
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -18,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

/**
* Auth credentials for lti oauth based authentication
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -18,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

/**
* OAuth based lti authentication
* @author Stefan Meyer <[email protected]>
Expand Down Expand Up @@ -264,7 +264,13 @@ public function doAuthentication(\ilAuthStatus $status): bool
return false;
}

$this->ref_id = $this->provider->platform->getRefId();
$platformId = $this->messageParameters['platform_id'];
$clientId = $this->provider->platform->clientId;
$deploymentId = $this->messageParameters['deployment_id'];

$platform = ilLTIPlatform::fromPlatformId($platformId, $clientId, $deploymentId, $this->dataConnector);

$this->ref_id = $platform->getRefId();
// stores ref_ids of all lti consumer within active LTI User Session
$lti_context_ids = ilSession::get('lti_context_ids');
// if session object exists only add ref_id if not already exists
Expand Down Expand Up @@ -298,41 +304,44 @@ public function doAuthentication(\ilAuthStatus $status): bool
ilSession::set('lti_init_target', ilObject::_lookupType($this->ref_id, true) . '_' . $this->ref_id);

// lti service activation
if (!$this->provider->platform->enabled) {
if (!$platform->enabled) {
$this->getLogger()->warning('Consumer is not enabled');
$status->setReason('lti_consumer_inactive');
$status->setStatus(ilAuthStatus::STATUS_AUTHENTICATION_FAILED);
return false;
}
// global activation status
if (!$this->provider->platform->getActive()) {
if (!$platform->getActive()) {
$this->getLogger()->warning('Consumer is not active');
$status->setReason('lti_consumer_inactive');
$status->setStatus(ilAuthStatus::STATUS_AUTHENTICATION_FAILED);
return false;
}
$lti_id = $this->provider->platform->getExtConsumerId();
$lti_id = $platform->getExtConsumerId();
if (!$lti_id) {
$status->setReason('lti_auth_failed_invalid_key');
$status->setStatus(ilAuthStatus::STATUS_AUTHENTICATION_FAILED);
return false;
}

$this->getLogger()->debug('Using prefix:' . $this->provider->platform->getPrefix());
$this->getLogger()->debug('Using prefix:' . $platform->getPrefix());

// Use user_id as username instead of user email to avoid problems with uniqueness of the lti user.
$this->getCredentials()->setUsername($this->messageParameters['user_id']);

$internal_account = $this->findUserId(
$this->getCredentials()->getUsername(),
(string) $lti_id,
$this->provider->platform->getPrefix()
$platform->getPrefix()
);

if ($internal_account) {
$this->updateUser($internal_account, $this->provider->platform);
$this->updateUser($internal_account, $platform);
} else {
$internal_account = $this->createUser($this->provider->platform);
$internal_account = $this->createUser($platform);
}

$this->handleLocalRoleAssignments($internal_account, $this->provider->platform);
$this->handleLocalRoleAssignments($internal_account, $platform);

$status->setStatus(ilAuthStatus::STATUS_AUTHENTICATED);
$status->setAuthenticatedUserId($internal_account);
Expand Down Expand Up @@ -427,12 +436,12 @@ protected function createUser(ilLTIPlatform $consumer): int
$local_user = ilAuthUtils::_generateLogin($consumer->getPrefix() . '_' . $this->getCredentials()->getUsername());

$newUser["login"] = $local_user;
if(isset($this->messageParameters['lis_person_name_given'])) {
if (isset($this->messageParameters['lis_person_name_given'])) {
$newUser["firstname"] = $this->messageParameters['lis_person_name_given'];
} else {
$newUser["firstname"] = '-';
}
if(isset($this->messageParameters['lis_person_name_family'])) {
if (isset($this->messageParameters['lis_person_name_family'])) {
$newUser["lastname"] = $this->messageParameters['lis_person_name_family'];
} else {
$newUser["lastname"] = '-';
Expand Down Expand Up @@ -552,6 +561,11 @@ protected function handleLocalRoleAssignments(int $user_id, ilLTIPlatform $consu
$role_arr = explode(',', $roles);
foreach ($role_arr as $role_name) {
$role_name = trim($role_name);
$role_name = str_replace('http://purl.imsglobal.org/vocab/lis/v2/membership#', '', $role_name);
$role_name = str_replace('http://purl.imsglobal.org/vocab/lis/v2/person#', '', $role_name);
$role_name = str_replace('http://purl.imsglobal.org/vocab/lis/v2/institution/person#', '', $role_name);
$role_name = str_replace('http://purl.imsglobal.org/vocab/lis/v2/system/person#', '', $role_name);

switch ($role_name) {
case 'Administrator':
$this->getLogger()->info('Administrator role handling');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -18,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

/**
* Stores
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ protected function initObjectSettingsForm(): \ilPropertyFormGUI
protected function updateSettings(): void
{
$form = $this->initObjectSettingsForm();
$form->setValuesByPost();
if (!$form->checkInput()) {
$form->setValuesByPost();
$this->settings($form);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ class ilLTITool extends Tool
*/
public function __construct(ilLTIDataConnector $dataConnector)
{
global $DIC;
$this->logger = ilLoggerFactory::getLogger('ltis');
// $this->initialize();
if (empty($dataConnector)) {
$dataConnector = ilLTIDataConnector::getDataConnector();
}
$this->dataConnector = $dataConnector;
// parent::__construct($dataConnector);
//parent::__construct($dataConnector);
$this->setParameterConstraint('resource_link_id', true, 50, array('basic-lti-launch-request'));
$this->setParameterConstraint('user_id', true, 64, array('basic-lti-launch-request'));
$this->setParameterConstraint('roles', true, null, array('basic-lti-launch-request'));
Expand Down Expand Up @@ -76,4 +75,13 @@ protected function onLaunch(): void
$this->resourceLink->save();
}
}

public function handleRequest(bool $strictMode = null, bool $disableCookieCheck = false, bool $generateWarnings = false): void
{
global $DIC;

$_POST = $DIC->http()->request()->getParsedBody();
$_GET = $DIC->http()->request()->getQueryParams();
parent::handleRequest($strictMode, $disableCookieCheck, $generateWarnings);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -18,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

class ilLTIDatabaseUpdateSteps implements ilDatabaseUpdateSteps
{
protected ilDBInterface $db;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

declare(strict_types=1);
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -17,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

namespace LTI;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -18,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

use ILIAS\Cron\Schedule\CronJobScheduleType;

/**
Expand Down
16 changes: 11 additions & 5 deletions components/ILIAS/LTIProvider/classes/class.ilLTIDataConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public function __construct()
* @param Platform $platform Platform object
* @return boolean True if the tool consumer object was successfully loaded
*/
public function loadPlatform(Platform $platform): bool
public function loadPlatform(Platform | ilLTIPlatform $platform): bool
{

$ok = false;
$allowMultiple = false;
$id = $platform->getRecordId();
Expand Down Expand Up @@ -146,8 +147,12 @@ public function loadPlatform(Platform $platform): bool
$platform->created = strtotime($row->created);
$platform->updated = strtotime($row->updated);
//ILIAS specific
$platform->setExtConsumerId(intval($row->ext_consumer_id));
$platform->setRefId((int) $row->ref_id);
// Fix for ILIAS: Change method param type to union type Platform | ilLTIPlatform
// Then check if $platform is ilLTIPlatform
if ($platform instanceof ilLTIPlatform) {
$platform->setExtConsumerId(intval($row->ext_consumer_id));
$platform->setRefId((int) $row->ref_id);
}
// if ($platform->setTitle) $platform->setTitle($row->title);
// if ($platform->setDescription) $platform->setDescription($row->description);
// if ($platform->setPrefix) $platform->setPrefix($row->prefix);
Expand All @@ -158,6 +163,7 @@ public function loadPlatform(Platform $platform): bool
$this->fixPlatformSettings($platform, false);
$ok = true;
}

return $ok;
}
#######
Expand Down Expand Up @@ -1573,7 +1579,7 @@ public function loadUserResult(User $userresult): bool
'WHERE user_pk = ' . $ilDB->quote($id, 'integer');
} else {
$rid = $userresult->getResourceLink()->getRecordId();
$uid = $userresult->getId(ToolProvider\Tool::ID_SCOPE_ID_ONLY);
$uid = $userresult->getId(IdScope::IdOnly);

$query = 'SELECT user_pk, resource_link_pk, lti_user_id, lti_result_sourcedid, created, updated ' .
'FROM ' . $this->dbTableNamePrefix . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
Expand Down Expand Up @@ -1619,7 +1625,7 @@ public function saveUserResult(User $userresult): bool
$userresult->setRecordId($ilDB->nextId($this->dbTableNamePrefix . DataConnector::USER_RESULT_TABLE_NAME));
$userresult->created = $time;
$rid = $userresult->getResourceLink()->getRecordId();
$uid = $userresult->getId(ToolProvider\Tool::ID_SCOPE_ID_ONLY);
$uid = $userresult->getId(IdScope::IdOnly);
$query = 'INSERT INTO ' . $this->dbTableNamePrefix . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
'(user_pk,resource_link_pk,lti_user_id, lti_result_sourcedid, created, updated) ' .
'VALUES( ' .
Expand Down
4 changes: 2 additions & 2 deletions components/ILIAS/LTIProvider/classes/class.ilLTIRouterGUI.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -18,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

/**
* Service ilViewRouterGUI
* This service is used by LTI. It allows any plugin to get called by a http request without dependencies to a
Expand Down
21 changes: 18 additions & 3 deletions components/ILIAS/LTIProvider/classes/class.ilLTIViewGUI.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -18,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

use ILIAS\LTI\Screen\LtiViewLayoutProvider;

/**
Expand Down Expand Up @@ -136,15 +136,30 @@ public function initGUI(): void
{
$this->log->debug("initGUI");
$baseclass = '';
$cmdclass = '';
if ($this->wrapper->query()->has('baseClass')) {
$baseclass = strtolower($this->wrapper->query()->retrieve('baseClass', $this->kindlyTo->string()));
}
if ($this->wrapper->query()->has('cmdClass')) {
$cmdclass = strtolower($this->wrapper->query()->retrieve('cmdClass', $this->kindlyTo->string()));
}

if ($baseclass == 'illtiroutergui') {
return;
}

$target = ilSession::get('lti_init_target');
if ($target) {
list($type, $ref_id) = explode('_', $target);

ilSession::clear('lti_init_target');

ilUtil::redirect(
"ilias.php?baseClass=ilRepositoryGUI&ref_id=" . $ref_id
. "&cmd=view"
. "&cmdClass=ilobj" . strtolower($type) . "gui"
);
}
}

/**
Expand Down Expand Up @@ -203,7 +218,7 @@ protected function getContextId(): ?int
}
}

$referrer = (int) $this->effectiveRefId;
$referer = (int) $this->effectiveRefId;

if ($referer > 0) {
if (ilSession::has('lti_' . $referer . '_post_data')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

declare(strict_types=1);
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -17,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

/**
* Class ilObjLTIAdministration
* @author Jesús López <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

declare(strict_types=1);
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -17,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

/**
* Class ilObjLTIAdministrationAccess
*
Expand Down
Loading

0 comments on commit 4b79db7

Please sign in to comment.