Skip to content

Commit cfe241a

Browse files
committed
Merge pull request #13964 from rullzer/capabilities
Add OCS sharing info to capabilities - take 2
2 parents 00b2be1 + ec31ee9 commit cfe241a

File tree

3 files changed

+280
-0
lines changed

3 files changed

+280
-0
lines changed

apps/files_sharing/appinfo/routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ function() {
7979
'/apps/files_sharing/api/v1/shares/{id}',
8080
array('\OCA\Files_Sharing\API\Local', 'deleteShare'),
8181
'files_sharing');
82+
83+
// Register with the capabilities API
84+
\OC_API::register('get',
85+
'/cloud/capabilities',
86+
array('OCA\Files_Sharing\Capabilities', 'getCapabilities'),
87+
'files_sharing', \OC_API::USER_AUTH);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* @author Roeland Jago Douma <roeland@famdouma.nl>
4+
*
5+
* @copyright Copyright (c) 2015, ownCloud, Inc.
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
namespace OCA\Files_Sharing;
22+
23+
use \OCP\IConfig;
24+
25+
/**
26+
* Class Capabilities
27+
*
28+
* @package OCA\Files_Sharing
29+
*/
30+
class Capabilities {
31+
32+
/** @var IConfig */
33+
private $config;
34+
35+
/**
36+
* @param IConfig $config
37+
*/
38+
public function __construct(IConfig $config) {
39+
$this->config = $config;
40+
}
41+
42+
/**
43+
* @return \OC_OCS_Result
44+
*/
45+
public static function getCapabilities() {
46+
$config = \OC::$server->getConfig();
47+
$cap = new Capabilities($config);
48+
return $cap->getCaps();
49+
}
50+
51+
52+
/**
53+
* @return \OC_OCS_Result
54+
*/
55+
public function getCaps() {
56+
$res = [];
57+
58+
$public = [];
59+
$public['enabled'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
60+
if ($public['enabled']) {
61+
$public['password'] = [];
62+
$public['password']['enforced'] = ($this->config->getAppValue('core', 'shareapi_enforce_links_password', 'yes') === 'yes');
63+
64+
$public['expire_date'] = [];
65+
$public['expire_date']['enabled'] = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'yes') === 'yes';
66+
if ($public['expire_date']['enabled']) {
67+
$public['expire_date']['days'] = $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
68+
$public['expire_date']['enforced'] = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'yes') === 'yes';
69+
}
70+
71+
$public['send_mail'] = $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'yes') === 'yes';
72+
}
73+
$res["public"] = $public;
74+
75+
$res['user']['send_mail'] = $this->config->getAppValue('core', 'shareapi_allow_mail_notification', 'yes') === 'yes';
76+
77+
$res['resharing'] = $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes') === 'yes';
78+
79+
80+
return new \OC_OCS_Result([
81+
'capabilities' => [
82+
'files_sharing' => $res
83+
],
84+
]);
85+
}
86+
87+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
/**
3+
* @author Roeland Jago Douma <roeland@famdouma.nl>
4+
*
5+
* @copyright Copyright (c) 2015, ownCloud, Inc.
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
namespace OCA\Files_Sharing\Tests;
22+
23+
use OCA\Files_Sharing\Capabilities;
24+
use OCA\Files_Sharing\Tests\TestCase;
25+
26+
/**
27+
* Class FilesSharingCapabilitiesTest
28+
*/
29+
class FilesSharingCapabilitiesTest extends \Test\TestCase {
30+
31+
/**
32+
* Test for the general part in each return statement and assert.
33+
* Strip of the general part on the way.
34+
*
35+
* @param string[] $data Capabilities
36+
* @return string[]
37+
*/
38+
private function getFilesSharingPart(array $data) {
39+
$this->assertArrayHasKey('capabilities', $data);
40+
$this->assertArrayHasKey('files_sharing', $data['capabilities']);
41+
return $data['capabilities']['files_sharing'];
42+
}
43+
44+
/**
45+
* Create a mock config object and insert the values in $map tot the getAppValue
46+
* function. Then obtain the capabilities and extract the first few
47+
* levels in the array
48+
*
49+
* @param (string[])[] $map Map of arguments to return types for the getAppValue function in the mock
50+
* @return string[]
51+
*/
52+
private function getResults(array $map) {
53+
$stub = $this->getMockBuilder('\OCP\IConfig')->disableOriginalConstructor()->getMock();
54+
$stub->method('getAppValue')->will($this->returnValueMap($map));
55+
$cap = new Capabilities($stub);
56+
$result = $this->getFilesSharingPart($cap->getCaps()->getData());
57+
return $result;
58+
}
59+
60+
public function testNoLinkSharing() {
61+
$map = [
62+
['core', 'shareapi_allow_links', 'yes', 'no'],
63+
];
64+
$result = $this->getResults($map);
65+
$this->assertInternalType('array', $result['public']);
66+
$this->assertFalse($result['public']['enabled']);
67+
}
68+
69+
public function testOnlyLinkSharing() {
70+
$map = [
71+
['core', 'shareapi_allow_links', 'yes', 'yes'],
72+
];
73+
$result = $this->getResults($map);
74+
$this->assertInternalType('array', $result['public']);
75+
$this->assertTrue($result['public']['enabled']);
76+
}
77+
78+
public function testLinkPassword() {
79+
$map = [
80+
['core', 'shareapi_allow_links', 'yes', 'yes'],
81+
['core', 'shareapi_enforce_links_password', 'yes', 'yes'],
82+
];
83+
$result = $this->getResults($map);
84+
$this->assertArrayHasKey('password', $result['public']);
85+
$this->assertArrayHasKey('enforced', $result['public']['password']);
86+
$this->assertTrue($result['public']['password']['enforced']);
87+
}
88+
89+
public function testLinkNoPassword() {
90+
$map = [
91+
['core', 'shareapi_allow_links', 'yes', 'yes'],
92+
['core', 'shareapi_enforce_links_password', 'yes', 'no'],
93+
];
94+
$result = $this->getResults($map);
95+
$this->assertArrayHasKey('password', $result['public']);
96+
$this->assertArrayHasKey('enforced', $result['public']['password']);
97+
$this->assertFalse($result['public']['password']['enforced']);
98+
}
99+
100+
public function testLinkNoExpireDate() {
101+
$map = [
102+
['core', 'shareapi_allow_links', 'yes', 'yes'],
103+
['core', 'shareapi_default_expire_date', 'yes', 'no'],
104+
];
105+
$result = $this->getResults($map);
106+
$this->assertArrayHasKey('expire_date', $result['public']);
107+
$this->assertInternalType('array', $result['public']['expire_date']);
108+
$this->assertFalse($result['public']['expire_date']['enabled']);
109+
}
110+
111+
public function testLinkExpireDate() {
112+
$map = [
113+
['core', 'shareapi_allow_links', 'yes', 'yes'],
114+
['core', 'shareapi_default_expire_date', 'yes', 'yes'],
115+
['core', 'shareapi_expire_after_n_days', '7', '7'],
116+
['core', 'shareapi_enforce_expire_date', 'yes', 'no'],
117+
];
118+
$result = $this->getResults($map);
119+
$this->assertArrayHasKey('expire_date', $result['public']);
120+
$this->assertInternalType('array', $result['public']['expire_date']);
121+
$this->assertTrue($result['public']['expire_date']['enabled']);
122+
$this->assertArrayHasKey('days', $result['public']['expire_date']);
123+
$this->assertFalse($result['public']['expire_date']['enforced']);
124+
}
125+
126+
public function testLinkExpireDateEnforced() {
127+
$map = [
128+
['core', 'shareapi_allow_links', 'yes', 'yes'],
129+
['core', 'shareapi_default_expire_date', 'yes', 'yes'],
130+
['core', 'shareapi_enforce_expire_date', 'yes', 'yes'],
131+
];
132+
$result = $this->getResults($map);
133+
$this->assertArrayHasKey('expire_date', $result['public']);
134+
$this->assertInternalType('array', $result['public']['expire_date']);
135+
$this->assertTrue($result['public']['expire_date']['enforced']);
136+
}
137+
138+
public function testLinkSendMail() {
139+
$map = [
140+
['core', 'shareapi_allow_links', 'yes', 'yes'],
141+
['core', 'shareapi_allow_public_notification', 'yes', 'yes'],
142+
];
143+
$result = $this->getResults($map);
144+
$this->assertTrue($result['public']['send_mail']);
145+
}
146+
147+
public function testLinkNoSendMail() {
148+
$map = [
149+
['core', 'shareapi_allow_links', 'yes', 'yes'],
150+
['core', 'shareapi_allow_public_notification', 'yes', 'no'],
151+
];
152+
$result = $this->getResults($map);
153+
$this->assertFalse($result['public']['send_mail']);
154+
}
155+
156+
public function testUserSendMail() {
157+
$map = [
158+
['core', 'shareapi_allow_mail_notification', 'yes', 'yes'],
159+
];
160+
$result = $this->getResults($map);
161+
$this->assertTrue($result['user']['send_mail']);
162+
}
163+
164+
public function testUserNoSendMail() {
165+
$map = [
166+
['core', 'shareapi_allow_mail_notification', 'yes', 'no'],
167+
];
168+
$result = $this->getResults($map);
169+
$this->assertFalse($result['user']['send_mail']);
170+
}
171+
172+
public function testResharing() {
173+
$map = [
174+
['core', 'shareapi_allow_resharing', 'yes', 'yes'],
175+
];
176+
$result = $this->getResults($map);
177+
$this->assertTrue($result['resharing']);
178+
}
179+
180+
public function testNoResharing() {
181+
$map = [
182+
['core', 'shareapi_allow_resharing', 'yes', 'no'],
183+
];
184+
$result = $this->getResults($map);
185+
$this->assertFalse($result['resharing']);
186+
}
187+
}

0 commit comments

Comments
 (0)