Skip to content

Commit

Permalink
Add storage and cache wrappers to apply a permissions mask to a storage
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Nov 26, 2014
1 parent 6100117 commit 7605cd6
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 11 deletions.
32 changes: 32 additions & 0 deletions lib/private/files/cache/wrapper/cachepermissionsmask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <[email protected]>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/

namespace OC\Files\Cache\Wrapper;

class CachePermissionsMask extends CacheWrapper {
/**
* @var int
*/
protected $mask;

/**
* @param \OC\Files\Cache\Cache $cache
* @param int $mask
*/
public function __construct($cache, $mask) {
parent::__construct($cache);
$this->mask = $mask;
}

protected function formatCacheEntry($entry) {
if (isset($entry['permissions'])) {
$entry['permissions'] &= $this->mask;
}
return $entry;
}
}
23 changes: 12 additions & 11 deletions lib/private/files/storage/wrapper/permissionsmask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OC\Files\Storage\Wrapper;

use OC\Files\Cache\Wrapper\CachePermissionsMask;
use OCP\Constants;

/**
* Mask the permissions of a storage
Expand All @@ -31,56 +32,56 @@ private function checkMask($permissions) {
}

public function isUpdatable($path) {
return $this->checkMask(\OCP\PERMISSION_UPDATE) and parent::isUpdatable($path);
return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::isUpdatable($path);
}

public function isCreatable($path) {
return $this->checkMask(\OCP\PERMISSION_CREATE) and parent::isCreatable($path);
return $this->checkMask(Constants::PERMISSION_CREATE) and parent::isCreatable($path);
}

public function isDeletable($path) {
return $this->checkMask(\OCP\PERMISSION_DELETE) and parent::isDeletable($path);
return $this->checkMask(Constants::PERMISSION_DELETE) and parent::isDeletable($path);
}

public function getPermissions($path) {
return $this->storage->getPermissions($path) & $this->mask;
}

public function rename($path1, $path2) {
return $this->checkMask(\OCP\PERMISSION_UPDATE) and parent::rename($path1, $path2);
return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($path1, $path2);
}

public function copy($path1, $path2) {
return $this->checkMask(\OCP\PERMISSION_CREATE) and parent::copy($path1, $path2);
return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($path1, $path2);
}

public function touch($path, $mtime = null) {
$permissions = $this->file_exists($path) ? \OCP\PERMISSION_UPDATE : \OCP\PERMISSION_CREATE;
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkMask($permissions) and parent::touch($path, $mtime);
}

public function mkdir($path) {
return $this->checkMask(\OCP\PERMISSION_CREATE) and parent::mkdir($path);
return $this->checkMask(Constants::PERMISSION_CREATE) and parent::mkdir($path);
}

public function rmdir($path) {
return $this->checkMask(\OCP\PERMISSION_DELETE) and parent::rmdir($path);
return $this->checkMask(Constants::PERMISSION_DELETE) and parent::rmdir($path);
}

public function unlink($path) {
return $this->checkMask(\OCP\PERMISSION_DELETE) and parent::unlink($path);
return $this->checkMask(Constants::PERMISSION_DELETE) and parent::unlink($path);
}

public function file_put_contents($path, $data) {
$permissions = $this->file_exists($path) ? \OCP\PERMISSION_UPDATE : \OCP\PERMISSION_CREATE;
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkMask($permissions) and parent::file_put_contents($path, $data);
}

public function fopen($path, $mode) {
if ($mode === 'r' or $mode === 'rb') {
return parent::fopen($path, $mode);
} else {
$permissions = $this->file_exists($path) ? \OCP\PERMISSION_UPDATE : \OCP\PERMISSION_CREATE;
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkMask($permissions) ? parent::fopen($path, $mode) : false;
}
}
Expand Down
94 changes: 94 additions & 0 deletions tests/lib/files/cache/wrapper/cachepermissionsmask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <[email protected]>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/

namespace Test\Files\Cache\Wrapper;

use OCP\Constants;
use Test\Files\Cache\Cache;

class CachePermissionsMask extends Cache {
/**
* @var \OC\Files\Cache\Cache $sourceCache
*/
protected $sourceCache;

public function setUp() {
parent::setUp();
$this->storage->mkdir('foo');
$this->sourceCache = $this->cache;
$this->cache = $this->getMaskedCached(Constants::PERMISSION_ALL);
}

protected function getMaskedCached($mask) {
return new \OC\Files\Cache\Wrapper\CachePermissionsMask($this->sourceCache, $mask);
}

public function maskProvider() {
return array(
array(Constants::PERMISSION_ALL),
array(Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE),
array(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE),
array(Constants::PERMISSION_READ)
);
}

/**
* @dataProvider maskProvider
* @param int $mask
*/
public function testGetMasked($mask) {
$cache = $this->getMaskedCached($mask);
$data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'permissions' => Constants::PERMISSION_ALL);
$this->sourceCache->put('foo', $data);
$result = $cache->get('foo');
$this->assertEquals($mask, $result['permissions']);

$data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE);
$this->sourceCache->put('bar', $data);
$result = $cache->get('bar');
$this->assertEquals($mask & ~Constants::PERMISSION_DELETE, $result['permissions']);
}

/**
* @dataProvider maskProvider
* @param int $mask
*/
public function testGetFolderContentMasked($mask) {
$this->storage->mkdir('foo');
$this->storage->file_put_contents('foo/bar', 'asd');
$this->storage->file_put_contents('foo/asd', 'bar');
$this->storage->getScanner()->scan('');

$cache = $this->getMaskedCached($mask);
$files = $cache->getFolderContents('foo');
$this->assertCount(2, $files);

foreach ($files as $file) {
$this->assertEquals($mask & ~Constants::PERMISSION_CREATE, $file['permissions']);
}
}

/**
* @dataProvider maskProvider
* @param int $mask
*/
public function testSearchMasked($mask) {
$this->storage->mkdir('foo');
$this->storage->file_put_contents('foo/bar', 'asd');
$this->storage->file_put_contents('foo/foobar', 'bar');
$this->storage->getScanner()->scan('');

$cache = $this->getMaskedCached($mask);
$files = $cache->search('%bar');
$this->assertCount(2, $files);

foreach ($files as $file) {
$this->assertEquals($mask & ~Constants::PERMISSION_CREATE, $file['permissions']);
}
}
}
105 changes: 105 additions & 0 deletions tests/lib/files/storage/wrapper/permissionsmask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <[email protected]>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/

namespace Test\Files\Storage\Wrapper;

use OCP\Constants;

class PermissionsMask extends \Test\Files\Storage\Storage {

/**
* @var \OC\Files\Storage\Temporary
*/
private $sourceStorage;

public function setUp() {
parent::setUp();
$this->sourceStorage = new \OC\Files\Storage\Temporary(array());
$this->instance = $this->getMaskedStorage(Constants::PERMISSION_ALL);
}

public function tearDown() {
$this->sourceStorage->cleanUp();
parent::tearDown();
}

protected function getMaskedStorage($mask) {
return new \OC\Files\Storage\Wrapper\PermissionsMask(array(
'storage' => $this->sourceStorage,
'mask' => $mask
));
}

public function testMkdirNoCreate() {
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE);
$this->assertFalse($storage->mkdir('foo'));
$this->assertFalse($storage->file_exists('foo'));
}

public function testRmdirNoDelete() {
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE);
$this->assertTrue($storage->mkdir('foo'));
$this->assertTrue($storage->file_exists('foo'));
$this->assertFalse($storage->rmdir('foo'));
$this->assertTrue($storage->file_exists('foo'));
}

public function testTouchNewFileNoCreate() {
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE);
$this->assertFalse($storage->touch('foo'));
$this->assertFalse($storage->file_exists('foo'));
}

public function testTouchNewFileNoUpdate() {
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
$this->assertTrue($storage->touch('foo'));
$this->assertTrue($storage->file_exists('foo'));
}

public function testTouchExistingFileNoUpdate() {
$this->sourceStorage->touch('foo');
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
$this->assertFalse($storage->touch('foo'));
}

public function testUnlinkNoDelete() {
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE);
$this->assertTrue($storage->touch('foo'));
$this->assertTrue($storage->file_exists('foo'));
$this->assertFalse($storage->unlink('foo'));
$this->assertTrue($storage->file_exists('foo'));
}

public function testPutContentsNewFileNoUpdate() {
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
$this->assertTrue($storage->file_put_contents('foo', 'bar'));
$this->assertEquals('bar', $storage->file_get_contents('foo'));
}

public function testPutContentsNewFileNoCreate() {
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE);
$this->assertFalse($storage->file_put_contents('foo', 'bar'));
}

public function testPutContentsExistingFileNoUpdate() {
$this->sourceStorage->touch('foo');
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
$this->assertFalse($storage->file_put_contents('foo', 'bar'));
}

public function testFopenExistingFileNoUpdate() {
$this->sourceStorage->touch('foo');
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
$this->assertFalse($storage->fopen('foo', 'w'));
}

public function testFopenNewFileNoCreate() {
$storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE);
$this->assertFalse($storage->fopen('foo', 'w'));
}
}

0 comments on commit 7605cd6

Please sign in to comment.