Skip to content

Commit f1dbabd

Browse files
authored
Merge pull request #26727 from nextcloud/group-exclude-link-share
Add option to exclude groups from creating link shares
2 parents b1ad3fa + 8d7fae8 commit f1dbabd

16 files changed

Lines changed: 253 additions & 179 deletions

File tree

apps/files/lib/Controller/ViewController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
use OCP\IRequest;
5656
use OCP\IURLGenerator;
5757
use OCP\IUserSession;
58+
use OCP\Share\IManager;
5859

5960
/**
6061
* Class ViewController
@@ -86,6 +87,8 @@ class ViewController extends Controller {
8687
private $initialState;
8788
/** @var ITemplateManager */
8889
private $templateManager;
90+
/** @var IManager */
91+
private $shareManager;
8992

9093
public function __construct(string $appName,
9194
IRequest $request,
@@ -98,7 +101,8 @@ public function __construct(string $appName,
98101
IRootFolder $rootFolder,
99102
Helper $activityHelper,
100103
IInitialState $initialState,
101-
ITemplateManager $templateManager
104+
ITemplateManager $templateManager,
105+
IManager $shareManager
102106
) {
103107
parent::__construct($appName, $request);
104108
$this->appName = $appName;
@@ -113,6 +117,7 @@ public function __construct(string $appName,
113117
$this->activityHelper = $activityHelper;
114118
$this->initialState = $initialState;
115119
$this->templateManager = $templateManager;
120+
$this->shareManager = $shareManager;
116121
}
117122

118123
/**
@@ -302,7 +307,7 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
302307
$params['owner'] = $storageInfo['owner'] ?? '';
303308
$params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? '';
304309
$params['isPublic'] = false;
305-
$params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
310+
$params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no';
306311
$params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
307312
$params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc');
308313
$params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false);

apps/files/list.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
*
2323
*/
2424

25+
use OCP\Share\IManager;
26+
2527
$config = \OC::$server->getConfig();
2628
$userSession = \OC::$server->getUserSession();
2729
// TODO: move this to the generated config.js
28-
$publicUploadEnabled = $config->getAppValue('core', 'shareapi_allow_public_upload', 'yes');
30+
/** @var IManager $shareManager */
31+
$shareManager = \OC::$server->get(IManager::class);
32+
$publicUploadEnabled = $shareManager->shareApiLinkAllowPublicUpload() ? 'yes' : 'no';;
2933

3034
$showgridview = $config->getUserValue($userSession->getUser()->getUID(), 'files', 'show_grid', false);
3135
$isIE = OC_Util::isIe();

apps/files/tests/Controller/ViewControllerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use OCP\IURLGenerator;
4949
use OCP\IUser;
5050
use OCP\IUserSession;
51+
use OCP\Share\IManager;
5152
use OCP\Template;
5253
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
5354
use Test\TestCase;
@@ -84,6 +85,8 @@ class ViewControllerTest extends TestCase {
8485
private $initialState;
8586
/** @var ITemplateManager|\PHPUnit\Framework\MockObject\MockObject */
8687
private $templateManager;
88+
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
89+
private $shareManager;
8790

8891
protected function setUp(): void {
8992
parent::setUp();
@@ -105,6 +108,7 @@ protected function setUp(): void {
105108
$this->activityHelper = $this->createMock(Helper::class);
106109
$this->initialState = $this->createMock(IInitialState::class);
107110
$this->templateManager = $this->createMock(ITemplateManager::class);
111+
$this->shareManager = $this->createMock(IManager::class);
108112
$this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController')
109113
->setConstructorArgs([
110114
'files',
@@ -119,6 +123,7 @@ protected function setUp(): void {
119123
$this->activityHelper,
120124
$this->initialState,
121125
$this->templateManager,
126+
$this->shareManager,
122127
])
123128
->setMethods([
124129
'getStorageInfo',
@@ -153,6 +158,8 @@ public function testIndexWithRegularBrowser() {
153158
->expects($this->any())
154159
->method('getAppValue')
155160
->willReturnArgument(2);
161+
$this->shareManager->method('shareApiAllowLinks')
162+
->willReturn(true);
156163

157164
$nav = new Template('files', 'appnavigation');
158165
$nav->assign('usage_relative', 123);

apps/files_sharing/lib/AppInfo/Application.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
use OCP\IDBConnection;
5757
use OCP\IGroup;
5858
use OCP\IServerContainer;
59+
use OCP\IUserSession;
5960
use OCP\Share\Events\ShareCreatedEvent;
61+
use OCP\Share\IManager;
6062
use OCP\Util;
6163
use Psr\Container\ContainerInterface;
6264
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -166,20 +168,24 @@ protected function registerEventsScripts(IEventDispatcher $dispatcher, EventDisp
166168
}
167169

168170
protected function setupSharingMenus() {
169-
$config = \OC::$server->getConfig();
171+
/** @var IManager $shareManager */
172+
$shareManager = \OC::$server->get(IManager::class);
170173

171-
if ($config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes' || !class_exists('\OCA\Files\App')) {
174+
if (!$shareManager->shareApiEnabled() || !class_exists('\OCA\Files\App')) {
172175
return;
173176
}
174177

175178
// show_Quick_Access stored as string
176-
\OCA\Files\App::getNavigationManager()->add(function () {
177-
$config = \OC::$server->getConfig();
179+
\OCA\Files\App::getNavigationManager()->add(function () use ($shareManager) {
178180
$l = \OC::$server->getL10N('files_sharing');
181+
/** @var IUserSession $userSession */
182+
$userSession = \OC::$server->get(IUserSession::class);
183+
$user = $userSession->getUser();
184+
$userId = $user ? $user->getUID() : null;
179185

180186
$sharingSublistArray = [];
181187

182-
if (\OCP\Util::isSharingDisabledForUser() === false) {
188+
if ($shareManager->sharingDisabledForUser($userId) === false) {
183189
$sharingSublistArray[] = [
184190
'id' => 'sharingout',
185191
'appname' => 'files_sharing',
@@ -197,9 +203,9 @@ protected function setupSharingMenus() {
197203
'name' => $l->t('Shared with you'),
198204
];
199205

200-
if (\OCP\Util::isSharingDisabledForUser() === false) {
206+
if ($shareManager->sharingDisabledForUser($userId) === false) {
201207
// Check if sharing by link is enabled
202-
if ($config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes') {
208+
if ($shareManager->shareApiAllowLinks()) {
203209
$sharingSublistArray[] = [
204210
'id' => 'sharinglinks',
205211
'appname' => 'files_sharing',

apps/files_sharing/lib/Capabilities.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCP\Capabilities\ICapability;
2929
use OCP\Constants;
3030
use OCP\IConfig;
31+
use OCP\Share\IManager;
3132

3233
/**
3334
* Class Capabilities
@@ -38,9 +39,12 @@ class Capabilities implements ICapability {
3839

3940
/** @var IConfig */
4041
private $config;
42+
/** @var IManager */
43+
private $shareManager;
4144

42-
public function __construct(IConfig $config) {
45+
public function __construct(IConfig $config, IManager $shareManager) {
4346
$this->config = $config;
47+
$this->shareManager = $shareManager;
4448
}
4549

4650
/**
@@ -51,7 +55,7 @@ public function __construct(IConfig $config) {
5155
public function getCapabilities() {
5256
$res = [];
5357

54-
if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') {
58+
if (!$this->shareManager->shareApiEnabled()) {
5559
$res['api_enabled'] = false;
5660
$res['public'] = ['enabled' => false];
5761
$res['user'] = ['send_mail' => false];
@@ -60,10 +64,10 @@ public function getCapabilities() {
6064
$res['api_enabled'] = true;
6165

6266
$public = [];
63-
$public['enabled'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
67+
$public['enabled'] = $this->shareManager->shareApiAllowLinks();
6468
if ($public['enabled']) {
6569
$public['password'] = [];
66-
$public['password']['enforced'] = ($this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes');
70+
$public['password']['enforced'] = $this->shareManager->shareApiLinkEnforcePassword();
6771

6872
if ($public['password']['enforced']) {
6973
$public['password']['askForOptionalPassword'] = false;
@@ -73,28 +77,28 @@ public function getCapabilities() {
7377

7478
$public['expire_date'] = [];
7579
$public['multiple_links'] = true;
76-
$public['expire_date']['enabled'] = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
80+
$public['expire_date']['enabled'] = $this->shareManager->shareApiLinkDefaultExpireDate();
7781
if ($public['expire_date']['enabled']) {
78-
$public['expire_date']['days'] = $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
79-
$public['expire_date']['enforced'] = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
82+
$public['expire_date']['days'] = $this->shareManager->shareApiLinkDefaultExpireDays();
83+
$public['expire_date']['enforced'] = $this->shareManager->shareApiLinkDefaultExpireDateEnforced();
8084
}
8185

8286
$public['expire_date_internal'] = [];
83-
$public['expire_date_internal']['enabled'] = $this->config->getAppValue('core', 'shareapi_default_internal_expire_date', 'no') === 'yes';
87+
$public['expire_date_internal']['enabled'] = $this->shareManager->shareApiInternalDefaultExpireDate();
8488
if ($public['expire_date_internal']['enabled']) {
85-
$public['expire_date_internal']['days'] = $this->config->getAppValue('core', 'shareapi_internal_expire_after_n_days', '7');
86-
$public['expire_date_internal']['enforced'] = $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes';
89+
$public['expire_date_internal']['days'] = $this->shareManager->shareApiInternalDefaultExpireDays();
90+
$public['expire_date_internal']['enforced'] = $this->shareManager->shareApiInternalDefaultExpireDateEnforced();
8791
}
8892

8993
$public['expire_date_remote'] = [];
90-
$public['expire_date_remote']['enabled'] = $this->config->getAppValue('core', 'shareapi_default_remote_expire_date', 'no') === 'yes';
94+
$public['expire_date_remote']['enabled'] = $this->shareManager->shareApiRemoteDefaultExpireDate();
9195
if ($public['expire_date_remote']['enabled']) {
92-
$public['expire_date_remote']['days'] = $this->config->getAppValue('core', 'shareapi_remote_expire_after_n_days', '7');
93-
$public['expire_date_remote']['enforced'] = $this->config->getAppValue('core', 'shareapi_enforce_remote_expire_date', 'no') === 'yes';
96+
$public['expire_date_remote']['days'] = $this->shareManager->shareApiRemoteDefaultExpireDays();
97+
$public['expire_date_remote']['enforced'] = $this->shareManager->shareApiRemoteDefaultExpireDateEnforced();
9498
}
9599

96100
$public['send_mail'] = $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no') === 'yes';
97-
$public['upload'] = $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
101+
$public['upload'] = $this->shareManager->shareApiLinkAllowPublicUpload();
98102
$public['upload_files_drop'] = $public['upload'];
99103
}
100104
$res['public'] = $public;
@@ -106,17 +110,17 @@ public function getCapabilities() {
106110

107111
// deprecated in favour of 'group', but we need to keep it for now
108112
// in order to stay compatible with older clients
109-
$res['group_sharing'] = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
113+
$res['group_sharing'] = $this->shareManager->allowGroupSharing();
110114

111115
$res['group'] = [];
112-
$res['group']['enabled'] = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
116+
$res['group']['enabled'] = $this->shareManager->allowGroupSharing();
113117
$res['group']['expire_date']['enabled'] = true;
114118
$res['default_permissions'] = (int)$this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL);
115119
}
116120

117121
//Federated sharing
118122
$res['federation'] = [
119-
'outgoing' => $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes',
123+
'outgoing' => $this->shareManager->outgoingServer2ServerSharesAllowed(),
120124
'incoming' => $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') === 'yes',
121125
// old bogus one, expire_date was not working before, keeping for compatibility
122126
'expire_date' => ['enabled' => true],

apps/files_sharing/tests/CapabilitiesTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,24 @@
2727

2828
namespace OCA\Files_Sharing\Tests;
2929

30+
use OC\Share20\Manager;
3031
use OCA\Files_Sharing\Capabilities;
32+
use OCP\EventDispatcher\IEventDispatcher;
33+
use OCP\Files\IRootFolder;
34+
use OCP\Files\Mount\IMountManager;
3135
use OCP\IConfig;
36+
use OCP\IGroupManager;
37+
use OCP\IL10N;
38+
use OCP\ILogger;
39+
use OCP\IURLGenerator;
40+
use OCP\IUserManager;
41+
use OCP\IUserSession;
42+
use OCP\L10N\IFactory;
43+
use OCP\Mail\IMailer;
44+
use OCP\Security\IHasher;
45+
use OCP\Security\ISecureRandom;
46+
use OCP\Share\IProviderFactory;
47+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
3248

3349
/**
3450
* Class CapabilitiesTest
@@ -60,7 +76,26 @@ private function getFilesSharingPart(array $data) {
6076
private function getResults(array $map) {
6177
$config = $this->getMockBuilder(IConfig::class)->disableOriginalConstructor()->getMock();
6278
$config->method('getAppValue')->willReturnMap($map);
63-
$cap = new Capabilities($config);
79+
$shareManager = new Manager(
80+
$this->createMock(ILogger::class),
81+
$config,
82+
$this->createMock(ISecureRandom::class),
83+
$this->createMock(IHasher::class),
84+
$this->createMock(IMountManager::class),
85+
$this->createMock(IGroupManager::class),
86+
$this->createMock(IL10N::class),
87+
$this->createMock(IFactory::class),
88+
$this->createMock(IProviderFactory::class),
89+
$this->createMock(IUserManager::class),
90+
$this->createMock(IRootFolder::class),
91+
$this->createMock(EventDispatcherInterface::class),
92+
$this->createMock(IMailer::class),
93+
$this->createMock(IURLGenerator::class),
94+
$this->createMock(\OC_Defaults::class),
95+
$this->createMock(IEventDispatcher::class),
96+
$this->createMock(IUserSession::class)
97+
);
98+
$cap = new Capabilities($config, $shareManager);
6499
$result = $this->getFilesSharingPart($cap->getCapabilities());
65100
return $result;
66101
}

apps/settings/js/admin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
window.addEventListener('DOMContentLoaded', function(){
2-
$('#excludedGroups').each(function (index, element) {
2+
$('#excludedGroups,#linksExcludedGroups').each(function (index, element) {
33
OC.Settings.setupGroupsSelect($(element));
44
$(element).change(function(ev) {
55
var groups = ev.val || [];

apps/settings/lib/Settings/Admin/Sharing.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,15 @@ public function getForm() {
6464
$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
6565
$excludeGroupsList = !is_null(json_decode($excludedGroups))
6666
? implode('|', json_decode($excludedGroups, true)) : '';
67+
$linksExcludedGroups = $this->config->getAppValue('core', 'shareapi_allow_links_exclude_groups', '');
68+
$linksExcludeGroupsList = !is_null(json_decode($linksExcludedGroups))
69+
? implode('|', json_decode($linksExcludedGroups, true)) : '';
6770

6871
$parameters = [
6972
// Built-In Sharing
7073
'allowGroupSharing' => $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes'),
7174
'allowLinks' => $this->config->getAppValue('core', 'shareapi_allow_links', 'yes'),
75+
'allowLinksExcludeGroups' => $linksExcludeGroupsList,
7276
'allowPublicUpload' => $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes'),
7377
'allowResharing' => $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes'),
7478
'allowShareDialogUserEnumeration' => $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'),

apps/settings/templates/settings/admin/sharing.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@
138138
<p class="<?php if ($_['shareAPIEnabled'] === 'no') {
139139
p('hidden');
140140
}?>">
141+
<p class="indent">
142+
<?php p($l->t('Exclude groups from creating link shares:'));?>
143+
</p>
144+
<p id="selectLinksExcludedGroups" class="indent <?php if ($_['allowLinks'] === 'no') {
145+
p('hidden');
146+
} ?>">
147+
<input name="shareapi_allow_links_exclude_groups" type="hidden" id="linksExcludedGroups" value="<?php p($_['allowLinksExcludeGroups']) ?>" style="width: 400px" class="noJSAutoUpdate"/>
148+
</p>
141149
<input type="checkbox" name="shareapi_allow_resharing" id="allowResharing" class="checkbox"
142150
value="1" <?php if ($_['allowResharing'] === 'yes') {
143151
print_unescaped('checked="checked"');
@@ -176,7 +184,7 @@
176184
} ?>">
177185
<input name="shareapi_exclude_groups_list" type="hidden" id="excludedGroups" value="<?php p($_['shareExcludedGroupsList']) ?>" style="width: 400px" class="noJSAutoUpdate"/>
178186
<br />
179-
<em><?php p($l->t('These groups will still be able to receive shares, but not to initiate them.')); ?></em>
187+
<em><?php p($l->t('These groups will still be able to receive shares, but not to initiate them.')); ?></em>
180188
</p>
181189

182190
<p class="<?php if ($_['shareAPIEnabled'] === 'no') {

apps/settings/tests/Settings/Admin/SharingTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public function testGetFormWithoutExcludedGroups() {
9090
['core', 'shareapi_remote_expire_after_n_days', '7', '7'],
9191
['core', 'shareapi_enforce_remote_expire_date', 'no', 'no'],
9292
]);
93+
$this->shareManager->method('shareWithGroupMembersOnly')
94+
->willReturn(false);
9395

9496
$expected = new TemplateResponse(
9597
'settings',
@@ -121,6 +123,7 @@ public function testGetFormWithoutExcludedGroups() {
121123
'shareDefaultRemoteExpireDateSet' => 'no',
122124
'shareRemoteExpireAfterNDays' => '7',
123125
'shareRemoteEnforceExpireDate' => 'no',
126+
'allowLinksExcludeGroups' => '',
124127
],
125128
''
126129
);
@@ -156,6 +159,8 @@ public function testGetFormWithExcludedGroups() {
156159
['core', 'shareapi_remote_expire_after_n_days', '7', '7'],
157160
['core', 'shareapi_enforce_remote_expire_date', 'no', 'no'],
158161
]);
162+
$this->shareManager->method('shareWithGroupMembersOnly')
163+
->willReturn(false);
159164

160165
$expected = new TemplateResponse(
161166
'settings',
@@ -187,6 +192,7 @@ public function testGetFormWithExcludedGroups() {
187192
'shareDefaultRemoteExpireDateSet' => 'no',
188193
'shareRemoteExpireAfterNDays' => '7',
189194
'shareRemoteEnforceExpireDate' => 'no',
195+
'allowLinksExcludeGroups' => '',
190196
],
191197
''
192198
);

0 commit comments

Comments
 (0)