Completed
Push — master ( 4d412b...c0b425 )
by Morris
11s
created

DirectViewController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 7
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[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
namespace OCA\Richdocuments\Controller;
24
25
use OCA\Richdocuments\AppConfig;
26
use OCA\Richdocuments\Db\DirectMapper;
27
use OCA\Richdocuments\TokenManager;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Db\DoesNotExistException;
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\ContentSecurityPolicy;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\AppFramework\Http\TemplateResponse;
34
use OCP\Files\IRootFolder;
35
use OCP\Files\Node;
36
use OCP\IConfig;
37
use OCP\IRequest;
38
39
class DirectViewController extends Controller {
40
	/** @var IRootFolder */
41
	private $rootFolder;
42
43
	/** @var TokenManager */
44
	private $tokenManager;
45
46
	/** @var DirectMapper */
47
	private $directMapper;
48
49
	/** @var IConfig */
50
	private $config;
51
52
	/** @var AppConfig */
53
	private $appConfig;
54
55
	public function __construct($appName,
56
								IRequest $request,
57
								IRootFolder $rootFolder,
58
								TokenManager $tokenManager,
59
								DirectMapper $directMapper,
60
								IConfig $config,
61
								AppConfig $appConfig) {
62
		parent::__construct($appName, $request);
63
64
		$this->rootFolder = $rootFolder;
65
		$this->tokenManager = $tokenManager;
66
		$this->directMapper = $directMapper;
67
		$this->config = $config;
68
		$this->appConfig = $appConfig;
69
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 * @NoCSRFRequired
74
	 * @PublicPage
75
	 *
76
	 * @param string $token
77
	 */
78
	public function show($token) {
79
		try {
80
			$direct = $this->directMapper->getByToken($token);
81
		} catch (DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
82
			//TODO show 404
83
			return new JSONResponse([], Http::STATUS_NOT_FOUND);
84
		}
85
86
		// Delete the token. They are for 1 time use only
87
		$this->directMapper->delete($direct);
88
89
		try {
90
			$folder = $this->rootFolder->getUserFolder($direct->getUid());
91
			$item = $folder->getById($direct->getFileid())[0];
92
			if(!($item instanceof Node)) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Node does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
93
				throw new \Exception();
94
			}
95
			list($urlSrc, $token) = $this->tokenManager->getToken($item->getId());
96
			$params = [
97
				'permissions' => $item->getPermissions(),
98
				'title' => $item->getName(),
99
				'fileId' => $item->getId() . '_' . $this->config->getSystemValue('instanceid'),
100
				'token' => $token,
101
				'urlsrc' => $urlSrc,
102
				'path' => $folder->getRelativePath($item->getPath()),
103
				'instanceId' => $this->config->getSystemValue('instanceid'),
104
				'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'),
105
				'direct' => true,
106
			];
107
108
			$response = new TemplateResponse('richdocuments', 'documents', $params, 'empty');
109
			$policy = new ContentSecurityPolicy();
110
			$policy->allowInlineScript(true);
111
			$policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url'));
112
			$response->setContentSecurityPolicy($policy);
113
			return $response;
114
		} catch (\Exception $e) {
115
			return new JSONResponse([], Http::STATUS_BAD_REQUEST);
116
		}
117
118
	}
119
}
120