Completed
Push — master ( f2b8d7...c7becd )
by Roeland
11:44 queued 10:17
created

TokenManager::getTokenForTemplate()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 30

Duplication

Lines 10
Ratio 33.33 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 10
loc 30
ccs 0
cts 23
cp 0
rs 8.8177
c 0
b 0
f 0
cc 6
nc 4
nop 3
crap 42
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
				$updatable = $rootFolder->isUpdateable();
103
				// Check if the editor (user who is accessing) is in editable group
104
				// UserCanWrite only if
105
				// 1. No edit groups are set or
106
				// 2. if they are set, it is in one of the edit groups
107
				$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
108 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...
109
					$updatable = false;
110
					foreach($editGroups as $editGroup) {
111
						 $editorGroup = \OC::$server->getGroupManager()->get($editGroup);
112
						 if ($editorGroup !== null && sizeof($editorGroup->searchUsers($editoruid)) > 0) {
113
							$updatable = true;
114
							break;
115
						 }
116
					}
117
				}
118
			} catch (\Exception $e) {
119
				throw $e;
120
			}
121
		} else {
122
			// no active user login while generating the token
123
			// this is required during WopiPutRelativeFile
124
			if (is_null($editoruid)) {
125
				\OC::$server->getLogger()->warning('Generating token for SaveAs without editoruid');
126
			}
127
			$rootFolder = $this->rootFolder;
128
			$updatable = true;
129
		}
130
		/** @var File $file */
131
		$file = $rootFolder->getById($fileId)[0];
132
		// If its a public share, use the owner from the share, otherwise check the file object
133
		if (is_null($owneruid)) {
134
			$owneruid = $file->getOwner()->getUID();
135
		}
136
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');//$this->request->getServerProtocol() . '://' . $this->request->getServerHost();
137
138
		if (is_null($this->userId) && isset($_COOKIE['guestUser'])) {
139
			$guest_name = $this->trans->t('Guest: %s', $_COOKIE['guestUser']);
140
		} else {
141
			$guest_name = NULL;
142
		}
143
144
		$wopi = $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, (int)$updatable, $serverHost, $guest_name);
145
146
		try {
147
148
			return [
149
				$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'],
150
				$wopi->getToken(),
151
			];
152
		} catch (\Exception $e){
153
			throw $e;
154
		}
155
	}
156
157
	public function getTokenForTemplate(File $file, $userId, $templateDestination) {
158
		$owneruid = $userId;
159
		$editoruid = $userId;
160
		$rootFolder = $this->rootFolder->getUserFolder($editoruid);
161
		$updatable = $rootFolder->isUpdateable();
162
		// Check if the editor (user who is accessing) is in editable group
163
		// UserCanWrite only if
164
		// 1. No edit groups are set or
165
		// 2. if they are set, it is in one of the edit groups
166
		$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
167 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...
168
			$updatable = false;
169
			foreach($editGroups as $editGroup) {
170
				$editorGroup = \OC::$server->getGroupManager()->get($editGroup);
171
				if ($editorGroup !== null && sizeof($editorGroup->searchUsers($editoruid)) > 0) {
172
					$updatable = true;
173
					break;
174
				}
175
			}
176
		}
177
178
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');
179
180
		$wopi = $this->wopiMapper->generateFileToken($file->getId(), $owneruid, $editoruid, 0, (int)$updatable, $serverHost, null, $templateDestination);
181
182
		return [
183
			$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'],
184
			$wopi->getToken(),
185
		];
186
	}
187
}
188