From 7d0102bf7302a483209e0d1c926260713f0e56c6 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 1 Dec 2017 10:36:13 +0100 Subject: expose capabilities in js Signed-off-by: Bjoern Schiessle --- core/Controller/OCJSController.php | 8 ++++++-- core/js/js.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/Controller/OCJSController.php b/core/Controller/OCJSController.php index 8db26dd3d38..37fe17c8dd1 100644 --- a/core/Controller/OCJSController.php +++ b/core/Controller/OCJSController.php @@ -26,6 +26,7 @@ namespace OC\Core\Controller; use bantu\IniGetWrapper\IniGetWrapper; +use OC\CapabilitiesManager; use OC\Template\JSConfigHelper; use OCP\App\IAppManager; use OCP\AppFramework\Controller; @@ -59,6 +60,7 @@ class OCJSController extends Controller { * @param IGroupManager $groupManager * @param IniGetWrapper $iniWrapper * @param IURLGenerator $urlGenerator + * @param CapabilitiesManager $capabilitiesManager */ public function __construct($appName, IRequest $request, @@ -70,7 +72,8 @@ class OCJSController extends Controller { IConfig $config, IGroupManager $groupManager, IniGetWrapper $iniWrapper, - IURLGenerator $urlGenerator) { + IURLGenerator $urlGenerator, + CapabilitiesManager $capabilitiesManager) { parent::__construct($appName, $request); $this->helper = new JSConfigHelper( @@ -82,7 +85,8 @@ class OCJSController extends Controller { $config, $groupManager, $iniWrapper, - $urlGenerator + $urlGenerator, + $capabilitiesManager ); } diff --git a/core/js/js.js b/core/js/js.js index 9b8829de250..cefe1d92a0f 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -80,6 +80,13 @@ var OCP = {}, */ webroot:oc_webroot, + /** + * Capabilities + * + * @type array + */ + _capabilities: window.oc_capabilities || null, + appswebroots:(typeof oc_appswebroots !== 'undefined') ? oc_appswebroots:false, /** * Currently logged in user or null if none @@ -308,6 +315,18 @@ var OCP = {}, return OC.webroot; }, + + /** + * Returns the capabilities + * + * @return {array} capabilities + * + * @since 13.0 + */ + getCapabilities: function() { + return OC._capabilities; + }, + /** * Returns the currently logged in user or null if there is no logged in * user (public page mode) -- cgit v1.2.3 From 1615312bf1044dcbd98c7e4739467314ada618cf Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 1 Dec 2017 11:35:01 +0100 Subject: add share permissions to settings page Signed-off-by: Bjoern Schiessle --- apps/files_sharing/lib/Capabilities.php | 2 + core/js/shareitemmodel.js | 17 ++++---- lib/private/Settings/Admin/Sharing.php | 63 ++++++++++++++++++++------- lib/private/Settings/Manager.php | 2 +- settings/css/settings.scss | 7 +++ settings/js/admin.js | 22 ++++++++++ settings/templates/settings/admin/sharing.php | 12 +++++ tests/lib/Settings/Admin/SharingTest.php | 7 ++- tests/lib/Settings/ManagerTest.php | 2 +- 9 files changed, 107 insertions(+), 27 deletions(-) (limited to 'core') diff --git a/apps/files_sharing/lib/Capabilities.php b/apps/files_sharing/lib/Capabilities.php index af41add250c..ce10c8df8a1 100644 --- a/apps/files_sharing/lib/Capabilities.php +++ b/apps/files_sharing/lib/Capabilities.php @@ -23,6 +23,7 @@ namespace OCA\Files_Sharing; use OCP\Capabilities\ICapability; +use OCP\Constants; use \OCP\IConfig; /** @@ -86,6 +87,7 @@ class Capabilities implements ICapability { $res['group'] = []; $res['group']['enabled'] = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; $res['group']['expire_date']['enabled'] = true; + $res['default_permissions'] = (int)$this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL); } //Federated sharing diff --git a/core/js/shareitemmodel.js b/core/js/shareitemmodel.js index b699513c734..e7824aca33a 100644 --- a/core/js/shareitemmodel.js +++ b/core/js/shareitemmodel.js @@ -158,23 +158,24 @@ var shareType = attributes.shareType; attributes = _.extend({}, attributes); - // Default permissions are Edit (CRUD) and Share - // Check if these permissions are possible - var permissions = OC.PERMISSION_READ; + // get default permissions + var defaultPermissions = OC.getCapabilities()['files_sharing']['default_permissions'] || OC.PERMISSION_ALL; + var possiblePermissions = OC.PERMISSION_READ; + if (this.updatePermissionPossible()) { - permissions = permissions | OC.PERMISSION_UPDATE; + possiblePermissions = possiblePermissions | OC.PERMISSION_UPDATE; } if (this.createPermissionPossible()) { - permissions = permissions | OC.PERMISSION_CREATE; + possiblePermissions = possiblePermissions | OC.PERMISSION_CREATE; } if (this.deletePermissionPossible()) { - permissions = permissions | OC.PERMISSION_DELETE; + possiblePermissions = possiblePermissions | OC.PERMISSION_DELETE; } if (this.configModel.get('isResharingAllowed') && (this.sharePermissionPossible())) { - permissions = permissions | OC.PERMISSION_SHARE; + possiblePermissions = possiblePermissions | OC.PERMISSION_SHARE; } - attributes.permissions = permissions; + attributes.permissions = defaultPermissions & possiblePermissions; if (_.isUndefined(attributes.path)) { attributes.path = this.fileInfoModel.getFullPath(); } diff --git a/lib/private/Settings/Admin/Sharing.php b/lib/private/Settings/Admin/Sharing.php index dbdacf78dab..7b60efdc67b 100644 --- a/lib/private/Settings/Admin/Sharing.php +++ b/lib/private/Settings/Admin/Sharing.php @@ -28,7 +28,9 @@ namespace OC\Settings\Admin; use OC\Share\Share; use OCP\AppFramework\Http\TemplateResponse; +use OCP\Constants; use OCP\IConfig; +use OCP\IL10N; use OCP\Settings\ISettings; use OCP\Util; @@ -36,11 +38,15 @@ class Sharing implements ISettings { /** @var IConfig */ private $config; + /** @var IL10N */ + private $l; + /** * @param IConfig $config */ - public function __construct(IConfig $config) { + public function __construct(IConfig $config, IL10N $l) { $this->config = $config; + $this->l = $l; } /** @@ -51,23 +57,48 @@ class Sharing implements ISettings { $excludeGroupsList = !is_null(json_decode($excludedGroups)) ? implode('|', json_decode($excludedGroups, true)) : ''; + $permList = [ + [ + 'id' => 'cancreate', + 'label' => $this->l->t('Create'), + 'value' => Constants::PERMISSION_CREATE + ], + [ + 'id' => 'canupdate', + 'label' => $this->l->t('Change'), + 'value' => Constants::PERMISSION_UPDATE + ], + [ + 'id' => 'candelete', + 'label' => $this->l->t('Delete'), + 'value' => Constants::PERMISSION_DELETE + ], + [ + 'id' => 'canshare', + 'label' => $this->l->t('Share'), + 'value' => Constants::PERMISSION_SHARE + ], + ]; + $parameters = [ // Built-In Sharing - 'allowGroupSharing' => $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes'), - 'allowLinks' => $this->config->getAppValue('core', 'shareapi_allow_links', 'yes'), - 'allowPublicUpload' => $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes'), - 'allowResharing' => $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes'), - 'allowShareDialogUserEnumeration' => $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'), - 'enforceLinkPassword' => Util::isPublicLinkPasswordRequired(), - 'onlyShareWithGroupMembers' => Share::shareWithGroupMembersOnly(), - 'shareAPIEnabled' => $this->config->getAppValue('core', 'shareapi_enabled', 'yes'), - 'shareDefaultExpireDateSet' => $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no'), - 'shareExpireAfterNDays' => $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'), - 'shareEnforceExpireDate' => $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no'), - 'shareExcludeGroups' => $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes', - 'shareExcludedGroupsList' => $excludeGroupsList, - 'publicShareDisclaimerText' => $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null), - 'enableLinkPasswordByDefault' => $this->config->getAppValue('core', 'shareapi_enable_link_password_by_default', 'no'), + 'allowGroupSharing' => $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes'), + 'allowLinks' => $this->config->getAppValue('core', 'shareapi_allow_links', 'yes'), + 'allowPublicUpload' => $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes'), + 'allowResharing' => $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes'), + 'allowShareDialogUserEnumeration' => $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'), + 'enforceLinkPassword' => Util::isPublicLinkPasswordRequired(), + 'onlyShareWithGroupMembers' => Share::shareWithGroupMembersOnly(), + 'shareAPIEnabled' => $this->config->getAppValue('core', 'shareapi_enabled', 'yes'), + 'shareDefaultExpireDateSet' => $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no'), + 'shareExpireAfterNDays' => $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'), + 'shareEnforceExpireDate' => $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no'), + 'shareExcludeGroups' => $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes', + 'shareExcludedGroupsList' => $excludeGroupsList, + 'publicShareDisclaimerText' => $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null), + 'enableLinkPasswordByDefault' => $this->config->getAppValue('core', 'shareapi_enable_link_password_by_default', 'no'), + 'shareApiDefaultPermissions' => $this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL), + 'shareApiDefaultPermissionsCheckboxes' => $permList, ]; return new TemplateResponse('settings', 'settings/admin/sharing', $parameters, ''); diff --git a/lib/private/Settings/Manager.php b/lib/private/Settings/Manager.php index 387460852ab..7111511eca2 100644 --- a/lib/private/Settings/Manager.php +++ b/lib/private/Settings/Manager.php @@ -271,7 +271,7 @@ class Manager implements IManager { } if ($section === 'sharing') { /** @var ISettings $form */ - $form = new Admin\Sharing($this->config); + $form = new Admin\Sharing($this->config, $this->l); $forms[$form->getPriority()] = [$form]; } if ($section === 'additional') { diff --git a/settings/css/settings.scss b/settings/css/settings.scss index 05d62423d17..611961b1065 100644 --- a/settings/css/settings.scss +++ b/settings/css/settings.scss @@ -1030,6 +1030,13 @@ table.grid td.date { .double-indent { padding-left: 56px; } + .nocheckbox { + padding-left: 20px; + } +} + +#shareApiDefaultPermissionsSection label { + margin-right: 20px; } #fileSharingSettings h3 { diff --git a/settings/js/admin.js b/settings/js/admin.js index 177f6d3f7f4..f637cb64592 100644 --- a/settings/js/admin.js +++ b/settings/js/admin.js @@ -121,6 +121,28 @@ $(document).ready(function(){ } }); + $('#shareApiDefaultPermissionsSection input').change(function(ev) { + var $el = $('#shareApiDefaultPermissions'); + var $target = $(ev.target); + + var value = $el.val(); + if ($target.is(':checked')) { + value = value | $target.val(); + } else { + value = value & ~$target.val(); + } + + // always set read permission + value |= OC.PERMISSION_READ; + + // this will trigger the field's change event and will save it + $el.val(value).change(); + + ev.preventDefault(); + + return false; + }); + var savePublicShareDisclaimerText = _.debounce(function(value) { var options = { success: function() { diff --git a/settings/templates/settings/admin/sharing.php b/settings/templates/settings/admin/sharing.php index 156e8ddd81d..1f8e72910c4 100644 --- a/settings/templates/settings/admin/sharing.php +++ b/settings/templates/settings/admin/sharing.php @@ -78,6 +78,18 @@ value="1" />

+

+ + t('Default user and group share permissions'));?> +

+

+ + /> + + +

/> diff --git a/tests/lib/Settings/Admin/SharingTest.php b/tests/lib/Settings/Admin/SharingTest.php index 9498a1466d3..ee60979b96d 100644 --- a/tests/lib/Settings/Admin/SharingTest.php +++ b/tests/lib/Settings/Admin/SharingTest.php @@ -26,6 +26,7 @@ namespace Test\Settings\Admin; use OC\Settings\Admin\Sharing; use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; +use OCP\IL10N; use Test\TestCase; class SharingTest extends TestCase { @@ -33,13 +34,17 @@ class SharingTest extends TestCase { private $admin; /** @var IConfig */ private $config; + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + private $l10n; public function setUp() { parent::setUp(); $this->config = $this->getMockBuilder(IConfig::class)->getMock(); + $this->l10n = $this->getMockBuilder(IL10N::class)->getMock(); $this->admin = new Sharing( - $this->config + $this->config, + $this->l10n ); } diff --git a/tests/lib/Settings/ManagerTest.php b/tests/lib/Settings/ManagerTest.php index 577abc7915c..b218a347e85 100644 --- a/tests/lib/Settings/ManagerTest.php +++ b/tests/lib/Settings/ManagerTest.php @@ -209,7 +209,7 @@ class ManagerTest extends TestCase { public function testGetAdminSettings() { $this->assertEquals([ - 0 => [new Sharing($this->config)], + 0 => [new Sharing($this->config, $this->l10n)], ], $this->manager->getAdminSettings('sharing')); } -- cgit v1.2.3 From 7466468af1a4c830c7bb825fda141a336d541b0d Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 1 Sep 2017 12:51:49 +0200 Subject: Fix share capabilities JS tests Signed-off-by: Bjoern Schiessle --- core/js/tests/specs/shareitemmodelSpec.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/js/tests/specs/shareitemmodelSpec.js b/core/js/tests/specs/shareitemmodelSpec.js index 3b17051508e..0a345786b73 100644 --- a/core/js/tests/specs/shareitemmodelSpec.js +++ b/core/js/tests/specs/shareitemmodelSpec.js @@ -25,6 +25,7 @@ describe('OC.Share.ShareItemModel', function() { var fetchSharesDeferred, fetchReshareDeferred; var fileInfoModel, configModel, model; var oldCurrentUser; + var capsSpec; beforeEach(function() { oldCurrentUser = OC.currentUser; @@ -56,8 +57,15 @@ describe('OC.Share.ShareItemModel', function() { configModel: configModel, fileInfoModel: fileInfoModel }); + capsSpec = sinon.stub(OC, 'getCapabilities'); + capsSpec.returns({ + 'files_sharing': { + 'default_permissions': OC.PERMISSION_ALL + } + }); }); afterEach(function() { + capsSpec.restore(); if (fetchSharesStub) { fetchSharesStub.restore(); } @@ -527,7 +535,22 @@ describe('OC.Share.ShareItemModel', function() { }); expect( testWithPermissions(OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE) - ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_UPDATE); + ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE); + }); + it('uses default permissions from capabilities', function() { + capsSpec.returns({ + 'files_sharing': { + 'default_permissions': OC.PERMISSION_READ | OC.PERMISSION_CREATE | OC.PERMISSION_SHARE + } + }); + configModel.set('isResharingAllowed', true); + model.set({ + reshare: {}, + shares: [] + }); + expect( + testWithPermissions(OC.PERMISSION_ALL) + ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_CREATE | OC.PERMISSION_SHARE); }); }); }); -- cgit v1.2.3 From 20ec0344a26faf725762e3b344b66bb45ef1a5a2 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Thu, 15 Feb 2018 11:06:51 +0100 Subject: Fix JSDoc Signed-off-by: Morris Jobke --- core/js/js.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/js/js.js b/core/js/js.js index cefe1d92a0f..fa92508ff7a 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -321,7 +321,7 @@ var OCP = {}, * * @return {array} capabilities * - * @since 13.0 + * @since 14.0 */ getCapabilities: function() { return OC._capabilities; -- cgit v1.2.3