Refactor settings, create SettingsService

Fix settings not working in ownCloud
Signed-off-by: brantje <brantje@gmail.com>
This commit is contained in:
brantje 2016-12-30 12:51:32 +01:00
parent 9b83946f0b
commit 7bb1839184
No known key found for this signature in database
GPG key ID: 5FF1D117F918687F
13 changed files with 262 additions and 100 deletions

View file

@ -14,6 +14,7 @@ namespace OCA\Passman\AppInfo;
use OCP\Util; use OCP\Util;
use OCP\BackgroundJob; use OCP\BackgroundJob;
use OCP\App;
use OCA\Passman\Notifier; use OCA\Passman\Notifier;
use OCA\Passman\Activity; use OCA\Passman\Activity;
require_once __DIR__ . '/autoload.php'; require_once __DIR__ . '/autoload.php';
@ -49,3 +50,4 @@ $manager->registerExtension(function() {
* The string has to match the app's folder name * The string has to match the app's folder name
*/ */
Util::addTranslations('passman'); Util::addTranslations('passman');
\OCP\App::registerAdmin('passman', 'templates/admin.settings');

View file

@ -40,8 +40,9 @@ For an demo of this app visit [https://demo.passman.cc](https://demo.passman.cc)
<database>sqlite</database> <database>sqlite</database>
<database min-version="5.5">mysql</database> <database min-version="5.5">mysql</database>
</dependencies> </dependencies>
<settings> <settings>
<admin>OCA\Passman\Settings\Admin</admin> <admin>OCA\Passman\Controller\SettingsController</admin>
</settings> </settings>
<background-jobs> <background-jobs>

View file

@ -67,6 +67,11 @@ return [
['name' => 'share#updateSharedCredentialACL', 'url' => '/api/v2/sharing/credential/{item_guid}/acl', 'verb' => 'PATCH'], ['name' => 'share#updateSharedCredentialACL', 'url' => '/api/v2/sharing/credential/{item_guid}/acl', 'verb' => 'PATCH'],
['name' => 'internal#getAppVersion', 'url' => '/api/v2/version', 'verb' => 'GET'], ['name' => 'internal#getAppVersion', 'url' => '/api/v2/version', 'verb' => 'GET'],
//Settings
['name' => 'settings#getSettings', 'url' => '/api/v2/settings', 'verb' => 'GET'],
['name' => 'settings#saveUserSetting', 'url' => '/api/v2/settings/{key}/{value}', 'verb' => 'POST'],
['name' => 'settings#saveAdminSetting', 'url' => '/api/v2/settings/{key}/{value}/admin1/admin2', 'verb' => 'POST'],
//Translations //Translations
['name' => 'translation#getLanguageStrings', 'url' => '/api/v2/language', 'verb' => 'GET'], ['name' => 'translation#getLanguageStrings', 'url' => '/api/v2/language', 'verb' => 'GET'],
@ -76,8 +81,5 @@ return [
['name' => 'internal#read', 'url' => '/api/internal/notifications/read/{credential_id}', 'verb' => 'DELETE'], ['name' => 'internal#read', 'url' => '/api/internal/notifications/read/{credential_id}', 'verb' => 'DELETE'],
['name' => 'internal#getAppVersion', 'url' => '/api/internal/version', 'verb' => 'GET'], ['name' => 'internal#getAppVersion', 'url' => '/api/internal/version', 'verb' => 'GET'],
['name' => 'internal#generatePerson', 'url' => '/api/internal/generate_person', 'verb' => 'GET'], ['name' => 'internal#generatePerson', 'url' => '/api/internal/generate_person', 'verb' => 'GET'],
['name' => 'internal#save_settings', 'url' => '/api/internal/settings/{key}/{value}', 'verb' => 'POST'],
['name' => 'internal#get_settings', 'url' => '/api/internal/settings', 'verb' => 'GET'],
] ]
]; ];

View file

@ -11,13 +11,11 @@
namespace OCA\Passman\Controller; namespace OCA\Passman\Controller;
use OCA\Files_External\NotFoundException;
use OCA\Passman\Db\SharingACL; use OCA\Passman\Db\SharingACL;
use OCA\Passman\Service\SettingsService;
use OCA\Passman\Utility\NotFoundJSONResponse; use OCA\Passman\Utility\NotFoundJSONResponse;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IRequest; use OCP\IRequest;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\ApiController; use OCP\AppFramework\ApiController;
@ -26,7 +24,7 @@ use OCA\Passman\Activity;
use OCA\Passman\Service\ActivityService; use OCA\Passman\Service\ActivityService;
use OCA\Passman\Service\CredentialRevisionService; use OCA\Passman\Service\CredentialRevisionService;
use OCA\Passman\Service\ShareService; use OCA\Passman\Service\ShareService;
use OCP\IUser;
class CredentialController extends ApiController { class CredentialController extends ApiController {
private $userId; private $userId;
@ -34,7 +32,7 @@ class CredentialController extends ApiController {
private $activityService; private $activityService;
private $credentialRevisionService; private $credentialRevisionService;
private $sharingService; private $sharingService;
private $config; private $settings;
public function __construct($AppName, public function __construct($AppName,
IRequest $request, IRequest $request,
@ -43,7 +41,7 @@ class CredentialController extends ApiController {
ActivityService $activityService, ActivityService $activityService,
CredentialRevisionService $credentialRevisionService, CredentialRevisionService $credentialRevisionService,
ShareService $sharingService, ShareService $sharingService,
IConfig $config SettingsService $settings
) { ) {
parent::__construct($AppName, $request); parent::__construct($AppName, $request);
$this->userId = $userId; $this->userId = $userId;
@ -51,7 +49,7 @@ class CredentialController extends ApiController {
$this->activityService = $activityService; $this->activityService = $activityService;
$this->credentialRevisionService = $credentialRevisionService; $this->credentialRevisionService = $credentialRevisionService;
$this->sharingService = $sharingService; $this->sharingService = $sharingService;
$this->config = $config; $this->settings = $settings;
} }
@ -151,7 +149,7 @@ class CredentialController extends ApiController {
} else { } else {
return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED); return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED);
} }
if ($this->config->getAppValue('passman', 'user_sharing_enabled', 1) === 0 || $this->config->getAppValue('passman', 'user_sharing_enabled', 1) === '0') { if ($this->settings->isEnabled('user_sharing_enabled')) {
return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED); return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED);
} }
} }

View file

@ -0,0 +1,96 @@
<?php
/**
* Nextcloud - passman
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Sander Brand <brantje@gmail.com>
* @copyright Sander Brand 2016
*/
namespace OCA\Passman\Controller;
use OCP\IL10N;
use OCP\Settings\ISettings;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\ApiController;
use OCP\IRequest;
use OCA\Passman\Service\SettingsService;
class SettingsController extends ApiController {
private $userId;
private $settings;
public function __construct(
$AppName,
IRequest $request,
$userId,
SettingsService $settings,
IL10N $l) {
parent::__construct($AppName, $request);
$this->settings = $settings;
$this->l = $l;
$this->userId = $userId;
}
/**
* @return TemplateResponse
*/
public function getForm() {
return new TemplateResponse('passman', 'part.admin');
}
/**
* @return string the section ID, e.g. 'sharing'
*/
public function getSection() {
return 'additional';
}
/**
* @return int whether the form should be rather on the top or bottom of
* the admin section. The forms are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
*
* E.g.: 70
*/
public function getPriority() {
return 0;
}
/**
* Get all settings
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getSettings() {
$settings = $this->settings->getAppSettings();
return new JSONResponse($settings);
}
/**
* Save a user setting
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function saveUserSetting($key, $value) {
$this->settings->setUserSetting($key, $value);
return new JSONResponse('OK');
}
/**
* Save a app setting
*
* @NoCSRFRequired
*/
public function saveAdminSetting($key, $value) {
$this->settings->setAppSetting($key, $value);
return new JSONResponse('OK');
}
}

View file

@ -16,6 +16,7 @@ use OCA\Passman\Db\Vault;
use OCA\Passman\Service\CredentialService; use OCA\Passman\Service\CredentialService;
use OCA\Passman\Service\FileService; use OCA\Passman\Service\FileService;
use OCA\Passman\Service\NotificationService; use OCA\Passman\Service\NotificationService;
use OCA\Passman\Service\SettingsService;
use OCA\Passman\Service\ShareService; use OCA\Passman\Service\ShareService;
use OCA\Passman\Utility\NotFoundJSONResponse; use OCA\Passman\Utility\NotFoundJSONResponse;
use OCA\Passman\Utility\Utils; use OCA\Passman\Utility\Utils;
@ -44,7 +45,7 @@ class ShareController extends ApiController {
private $credentialService; private $credentialService;
private $notificationService; private $notificationService;
private $fileService; private $fileService;
private $config; private $settings;
private $limit = 50; private $limit = 50;
private $offset = 0; private $offset = 0;
@ -60,7 +61,7 @@ class ShareController extends ApiController {
CredentialService $credentialService, CredentialService $credentialService,
NotificationService $notificationService, NotificationService $notificationService,
FileService $fileService, FileService $fileService,
IConfig $config SettingsService $config
) { ) {
parent::__construct($AppName, $request); parent::__construct($AppName, $request);
@ -73,14 +74,9 @@ class ShareController extends ApiController {
$this->credentialService = $credentialService; $this->credentialService = $credentialService;
$this->notificationService = $notificationService; $this->notificationService = $notificationService;
$this->fileService = $fileService; $this->fileService = $fileService;
$this->config = $config; $this->settings = $config;
} }
private function isSharingEnabled() {
if ($this->config->getAppValue('passman', 'link_sharing_enabled', 1) === 0 || $this->config->getAppValue('passman', 'link_sharing_enabled', 1) === '0') {
return new JSONResponse(array());
}
}
/** /**
* @param $item_id * @param $item_id
@ -91,8 +87,10 @@ class ShareController extends ApiController {
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function createPublicShare($item_id, $item_guid, $permissions, $expire_timestamp, $expire_views) { public function createPublicShare($item_id, $item_guid, $permissions, $expire_timestamp, $expire_views) {
$this->isSharingEnabled();
if (!$this->settings->isEnabled('link_sharing_enabled')) {
return new JSONResponse(array());
}
try { try {
$credential = $this->credentialService->getCredentialByGUID($item_guid); $credential = $this->credentialService->getCredentialByGUID($item_guid);
@ -130,7 +128,9 @@ class ShareController extends ApiController {
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) { public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) {
$this->isSharingEnabled(); if (!$this->settings->isEnabled('user_sharing_enabled')) {
return new JSONResponse(array());
}
/** /**
* Assemble notification * Assemble notification
*/ */
@ -223,7 +223,9 @@ class ShareController extends ApiController {
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function unshareCredential($item_guid) { public function unshareCredential($item_guid) {
$this->isSharingEnabled(); if (!$this->settings->isEnabled('user_sharing_enabled')) {
return new JSONResponse(array());
}
$acl_list = $this->shareService->getCredentialAclList($item_guid); $acl_list = $this->shareService->getCredentialAclList($item_guid);
$request_list = $this->shareService->getShareRequestsByGuid($item_guid); $request_list = $this->shareService->getShareRequestsByGuid($item_guid);
foreach ($acl_list as $ACL) { foreach ($acl_list as $ACL) {
@ -338,6 +340,9 @@ class ShareController extends ApiController {
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function getPendingRequests() { public function getPendingRequests() {
if (!$this->settings->isEnabled('user_sharing_enabled')) {
return new JSONResponse(array());
}
try { try {
$requests = $this->shareService->getUserPendingRequests($this->userId->getUID()); $requests = $this->shareService->getUserPendingRequests($this->userId->getUID());
$results = array(); $results = array();
@ -374,7 +379,9 @@ class ShareController extends ApiController {
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function getVaultItems($vault_guid) { public function getVaultItems($vault_guid) {
$this->isSharingEnabled(); if (!$this->settings->isEnabled('user_sharing_enabled')) {
return new JSONResponse(array());
}
try { try {
return new JSONResponse($this->shareService->getSharedItems($this->userId->getUID(), $vault_guid)); return new JSONResponse($this->shareService->getSharedItems($this->userId->getUID(), $vault_guid));
@ -426,7 +433,9 @@ class ShareController extends ApiController {
* @PublicPage * @PublicPage
*/ */
public function getPublicCredentialData($credential_guid) { public function getPublicCredentialData($credential_guid) {
$this->isSharingEnabled(); if (!$this->settings->isEnabled('user_sharing_enabled')) {
return new JSONResponse(array());
}
//@TODO Check expire date //@TODO Check expire date
$acl = $this->shareService->getACL(null, $credential_guid); $acl = $this->shareService->getACL(null, $credential_guid);

View file

@ -38,7 +38,7 @@
defaultVaultPass: null defaultVaultPass: null
}; };
$http.get(OC.generateUrl('apps/passman/api/internal/settings')).then(function (response) { $http.get(OC.generateUrl('apps/passman/api/v2/settings')).then(function (response) {
if (response.data) { if (response.data) {
settings = angular.merge(settings, response.data); settings = angular.merge(settings, response.data);
$rootScope.$broadcast('settings_loaded'); $rootScope.$broadcast('settings_loaded');

View file

@ -60,7 +60,7 @@ $(document).ready(function () {
setAdminKey: function (key, value) { setAdminKey: function (key, value) {
var request = $.ajax({ var request = $.ajax({
url: this._baseUrl + '/' + key + '/' + value, url: this._baseUrl + '/' + key + '/' + value +'/admin1/admin2',
method: 'POST' method: 'POST'
}); });
request.done(function () { request.done(function () {
@ -84,7 +84,7 @@ $(document).ready(function () {
}; };
var settings = new Settings(OC.generateUrl('apps/passman/api/internal/settings')); var settings = new Settings(OC.generateUrl('apps/passman/api/v2/settings'));
settings.load(); settings.load();
// ADMIN SETTINGS // ADMIN SETTINGS
@ -121,4 +121,8 @@ $(document).ready(function () {
settings.setAdminKey('vault_key_strength', $(this).val()); settings.setAdminKey('vault_key_strength', $(this).val());
}); });
if($('form[name="passman_settings"]').length === 2){
$('form[name="passman_settings"]')[1].remove();
}
}); });

View file

@ -36,6 +36,7 @@ use OCA\Passman\Service\FileService;
use OCA\Passman\Service\VaultService; use OCA\Passman\Service\VaultService;
use OCA\Passman\Utility\Utils; use OCA\Passman\Utility\Utils;
use OCA\Passman\Service\NotificationService; use OCA\Passman\Service\NotificationService;
Use OCA\Passman\Service\SettingsService;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
@ -72,7 +73,7 @@ class Application extends App {
$c->query('CredentialService'), $c->query('CredentialService'),
$c->query('NotificationService'), $c->query('NotificationService'),
$c->query('FileService'), $c->query('FileService'),
$c->query('IConfig') $c->query('SettingsService')
); );
}); });
@ -112,6 +113,7 @@ class Application extends App {
$container->registerAlias('Utils', Utils::class); $container->registerAlias('Utils', Utils::class);
$container->registerAlias('IDBConnection', IDBConnection::class); $container->registerAlias('IDBConnection', IDBConnection::class);
$container->registerAlias('IConfig', IConfig::class); $container->registerAlias('IConfig', IConfig::class);
$container->registerAlias('SettingsService', SettingsService::class);
} }
/** /**

View file

@ -0,0 +1,114 @@
<?php
/**
* Nextcloud - passman
*
* @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Passman\Service;
use OCP\IConfig;
class SettingsService {
private $userId;
private $config;
private $appName;
public $settings;
private $numeric_settings = array(
'link_sharing_enabled',
'user_sharing_enabled',
'vault_key_strength',
'check_version',
'https_check',
'disable_contextmenu'
);
public function __construct($UserId, IConfig $config, $AppName) {
$this->userId = $UserId;
$this->config = $config;
$this->appName = $AppName;
}
/**
* Get all app settings
*
* @return array
*/
public function getAppSettings() {
$this->settings = array(
'link_sharing_enabled' => intval($this->config->getAppValue('passman', 'link_sharing_enabled', 1)),
'user_sharing_enabled' => intval($this->config->getAppValue('passman', 'user_sharing_enabled', 1)),
'vault_key_strength' => intval($this->config->getAppValue('passman', 'vault_key_strength', 3)),
'check_version' => intval($this->config->getAppValue('passman', 'check_version', 1)),
'https_check' => intval($this->config->getAppValue('passman', 'https_check', 1)),
'disable_contextmenu' => intval($this->config->getAppValue('passman', 'disable_contextmenu', 1)),
);
return $this->settings;
}
/**
* Get a app setting
*
* @param $key string
* @param null $default_value The default value if key does not exist
* @return mixed
*/
public function getAppSetting($key, $default_value = null) {
$value = $this->config->getAppValue('passman', $key, $default_value);
if (in_array($key, $this->numeric_settings)) {
$value = intval($value);
}
return $value;
}
/**
* Set a app setting
*
* @param $key string Setting name
* @param $value mixed Value of the setting
*/
public function setAppSetting($key, $value) {
$this->config->setAppValue('passman', $key, $value);
}
/**
* Set a user setting
*
* @param $key string Setting name
* @param $value mixed Value of the setting
*/
public function setUserSetting($key, $value){
return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
}
/**
* Check if an setting is enabled (value of 1)
*
* @param $setting
* @return bool
*/
public function isEnabled($setting){
$value = intval($this->config->getAppValue('passman', $setting, false));
return ($value === 1);
}
}

View file

@ -1,70 +0,0 @@
<?php
/**
* Nextcloud - passman
*
* @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Passman\Settings;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\Settings\ISettings;
class Admin implements ISettings {
private $config;
private $l;
public function __construct(
IConfig $config,
IL10N $l) {
$this->config = $config;
$this->l = $l;
}
/**
* @return TemplateResponse
*/
public function getForm() {
return new TemplateResponse('passman', 'settings-admin');
}
/**
* @return string the section ID, e.g. 'sharing'
*/
public function getSection() {
return 'additional';
}
/**
* @return int whether the form should be rather on the top or bottom of
* the admin section. The forms are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
*
* E.g.: 70
*/
public function getPriority() {
return 0;
}
}

View file

@ -0,0 +1,4 @@
<?php
namespace OCA\Passman;
$tmpl = new \OCP\Template('passman', 'part.admin');
return $tmpl->fetchPage();

View file

@ -25,7 +25,7 @@ if ($checkVersion) {
} }
?> ?>
<div id="passwordSharingSettings" class="followupsection"> <div id="passwordSharingSettings" class="followup section">
<form name="passman_settings"> <form name="passman_settings">
<h2><?php p($l->t('Passman Settings')); ?></h2> <h2><?php p($l->t('Passman Settings')); ?></h2>
<?php <?php