diff --git a/lib/Models/Config.php b/lib/Models/Config.php index 77dd37f9b..2426442fe 100644 --- a/lib/Models/Config.php +++ b/lib/Models/Config.php @@ -166,7 +166,6 @@ public function updateEndpoints($container) ]; Endpoints::deleteBySql('config_id = ?', [$this->id]); - Config::deleteBySql('id = ?', [$this->id]); } else { $service_host = $service_url['scheme'] .'://' . @@ -186,7 +185,18 @@ public function updateEndpoints($container) $services_client = new ServicesClient($this->id); $comp = null; - $comp = $services_client->getRESTComponents(); + try { + $comp = $services_client->getRESTComponents(); + } + catch(\Exception $e) { + return [ + 'type' => 'error', + 'text' => sprintf( + _('%s'), + $e->getMessage() + ) + ]; + } } catch (AccessDeniedException $e) { Endpoints::removeEndpoint($this->id, 'services'); diff --git a/lib/Models/Helpers.php b/lib/Models/Helpers.php index 04346d96e..cd1e2a736 100644 --- a/lib/Models/Helpers.php +++ b/lib/Models/Helpers.php @@ -145,6 +145,31 @@ static function getMyCourses($user_id) return $courses; } + /** + * Check and make sure, that a valid server is set. + * If no server is detected, -1 will be stored. + * + * @return + */ + static function validateDefaultServer() { + $config = new \SimpleCollection(Config::findBySql(1)); + $config_ids = $config->pluck('id'); + + $value = \Config::get()->OPENCAST_DEFAULT_SERVER; + $valid_value = $value; + + if (empty($config_ids)) { + $valid_value = -1; + } + elseif (in_array($value, $config_ids) === false) { + $valid_value = reset($config_ids); + } + if ($valid_value != $value) { + $value = $valid_value; + \Config::get()->store('OPENCAST_DEFAULT_SERVER', $valid_value); + } + } + /** * Check if the default course playlists exists and create if necessary * diff --git a/lib/Routes/Config/ConfigAdd.php b/lib/Routes/Config/ConfigAdd.php index 24dc84b6f..dfcf39f26 100644 --- a/lib/Routes/Config/ConfigAdd.php +++ b/lib/Routes/Config/ConfigAdd.php @@ -11,6 +11,7 @@ use Opencast\Models\Endpoints; use Opencast\Models\SeminarEpisodes; use Opencast\Models\LTI\LtiHelper; +use Opencast\Models\Helpers; use Opencast\Models\I18N as _; @@ -52,9 +53,16 @@ public function __invoke(Request $request, Response $response, $args) // check settings and store them to the database $config->updateSettings($json['config']); + // Validate that a correct default server is set + Helpers::validateDefaultServer(); // check configuration and load endpoints $message = $config->updateEndpoints($this->container); + // Dont save configuration if it failed + if ($message['type'] == 'error') { + Endpoints::removeEndpoint($config->id, 'services'); + Config::deleteBySql('id = ?', [$config->id]); + } $ret_config = $config->toArray(); $ret_config = array_merge($ret_config, $ret_config['settings']); diff --git a/lib/Routes/Config/ConfigDelete.php b/lib/Routes/Config/ConfigDelete.php index fff6fed97..9adb6d640 100644 --- a/lib/Routes/Config/ConfigDelete.php +++ b/lib/Routes/Config/ConfigDelete.php @@ -10,6 +10,7 @@ use Opencast\OpencastController; use Opencast\Models\Config; use Opencast\Models\Videos; +use Opencast\Models\Helpers; class ConfigDelete extends OpencastController { @@ -30,6 +31,9 @@ public function __invoke(Request $request, Response $response, $args) if (!$config->delete()) { throw new Error('Could not delete config.', 500); } + + // Validate that a correct default server is set + Helpers::validateDefaultServer(); return $response->withStatus(204); } diff --git a/lib/Routes/Config/ConfigEdit.php b/lib/Routes/Config/ConfigEdit.php index e7ac380ad..b994a1ae9 100644 --- a/lib/Routes/Config/ConfigEdit.php +++ b/lib/Routes/Config/ConfigEdit.php @@ -28,6 +28,7 @@ public function __invoke(Request $request, Response $response, $args) $duplicate_url = false; $config = Config::find($args['id']); + $config_old = $config->toArray(); if (empty($config)) { throw new Error('Could not find config with id '. $args['id'] .'.', 500); @@ -38,6 +39,11 @@ public function __invoke(Request $request, Response $response, $args) // check configuration and load endpoints $message = $config->updateEndpoints($this->container); + // Restore configuration if it failed + if ($message['type'] == 'error') { + $config->setData($config_old); + $config->store(); + } $ret_config = $config->toArray(); $ret_config = array_merge($ret_config, $ret_config['settings']); diff --git a/lib/Routes/Config/ConfigUpdate.php b/lib/Routes/Config/ConfigUpdate.php index ae7359ee7..90336286b 100644 --- a/lib/Routes/Config/ConfigUpdate.php +++ b/lib/Routes/Config/ConfigUpdate.php @@ -7,6 +7,7 @@ use Opencast\OpencastTrait; use Opencast\OpencastController; use Opencast\Models\ScheduleHelper; +use Opencast\Models\Helpers; use Opencast\Models\Config; use Opencast\Models\WorkflowConfig; @@ -24,22 +25,14 @@ public function __invoke(Request $request, Response $response, $args) // load oc server configs $config = new \SimpleCollection(Config::findBySql(1)); - $config_ids = $config->pluck('id'); - // Storing General Configs. foreach ($json['settings'] as $config) { - // validate values - if ($config['name'] == 'OPENCAST_DEFAULT_SERVER') { - // check, that a correct server is set - if (in_array($config['value'], $config_ids) === false) { - $config['value'] = reset($config_ids); - } - } - if (in_array($config['name'], $constants['global_config_options'])) { \Config::get()->store($config['name'], $config['value']); } } + // Validate that a correct default server is set + Helpers::validateDefaultServer(); // Storing Resources Configs. $messages = []; diff --git a/vueapp/components/Config/AdminConfigs.vue b/vueapp/components/Config/AdminConfigs.vue index 66f3bb127..bdb9cd268 100644 --- a/vueapp/components/Config/AdminConfigs.vue +++ b/vueapp/components/Config/AdminConfigs.vue @@ -76,6 +76,7 @@ export default { } this.$store.dispatch('configListUpdate', params) .then(({ data }) => { + this.$store.dispatch('configListRead'); if (data.messages.length) { for (let i = 0; i < data.messages.length; i++ ) { this.$store.dispatch('addMessage', data.messages[i]); diff --git a/vueapp/components/Config/ConfigOption.vue b/vueapp/components/Config/ConfigOption.vue index d9e9096d3..0d16f5a8f 100644 --- a/vueapp/components/Config/ConfigOption.vue +++ b/vueapp/components/Config/ConfigOption.vue @@ -47,7 +47,7 @@ @change="setValue(setting.value)"> -