Passed
Push — master ( fa41e0...4bd63f )
by Daniel
07:07 queued 12s
created

WebsiteRequest   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 62.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
eloc 68
c 1
b 0
f 0
dl 0
loc 174
ccs 42
cts 67
cp 0.6269
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getViewer() 0 3 1
D assertViewerAccess() 0 89 23
A getPage() 0 3 1
A getWebsite() 0 3 1
A isProxyRequest() 0 3 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() === Website::TYPE_PUBLIC) {
96 7
			if (empty($meta['access'])) {
97 7
				return;
98
			}
99
100
			$groupPageAccess = $meta['access'];
101
			if (!is_array($groupPageAccess)) {
102
				$groupPageAccess = explode(',', $groupPageAccess);
103
			}
104
105
			foreach ($groupPageAccess 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 1
			$groupAccess = $this->website->getOption('group_access') ?? [];
130 1
			foreach ($groupAccess as $group) {
131
				if ($this->groupManager->groupExists($group)) {
132
					if ($this->groupManager->isInGroup($this->getViewer(), $group)) {
133
						return;
134
					}
135
				}
136
			}
137
138
			/** @var OCFolder $viewerOCFolder */
139 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

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