Completed
Push — master ( dcad0e...b67dab )
by
unknown
02:27
created

TokenManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 92
ccs 0
cts 57
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
C getToken() 0 55 10
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\Helper;
26
use OCA\Richdocuments\Db\Wopi;
27
use OCA\Richdocuments\WOPI\Parser;
28
use OCP\Files\File;
29
use OCP\Files\IRootFolder;
30
use OCP\IURLGenerator;
31
use OCP\Share\IManager;
32
33
class TokenManager {
34
	/** @var IRootFolder */
35
	private $rootFolder;
36
	/** @var IManager */
37
	private $shareManager;
38
	/** @var IURLGenerator */
39
	private $urlGenerator;
40
	/** @var Parser */
41
	private $wopiParser;
42
43
	/**
44
	 * @param IRootFolder $rootFolder
45
	 * @param IManager $shareManager
46
	 * @param IURLGenerator $urlGenerator
47
	 * @param string $UserId
48
	 */
49
	public function __construct(IRootFolder $rootFolder,
50
								IManager $shareManager,
51
								IURLGenerator $urlGenerator,
52
								Parser $wopiParser,
53
								AppConfig $appConfig,
54
								$UserId) {
55
		$this->rootFolder = $rootFolder;
56
		$this->shareManager = $shareManager;
57
		$this->urlGenerator = $urlGenerator;
58
		$this->wopiParser = $wopiParser;
59
		$this->appConfig = $appConfig;
0 ignored issues
show
Bug introduced by
The property appConfig does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
		$this->userId = $UserId;
0 ignored issues
show
Bug introduced by
The property userId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
	}
62
63
	/**
64
	 * @param string $fileId
65
	 * @param string $shareToken
66
	 * @return array
67
	 * @throws \Exception
68
	 */
69
	public function getToken($fileId, $shareToken = null) {
70
		list($fileId,, $version) = Helper::parseFileId($fileId);
71
		$owneruid = null;
72
		// if the user is not logged-in do use the sharers storage
73
		if($shareToken !== null) {
74
			/** @var File $file */
75
			$rootFolder = $this->rootFolder;
76
			$share = $this->shareManager->getShareByToken($shareToken);
77
			$updatable = (bool)($share->getPermissions() & \OCP\Constants::PERMISSION_UPDATE);
78
			$owneruid = $share->getShareOwner();
79
		} else {
80
			try {
81
				/** @var File $file */
82
				$rootFolder = $this->rootFolder->getUserFolder($this->userId);
83
				$updatable = $rootFolder->isUpdateable();
84
				// Check if the editor (user who is accessing) is in editable group
85
				// UserCanWrite only if
86
				// 1. No edit groups are set or
87
				// 2. if they are set, it is in one of the edit groups
88
				$editorUid = \OC::$server->getUserSession()->getUser()->getUID();
89
				$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
90
				if ($updatable && count($editGroups) > 0) {
91
					$updatable = false;
92
					foreach($editGroups as $editGroup) {
93
						 $editorGroup = \OC::$server->getGroupManager()->get($editGroup);
94
						 if ($editorGroup !== null && sizeof($editorGroup->searchUsers($editorUid)) > 0) {
95
							$updatable = true;
96
							break;
97
						 }
98
					}
99
				}
100
			} catch (\Exception $e) {
101
				throw $e;
102
			}
103
		}
104
		/** @var File $file */
105
		$file = $rootFolder->getById($fileId)[0];
106
		// If its a public share, use the owner from the share, otherwise check the file object
107
		if (is_null($owneruid)) {
108
			$owneruid = $file->getOwner()->getUID();
109
		}
110
		$row = new Wopi();
111
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');//$this->request->getServerProtocol() . '://' . $this->request->getServerHost();
112
		$token = $row->generateFileToken($fileId, $owneruid, $this->userId, $version, (int)$updatable, $serverHost);
113
114
		try {
115
116
			return [
117
				$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'],
118
				$token,
119
			];
120
		} catch (\Exception $e){
121
			throw $e;
122
		}
123
	}
124
}
125