Completed
Push — master ( 1d0c4f...3560a7 )
by Julius
02:27 queued 10s
created

TokenManager::getToken()   D

Complexity

Conditions 17
Paths 195

Size

Total Lines 83

Duplication

Lines 10
Ratio 12.05 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
dl 10
loc 83
ccs 0
cts 66
cp 0
rs 4.425
c 0
b 0
f 0
cc 17
nc 195
nop 3
crap 306

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Richdocuments;
23
24
use OC\Share\Constants;
25
use OCA\Richdocuments\Db\WopiMapper;
26
use OCA\Richdocuments\Helper;
27
use OCA\Richdocuments\Db\Wopi;
28
use OCA\Richdocuments\WOPI\Parser;
29
use OCP\Files\File;
30
use OCP\Files\IRootFolder;
31
use OCP\IURLGenerator;
32
use OCP\Share\IManager;
33
use OCP\IL10N;
34
35
class TokenManager {
36
	/** @var IRootFolder */
37
	private $rootFolder;
38
	/** @var IManager */
39
	private $shareManager;
40
	/** @var IURLGenerator */
41
	private $urlGenerator;
42
	/** @var Parser */
43
	private $wopiParser;
44
	/** @var AppConfig */
45
	private $appConfig;
46
	/** @var string */
47
	private $userId;
48
	/** @var WopiMapper */
49
	private $wopiMapper;
50
	/** @var IL10N */
51
	private $trans;
52
53
	/**
54
	 * @param IRootFolder $rootFolder
55
	 * @param IManager $shareManager
56
	 * @param IURLGenerator $urlGenerator
57
	 * @param Parser $wopiParser
58
	 * @param AppConfig $appConfig
59
	 * @param string $UserId
60
	 * @param WopiMapper $wopiMapper
61
	 * @param IL10N $trans
62
	 */
63
	public function __construct(IRootFolder $rootFolder,
64
								IManager $shareManager,
65
								IURLGenerator $urlGenerator,
66
								Parser $wopiParser,
67
								AppConfig $appConfig,
68
								$UserId,
69
								WopiMapper $wopiMapper,
70
								IL10N $trans) {
71
		$this->rootFolder = $rootFolder;
72
		$this->shareManager = $shareManager;
73
		$this->urlGenerator = $urlGenerator;
74
		$this->wopiParser = $wopiParser;
75
		$this->appConfig = $appConfig;
76
		$this->trans = $trans;
77
		$this->userId = $UserId;
78
		$this->wopiMapper = $wopiMapper;
79
	}
80
81
	/**
82
	 * @param string $fileId
83
	 * @param string $shareToken
84
	 * @param string $editoruid
85
	 * @return array
86
	 * @throws \Exception
87
	 */
88
	public function getToken($fileId, $shareToken = null, $editoruid = null) {
89
		list($fileId,, $version) = Helper::parseFileId($fileId);
90
		$owneruid = null;
91
		// if the user is not logged-in do use the sharers storage
92
		if($shareToken !== null) {
93
			/** @var File $file */
94
			$rootFolder = $this->rootFolder;
95
			$share = $this->shareManager->getShareByToken($shareToken);
96
			$updatable = (bool)($share->getPermissions() & \OCP\Constants::PERMISSION_UPDATE);
97
			$owneruid = $share->getShareOwner();
98
		} else if (!is_null($this->userId)) {
99
			try {
100
				$editoruid = $this->userId;
101
				$rootFolder = $this->rootFolder->getUserFolder($editoruid);
102
103
				$files = $rootFolder->getById((int)$fileId);
104
				$updatable = false;
105
				foreach ($files as $file) {
106
					if ($file->isUpdateable()) {
107
						$updatable = true;
108
						break;
109
					}
110
				}
111
112
				// Check if the editor (user who is accessing) is in editable group
113
				// UserCanWrite only if
114
				// 1. No edit groups are set or
115
				// 2. if they are set, it is in one of the edit groups
116
				$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
117 View Code Duplication
				if ($updatable && count($editGroups) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
					$updatable = false;
119
					foreach($editGroups as $editGroup) {
120
						 $editorGroup = \OC::$server->getGroupManager()->get($editGroup);
121
						 if ($editorGroup !== null && sizeof($editorGroup->searchUsers($editoruid)) > 0) {
122
							$updatable = true;
123
							break;
124
						 }
125
					}
126
				}
127
			} catch (\Exception $e) {
128
				throw $e;
129
			}
130
		} else {
131
			// no active user login while generating the token
132
			// this is required during WopiPutRelativeFile
133
			if (is_null($editoruid)) {
134
				\OC::$server->getLogger()->warning('Generating token for SaveAs without editoruid');
135
			}
136
			$rootFolder = $this->rootFolder;
137
			$updatable = true;
138
		}
139
		/** @var File $file */
140
		$file = $rootFolder->getById($fileId)[0];
141
		// If its a public share, use the owner from the share, otherwise check the file object
142
		if (is_null($owneruid)) {
143
			$owner = $file->getOwner();
144
			if (is_null($owner)) {
145
				// Editor UID instead of owner UID in case owner is null e.g. group folders
146
				$owneruid = $editoruid;
147
			} else {
148
				$owneruid = $owner->getUID();
149
			}
150
		}
151
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');//$this->request->getServerProtocol() . '://' . $this->request->getServerHost();
152
153
		if ($this->userId === null && isset($_COOKIE['guestUser'])) {
154
			$guest_name = $this->trans->t('Guest: %s', \OC_Util::sanitizeHTML($_COOKIE['guestUser']));
155
		} else {
156
			$guest_name = NULL;
157
		}
158
159
		$wopi = $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, (int)$updatable, $serverHost, $guest_name);
160
161
		try {
162
163
			return [
164
				$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'],
165
				$wopi->getToken(),
166
			];
167
		} catch (\Exception $e){
168
			throw $e;
169
		}
170
	}
171
172
	public function getTokenForTemplate(File $file, $userId, $templateDestination) {
173
		$owneruid = $userId;
174
		$editoruid = $userId;
175
		$updatable = $file->isUpdateable();
176
		// Check if the editor (user who is accessing) is in editable group
177
		// UserCanWrite only if
178
		// 1. No edit groups are set or
179
		// 2. if they are set, it is in one of the edit groups
180
		$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
181 View Code Duplication
		if ($updatable && count($editGroups) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
			$updatable = false;
183
			foreach($editGroups as $editGroup) {
184
				$editorGroup = \OC::$server->getGroupManager()->get($editGroup);
185
				if ($editorGroup !== null && sizeof($editorGroup->searchUsers($editoruid)) > 0) {
186
					$updatable = true;
187
					break;
188
				}
189
			}
190
		}
191
192
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');
193
194
		$wopi = $this->wopiMapper->generateFileToken($file->getId(), $owneruid, $editoruid, 0, (int)$updatable, $serverHost, null, $templateDestination);
195
196
		return [
197
			$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'],
198
			$wopi->getToken(),
199
		];
200
	}
201
}
202