diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 diff --git a/AUTHORS.md b/AUTHORS.md old mode 100644 new mode 100755 diff --git a/CHANGELOG.md b/CHANGELOG.md old mode 100644 new mode 100755 diff --git a/COPYING b/COPYING old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/Makefile b/Makefile old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/appinfo/Migrations/Version20191002101015.php b/appinfo/Migrations/Version20191002101015.php old mode 100644 new mode 100755 diff --git a/appinfo/Migrations/Version20210324134421.php b/appinfo/Migrations/Version20210324134421.php new file mode 100644 index 0000000..6dd38ae --- /dev/null +++ b/appinfo/Migrations/Version20210324134421.php @@ -0,0 +1,56 @@ +hasTable("{$prefix}sciencemesh")) { + $schema->dropTable("{$prefix}sciencemesh"); + } + + $table = $schema->createTable("{$prefix}sciencemesh"); + $table->addColumn('apikey', 'string', [ + 'notnull' => true, + ]); + $table->addColumn('sitename', 'string', [ + 'notnull' => true, + ]); + $table->addColumn('siteurl', 'string', [ + 'notnull' => true, + ]); + $table->addColumn('siteid', 'string', [ + 'notnull' => false, + ]); + $table->addColumn('country', 'string', [ + 'notnull' => true, + 'length' => 3, + ]); + $table->addColumn('iopurl', 'string', [ + 'notnull' => true, + ]); + $table->addColumn('numusers', Type::BIGINT, [ + 'notnull' => true, + 'default' => 0, + 'unsigned' => true, + ]); + $table->addColumn('numfiles', Type::BIGINT, [ + 'notnull' => true, + 'default' => 0, + 'unsigned' => true, + ]); + $table->addColumn('numstorage', Type::BIGINT, [ + 'notnull' => true, + 'default' => 0, + 'unsigned' => true, + ]); + } +} diff --git a/appinfo/app.php b/appinfo/app.php old mode 100644 new mode 100755 diff --git a/appinfo/application.php b/appinfo/application.php old mode 100644 new mode 100755 diff --git a/appinfo/autoload.php b/appinfo/autoload.php old mode 100644 new mode 100755 diff --git a/appinfo/info.xml b/appinfo/info.xml old mode 100644 new mode 100755 index 83898e3..30998ea --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,14 +5,14 @@ This apps connects the site to the Science Mesh MIT Hugo Gonzalez Labrador - 1.0.1 + 1.1.0 ScienceMesh productivity - - - + + + - OCA\Sciencemesh\AdminSettings + OCA\Sciencemesh\AdminSettings true - + diff --git a/appinfo/routes.php b/appinfo/routes.php old mode 100644 new mode 100755 diff --git a/controller/pagecontroller.php b/controller/pagecontroller.php old mode 100644 new mode 100755 index 5ef6a2e..c66b4db --- a/controller/pagecontroller.php +++ b/controller/pagecontroller.php @@ -21,24 +21,26 @@ use OCP\Http\Client\IClientService; use OCP\AppFramework\Http; -class PageController extends Controller { - private $logger; +class PageController extends Controller +{ + private $logger; private $userId; protected $connection; /** @var IClientService */ private $httpClientService; - public function __construct($AppName, - IRequest $request, - $UserId, - IDBConnection $connection, - IClientService $httpClientService, - ILogger $logger) { + public function __construct($AppName, + IRequest $request, + $UserId, + IDBConnection $connection, + IClientService $httpClientService, + ILogger $logger) + { parent::__construct($AppName, $request); $this->userId = $UserId; - $this->connection = $connection; + $this->connection = $connection; $this->httpClientService = $httpClientService; $this->logger = $logger; } @@ -53,7 +55,8 @@ public function __construct($AppName, * @NoAdminRequired * @NoCSRFRequired */ - public function index() { + public function index() + { $params = ['user' => $this->userId]; return new TemplateResponse('sciencemesh', 'main', $params); // templates/main.php } @@ -62,7 +65,8 @@ public function index() { * Simply method that posts back the payload of the request * @NoAdminRequired */ - public function doEcho($echo) { + public function doEcho($echo) + { return new DataResponse(['echo' => $echo]); } @@ -71,7 +75,8 @@ public function doEcho($echo) { * @NoAdminRequired * @NoCSRFRequired */ - public function getMetrics() { + public function getMetrics() + { // for localhost requests is needed to add // 'allow_local_remote_servers' => true, // to config.php @@ -108,7 +113,8 @@ public function getMetrics() { * @NoAdminRequired * @NoCSRFRequired */ - public function getInternalMetrics() { + public function getInternalMetrics() + { //$metrics = $this->getInternal(); $settings = $this->loadSettings(); if (!$settings) { @@ -125,7 +131,8 @@ public function getInternalMetrics() { return new JSONResponse($payload); } - private function loadSettings(){ + private function loadSettings() + { $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $query->select('*')->from('sciencemesh'); $result = $query->execute(); @@ -134,6 +141,7 @@ private function loadSettings(){ $row['numusers'] = intval($row['numusers']); $row['numfiles'] = intval($row['numfiles']); $row['numstorage'] = intval($row['numstorage']); + unset($row['apikey']); // Remove the private API key from the exposed settings return $row; } diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php old mode 100644 new mode 100755 index 66be908..4c62af4 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -18,119 +18,143 @@ /** * Settings controller for the administration page */ -class SettingsController extends Controller { - private $logger; - private $config; - private $urlGenerator; - - /** - * @param string $AppName - application name - * @param IRequest $request - request object - * @param IURLGenerator $urlGenerator - url generator service - * @param IL10N $trans - l10n service - * @param ILogger $logger - logger - * @param AppConfig $config - application configuration - */ - public function __construct($AppName, - IRequest $request, - IURLGenerator $urlGenerator, - IL10N $trans, - ILogger $logger, - AppConfig $config - ) { - parent::__construct($AppName, $request); - - $this->urlGenerator = $urlGenerator; - $this->logger = $logger; - $this->config = $config; - - $eventDispatcher = \OC::$server->getEventDispatcher(); - $eventDispatcher->addListener( - 'OCA\Files::loadAdditionalScripts', - function() { - \OCP\Util::addScript('sciencemesh', 'settings'); - \OCP\Util::addStyle('sciencemesh', 'style'); - } -); - } - - /** - * Print config section - * - * @return TemplateResponse - */ - public function index() { - $data = $this->loadSettings(); - if (!$data) { - // settings has not been set - $hostname = \OCP\Util::getServerHostName(); - $data = ["hostname" => $hostname]; - $data["iopurl"] = ""; - $data["country"] = ""; - $data["sitename"] = ""; - $data["siteurl"] = ""; - $data["numusers"] = 0; - $data["numfiles"] = 0; - $data["numstorage"] = 0; - } +class SettingsController extends Controller +{ + private $logger; + private $config; + private $urlGenerator; + const CATALOG_URL = "https://sciencemesh-test.uni-muenster.de/api/mentix/sitereg"; + + /** + * @param string $AppName - application name + * @param IRequest $request - request object + * @param IURLGenerator $urlGenerator - url generator service + * @param IL10N $trans - l10n service + * @param ILogger $logger - logger + * @param AppConfig $config - application configuration + */ + public function __construct($AppName, + IRequest $request, + IURLGenerator $urlGenerator, + IL10N $trans, + ILogger $logger, + AppConfig $config + ) + { + parent::__construct($AppName, $request); + + $this->urlGenerator = $urlGenerator; + $this->logger = $logger; + $this->config = $config; + + $eventDispatcher = \OC::$server->getEventDispatcher(); + $eventDispatcher->addListener( + 'OCA\Files::loadAdditionalScripts', + function () { + \OCP\Util::addScript('sciencemesh', 'settings'); + \OCP\Util::addStyle('sciencemesh', 'style'); + } + ); + } - return new TemplateResponse($this->appName, "settings", $data, "blank"); - } + /** + * Print config section + * + * @return TemplateResponse + */ + public function index() + { + $data = $this->loadSettings(); + if (!$data) { + // settings has not been set + $data = [ + "apikey" => "", + "sitename" => "", + "siteurl" => "", + "siteid" => "", + "country" => "", + "iopurl" => "", + "numusers" => 0, + "numfiles" => 0, + "numstorage" => 0 + ]; + } + return new TemplateResponse($this->appName, "settings", $data, "blank"); + } /** * Simply method that posts back the payload of the request * @NoAdminRequired */ - public function saveSettings($iopurl, $country, $hostname, $sitename, $siteurl, $numusers, $numfiles, $numstorage) { + public function saveSettings($apikey, $sitename, $siteurl, $country, $iopurl, $numusers, $numfiles, $numstorage) + { + $siteid = null; + + if ($numusers == null) { + $numusers = 0; + } + if ($numfiles == null) { + $numfiles = 0; + } + if ($numstorage == null) { + $numstorage = 0; + } + + // submit settings to Mentix (if they are valid) + if ($apikey !== "" && $sitename !== "" && $siteurl !== "" && $iopurl !== "") { + try { + $siteid = $this->submitSettings($apikey, $sitename, $siteurl, $country, $iopurl); + } catch (\Exception $e) { + return new DataResponse([ + 'error' => $e->getMessage() + ]); + } + } + // store settings in DB $this->deleteSettings(); - $ok = $this->storeSettings($iopurl, $country, $hostname, $sitename, $siteurl, $numusers, $numfiles, $numstorage); - if (!$ok) { + try { + $this->storeSettings($apikey, $sitename, $siteurl, $siteid, $country, $iopurl, $numusers, $numfiles, $numstorage); + } catch (\Exception $e) { return new DataResponse([ - 'error' => 'error storing settings, check server logs' + 'error' => 'error storing settings: ' . $e->getMessage() ]); } - return new DataResponse([ - 'iopurl' => $iopurl, - 'country' => $country, - 'hostname' => $hostname, - 'sitename' => $sitename, - 'siteurl' => $siteurl, - 'numusers' => $numusers, - 'numfiles' => $numfiles, - 'numstorage' => $numstorage - ]); + return new DataResponse(["siteid" => $siteid]); } - private function storeSettings($iopurl, $country, $hostname, $sitename, $siteurl, $numusers, $numfiles, $numstorage){ + private function storeSettings($apikey, $sitename, $siteurl, $siteid, $country, $iopurl, $numusers, $numfiles, $numstorage) + { $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $query->insert('sciencemesh') - ->setValue('iopurl', $query->createNamedParameter($iopurl)) - ->setValue('country', $query->createNamedParameter($country)) + ->setValue('apikey', $query->createNamedParameter($apikey)) ->setValue('sitename', $query->createNamedParameter($sitename)) ->setValue('siteurl', $query->createNamedParameter($siteurl)) + ->setValue('siteid', $query->createNamedParameter($siteid)) + ->setValue('country', $query->createNamedParameter($country)) + ->setValue('iopurl', $query->createNamedParameter($iopurl)) ->setValue('numusers', $query->createNamedParameter($numusers)) ->setValue('numfiles', $query->createNamedParameter($numfiles)) - ->setValue('numstorage', $query->createNamedParameter($numstorage)) - ->setValue('hostname', $query->createNamedParameter($hostname)); + ->setValue('numstorage', $query->createNamedParameter($numstorage)); $result = $query->execute(); + if (!$result) { - \OC::$server->getLogger()->error('sciencemesh database cound not be updated', - ['app' => 'sciencemesh']); - return false; + \OC::$server->getLogger()->error('sciencemesh database cound not be updated', ['app' => 'sciencemesh']); + throw new \Exception('sciencemesh database cound not be updated'); } - return true; } - private function deleteSettings(){ + private function deleteSettings() + { $deleteQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $deleteQuery->delete('sciencemesh'); $deleteQuery->execute(); } - private function loadSettings(){ + private function loadSettings() + { $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $query->select('*')->from('sciencemesh'); $result = $query->execute(); @@ -138,23 +162,56 @@ private function loadSettings(){ $result->closeCursor(); return $row; } - - - - /** - * Get app settings - * - * @return array - * - * @NoAdminRequired - * @PublicPage - */ - public function GetSettings() { - $result = [ - "formats" => $this->config->FormatsSetting(), - "sameTab" => $this->config->GetSameTab(), - "shareAttributesVersion" => $this->config->ShareAttributesVersion() - ]; - return $result; - } + + private function submitSettings($apikey, $sitename, $siteurl, $country, $iopurl) + { + // fill out a data object as needed by Mentix + $iopPath = parse_url($iopurl, PHP_URL_PATH); + $data = json_encode([ + "name" => $sitename, + "url" => $siteurl, + "countryCode" => $country, + "reva" => [ + "url" => $iopurl, + "metricsPath" => rtrim($iopPath, "/") . "/metrics" + ] + ]); + $url = self::CATALOG_URL . "?action=register&apiKey=" . urlencode($apikey); + + // use CURL to send the request to Mentix + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_HEADER, false); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json")); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + $response = curl_exec($curl); + $respData = json_decode($response, true); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + + if ($status == 200) { + return $respData["id"]; + } else { + throw new \Exception($respData["error"]); + } + } + + /** + * Get app settings + * + * @return array + * + * @NoAdminRequired + * @PublicPage + */ + public function GetSettings() + { + $result = [ + "formats" => $this->config->FormatsSetting(), + "sameTab" => $this->config->GetSameTab(), + "shareAttributesVersion" => $this->config->ShareAttributesVersion() + ]; + return $result; + } } diff --git a/css/settings.css b/css/settings.css old mode 100644 new mode 100755 diff --git a/css/style.css b/css/style.css old mode 100644 new mode 100755 diff --git a/img/app.svg b/img/app.svg old mode 100644 new mode 100755 diff --git a/js/script.js b/js/script.js old mode 100644 new mode 100755 diff --git a/js/settings.js b/js/settings.js old mode 100644 new mode 100755 index 460a8c2..4527f98 --- a/js/settings.js +++ b/js/settings.js @@ -2,58 +2,47 @@ $(document).ready(function () { OCA.ScienceMesh = _.extend({ - AppName: "sciencemesh" - }, OCA.ScienceMesh) + AppName: "sciencemesh" + }, OCA.ScienceMesh) $("#sciencemeshSave").click(function () { $(".section-sciencemesh").addClass("icon-loading"); - var iopUrl = $("#sciencemeshIopUrl").val().trim(); + var apiKey = $("#sciencemeshAPIKey").val().trim(); + var sitename = $("#sciencemeshSitename").val().trim(); + var siteurl = $("#sciencemeshSiteurl").val().trim(); var countryCode = $("#sciencemeshCountryCode").val().trim(); - var siteName = $("#sciencemeshSitename").val().trim(); - var hostName = $("#sciencemeshHostname").val().trim(); - var siteURL= $("#sciencemeshSiteurl").val().trim(); + var iopurl = $("#sciencemeshIopUrl").val().trim(); var numUsers = $("#sciencemeshNumusers").val().trim(); var numFiles = $("#sciencemeshNumfiles").val().trim(); var numStorage = $("#sciencemeshNumstorage").val().trim(); - // - // if (!iopUrl.length) { - // $("#sciencemeshApiKey").val(""); - // } - - // TODO(labkode): add API key - // var apiKey = $("#sciencemeshApiKey").val().trim(); - $.ajax({ method: "PUT", url: OC.generateUrl("apps/" + OCA.ScienceMesh.AppName + "/ajax/settings/address"), data: { - iopurl: iopUrl, - country: countryCode, - sitename: siteName, - hostname: hostName, - siteurl: siteURL, - numusers: numUsers, - numfiles: numFiles, - numstorage: numStorage + apikey: apiKey, + sitename: sitename, + siteurl: siteurl, + country: countryCode, + iopurl: iopurl, + numusers: numUsers, + numfiles: numFiles, + numstorage: numStorage }, success: function onSuccess(response) { $(".section-sciencemesh").removeClass("icon-loading"); - if (response && (response.iopUrl != null)) { - $("#sciencemeshIopUrl").val(response.iopurl); - $("#sciencemeshApiKey").val(response.apikey); - + if (response) { var message = response.error - ? (t(OCA.ScienceMesh.AppName, "Error when trying to connect") + " (" + response.error + ")") + ? (t(OCA.ScienceMesh.AppName, "Error when trying to update the settings") + " (" + response.error + ")") : t(OCA.ScienceMesh.AppName, "Settings have been successfully updated"); var versionMessage = response.version ? (" (" + t(OCA.ScienceMesh.AppName, "version") + " " + response.version + ")") : ""; OC.Notification.show(message + versionMessage, { - type: response.error ? "error" : null, - timeout: 3 + type: response.error ? "error" : "info", + timeout: 10 }); } } @@ -62,7 +51,7 @@ $(".section-sciencemesh input").keypress(function (e) { var code = e.keyCode || e.which; - if (code === 13) { + if (code === 10 || code === 13) { $("#sciencemeshSave").click(); } }); diff --git a/lib/adminsettings.php b/lib/adminsettings.php old mode 100644 new mode 100755 diff --git a/lib/appconfig.php b/lib/appconfig.php old mode 100644 new mode 100755 diff --git a/phpunit.integration.xml b/phpunit.integration.xml old mode 100644 new mode 100755 diff --git a/phpunit.xml b/phpunit.xml old mode 100644 new mode 100755 diff --git a/settings.php b/settings.php old mode 100644 new mode 100755 diff --git a/templates/main.php b/templates/main.php old mode 100644 new mode 100755 diff --git a/templates/part.content.php b/templates/part.content.php old mode 100644 new mode 100755 diff --git a/templates/part.navigation.php b/templates/part.navigation.php old mode 100644 new mode 100755 diff --git a/templates/part.settings.php b/templates/part.settings.php old mode 100644 new mode 100755 diff --git a/templates/settings.php b/templates/settings.php old mode 100644 new mode 100755 index 76a5943..60d16eb --- a/templates/settings.php +++ b/templates/settings.php @@ -7,19 +7,27 @@ ScienceMesh -

t("Settings")) ?>

- -
+

t("Site Settings")) ?>

+

t("Site Name")) ?>

-

" placeholder="CERN" type="text">

+

" placeholder="CERN" type="text">The name of this site.

+

t("Site URL")) ?>

-

" placeholder="http://localhost" type="text">

+

" placeholder="https://owncloud.example.com" type="text">The URL at which your site can be reached.

+

t("Country Code")) ?>

-

" placeholder="CH" type="text">

-

t("Hostname")) ?>

-

" placeholder="example.org/xcloud/" type="text">

+

" placeholder="CH" type="text">The 2- or 3-digit code of the site's country. A list of all codes can be found here.

+
+ +

t("IOP Settings")) ?>

+

t("IOP Service Address")) ?>

-

" placeholder="http:///" type="text">

+

" placeholder="https://owncloud.example.com/iop" type="text">The main URL of your IOP service. If the IOP is running on the same host as this ownCloud instance, you can simply use http://localhost:<iop-port> here.

+
+ +

t("Metrics")) ?>

+
+ Note: The following settings need to be provided manually for now, as they are not yet extracted automatically from ownCloud. This will change in the future, though.

t("Number of users")) ?>

" placeholder="0" type="number">

t("Number of files")) ?>

@@ -28,7 +36,17 @@

" placeholder="0" type="number">

+

t("API Key")) ?>

+
+

t("API Key")) ?>

+

" placeholder="" type="text">An API key is needed to register your site with ScienceMesh. If you do not have a key yet, you can register for a free ScienceMesh account using this link.

+
+ +
 
-

+

+ + Note: Clicking 'Save' will, if a valid API key has been entered above, register your site with ScienceMesh (or update your existing entry). +

diff --git a/tests/bootstrap.php b/tests/bootstrap.php old mode 100644 new mode 100755 diff --git a/tests/integration/AppTest.php b/tests/integration/AppTest.php old mode 100644 new mode 100755 diff --git a/tests/unit/controller/PageControllerTest.php b/tests/unit/controller/PageControllerTest.php old mode 100644 new mode 100755