Minor - Flint fixes

pull/3944/head
Angel Fernando Quiroz Campos 4 years ago
parent 080d0e9854
commit e78a374ab3
  1. 3
      plugin/lti_provider/Entity/Platform.php
  2. 1
      plugin/lti_provider/Entity/PlatformKey.php
  3. 154
      plugin/lti_provider/LtiProviderPlugin.php
  4. 30
      plugin/lti_provider/db/lti13_cache.php
  5. 21
      plugin/lti_provider/db/lti13_database.php
  6. 1
      plugin/lti_provider/lang/english.php
  7. 1
      plugin/lti_provider/src/Form/FrmAdd.php
  8. 7
      plugin/lti_provider/src/LtiProvider.php

@ -85,7 +85,6 @@ class Platform
}
/**
*
* Get key id.
*/
public function getKid(): string
@ -95,8 +94,6 @@ class Platform
/**
* Set key id.
*
* @param string $kid
*/
public function setKid(string $kid): Platform
{

@ -61,7 +61,6 @@ class PlatformKey
}
/**
*
* Get key id.
*/
public function getKid(): string

@ -13,7 +13,6 @@ use Symfony\Component\Filesystem\Filesystem;
*/
class LtiProviderPlugin extends Plugin
{
const TABLE_PLATFORM = 'plugin_lti_provider_platform';
public $isAdminPlugin = true;
@ -50,7 +49,6 @@ class LtiProviderPlugin extends Plugin
'enabled' => 'boolean',
];
parent::__construct($version, $author, $settings);
}
/**
@ -131,9 +129,76 @@ class LtiProviderPlugin extends Plugin
}
/**
* Creates the plugin tables on database
* Save configuration for plugin.
*
* Generate a new key pair for platform when enabling plugin.
*
* @return void
* @throws OptimisticLockException
* @throws \Doctrine\ORM\ORMException
*
* @return $this|Plugin
*/
public function performActionsAfterConfigure()
{
$em = Database::getManager();
/** @var PlatformKey $platformKey */
$platformKey = $em
->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey')
->findOneBy([]);
if ($this->get('enabled') === 'true') {
if (!$platformKey) {
$platformKey = new PlatformKey();
}
$keyPair = self::generatePlatformKeys();
$platformKey->setKid($keyPair['kid']);
$platformKey->publicKey = $keyPair['public'];
$platformKey->setPrivateKey($keyPair['private']);
$em->persist($platformKey);
} else {
if ($platformKey) {
$em->remove($platformKey);
}
}
$em->flush();
return $this;
}
/**
* Unistall plugin. Clear the database.
*/
public function uninstall()
{
$pluginEntityPath = $this->getEntityPath();
$fs = new Filesystem();
if ($fs->exists($pluginEntityPath)) {
$fs->remove($pluginEntityPath);
}
try {
$this->dropPluginTables();
} catch (DBALException $e) {
error_log('Error while uninstalling IMS/LTI plugin: '.$e->getMessage());
}
}
public function trimParams(array &$params)
{
foreach ($params as $key => $value) {
$newValue = preg_replace('/\s+/', ' ', $value);
$params[$key] = trim($newValue);
}
}
/**
* Creates the plugin tables on database.
*/
private function createPluginTables(): void
{
@ -165,7 +230,6 @@ class LtiProviderPlugin extends Plugin
foreach ($queries as $query) {
Database::query($query);
}
}
private function areTablesCreated(): bool
@ -176,54 +240,10 @@ class LtiProviderPlugin extends Plugin
return $connection->getSchemaManager()->tablesExist(self::TABLE_PLATFORM);
}
/**
* Save configuration for plugin.
*
* Generate a new key pair for platform when enabling plugin.
*
* @throws OptimisticLockException
* @throws \Doctrine\ORM\ORMException
*
* @return $this|Plugin
*/
public function performActionsAfterConfigure()
{
$em = Database::getManager();
/** @var PlatformKey $platformKey */
$platformKey = $em
->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey')
->findOneBy([]);
if ($this->get('enabled') === 'true') {
if (!$platformKey) {
$platformKey = new PlatformKey();
}
$keyPair = self::generatePlatformKeys();
$platformKey->setKid($keyPair['kid']);
$platformKey->publicKey = $keyPair['public'];
$platformKey->setPrivateKey($keyPair['private']);
$em->persist($platformKey);
} else {
if ($platformKey) {
$em->remove($platformKey);
}
}
$em->flush();
return $this;
}
/**
* Generate a key pair and key id for the platform.
*
* Return a associative array like ['kid' => '...', 'private' => '...', 'public' => '...'].
*
* @return array
*/
private static function generatePlatformKeys(): array
{
@ -251,28 +271,7 @@ class LtiProviderPlugin extends Plugin
}
/**
* Unistall plugin. Clear the database
*/
public function uninstall()
{
$pluginEntityPath = $this->getEntityPath();
$fs = new Filesystem();
if ($fs->exists($pluginEntityPath)) {
$fs->remove($pluginEntityPath);
}
try {
$this->dropPluginTables();
} catch (DBALException $e) {
error_log('Error while uninstalling IMS/LTI plugin: '.$e->getMessage());
}
}
/**
* Drops the plugin tables on database
*
* @return boolean
* Drops the plugin tables on database.
*/
private function dropPluginTables(): bool
{
@ -282,19 +281,9 @@ class LtiProviderPlugin extends Plugin
return true;
}
/**
* @param array $params
*/
public function trimParams(array &$params)
{
foreach ($params as $key => $value) {
$newValue = preg_replace('/\s+/', ' ', $value);
$params[$key] = trim($newValue);
}
}
/**
* Get a SimpleXMLElement object with the request received on php://input.
*
* @throws Exception
*/
private function getRequestXmlElement(): ?SimpleXMLElement
@ -307,5 +296,4 @@ class LtiProviderPlugin extends Plugin
return new SimpleXMLElement($request);
}
}

@ -16,16 +16,6 @@ class Lti13Cache implements Lti1p3Cache
return $this->cache[$key];
}
private function loadCache()
{
$cache = file_get_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt');
if (empty($cache)) {
file_put_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt', '{}');
$this->cache = [];
}
$this->cache = json_decode($cache, true);
}
public function cacheLaunchData($key, $jwtBody): Lti13Cache
{
$this->cache[$key] = $jwtBody;
@ -34,11 +24,6 @@ class Lti13Cache implements Lti1p3Cache
return $this;
}
private function saveCache()
{
file_put_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt', json_encode($this->cache));
}
public function cacheNonce($nonce): Lti13Cache
{
$this->cache['nonce'][$nonce] = true;
@ -56,4 +41,19 @@ class Lti13Cache implements Lti1p3Cache
return true;
}
private function loadCache()
{
$cache = file_get_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt');
if (empty($cache)) {
file_put_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt', '{}');
$this->cache = [];
}
$this->cache = json_decode($cache, true);
}
private function saveCache()
{
file_put_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt', json_encode($this->cache));
}
}

@ -8,7 +8,6 @@ use Packback\Lti1p3\LtiRegistration;
class Lti13Database implements Interfaces\Database
{
public function findRegistrationByIssuer($iss, $clientId = null)
{
$ltiCustomers = $this->getLtiConnection();
@ -30,6 +29,16 @@ class Lti13Database implements Interfaces\Database
->setToolPrivateKey($this->getPrivateKey());
}
public function findDeployment($iss, $deploymentId, $clientId = null)
{
$issSession = Session::read('iss');
if (!in_array($deploymentId, $issSession[$iss]['deployment'])) {
return false;
}
return LtiDeployment::new()->setDeploymentId($deploymentId);
}
private function getLtiConnection(): array
{
$em = Database::getManager();
@ -64,14 +73,4 @@ class Lti13Database implements Interfaces\Database
return $privateKey;
}
public function findDeployment($iss, $deploymentId, $clientId = null)
{
$issSession = Session::read('iss');
if (!in_array($deploymentId, $issSession[$iss]['deployment'])) {
return false;
}
return LtiDeployment::new()->setDeploymentId($deploymentId);
}
}

@ -28,4 +28,3 @@ $strings['Enabled'] = 'Enabled';
$strings['GenerateKeyPairInfo'] = 'A new private and public key pair will be created when enabling.';
$strings['URLs'] = 'Endpoints Urls';
$strings['PlatformConnectionAdded'] = 'A platform connection is added.';

@ -45,7 +45,6 @@ class FrmAdd extends FormValidator
$this->addButtonCreate($plugin->get_lang('AddPlatform'));
$this->applyFilter('__ALL__', 'trim');
}
public function setDefaultValues(): void

@ -9,15 +9,13 @@ require_once __DIR__.'/../db/lti13_cookie.php';
require_once __DIR__.'/../db/lti13_cache.php';
require_once __DIR__.'/../db/lti13_database.php';
/**
* Class LtiProvider.
*/
class LtiProvider
{
/**
* Get the class instance
* Get the class instance.
*
* @staticvar LtiProvider $result
*
@ -44,8 +42,6 @@ class LtiProvider
/**
* Lti Message Launch.
*
* @return LtiMessageLaunch
*/
public function launch(bool $fromCache = false, ?int $launchId = null): LtiMessageLaunch
{
@ -57,5 +53,4 @@ class LtiProvider
return $launch;
}
}

Loading…
Cancel
Save