diff --git a/appinfo/app.php b/appinfo/app.php index 6e415c3b..1403c4ae 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -14,6 +14,7 @@ namespace OCA\Passman\AppInfo; use OCP\Util; use OCP\BackgroundJob; +use OCP\App; use OCA\Passman\Notifier; use OCA\Passman\Activity; require_once __DIR__ . '/autoload.php'; @@ -49,3 +50,4 @@ $manager->registerExtension(function() { * The string has to match the app's folder name */ Util::addTranslations('passman'); +\OCP\App::registerAdmin('passman', 'templates/admin.settings'); \ No newline at end of file diff --git a/appinfo/info.xml b/appinfo/info.xml index 550fbc66..f700dc9f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -40,8 +40,9 @@ For an demo of this app visit [https://demo.passman.cc](https://demo.passman.cc) sqlite mysql + - OCA\Passman\Settings\Admin + OCA\Passman\Controller\SettingsController diff --git a/appinfo/routes.php b/appinfo/routes.php index 8a90558c..5413b79f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -67,6 +67,11 @@ return [ ['name' => 'share#updateSharedCredentialACL', 'url' => '/api/v2/sharing/credential/{item_guid}/acl', 'verb' => 'PATCH'], ['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 ['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#getAppVersion', 'url' => '/api/internal/version', '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'], - ] ]; \ No newline at end of file diff --git a/controller/credentialcontroller.php b/controller/credentialcontroller.php index 2ab356d3..51645fb7 100644 --- a/controller/credentialcontroller.php +++ b/controller/credentialcontroller.php @@ -11,13 +11,11 @@ namespace OCA\Passman\Controller; -use OCA\Files_External\NotFoundException; use OCA\Passman\Db\SharingACL; +use OCA\Passman\Service\SettingsService; use OCA\Passman\Utility\NotFoundJSONResponse; -use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCP\IConfig; use OCP\IRequest; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\ApiController; @@ -26,7 +24,7 @@ use OCA\Passman\Activity; use OCA\Passman\Service\ActivityService; use OCA\Passman\Service\CredentialRevisionService; use OCA\Passman\Service\ShareService; -use OCP\IUser; + class CredentialController extends ApiController { private $userId; @@ -34,7 +32,7 @@ class CredentialController extends ApiController { private $activityService; private $credentialRevisionService; private $sharingService; - private $config; + private $settings; public function __construct($AppName, IRequest $request, @@ -43,7 +41,7 @@ class CredentialController extends ApiController { ActivityService $activityService, CredentialRevisionService $credentialRevisionService, ShareService $sharingService, - IConfig $config + SettingsService $settings ) { parent::__construct($AppName, $request); $this->userId = $userId; @@ -51,7 +49,7 @@ class CredentialController extends ApiController { $this->activityService = $activityService; $this->credentialRevisionService = $credentialRevisionService; $this->sharingService = $sharingService; - $this->config = $config; + $this->settings = $settings; } @@ -151,7 +149,7 @@ class CredentialController extends ApiController { } else { 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); } } diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php new file mode 100644 index 00000000..50a2ac7c --- /dev/null +++ b/controller/settingscontroller.php @@ -0,0 +1,96 @@ + + * @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'); + } + +} \ No newline at end of file diff --git a/controller/sharecontroller.php b/controller/sharecontroller.php index f8c137c2..38121e6b 100644 --- a/controller/sharecontroller.php +++ b/controller/sharecontroller.php @@ -16,6 +16,7 @@ use OCA\Passman\Db\Vault; use OCA\Passman\Service\CredentialService; use OCA\Passman\Service\FileService; use OCA\Passman\Service\NotificationService; +use OCA\Passman\Service\SettingsService; use OCA\Passman\Service\ShareService; use OCA\Passman\Utility\NotFoundJSONResponse; use OCA\Passman\Utility\Utils; @@ -44,7 +45,7 @@ class ShareController extends ApiController { private $credentialService; private $notificationService; private $fileService; - private $config; + private $settings; private $limit = 50; private $offset = 0; @@ -60,7 +61,7 @@ class ShareController extends ApiController { CredentialService $credentialService, NotificationService $notificationService, FileService $fileService, - IConfig $config + SettingsService $config ) { parent::__construct($AppName, $request); @@ -73,14 +74,9 @@ class ShareController extends ApiController { $this->credentialService = $credentialService; $this->notificationService = $notificationService; $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 @@ -91,8 +87,10 @@ class ShareController extends ApiController { * @NoCSRFRequired */ 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 { $credential = $this->credentialService->getCredentialByGUID($item_guid); @@ -130,7 +128,9 @@ class ShareController extends ApiController { * @NoCSRFRequired */ public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) { - $this->isSharingEnabled(); + if (!$this->settings->isEnabled('user_sharing_enabled')) { + return new JSONResponse(array()); + } /** * Assemble notification */ @@ -223,7 +223,9 @@ class ShareController extends ApiController { * @NoCSRFRequired */ 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); $request_list = $this->shareService->getShareRequestsByGuid($item_guid); foreach ($acl_list as $ACL) { @@ -338,6 +340,9 @@ class ShareController extends ApiController { * @NoCSRFRequired */ public function getPendingRequests() { + if (!$this->settings->isEnabled('user_sharing_enabled')) { + return new JSONResponse(array()); + } try { $requests = $this->shareService->getUserPendingRequests($this->userId->getUID()); $results = array(); @@ -374,7 +379,9 @@ class ShareController extends ApiController { * @NoCSRFRequired */ public function getVaultItems($vault_guid) { - $this->isSharingEnabled(); + if (!$this->settings->isEnabled('user_sharing_enabled')) { + return new JSONResponse(array()); + } try { return new JSONResponse($this->shareService->getSharedItems($this->userId->getUID(), $vault_guid)); @@ -426,7 +433,9 @@ class ShareController extends ApiController { * @PublicPage */ public function getPublicCredentialData($credential_guid) { - $this->isSharingEnabled(); + if (!$this->settings->isEnabled('user_sharing_enabled')) { + return new JSONResponse(array()); + } //@TODO Check expire date $acl = $this->shareService->getACL(null, $credential_guid); diff --git a/js/app/services/settingsservice.js b/js/app/services/settingsservice.js index 16cf4d29..7db91069 100644 --- a/js/app/services/settingsservice.js +++ b/js/app/services/settingsservice.js @@ -38,7 +38,7 @@ 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) { settings = angular.merge(settings, response.data); $rootScope.$broadcast('settings_loaded'); diff --git a/js/settings-admin.js b/js/settings-admin.js index 4adb8bb1..960753b2 100644 --- a/js/settings-admin.js +++ b/js/settings-admin.js @@ -60,7 +60,7 @@ $(document).ready(function () { setAdminKey: function (key, value) { var request = $.ajax({ - url: this._baseUrl + '/' + key + '/' + value, + url: this._baseUrl + '/' + key + '/' + value +'/admin1/admin2', method: 'POST' }); 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(); // ADMIN SETTINGS @@ -121,4 +121,8 @@ $(document).ready(function () { settings.setAdminKey('vault_key_strength', $(this).val()); }); + if($('form[name="passman_settings"]').length === 2){ + $('form[name="passman_settings"]')[1].remove(); + } + }); diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index ca9cdc3a..711365ba 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -36,6 +36,7 @@ use OCA\Passman\Service\FileService; use OCA\Passman\Service\VaultService; use OCA\Passman\Utility\Utils; use OCA\Passman\Service\NotificationService; +Use OCA\Passman\Service\SettingsService; use OCP\IConfig; use OCP\IDBConnection; @@ -72,7 +73,7 @@ class Application extends App { $c->query('CredentialService'), $c->query('NotificationService'), $c->query('FileService'), - $c->query('IConfig') + $c->query('SettingsService') ); }); @@ -112,6 +113,7 @@ class Application extends App { $container->registerAlias('Utils', Utils::class); $container->registerAlias('IDBConnection', IDBConnection::class); $container->registerAlias('IConfig', IConfig::class); + $container->registerAlias('SettingsService', SettingsService::class); } /** diff --git a/lib/Service/SettingsService.php b/lib/Service/SettingsService.php new file mode 100644 index 00000000..c61af779 --- /dev/null +++ b/lib/Service/SettingsService.php @@ -0,0 +1,114 @@ +. + * + */ + +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); + } +} \ No newline at end of file diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php deleted file mode 100644 index 90f27ab4..00000000 --- a/lib/Settings/Admin.php +++ /dev/null @@ -1,70 +0,0 @@ -. - * - */ - -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; - } - -} diff --git a/templates/admin.settings.php b/templates/admin.settings.php new file mode 100644 index 00000000..b5280153 --- /dev/null +++ b/templates/admin.settings.php @@ -0,0 +1,4 @@ +fetchPage(); \ No newline at end of file diff --git a/templates/settings-admin.php b/templates/part.admin.php similarity index 97% rename from templates/settings-admin.php rename to templates/part.admin.php index 4ca60bf0..ac5f4d2e 100644 --- a/templates/settings-admin.php +++ b/templates/part.admin.php @@ -25,7 +25,7 @@ if ($checkVersion) { } ?> - + t('Passman Settings')); ?>