Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Default share perms #28903

Merged
merged 8 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/files_sharing/lib/API/OCSShareWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ private function getShare20OCS() {
\OC::$server->getRootFolder(),
\OC::$server->getURLGenerator(),
\OC::$server->getUserSession()->getUser(),
\OC::$server->getL10N('files_sharing')
\OC::$server->getL10N('files_sharing'),
\OC::$server->getConfig()
);
}

Expand Down
20 changes: 16 additions & 4 deletions apps/files_sharing/lib/API/Share20OCS.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Lock\ILockingProvider;
use OCP\IConfig;

/**
* Class Share20OCS
Expand All @@ -60,6 +61,8 @@ class Share20OCS {
private $currentUser;
/** @var IL10N */
private $l;
/** @var IConfig */
private $config;

/**
* Share20OCS constructor.
Expand All @@ -71,6 +74,8 @@ class Share20OCS {
* @param IRootFolder $rootFolder
* @param IURLGenerator $urlGenerator
* @param IUser $currentUser
* @param IL10N $l10n
* @param IConfig $config
*/
public function __construct(
IManager $shareManager,
Expand All @@ -80,7 +85,8 @@ public function __construct(
IRootFolder $rootFolder,
IURLGenerator $urlGenerator,
IUser $currentUser,
IL10N $l10n
IL10N $l10n,
IConfig $config
) {
$this->shareManager = $shareManager;
$this->userManager = $userManager;
Expand All @@ -90,6 +96,7 @@ public function __construct(
$this->urlGenerator = $urlGenerator;
$this->currentUser = $currentUser;
$this->l = $l10n;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -270,10 +277,17 @@ public function createShare() {
return new \OC\OCS\Result(null, 404, 'Could not create share');
}

$shareType = (int)$this->request->getParam('shareType', '-1');

// Parse permissions (if available)
$permissions = $this->request->getParam('permissions', null);
if ($permissions === null) {
$permissions = \OCP\Constants::PERMISSION_ALL;
if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) {
$permissions = $this->config->getAppValue('core', 'shareapi_default_permissions', \OCP\Constants::PERMISSION_ALL);
$permissions |= \OCP\Constants::PERMISSION_READ;
} else {
$permissions = \OCP\Constants::PERMISSION_ALL;
}
} else {
$permissions = (int)$permissions;
}
Expand All @@ -287,8 +301,6 @@ public function createShare() {
return new \OC\OCS\Result(null, 400, $this->l->t('Cannot remove all permissions'));
}

$shareType = (int)$this->request->getParam('shareType', '-1');

// link shares can have create-only without read (anonymous upload)
if ($shareType !== \OCP\Share::SHARE_TYPE_LINK && $permissions !== \OCP\Constants::PERMISSION_CREATE) {
// Shares always require read permissions
Expand Down
2 changes: 2 additions & 0 deletions apps/files_sharing/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function getCapabilities() {
$res['resharing'] = $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes') === 'yes';

$res['group_sharing'] = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';

$res['default_permissions'] = (int)$this->config->getAppValue('core', 'shareapi_default_permissions', \OCP\Constants::PERMISSION_ALL);
}

//Federated sharing
Expand Down
18 changes: 16 additions & 2 deletions apps/files_sharing/tests/API/Share20OCSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\Lock\LockedException;
use OCP\Share;
use Test\TestCase;
use OCP\IConfig;

/**
* Class Share20OCSTest
Expand Down Expand Up @@ -96,6 +97,12 @@ protected function setUp() {
return vsprintf($text, $parameters);
}));

$this->config = $this->createMock(IConfig::class);
$this->config->method('getAppValue')
->will($this->returnValueMap([
['core', 'shareapi_default_permissions', \OCP\Constants::PERMISSION_ALL, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE]
]));

$this->ocs = new Share20OCS(
$this->shareManager,
$this->groupManager,
Expand All @@ -104,7 +111,8 @@ protected function setUp() {
$this->rootFolder,
$this->urlGenerator,
$this->currentUser,
$this->l
$this->l,
$this->config
);
}

Expand All @@ -119,6 +127,7 @@ private function mockFormatShare() {
$this->urlGenerator,
$this->currentUser,
$this->l,
$this->config,
])->setMethods(['formatShare'])
->getMock();
}
Expand Down Expand Up @@ -425,6 +434,7 @@ public function testGetShare(\OCP\Share\IShare $share, array $result) {
$this->urlGenerator,
$this->currentUser,
$this->l,
$this->config,
])->setMethods(['canAccessShare'])
->getMock();

Expand Down Expand Up @@ -751,6 +761,7 @@ public function testCreateShareUser() {
$this->urlGenerator,
$this->currentUser,
$this->l,
$this->config,
])->setMethods(['formatShare'])
->getMock();

Expand Down Expand Up @@ -867,6 +878,7 @@ public function testCreateShareGroup() {
$this->urlGenerator,
$this->currentUser,
$this->l,
$this->config,
])->setMethods(['formatShare'])
->getMock();

Expand Down Expand Up @@ -1403,6 +1415,7 @@ public function testCreateReshareOfFederatedMountNoDeletePermissions() {
$this->urlGenerator,
$this->currentUser,
$this->l,
$this->config,
])->setMethods(['formatShare'])
->getMock();

Expand Down Expand Up @@ -2678,7 +2691,8 @@ public function getOcsDisabledAPI() {
$this->rootFolder,
$this->urlGenerator,
$this->currentUser,
$this->l
$this->l,
$this->config
);
}

Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ private function createOCS($request, $userId) {
\OC::$server->getRootFolder(),
\OC::$server->getURLGenerator(),
$currentUser,
$l
$l,
\OC::$server->getConfig()
);
}

Expand Down
4 changes: 4 additions & 0 deletions core/js/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@
$array['oc_config']['versionstring'] = OC_Util::getVersionString();
$array['oc_defaults']['docBaseUrl'] = $defaults->getDocBaseUrl();
$array['oc_defaults']['docPlaceholderUrl'] = $defaults->buildDocLinkToKey('PLACEHOLDER');
$caps = \OC::$server->getCapabilitiesManager()->getCapabilities();
// remove status.php info as we already have the version above
unset($caps['core']['status']);
$array['oc_capabilities'] = json_encode($caps);
}

// Allow hooks to modify the output values
Expand Down
16 changes: 16 additions & 0 deletions core/js/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ var OC = {
*/
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
Expand Down Expand Up @@ -298,6 +305,15 @@ var OC = {
return OC.webroot;
},

/**
* Returns the capabilities
*
* @return {array} capabilities
*/
getCapabilities: function() {
return OC._capabilities;
},

/**
* Returns the currently logged in user or null if there is no logged in
* user (public page mode)
Expand Down
14 changes: 8 additions & 6 deletions core/js/shareitemmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,25 @@
options = options || {};
attributes = _.extend({}, attributes);

var defaultPermissions = OC.getCapabilities()['files_sharing']['default_permissions'] || OC.PERMISSION_ALL;

// Default permissions are Edit (CRUD) and Share
// Check if these permissions are possible
var permissions = OC.PERMISSION_READ;
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();
}
Expand Down
25 changes: 24 additions & 1 deletion core/js/tests/specs/shareitemmodelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -559,7 +567,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);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Settings/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function getBuiltInPanel($className) {
$this->urlGenerator,
$this->certificateManager),
Encryption::class => new Encryption(),
FileSharing::class => new FileSharing($this->config, $this->helper),
FileSharing::class => new FileSharing($this->config, $this->helper, $this->l),
Logging::class => new Logging($this->config, $this->urlGenerator, $this->helper),
Mail::class => new Mail($this->config, $this->helper),
SecurityWarning::class => new SecurityWarning(
Expand Down
31 changes: 30 additions & 1 deletion settings/Panels/Admin/FileSharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@
use OCP\IConfig;
use OCP\Settings\ISettings;
use OCP\Template;
use OCP\IL10N;

class FileSharing implements ISettings {

/** @var IConfig */
protected $config;
/** @var Helper */
protected $helper;
/** @var IL10N */
protected $l;

public function __construct(IConfig $config, Helper $helper) {
public function __construct(IConfig $config, Helper $helper, IL10N $l) {
$this->config = $config;
$this->helper = $helper;
$this->l = $l;
}

public function getPriority() {
Expand Down Expand Up @@ -64,6 +68,31 @@ public function getPanel() {
$template->assign('shareExcludedGroupsList', !is_null($excludedGroupsList) ? implode('|', $excludedGroupsList) : '');
$template->assign('shareExpireAfterNDays', $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'));
$template->assign('shareEnforceExpireDate', $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no'));

$permList = [
[
'id' => 'cancreate',
'label' => $this->l->t('Create'),
'value' => \OCP\Constants::PERMISSION_CREATE
],
[
'id' => 'canupdate',
'label' => $this->l->t('Change'),
'value' => \OCP\Constants::PERMISSION_UPDATE
],
[
'id' => 'candelete',
'label' => $this->l->t('Delete'),
'value' => \OCP\Constants::PERMISSION_DELETE
],
[
'id' => 'canshare',
'label' => $this->l->t('Share'),
'value' => \OCP\Constants::PERMISSION_SHARE
],
];
$template->assign('shareApiDefaultPermissions', $this->config->getAppValue('core', 'shareapi_default_permissions', \OCP\Constants::PERMISSION_ALL));
$template->assign('shareApiDefaultPermissionsCheckboxes', $permList);
return $template;
}

Expand Down
10 changes: 9 additions & 1 deletion settings/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,24 @@ table.grid td.date{

#shareAPI p { padding-bottom: 0.8em; }
#shareAPI input#shareapiExpireAfterNDays {width: 25px;}
#shareAPI .nocheckbox {
padding-left: 20px;
}
#shareAPI .indent {
padding-left: 28px;
}
#shareAPI .double-indent {
padding-left: 56px;
}
#shareAPI
#fileSharingSettings h2 {
display: inline-block;
}

#shareApiDefaultPermissionsSection label {
margin-right: 20px;
}

/* correctly display help icons next to headings */
.icon-info {
padding: 11px 20px;
Expand Down Expand Up @@ -678,4 +686,4 @@ li > a.icon-settings {

div.app-settings .section {
margin-bottom: 0px;
}
}
Loading