Skip to content

Commit

Permalink
correctly use restful syntax, refs #718
Browse files Browse the repository at this point in the history
  • Loading branch information
tgloeggl committed Aug 5, 2023
1 parent ee910ef commit 8540831
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 221 deletions.
148 changes: 148 additions & 0 deletions lib/Models/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use Opencast\RelationshipTrait;
use Opencast\Models\UPMap;
use Opencast\Models\SeminarSeries;
use Opencast\Models\REST\Config as RESTConfig;
use Opencast\Models\REST\ServicesClient;
use Opencast\Models\WorkflowConfig;

class Config extends \SimpleOrMap
{
Expand Down Expand Up @@ -110,4 +113,149 @@ public function sanitizeSettings($event)
}
}
}

/**
* Update settings with the passed settings
*
* @param Array $json the settings to update, with workflows
* @return void
*/
public function updateSettings($json)
{
$new_settings = [];
$stored_config = $this->toArray();
foreach ($json as $setting_name => $setting) {
if (!in_array($setting_name, array_keys($stored_config)) && $setting_name != 'checked') {
$new_settings[$setting_name] = $setting;
}
}

$json['settings'] = $new_settings;

// save configured workflows to store them when installation is successfull
$workflows = [];
if (isset($json['settings']['workflow_configs'])) {
foreach ($json['settings']['workflow_configs'] as $wf_config) {
$workflows[$wf_config['id']] = $wf_config;
}
unset($json['settings']['workflow_configs']);
}

$this->setData($json);
return $this->store();
}

/**
* load and update endpoihnts for reference OC server
*
* @param Container $container
* @return void
*/
public function updateEndpoints($container)
{
$service_url = parse_url($this->service_url);

// check the selected url for validity
if (!array_key_exists('scheme', $service_url)) {
$message = [
'type' => 'error',
'text' => sprintf(
_('Ungültiges URL-Schema: "%s"'),
$this->service_url
)
];

Endpoints::deleteBySql('config_id = ?', [$this->id]);
Config::deleteBySql('id = ?', [$this->id]);
} else {
$service_host =
$service_url['scheme'] .'://' .
$service_url['host'] .
(isset($service_url['port']) ? ':' . $service_url['port'] : '');

try {
$version = RESTConfig::getOCBaseVersion($this->id);

Endpoints::deleteBySql('config_id = ?', [$this->id]);

$this->service_version = $version;
$this->store();

Endpoints::setEndpoint($this->id, $service_host .'/services', 'services');

$services_client = new ServicesClient($this->id);

$comp = null;
$comp = $services_client->getRESTComponents();
} catch (AccessDeniedException $e) {
Endpoints::removeEndpoint($this->id, 'services');

$message = [
'type' => 'error',
'text' => sprintf(
_('Fehlerhafte Zugangsdaten für die Opencast Installation mit der URL "%s". Überprüfen Sie bitte die eingebenen Daten.'),
$service_host
)
];

$this->redirect('admin/config');
return;
}

if ($comp) {
$services = RESTConfig::retrieveRESTservices($comp, $service_url['scheme']);

if (empty($services)) {
Endpoints::removeEndpoint($this->id, 'services');
$message = [
'type' => 'error',
'text' => sprintf(
_('Es wurden keine Endpoints für die Opencast Installation mit der URL "%s" gefunden. '
. 'Überprüfen Sie bitte die eingebenen Daten, achten Sie dabei auch auf http vs https und '
. 'ob ihre Opencast-Installation https unterstützt.'),
$service_host
)
];
} else {

foreach($services as $service_url => $service_type) {
if (in_array(
strtolower($service_type),
$container['opencast']['services']
) !== false
) {
Endpoints::setEndpoint($this->id, $service_url, $service_type);
} else {
unset($services[$service_url]);
}
}

// create new entries for workflow_config table
WorkflowConfig::createAndUpdateByConfigId($this->id, $workflows);

$success_message[] = sprintf(
_('Die Opencast Installation "%s" wurde erfolgreich konfiguriert.'),
$service_host
);

$message = [
'type' => 'success',
'text' => implode('<br>', $success_message)
];

$config_checked = true;
}
} else {
$message = [
'type' => 'error',
'text' => sprintf(
_('Es wurden keine Endpoints für die Opencast Installation mit der URL "%s" gefunden. Überprüfen Sie bitte die eingebenen Daten.'),
$service_host
)
];
}
}

return $message;
}
}
15 changes: 8 additions & 7 deletions lib/RouteMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ public function authenticatedRoutes()
*/
public function adminRoutes()
{
$this->app->get('/config', Routes\Config\ConfigList::class);
$this->app->put('/config', Routes\Config\ConfigUpdate::class);
$this->app->post('/config', Routes\Config\ConfigAddEdit::class);

$this->app->get('/config/{id}', Routes\Config\ConfigShow::class);
$this->app->put('/config/{id}', Routes\Config\ConfigAddEdit::class);
$this->app->delete('/config/{id}', Routes\Config\ConfigDelete::class);
// TODO: document in api docs
$this->app->get('/config', Routes\Config\ConfigList::class); // get a list of all configured servers and their settings
$this->app->put('/global_config', Routes\Config\ConfigUpdate::class);

$this->app->post('/config', Routes\Config\ConfigAdd::class); // create new config
$this->app->get('/config/{id}', Routes\Config\ConfigShow::class); // get config with itd
$this->app->put('/config/{id}', Routes\Config\ConfigEdit::class); // update existing config
$this->app->delete('/config/{id}', Routes\Config\ConfigDelete::class); // delete existing config
}

/**
Expand Down
78 changes: 78 additions & 0 deletions lib/Routes/Config/ConfigAdd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Opencast\Routes\Config;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Opencast\OpencastTrait;
use Opencast\OpencastController;
use Opencast\Errors\AuthorizationFailedException;
use Opencast\Models\Config;
use Opencast\Models\Endpoints;
use Opencast\Models\SeminarEpisodes;
use Opencast\Models\LTI\LtiHelper;

use Opencast\Models\I18N as _;

class ConfigAdd extends OpencastController
{
use OpencastTrait;

public function __invoke(Request $request, Response $response, $args)
{
\SimpleOrMap::expireTableScheme();

$json = $this->getRequestData($request);

$config_checked = false;
$duplicate_url = false;

// check, if a config with the same data already exists:
$config = reset(Config::findBySql('service_url = ?', [$json['config']['service_url']]));

// POST request - create config
if ($config) {
$duplicate_url = true;
} else {
$config = new Config;
}

// Throw error if the url is already used
if ($duplicate_url) {
return $this->createResponse([
'message'=> [
'type' => 'error',
'text' => sprintf(
_('Eine Konfiguration mit der angegebenen URL ist bereits vorhanden: "%s"'),
$json['config']['service_url']
)
],
], $response);
}

// check settings and store them to the database
$config->updateSettings($json['config']);

// check configuration and load endpoints
$message = $config->updateEndpoints($this->container);

$ret_config = $config->toArray();
$ret_config = array_merge($ret_config, $ret_config['settings']);
unset($ret_config['settings']);

if ($config_checked) {
$lti = LtiHelper::getLaunchData($config->id);

return $this->createResponse([
'config' => $ret_config,
'message'=> $message,
'lti' => $lti
], $response);
}

return $this->createResponse([
'config' => $ret_config,
'message'=> $message,
], $response);
}
}
Loading

0 comments on commit 8540831

Please sign in to comment.