Implement API Key setting into admin (WIP)

master
Loic Blot 2017-07-03 22:13:17 +02:00
parent 229f28b8af
commit 3aa384b82d
6 changed files with 10 additions and 51 deletions

View File

@ -23,8 +23,7 @@ $application->registerRoutes($this, array('routes' => array(
array('name' => 'weather#get', 'url' => '/weather/get', 'verb' => 'GET'), array('name' => 'weather#get', 'url' => '/weather/get', 'verb' => 'GET'),
array('name' => 'settings#homeset', 'url' => '/settings/home/set', 'verb' => 'POST'), array('name' => 'settings#homeset', 'url' => '/settings/home/set', 'verb' => 'POST'),
array('name' => 'settings#apikeyset', 'url' => '/settings/apikey/set','verb' => 'POST'), array('name' => 'settings#apikeyset', 'url' => '/settings/apikey', 'verb' => 'POST'),
array('name' => 'settings#apikeyget', 'url' => '/settings/apikey/get','verb' => 'GET'),
array('name' => 'settings#metricset', 'url' => '/settings/metric/set','verb' => 'POST'), array('name' => 'settings#metricset', 'url' => '/settings/metric/set','verb' => 'POST'),
array('name' => 'settings#metricget', 'url' => '/settings/metric/get','verb' => 'GET'), array('name' => 'settings#metricget', 'url' => '/settings/metric/get','verb' => 'GET'),
))); )));

View File

@ -24,6 +24,7 @@ class SettingsController extends Controller {
private $userId; private $userId;
private $mapper; private $mapper;
private $cityMapper;
public function __construct ($appName, IRequest $request, $userId, SettingsMapper $mapper, CityMapper $cityMapper) { public function __construct ($appName, IRequest $request, $userId, SettingsMapper $mapper, CityMapper $cityMapper) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
@ -50,23 +51,13 @@ class SettingsController extends Controller {
} }
/** /**
* @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function apiKeySet ($apikey) { 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)); return new JSONResponse(array("set" => true));
} }
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function apiKeyGet () {
return new JSONResponse(array("apikey" => $this->mapper->getApiKey($this->userId)));
}
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired

View File

@ -24,7 +24,6 @@ function emptyStr (obj) {
app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compile', '$http', app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compile', '$http',
function ($scope, $interval, $timeout, $compile, $http) { function ($scope, $interval, $timeout, $compile, $http) {
$scope.owncloudAppImgPath = ''; $scope.owncloudAppImgPath = '';
$scope.apiKey = '';
$scope.userId = ''; $scope.userId = '';
$scope.metric = 'metric'; $scope.metric = 'metric';
$scope.metricRepresentation = '°C'; $scope.metricRepresentation = '°C';
@ -66,38 +65,6 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$timeout(function () { $scope.loadApiKey(); }); $timeout(function () { $scope.loadApiKey(); });
$timeout(function () { $scope.loadMetric(); }); $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 () { $scope.mapMetric = function () {
if ($scope.metric == 'kelvin') { if ($scope.metric == 'kelvin') {
$scope.metricRepresentation = '°K'; $scope.metricRepresentation = '°K';

View File

@ -35,12 +35,11 @@ class AdminSettings implements ISettings {
* @return TemplateResponse * @return TemplateResponse
*/ */
public function getForm() { public function getForm() {
/*$params = [ $params = [
"openweathermap_api_key" => $this->config->getAppValue('weather', 'openweathermap_api_key', ''), "openweathermap_api_key" => $this->config->getAppValue('weather', 'openweathermap_api_key', ''),
]; ];
return new TemplateResponse('weather', 'admin', $params); return new TemplateResponse('weather', 'admin', $params);
*/ return new TemplateResponse('weather', 'admin', []);
} }
/** /**

View File

@ -1,14 +1,19 @@
<?php <?php
\OCP\Util::addScript('weather', 'admin');
/** @var $l \OCP\IL10N */ /** @var $l \OCP\IL10N */
/** @var $_ array */ /** @var $_ array */
?> ?>
<div id="weather" class="section"> <div id="weather" class="section">
<h2><?php p($l->t('Weather')) ?></h2> <h2><?php p($l->t('Weather')) ?></h2>
<p> <p>
<label for="openweathermap-api-key"><?php p($l->t('OpenWeatherMap API Key')) ?></label> <label for="openweathermap-api-key"><?php p($l->t('OpenWeatherMap API Key')) ?></label>
<br /> <br />
<input id="openweathermap-api-key" type="text" value="<?php p($_['openweathermap_api_key']) ?>" /> <input id="openweathermap-api-key" type="text" value="<?php p($_['openweathermap_api_key']) ?>" />
<input type="submit" id="submitOWMKey" value="<?php p($l->t('Save')); ?>"/> <input type="submit" id="submitOWMApiKey" value="<?php p($l->t('Save')); ?>"/>
</p> </p>
</div> </div>

View File

@ -31,8 +31,6 @@
<button name="app settings" class="settings-button" data-apps-slide-toggle="#app-settings-content">Settings</button> <button name="app settings" class="settings-button" data-apps-slide-toggle="#app-settings-content">Settings</button>
</div> </div>
<div style="display: none;" id="app-settings-content"> <div style="display: none;" id="app-settings-content">
<h2>OpenWeatherMap API Key</h2>
<input type="text" name="apikey" ng-change="modifyAPIKey()" ng-model="apiKey" ng-model-options="{debounce:1000}" />
<h2>Metric</h2> <h2>Metric</h2>
<select name="metric" ng-change="modifyMetric()" ng-model="metric"> <select name="metric" ng-change="modifyMetric()" ng-model="metric">
<option value="metric">°C</option> <option value="metric">°C</option>