permissionsmask.php 3.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php
/**
 * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace OC\Files\Storage\Wrapper;

use OC\Files\Cache\Wrapper\CachePermissionsMask;
12
use OCP\Constants;
13
14
15
16

/**
 * Mask the permissions of a storage
 *
17
18
 * This can be used to restrict update, create, delete and/or share permissions of a storage
 *
19
20
21
22
 * Note that the read permissions cant be masked
 */
class PermissionsMask extends Wrapper {
	/**
23
	 * @var int the permissions bits we want to keep
24
25
26
	 */
	private $mask;

27
28
29
30
31
32
	/**
	 * @param array $arguments ['storage' => $storage, 'mask' => $mask]
	 *
	 * $storage: The storage the permissions mask should be applied on
	 * $mask: The permission bits that should be kept, a combination of the \OCP\Constant::PERMISSION_ constants
	 */
33
34
35
36
37
38
39
40
41
42
	public function __construct($arguments) {
		parent::__construct($arguments);
		$this->mask = $arguments['mask'];
	}

	private function checkMask($permissions) {
		return ($this->mask & $permissions) === $permissions;
	}

	public function isUpdatable($path) {
43
		return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::isUpdatable($path);
44
45
46
	}

	public function isCreatable($path) {
47
		return $this->checkMask(Constants::PERMISSION_CREATE) and parent::isCreatable($path);
48
49
50
	}

	public function isDeletable($path) {
51
		return $this->checkMask(Constants::PERMISSION_DELETE) and parent::isDeletable($path);
52
53
54
55
56
57
58
	}

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

	public function rename($path1, $path2) {
59
		return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($path1, $path2);
60
61
62
	}

	public function copy($path1, $path2) {
63
		return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($path1, $path2);
64
65
66
	}

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

	public function mkdir($path) {
72
		return $this->checkMask(Constants::PERMISSION_CREATE) and parent::mkdir($path);
73
74
75
	}

	public function rmdir($path) {
76
		return $this->checkMask(Constants::PERMISSION_DELETE) and parent::rmdir($path);
77
78
79
	}

	public function unlink($path) {
80
		return $this->checkMask(Constants::PERMISSION_DELETE) and parent::unlink($path);
81
82
83
	}

	public function file_put_contents($path, $data) {
84
		$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
85
86
87
88
89
90
91
		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 {
92
			$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
			return $this->checkMask($permissions) ? parent::fopen($path, $mode) : false;
		}
	}

	/**
	 * get a cache instance for the storage
	 *
	 * @param string $path
	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
	 * @return \OC\Files\Cache\Cache
	 */
	public function getCache($path = '', $storage = null) {
		if (!$storage) {
			$storage = $this;
		}
		$sourceCache = parent::getCache($path, $storage);
		return new CachePermissionsMask($sourceCache, $this->mask);
	}
}