summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2015-08-30 10:50:12 +0200
committerVincent Petry <pvince81@owncloud.com>2015-08-30 10:50:12 +0200
commit045f8cc97101521cafd664faf7b8f24ea9e88451 (patch)
tree9ae066c9370d09a88c8bb1890b05a7eac62ab0bc /apps
parent114d1acd2ca9f254b7e6639a2e1f0a1748e4c254 (diff)
parent2aff11c80bbc7fb0524c52d17824d68bda43aac5 (diff)
downloadnextcloud-server-045f8cc97101521cafd664faf7b8f24ea9e88451.tar.gz
nextcloud-server-045f8cc97101521cafd664faf7b8f24ea9e88451.zip
Merge pull request #18651 from owncloud/ocs_share_create_with_expire
Allow to directly set the expireDate on a new (link)share
Diffstat (limited to 'apps')
-rw-r--r--apps/files_sharing/api/local.php44
-rw-r--r--apps/files_sharing/tests/api.php142
2 files changed, 183 insertions, 3 deletions
diff --git a/apps/files_sharing/api/local.php b/apps/files_sharing/api/local.php
index eb0e0e0d846..87025998b3d 100644
--- a/apps/files_sharing/api/local.php
+++ b/apps/files_sharing/api/local.php
@@ -258,6 +258,7 @@ class Local {
$itemSource = self::getFileId($path);
$itemSourceName = $itemSource;
$itemType = self::getItemType($path);
+ $expirationDate = null;
if($itemSource === null) {
return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist.");
@@ -286,6 +287,14 @@ class Local {
// read, create, update (7) if public upload is enabled or
// read (1) if public upload is disabled
$permissions = $publicUpload === 'true' ? 7 : 1;
+
+ // Get the expiration date
+ try {
+ $expirationDate = isset($_POST['expireDate']) ? self::parseDate($_POST['expireDate']) : null;
+ } catch (\Exception $e) {
+ return new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.');
+ }
+
break;
default:
return new \OC_OCS_Result(null, 400, "unknown share type");
@@ -302,10 +311,15 @@ class Local {
$shareType,
$shareWith,
$permissions,
- $itemSourceName
- );
+ $itemSourceName,
+ $expirationDate
+ );
} catch (HintException $e) {
- return new \OC_OCS_Result(null, 400, $e->getHint());
+ if ($e->getCode() === 0) {
+ return new \OC_OCS_Result(null, 400, $e->getHint());
+ } else {
+ return new \OC_OCS_Result(null, $e->getCode(), $e->getHint());
+ }
} catch (\Exception $e) {
return new \OC_OCS_Result(null, 403, $e->getMessage());
}
@@ -538,6 +552,30 @@ class Local {
}
/**
+ * Make sure that the passed date is valid ISO 8601
+ * So YYYY-MM-DD
+ * If not throw an exception
+ *
+ * @param string $expireDate
+ *
+ * @throws \Exception
+ * @return \DateTime
+ */
+ private static function parseDate($expireDate) {
+ if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $expireDate) === 0) {
+ throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
+ }
+
+ $date = new \DateTime($expireDate);
+
+ if ($date === false) {
+ throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
+ }
+
+ return $date;
+ }
+
+ /**
* get file ID from a given path
* @param string $path
* @return string fileID or null
diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php
index 3bd568e47af..d0ee71cec5a 100644
--- a/apps/files_sharing/tests/api.php
+++ b/apps/files_sharing/tests/api.php
@@ -1487,4 +1487,146 @@ class Test_Files_Sharing_Api extends TestCase {
$config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
}
+
+ public function datesProvider() {
+ $date = new \DateTime();
+ $date->add(new \DateInterval('P5D'));
+
+ $year = (int)$date->format('Y');
+
+ return [
+ [$date->format('Y-m-d'), true],
+ [$year+1 . '-1-1', false],
+ [$date->format('Y-m-dTH:m'), false],
+ ['abc', false],
+ [$date->format('Y-m-d') . 'xyz', false],
+ ];
+ }
+
+ /**
+ * Make sure only ISO 8601 dates are accepted
+ *
+ * @dataProvider datesProvider
+ */
+ public function testPublicLinkExpireDate($date, $valid) {
+ $_POST['path'] = $this->folder;
+ $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
+ $_POST['expireDate'] = $date;
+
+ $result = \OCA\Files_Sharing\API\Local::createShare([]);
+
+ if ($valid === false) {
+ $this->assertFalse($result->succeeded());
+ $this->assertEquals(404, $result->getStatusCode());
+ $this->assertEquals('Invalid Date. Format must be YYYY-MM-DD.', $result->getMeta()['message']);
+ return;
+ }
+
+ $this->assertTrue($result->succeeded());
+
+ $data = $result->getData();
+ $this->assertTrue(is_string($data['token']));
+
+ // check for correct link
+ $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
+ $this->assertEquals($url, $data['url']);
+
+
+ $share = $this->getShareFromId($data['id']);
+ $items = \OCP\Share::getItemShared('file', $share['item_source']);
+ $this->assertTrue(!empty($items));
+
+ $item = reset($items);
+ $this->assertTrue(is_array($item));
+ $this->assertEquals($date, substr($item['expiration'], 0, 10));
+
+ $fileinfo = $this->view->getFileInfo($this->folder);
+ \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
+ }
+
+ public function testCreatePublicLinkExpireDateValid() {
+ $config = \OC::$server->getConfig();
+
+ // enforce expire date, by default 7 days after the file was shared
+ $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
+ $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
+
+ $date = new \DateTime();
+ $date->add(new \DateInterval('P5D'));
+
+ $_POST['path'] = $this->folder;
+ $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
+ $_POST['expireDate'] = $date->format('Y-m-d');
+
+ $result = \OCA\Files_Sharing\API\Local::createShare([]);
+
+ $this->assertTrue($result->succeeded());
+
+ $data = $result->getData();
+ $this->assertTrue(is_string($data['token']));
+
+ // check for correct link
+ $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
+ $this->assertEquals($url, $data['url']);
+
+
+ $share = $this->getShareFromId($data['id']);
+ $items = \OCP\Share::getItemShared('file', $share['item_source']);
+ $this->assertTrue(!empty($items));
+
+ $item = reset($items);
+ $this->assertTrue(is_array($item));
+ $this->assertEquals($date->format('Y-m-d'), substr($item['expiration'], 0, 10));
+
+ $fileinfo = $this->view->getFileInfo($this->folder);
+ \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
+
+ $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
+ $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
+ }
+
+ public function testCreatePublicLinkExpireDateInvalidFuture() {
+ $config = \OC::$server->getConfig();
+
+ // enforce expire date, by default 7 days after the file was shared
+ $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
+ $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
+
+ $date = new \DateTime();
+ $date->add(new \DateInterval('P8D'));
+
+ $_POST['path'] = $this->folder;
+ $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
+ $_POST['expireDate'] = $date->format('Y-m-d');
+
+ $result = \OCA\Files_Sharing\API\Local::createShare([]);
+
+ $this->assertFalse($result->succeeded());
+ $this->assertEquals(404, $result->getStatusCode());
+ $this->assertEquals('Cannot set expiration date. Shares cannot expire later than 7 after they have been shared', $result->getMeta()['message']);
+
+ $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
+ $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
+ }
+
+ public function testCreatePublicLinkExpireDateInvalidPast() {
+ $config = \OC::$server->getConfig();
+
+ $date = new \DateTime();
+ $date->sub(new \DateInterval('P8D'));
+
+ $_POST['path'] = $this->folder;
+ $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
+ $_POST['expireDate'] = $date->format('Y-m-d');
+
+ $result = \OCA\Files_Sharing\API\Local::createShare([]);
+
+ $this->assertFalse($result->succeeded());
+ $this->assertEquals(404, $result->getStatusCode());
+ $this->assertEquals('Cannot set expiration date. Expiration date is in the past', $result->getMeta()['message']);
+
+ $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
+ $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
+ }
+
}