Completed
Push — master ( 5926da...79706e )
by Lukas
13:35
created

PermissionsMask::checkMask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Robin McCorkell <[email protected]>
9
 * @author Stefan Weil <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\Files\Storage\Wrapper;
28
29
use OC\Files\Cache\Wrapper\CachePermissionsMask;
30
use OCP\Constants;
31
32
/**
33
 * Mask the permissions of a storage
34
 *
35
 * This can be used to restrict update, create, delete and/or share permissions of a storage
36
 *
37
 * Note that the read permissions can't be masked
38
 */
39
class PermissionsMask extends Wrapper {
40
	/**
41
	 * @var int the permissions bits we want to keep
42
	 */
43
	private $mask;
44
45
	/**
46
	 * @param array $arguments ['storage' => $storage, 'mask' => $mask]
47
	 *
48
	 * $storage: The storage the permissions mask should be applied on
49
	 * $mask: The permission bits that should be kept, a combination of the \OCP\Constant::PERMISSION_ constants
50
	 */
51
	public function __construct($arguments) {
52
		parent::__construct($arguments);
53
		$this->mask = $arguments['mask'];
54
	}
55
56
	private function checkMask($permissions) {
57
		return ($this->mask & $permissions) === $permissions;
58
	}
59
60
	public function isUpdatable($path) {
61
		return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::isUpdatable($path);
62
	}
63
64
	public function isCreatable($path) {
65
		return $this->checkMask(Constants::PERMISSION_CREATE) and parent::isCreatable($path);
66
	}
67
68
	public function isDeletable($path) {
69
		return $this->checkMask(Constants::PERMISSION_DELETE) and parent::isDeletable($path);
70
	}
71
72
	public function isSharable($path) {
73
		return $this->checkMask(Constants::PERMISSION_SHARE) and parent::isSharable($path);
74
	}
75
76
	public function getPermissions($path) {
77
		return $this->storage->getPermissions($path) & $this->mask;
78
	}
79
80
	public function rename($path1, $path2) {
81
		$p = strpos($path1, $path2);
82
		if ($p === 0) {
83
			$part = substr($path1, strlen($path2));
84
			//This is a rename of the transfer file to the original file
85
			if (strpos($part, '.ocTransferId') === 0) {
86
				return $this->checkMask(Constants::PERMISSION_CREATE) and parent::rename($path1, $path2);
87
			}
88
		}
89
		return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($path1, $path2);
90
	}
91
92
	public function copy($path1, $path2) {
93
		return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($path1, $path2);
94
	}
95
96
	public function touch($path, $mtime = null) {
97
		$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
98
		return $this->checkMask($permissions) and parent::touch($path, $mtime);
99
	}
100
101
	public function mkdir($path) {
102
		return $this->checkMask(Constants::PERMISSION_CREATE) and parent::mkdir($path);
103
	}
104
105
	public function rmdir($path) {
106
		return $this->checkMask(Constants::PERMISSION_DELETE) and parent::rmdir($path);
107
	}
108
109
	public function unlink($path) {
110
		return $this->checkMask(Constants::PERMISSION_DELETE) and parent::unlink($path);
111
	}
112
113
	public function file_put_contents($path, $data) {
114
		$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
115
		return $this->checkMask($permissions) and parent::file_put_contents($path, $data);
116
	}
117
118
	public function fopen($path, $mode) {
119
		if ($mode === 'r' or $mode === 'rb') {
120
			return parent::fopen($path, $mode);
121
		} else {
122
			$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
123
			return $this->checkMask($permissions) ? parent::fopen($path, $mode) : false;
124
		}
125
	}
126
127
	/**
128
	 * get a cache instance for the storage
129
	 *
130
	 * @param string $path
131
	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
132
	 * @return \OC\Files\Cache\Cache
133
	 */
134
	public function getCache($path = '', $storage = null) {
135
		if (!$storage) {
136
			$storage = $this;
137
		}
138
		$sourceCache = parent::getCache($path, $storage);
139
		return new CachePermissionsMask($sourceCache, $this->mask);
140
	}
141
}
142