Completed
Push — master ( 7f8a1e...9c5e64 )
by Julius
08:08 queued 10s
created

TokenManager::getToken()   F

Complexity

Conditions 19
Paths 198

Size

Total Lines 86

Duplication

Lines 10
Ratio 11.63 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 0
Metric Value
dl 10
loc 86
ccs 0
cts 69
cp 0
rs 3.7
c 0
b 0
f 0
cc 19
nc 198
nop 3
crap 380

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\IGroupManager;
32
use OCP\IURLGenerator;
33
use OCP\IUserManager;
34
use OCP\Share\IManager;
35
use OCP\IL10N;
36
37
class TokenManager {
38
	/** @var IRootFolder */
39
	private $rootFolder;
40
	/** @var IManager */
41
	private $shareManager;
42
	/** @var IURLGenerator */
43
	private $urlGenerator;
44
	/** @var Parser */
45
	private $wopiParser;
46
	/** @var AppConfig */
47
	private $appConfig;
48
	/** @var string */
49
	private $userId;
50
	/** @var WopiMapper */
51
	private $wopiMapper;
52
	/** @var IL10N */
53
	private $trans;
54
	/** @var IUserManager */
55
	private $userManager;
56
	/** @var IGroupManager */
57
	private $groupManager;
58
59
	/**
60
	 * @param IRootFolder $rootFolder
61
	 * @param IManager $shareManager
62
	 * @param IURLGenerator $urlGenerator
63
	 * @param Parser $wopiParser
64
	 * @param AppConfig $appConfig
65
	 * @param string $UserId
66
	 * @param WopiMapper $wopiMapper
67
	 * @param IL10N $trans
68
	 */
69 View Code Duplication
	public function __construct(IRootFolder $rootFolder,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
								IManager $shareManager,
71
								IURLGenerator $urlGenerator,
72
								Parser $wopiParser,
73
								AppConfig $appConfig,
74
								$UserId,
75
								WopiMapper $wopiMapper,
76
								IL10N $trans,
77
								IUserManager $userManager,
78
								IGroupManager $groupManager) {
79
		$this->rootFolder = $rootFolder;
80
		$this->shareManager = $shareManager;
81
		$this->urlGenerator = $urlGenerator;
82
		$this->wopiParser = $wopiParser;
83
		$this->appConfig = $appConfig;
84
		$this->trans = $trans;
85
		$this->userId = $UserId;
86
		$this->wopiMapper = $wopiMapper;
87
		$this->userManager = $userManager;
88
		$this->groupManager = $groupManager;
89
	}
90
91
	/**
92
	 * @param string $fileId
93
	 * @param string $shareToken
94
	 * @param string $editoruid
95
	 * @return array
96
	 * @throws \Exception
97
	 */
98
	public function getToken($fileId, $shareToken = null, $editoruid = null) {
99
		list($fileId,, $version) = Helper::parseFileId($fileId);
100
		$owneruid = null;
101
		$hideDownload = false;
102
		// if the user is not logged-in do use the sharers storage
103
		if($shareToken !== null) {
104
			/** @var File $file */
105
			$rootFolder = $this->rootFolder;
106
			$share = $this->shareManager->getShareByToken($shareToken);
107
			$updatable = (bool)($share->getPermissions() & \OCP\Constants::PERMISSION_UPDATE);
108
			$hideDownload = $share->getHideDownload();
109
			$owneruid = $share->getShareOwner();
110
		} else if (!is_null($this->userId)) {
111
			try {
112
				$editoruid = $this->userId;
113
				$rootFolder = $this->rootFolder->getUserFolder($editoruid);
114
115
				$files = $rootFolder->getById((int)$fileId);
116
				$updatable = false;
117
				foreach ($files as $file) {
118
					if ($file->isUpdateable()) {
119
						$updatable = true;
120
						break;
121
					}
122
				}
123
124
				// Check if the editor (user who is accessing) is in editable group
125
				// UserCanWrite only if
126
				// 1. No edit groups are set or
127
				// 2. if they are set, it is in one of the edit groups
128
				$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
129
				$editorUser = $this->userManager->get($editoruid);
130 View Code Duplication
				if ($updatable && count($editGroups) > 0 && $editorUser) {
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...
131
					$updatable = false;
132
					foreach($editGroups as $editGroup) {
133
						 $editorGroup = $this->groupManager->get($editGroup);
134
						 if ($editorGroup !== null && $editorGroup->inGroup($editorUser)) {
135
							$updatable = true;
136
							break;
137
						 }
138
					}
139
				}
140
			} catch (\Exception $e) {
141
				throw $e;
142
			}
143
		} else {
144
			// no active user login while generating the token
145
			// this is required during WopiPutRelativeFile
146
			if (is_null($editoruid)) {
147
				\OC::$server->getLogger()->warning('Generating token for SaveAs without editoruid');
148
			}
149
			$rootFolder = $this->rootFolder;
150
			$updatable = true;
151
		}
152
		/** @var File $file */
153
		$file = $rootFolder->getById($fileId)[0];
154
		// If its a public share, use the owner from the share, otherwise check the file object
155
		if (is_null($owneruid)) {
156
			$owner = $file->getOwner();
157
			if (is_null($owner)) {
158
				// Editor UID instead of owner UID in case owner is null e.g. group folders
159
				$owneruid = $editoruid;
160
			} else {
161
				$owneruid = $owner->getUID();
162
			}
163
		}
164
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');//$this->request->getServerProtocol() . '://' . $this->request->getServerHost();
165
166
		if ($this->userId === null && isset($_COOKIE['guestUser']) && $_COOKIE['guestUser'] !== '') {
167
			$guest_name = $this->trans->t('%s (Guest)', \OC_Util::sanitizeHTML($_COOKIE['guestUser']));
168
		} else {
169
			$guest_name = NULL;
170
		}
171
172
		$wopi = $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, (int)$updatable, $serverHost, $guest_name, 0, $hideDownload);
173
174
		try {
175
176
			return [
177
				$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'],
178
				$wopi->getToken(),
179
			];
180
		} catch (\Exception $e){
181
			throw $e;
182
		}
183
	}
184
185
	public function getTokenForTemplate(File $file, $userId, $templateDestination) {
186
		$owneruid = $userId;
187
		$editoruid = $userId;
188
		$updatable = $file->isUpdateable();
189
		// Check if the editor (user who is accessing) is in editable group
190
		// UserCanWrite only if
191
		// 1. No edit groups are set or
192
		// 2. if they are set, it is in one of the edit groups
193
		$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
194
		$editorUser = $this->userManager->get($editoruid);
195 View Code Duplication
		if ($updatable && count($editGroups) > 0 && $editorUser) {
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...
196
			$updatable = false;
197
			foreach($editGroups as $editGroup) {
198
				$editorGroup = $this->groupManager->get($editGroup);
199
				if ($editorGroup !== null && $editorGroup->inGroup($editorUser)) {
200
					$updatable = true;
201
					break;
202
				}
203
			}
204
		}
205
206
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');
207
208
		$wopi = $this->wopiMapper->generateFileToken($file->getId(), $owneruid, $editoruid, 0, (int)$updatable, $serverHost, null, $templateDestination);
209
210
		return [
211
			$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'],
212
			$wopi->getToken(),
213
		];
214
	}
215
}
216