Passed
Push — master ( 0c05c1...774e03 )
by Daniel
27:58
created

WebsiteRequest::getWebsite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2020, Daniel Rudolf (<[email protected]>)
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Model;
26
27
use OCA\CMSPico\Exceptions\WebsiteInvalidFilesystemException;
28
use OCA\CMSPico\Exceptions\WebsiteNotPermittedException;
29
use OCA\CMSPico\Files\StorageFile;
30
use OCA\CMSPico\Files\StorageFolder;
31
use OCA\CMSPico\Service\MiscService;
32
use OCP\Files\Folder as OCFolder;
33
use OCP\Files\InvalidPathException;
34
use OCP\Files\Node as OCNode;
35
use OCP\Files\NotFoundException;
36
use OCP\Files\NotPermittedException;
37
use OCP\IGroupManager;
38
39
class WebsiteRequest
40
{
41
	/** @var Website */
42
	private $website;
43
44
	/** @var string|null */
45
	private $viewer;
46
47
	/** @var string */
48
	private $page;
49
50
	/** @var bool */
51
	private $proxyRequest;
52
53
	/** @var IGroupManager */
54
	private $groupManager;
55
56
	/** @var MiscService */
57
	private $miscService;
58
59
	/**
60
	 * WebsiteRequest constructor.
61
	 *
62
	 * @param Website     $website
63
	 * @param string|null $viewer
64
	 * @param string      $page
65
	 * @param bool        $proxyRequest
66
	 */
67 9
	public function __construct(Website $website, string $viewer = null, string $page = '', bool $proxyRequest = false)
68
	{
69 9
		$this->groupManager = \OC::$server->getGroupManager();
0 ignored issues
show
Deprecated Code introduced by
The function OC\Server::getGroupManager() has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
		$this->groupManager = /** @scrutinizer ignore-deprecated */ \OC::$server->getGroupManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
70 9
		$this->miscService = \OC::$server->query(MiscService::class);
0 ignored issues
show
Deprecated Code introduced by
The function OC\ServerContainer::query() has been deprecated: 20.0.0 use \Psr\Container\ContainerInterface::get ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
		$this->miscService = /** @scrutinizer ignore-deprecated */ \OC::$server->query(MiscService::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
71
72 9
		$this->website = $website;
73 9
		$this->viewer = $viewer;
74 9
		$this->page = $page;
75 9
		$this->proxyRequest = $proxyRequest;
76
77 9
		if ($this->viewer === null) {
78 2
			$userSession = \OC::$server->getUserSession();
0 ignored issues
show
Deprecated Code introduced by
The function OC\Server::getUserSession() has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

78
			$userSession = /** @scrutinizer ignore-deprecated */ \OC::$server->getUserSession();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
79 2
			$this->viewer = $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : null;
80
		}
81 9
	}
82
83
	/**
84
	 * @param string $path
85
	 * @param array  $meta
86
	 *
87
	 * @throws InvalidPathException
88
	 * @throws WebsiteInvalidFilesystemException
89
	 * @throws WebsiteNotPermittedException
90
	 * @throws NotPermittedException
91
	 */
92 9
	public function assertViewerAccess(string $path, array $meta = []): void
93
	{
94 9
		$exceptionClass = WebsiteNotPermittedException::class;
95 9
		if ($this->website->getType() === WebsiteCore::TYPE_PUBLIC) {
96 7
			if (empty($meta['access'])) {
97 7
				return;
98
			}
99
100
			$groupAccess = $meta['access'];
101
			if (!is_array($groupAccess)) {
102
				$groupAccess = explode(',', $groupAccess);
103
			}
104
105
			foreach ($groupAccess as $group) {
106
				$group = trim($group);
107
108
				if ($group === 'public') {
109
					return;
110
				} elseif ($group === 'private') {
111
					continue;
112
				}
113
114
				if ($this->getViewer() && $this->groupManager->groupExists($group)) {
115
					if ($this->groupManager->isInGroup($this->getViewer(), $group)) {
116
						return;
117
					}
118
				}
119
			}
120
121
			$exceptionClass = NotPermittedException::class;
122
		}
123
124 2
		if ($this->getViewer()) {
125 2
			if ($this->getViewer() === $this->website->getUserId()) {
126 1
				return;
127
			}
128
129
			/** @var OCFolder $viewerOCFolder */
130 1
			$viewerOCFolder = \OC::$server->getUserFolder($this->getViewer());
0 ignored issues
show
Deprecated Code introduced by
The function OC\Server::getUserFolder() has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

130
			$viewerOCFolder = /** @scrutinizer ignore-deprecated */ \OC::$server->getUserFolder($this->getViewer());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
131
			$viewerAccessClosure = function (OCNode $node) use ($viewerOCFolder) {
132 1
				$nodeId = $node->getId();
133
134 1
				$viewerNodes = $viewerOCFolder->getById($nodeId);
135 1
				foreach ($viewerNodes as $viewerNode) {
136
					if ($viewerNode->isReadable()) {
137
						return true;
138
					}
139
				}
140
141 1
				return false;
142 1
			};
143
144 1
			$websiteFolder = $this->website->getWebsiteFolder();
145
146 1
			$path = $this->miscService->normalizePath($path);
147 1
			while ($path && ($path !== '.')) {
148
				try {
149
					/** @var StorageFile|StorageFolder $file */
150 1
					$file = $websiteFolder->get($path);
151
				} catch (NotFoundException $e) {
152
					$file = null;
153
				}
154
155 1
				if ($file) {
156 1
					if ($viewerAccessClosure($file->getOCNode())) {
157
						return;
158
					}
159
160 1
					throw new $exceptionClass();
161
				}
162
163
				$path = dirname($path);
164
			}
165
166
			if ($viewerAccessClosure($websiteFolder->getOCNode())) {
167
				return;
168
			}
169
		}
170
171
		throw new $exceptionClass();
172
	}
173
174
	/**
175
	 * @return Website
176
	 */
177 9
	public function getWebsite(): Website
178
	{
179 9
		return $this->website;
180
	}
181
182
	/**
183
	 * @return string|null
184
	 */
185 2
	public function getViewer(): ?string
186
	{
187 2
		return $this->viewer;
188
	}
189
190
	/**
191
	 * @return string
192
	 */
193 9
	public function getPage(): string
194
	{
195 9
		return $this->page;
196
	}
197
198
	/**
199
	 * @return bool
200
	 */
201 5
	public function isProxyRequest(): bool
202
	{
203 5
		return $this->proxyRequest;
204
	}
205
}
206