From 3aa384b82df70dbe06b5403c0b8cd0de51ea9c75 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Mon, 3 Jul 2017 22:13:17 +0200 Subject: [PATCH] Implement API Key setting into admin (WIP) --- appinfo/routes.php | 3 +-- controller/settingscontroller.php | 13 ++---------- js/public/app.js | 33 ------------------------------- lib/Settings/AdminSettings.php | 3 +-- templates/admin.php | 7 ++++++- templates/main.php | 2 -- 6 files changed, 10 insertions(+), 51 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 0e9f168..c15c427 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -23,8 +23,7 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'weather#get', 'url' => '/weather/get', 'verb' => 'GET'), array('name' => 'settings#homeset', 'url' => '/settings/home/set', 'verb' => 'POST'), - array('name' => 'settings#apikeyset', 'url' => '/settings/apikey/set','verb' => 'POST'), - array('name' => 'settings#apikeyget', 'url' => '/settings/apikey/get','verb' => 'GET'), + array('name' => 'settings#apikeyset', 'url' => '/settings/apikey', 'verb' => 'POST'), array('name' => 'settings#metricset', 'url' => '/settings/metric/set','verb' => 'POST'), array('name' => 'settings#metricget', 'url' => '/settings/metric/get','verb' => 'GET'), ))); diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php index 09651f3..a2f9b61 100644 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -24,6 +24,7 @@ class SettingsController extends Controller { private $userId; private $mapper; + private $cityMapper; public function __construct ($appName, IRequest $request, $userId, SettingsMapper $mapper, CityMapper $cityMapper) { parent::__construct($appName, $request); @@ -50,23 +51,13 @@ class SettingsController extends Controller { } /** - * @NoAdminRequired * @NoCSRFRequired */ public function apiKeySet ($apikey) { - $this->mapper->setApiKey($this->userId, $apikey); + \OC::$server->getConfig()->setAppValue('weather', 'openweathermap_api_key', $apiKey); return new JSONResponse(array("set" => true)); } - /** - * @NoAdminRequired - * @NoCSRFRequired - */ - public function apiKeyGet () { - return new JSONResponse(array("apikey" => $this->mapper->getApiKey($this->userId))); - - } - /** * @NoAdminRequired * @NoCSRFRequired diff --git a/js/public/app.js b/js/public/app.js index 9b93932..3ee5490 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -24,7 +24,6 @@ function emptyStr (obj) { app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compile', '$http', function ($scope, $interval, $timeout, $compile, $http) { $scope.owncloudAppImgPath = ''; - $scope.apiKey = ''; $scope.userId = ''; $scope.metric = 'metric'; $scope.metricRepresentation = '°C'; @@ -66,38 +65,6 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil $timeout(function () { $scope.loadApiKey(); }); $timeout(function () { $scope.loadMetric(); }); - $scope.modifyAPIKey = function () { - $http.post(OC.generateUrl('/apps/weather/settings/apikey/set'), {'apikey': $scope.apiKey}). - then(function (r) { - if (r.data != null && !undef(r.data['set'])) { - $scope.loadCity($scope.domCity); - } - else { - $scope.settingError = 'Failed to set API key. Please contact your administrator'; - } - }, - function (r) { - if (r.status == 403) { - $scope.settingError = "This key doesn't work. Please provide a valid OpenWeatherMap API key"; - } - else { - $scope.settingError = g_error500; - } - }); - } - - $scope.loadApiKey = function () { - $http.get(OC.generateUrl('/apps/weather/settings/apikey/get')). - then(function (r) { - if (!undef(r.data['apikey'])) { - $scope.apiKey = r.data['apikey']; - } - }, - function (r) { - $scope.fatalError(); - }); - }; - $scope.mapMetric = function () { if ($scope.metric == 'kelvin') { $scope.metricRepresentation = '°K'; diff --git a/lib/Settings/AdminSettings.php b/lib/Settings/AdminSettings.php index 93358ce..b24466c 100644 --- a/lib/Settings/AdminSettings.php +++ b/lib/Settings/AdminSettings.php @@ -35,12 +35,11 @@ class AdminSettings implements ISettings { * @return TemplateResponse */ public function getForm() { - /*$params = [ + $params = [ "openweathermap_api_key" => $this->config->getAppValue('weather', 'openweathermap_api_key', ''), ]; return new TemplateResponse('weather', 'admin', $params); - */ return new TemplateResponse('weather', 'admin', []); } /** diff --git a/templates/admin.php b/templates/admin.php index d56b57f..7c4c11f 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -1,14 +1,19 @@ +

t('Weather')) ?>


- +

diff --git a/templates/main.php b/templates/main.php index 79f9640..ba1ea61 100644 --- a/templates/main.php +++ b/templates/main.php @@ -31,8 +31,6 @@