Skip to content
Closed
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: 0 additions & 3 deletions apps/dav/lib/DAV/PublicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class PublicAuth implements BackendInterface {
/** @var string[] */
private $publicURLs;

/**
* @param string[] $publicURLs
*/
public function __construct() {
$this->publicURLs = [
'public-calendars',
Expand Down
116 changes: 116 additions & 0 deletions apps/dav/lib/Files/PublicFiles/PublicSharingAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* @author Thomas Müller <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\DAV\Files\PublicFiles;

use OCP\Share\IManager;
use OCP\Share\IShare;
use Sabre\DAV\Auth\Backend\AbstractBasic;
use Sabre\DAV\Server;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;

class PublicSharingAuth extends AbstractBasic {

/** @var Server */
private $server;
/** @var IShare */
private $share;
/** @var IManager */
private $shareManager;

/**
* PublicSharingAuth constructor.
*
* @param Server $server
*/
public function __construct(Server $server, IManager $manager) {
$this->server = $server;
$this->shareManager = $manager;
$this->principalPrefix = 'principals/system/';
$this->setRealm('owncloud/share');
}

/**
* When this method is called, the backend must check if authentication was
* successful.
*
* The returned value must be one of the following
*
* [true, "principals/username"]
* [false, "reason for failure"]
*
* If authentication was successful, it's expected that the authentication
* backend returns a so-called principal url.
*
* Examples of a principal url:
*
* principals/admin
* principals/user1
* principals/users/joe
* principals/uid/123457
*
* If you don't use WebDAV ACL (RFC3744) we recommend that you simply
* return a string such as:
*
* principals/users/[username]
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return array
*/
function check(RequestInterface $request, ResponseInterface $response) {

$node = $this->server->tree->getNodeForPath($request->getPath());
if (!$node instanceof ShareNode && !$node instanceof SharedFile && !$node instanceof SharedFolder) {
return [true, "principals/system/public"];
}
$this->share = $node->getShare();
$password = $this->share->getPassword();
if ($password === null) {
return [true, "principals/system/public"];
}

return parent::check($request, $response);
}

/**
* @inheritdoc
*/
function challenge(RequestInterface $request, ResponseInterface $response) {
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if purposefully left empty, please add a comment stating so


/**
* Validates a username and password
*
* This method should return true or false depending on if login
* succeeded.
*
* @param string $username
* @param string $password
* @return bool
*/
protected function validateUserPass($username, $password) {
if ($username !== 'public') {
return false;
}
return $this->shareManager->checkPassword($this->share, $password);
}
}
87 changes: 87 additions & 0 deletions apps/dav/lib/Files/PublicFiles/RootCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* @author Thomas Müller <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\DAV\Files\PublicFiles;

use OC\Share\Constants;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
use Sabre\DAV\Collection;
use Sabre\DAV\Exception\MethodNotAllowed;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\SimpleCollection;
use Sabre\DAV\SimpleFile;

class RootCollection extends Collection {

/** @var IManager */
private $shareManager;
/** @var \OCP\IL10N */
protected $l10n;

/**
* If this value is set to true, it effectively disables listing of users
* it still allows user to find other users if they have an exact url.
*
* @var bool
*/
public $disableListing = false;

function __construct() {
$this->l10n = \OC::$server->getL10N('dav');
$this->shareManager = \OC::$server->getShareManager();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use DI ?

}

/**
* @inheritdoc
*/
function getName() {
return 'public-files';
}

/**
* @inheritdoc
*/
function getChild($name) {
try {
$share = $this->shareManager->getShareByToken($name);
$password = $share->getPassword();
return new ShareNode($share);
} catch (ShareNotFound $ex) {
throw new NotFound();
}
}

/**
* @inheritdoc
*/
function getChildren() {
if ($this->disableListing) {
throw new MethodNotAllowed('Listing members of this collection is disabled');
}

$shares = $this->shareManager->getAllSharedWith(null, [Constants::SHARE_TYPE_LINK]);
return array_map(function(IShare $share) {
return new ShareNode($share);
}, $shares);
}
}
59 changes: 59 additions & 0 deletions apps/dav/lib/Files/PublicFiles/ShareNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Created by PhpStorm.
* User: deepdiver
* Date: 26.10.17
* Time: 14:40
*/

namespace OCA\DAV\Files\PublicFiles;


use OCP\Files\FileInfo;
use OCP\Files\Node;
use OCP\Share\IShare;
use Sabre\DAV\Collection;
use Sabre\DAV\INode;

class ShareNode extends Collection {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPDoc, what is this node about ?


/** @var IShare */
private $share;

public function __construct(IShare $share) {
$this->share = $share;
}
/**
* Returns an array with all the child nodes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest clarifying and saying we are returning both SharedFolder and SharedFile nodes depending on child types

*
* @return INode[]
*/
function getChildren() {
if ($this->share->getNodeType() === 'folder') {
$nodes = $this->share->getNode()->getDirectoryListing();
} else {
$nodes = [$this->share->getNode()];
}
return array_map(function(Node $node) {
if ($node->getType() === FileInfo::TYPE_FOLDER) {
return new SharedFolder($node, $this->share);
}
return new SharedFile($node, $this->share);
}, $nodes);
}

/**
* Returns the name of the node.
*
* This is used to generate the url.
*
* @return string
*/
function getName() {
return $this->share->getToken();
}

function getShare() {
return $this->share;
}
}
109 changes: 109 additions & 0 deletions apps/dav/lib/Files/PublicFiles/SharedFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* @author Thomas Müller <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/


namespace OCA\DAV\Files\PublicFiles;


use OCP\Share\IShare;
use Sabre\DAV\File;
use Sabre\DAVACL\ACLTrait;
use Sabre\DAVACL\IACL;

/**
* Class MetaFile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please adjust, smells like copy-pasted from another PR 😉

* This is a Sabre based implementation of a file living in the /meta resource.
*
* @package OCA\DAV\Meta
*/
class SharedFile extends File implements IACL {

use ACLTrait;

/** @var \OCP\Files\File */
private $file;

/**
* MetaFolder constructor.
*
* @param \OCP\Files\File $file
* @param IShare $share
*/
public function __construct(\OCP\Files\File $file, IShare $share) {
$this->file = $file;
}

/**
* @inheritdoc
*/
function getName() {
return $this->file->getName();
}

public function getSize() {
return $this->file->getSize();
}

public function getContentType() {
return $this->file->getMimeType();
}

public function getETag() {
return $this->file->getETag();
}

function getLastModified() {
return $this->file->getMTime();
}

function delete() {
// TODO: check permissions - via ACL?
$this->file->delete();
}

// function setName($name) {
// $this->file->setName($name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can forbid renaming share file here. This is because the name itself isn't visible anyway but someone might attempt to hack the API to try it out.

For local shares the file name is a received mount point. But here for link shares this is no mount point.

Throw Forbidden ?

// }

function getOwner() {
return '';
}

function getACL() {
return [
[
'privilege' => '{DAV:}all',
'principal' => '{DAV:}owner',
'protected' => true,
],
[
'privilege' => '{DAV:}read',
'principal' => 'principals/system/public',
'protected' => true,
]
];
}

function getShare() {
return $this->share;
}

}
Loading