Skip to content

Commit f3ab5b9

Browse files
committed
Add public-files to DAV including ACL handling
1 parent df728bc commit f3ab5b9

File tree

7 files changed

+383
-1
lines changed

7 files changed

+383
-1
lines changed

apps/dav/lib/DAV/PublicAuth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class PublicAuth implements BackendInterface {
3535
public function __construct() {
3636
$this->publicURLs = [
3737
'public-calendars',
38+
'public-files',
3839
'principals/system/public'
3940
];
4041
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* @author Thomas Müller <[email protected]>
4+
*
5+
* @copyright Copyright (c) 2017, ownCloud GmbH
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+
22+
namespace OCA\DAV\Files\PublicFiles;
23+
24+
use OC\Share\Constants;
25+
use OCP\Share\Exceptions\ShareNotFound;
26+
use OCP\Share\IManager;
27+
use OCP\Share\IShare;
28+
use Sabre\DAV\Collection;
29+
use Sabre\DAV\Exception\MethodNotAllowed;
30+
use Sabre\DAV\Exception\NotFound;
31+
use Sabre\DAV\SimpleCollection;
32+
use Sabre\DAV\SimpleFile;
33+
34+
class RootCollection extends Collection {
35+
36+
/** @var IManager */
37+
private $shareManager;
38+
/** @var \OCP\IL10N */
39+
protected $l10n;
40+
41+
/**
42+
* If this value is set to true, it effectively disables listing of users
43+
* it still allows user to find other users if they have an exact url.
44+
*
45+
* @var bool
46+
*/
47+
public $disableListing = false;
48+
49+
function __construct() {
50+
$this->l10n = \OC::$server->getL10N('dav');
51+
$this->shareManager = \OC::$server->getShareManager();
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
function getName() {
58+
return 'public-files';
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
function getChild($name) {
65+
try {
66+
$share = $this->shareManager->getShareByToken($name);
67+
$password = $share->getPassword();
68+
// TODO: check password
69+
return new ShareNode($share);
70+
} catch (ShareNotFound $ex) {
71+
throw new NotFound();
72+
}
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
function getChildren() {
79+
if ($this->disableListing) {
80+
throw new MethodNotAllowed('Listing members of this collection is disabled');
81+
}
82+
83+
$shares = $this->shareManager->getAllSharedWith(null, [Constants::SHARE_TYPE_LINK]);
84+
return array_map(function(IShare $share) {
85+
return new ShareNode($share);
86+
}, $shares);
87+
}
88+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: deepdiver
5+
* Date: 26.10.17
6+
* Time: 14:40
7+
*/
8+
9+
namespace OCA\DAV\Files\PublicFiles;
10+
11+
12+
use OCP\Files\FileInfo;
13+
use OCP\Files\Node;
14+
use OCP\Share\IShare;
15+
use Sabre\DAV\Collection;
16+
use Sabre\DAV\INode;
17+
18+
class ShareNode extends Collection {
19+
20+
/** @var IShare */
21+
private $share;
22+
23+
public function __construct(IShare $share) {
24+
$this->share = $share;
25+
}
26+
/**
27+
* Returns an array with all the child nodes
28+
*
29+
* @return INode[]
30+
*/
31+
function getChildren() {
32+
if ($this->share->getNodeType() === 'folder') {
33+
$nodes = $this->share->getNode()->getDirectoryListing();
34+
} else {
35+
$nodes = [$this->share->getNode()];
36+
}
37+
return array_map(function(Node $node) {
38+
if ($node->getType() === FileInfo::TYPE_FOLDER) {
39+
return new SharedFolder($node, $this->share);
40+
}
41+
return new SharedFile($node, $this->share);
42+
}, $nodes);
43+
}
44+
45+
/**
46+
* Returns the name of the node.
47+
*
48+
* This is used to generate the url.
49+
*
50+
* @return string
51+
*/
52+
function getName() {
53+
return $this->share->getToken();
54+
}
55+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* @author Thomas Müller <[email protected]>
4+
*
5+
* @copyright Copyright (c) 2017, ownCloud GmbH
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+
22+
23+
namespace OCA\DAV\Files\PublicFiles;
24+
25+
26+
use OCP\Share\IShare;
27+
use Sabre\DAV\File;
28+
use Sabre\DAVACL\ACLTrait;
29+
use Sabre\DAVACL\IACL;
30+
31+
/**
32+
* Class MetaFile
33+
* This is a Sabre based implementation of a file living in the /meta resource.
34+
*
35+
* @package OCA\DAV\Meta
36+
*/
37+
class SharedFile extends File implements IACL {
38+
39+
use ACLTrait;
40+
41+
/** @var \OCP\Files\File */
42+
private $file;
43+
44+
/**
45+
* MetaFolder constructor.
46+
*
47+
* @param \OCP\Files\File $file
48+
* @param IShare $share
49+
*/
50+
public function __construct(\OCP\Files\File $file, IShare $share) {
51+
$this->file = $file;
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
function getName() {
58+
return $this->file->getName();
59+
}
60+
61+
public function getSize() {
62+
return $this->file->getSize();
63+
}
64+
65+
public function getContentType() {
66+
return $this->file->getMimeType();
67+
}
68+
69+
public function getETag() {
70+
return $this->file->getETag();
71+
}
72+
73+
function getLastModified() {
74+
return $this->file->getMTime();
75+
}
76+
77+
function delete() {
78+
// TODO: check permissions - via ACL?
79+
$this->file->delete();
80+
}
81+
82+
// function setName($name) {
83+
// $this->file->setName($name);
84+
// }
85+
86+
function getOwner() {
87+
return '';
88+
}
89+
90+
function getACL() {
91+
return [
92+
[
93+
'privilege' => '{DAV:}all',
94+
'principal' => '{DAV:}owner',
95+
'protected' => true,
96+
],
97+
[
98+
'privilege' => '{DAV:}read',
99+
'principal' => 'principals/system/public',
100+
'protected' => true,
101+
]
102+
];
103+
}
104+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* @author Thomas Müller <[email protected]>
4+
*
5+
* @copyright Copyright (c) 2017, ownCloud GmbH
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+
22+
23+
namespace OCA\DAV\Files\PublicFiles;
24+
25+
26+
use OCP\Constants;
27+
use OCP\Files\File;
28+
use OCP\Files\Folder;
29+
use OCP\Files\Node;
30+
use OCP\Share\IShare;
31+
use Sabre\DAV\Collection;
32+
use Sabre\DAVACL\ACLTrait;
33+
use Sabre\DAVACL\IACL;
34+
35+
/**
36+
* Class MetaFolder
37+
* This is a Sabre based implementation of a folder living in the /meta resource.
38+
*
39+
* @package OCA\DAV\Meta
40+
*/
41+
class SharedFolder extends Collection implements IACL {
42+
use ACLTrait;
43+
44+
/** @var Folder */
45+
private $folder;
46+
/** @var IShare */
47+
private $share;
48+
49+
/**
50+
* MetaFolder constructor.
51+
*
52+
* @param Folder $folder
53+
*/
54+
public function __construct(Folder $folder, IShare $share) {
55+
$this->folder = $folder;
56+
$this->share = $share;
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
function getChildren() {
63+
$nodes = $this->folder->getDirectoryListing();
64+
return array_map(function($node) {
65+
return $this->nodeFactory($node);
66+
}, $nodes);
67+
}
68+
69+
/**
70+
* @inheritdoc
71+
*/
72+
function getName() {
73+
return $this->folder->getName();
74+
}
75+
76+
function getLastModified() {
77+
return $this->folder->getMTime();
78+
}
79+
80+
function createDirectory($name) {
81+
$this->folder->newFolder($name);
82+
}
83+
84+
function createFile($name, $data = null) {
85+
$file = $this->folder->newFile($name);
86+
$file->putContent($data);
87+
88+
}
89+
90+
private function nodeFactory(Node $node) {
91+
if ($node instanceof Folder) {
92+
return new SharedFolder($node, $this->share);
93+
}
94+
if ($node instanceof File) {
95+
return new SharedFile($node, $this->share);
96+
}
97+
throw new \InvalidArgumentException();
98+
}
99+
100+
function getACL() {
101+
$acl = [
102+
[
103+
'privilege' => '{DAV:}all',
104+
'principal' => '{DAV:}owner',
105+
'protected' => true,
106+
],
107+
[
108+
'privilege' => '{DAV:}read',
109+
'principal' => 'principals/system/public',
110+
'protected' => true,
111+
]
112+
];
113+
114+
// TODO: add more acl to convert the logic
115+
if ($this->share->getPermissions() & Constants::PERMISSION_DELETE === Constants::PERMISSION_DELETE) {
116+
$acl[]= [
117+
[
118+
'privilege' => '{DAV:}unbind',
119+
'principal' => 'principals/system/public',
120+
'protected' => true,
121+
]
122+
];
123+
}
124+
125+
return $acl;
126+
}
127+
128+
}

0 commit comments

Comments
 (0)