Completed
Pull Request — master (#5174)
by Björn
17:50
created

Update   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 161
Duplicated Lines 12.42 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 20
loc 161
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A postShared() 10 10 4
A postUnshared() 10 10 4
A postRestore() 0 6 2
A postRename() 0 12 3
A getOwnerPath() 0 11 2
B update() 0 24 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Encryption;
26
27
use OC\Files\Filesystem;
28
use \OC\Files\Mount;
29
use \OC\Files\View;
30
31
/**
32
 * update encrypted files, e.g. because a file was shared
33
 */
34
class Update {
35
36
	/** @var \OC\Files\View */
37
	protected $view;
38
39
	/** @var \OC\Encryption\Util */
40
	protected $util;
41
42
	 /** @var \OC\Files\Mount\Manager */
43
	protected $mountManager;
44
45
	/** @var \OC\Encryption\Manager */
46
	protected $encryptionManager;
47
48
	/** @var string */
49
	protected $uid;
50
51
	/** @var \OC\Encryption\File */
52
	protected $file;
53
54
	/**
55
	 *
56
	 * @param \OC\Files\View $view
57
	 * @param \OC\Encryption\Util $util
58
	 * @param \OC\Files\Mount\Manager $mountManager
59
	 * @param \OC\Encryption\Manager $encryptionManager
60
	 * @param \OC\Encryption\File $file
61
	 * @param string $uid
62
	 */
63
	public function __construct(
64
			View $view,
65
			Util $util,
66
			Mount\Manager $mountManager,
67
			Manager $encryptionManager,
68
			File $file,
69
			$uid
70
		) {
71
72
		$this->view = $view;
73
		$this->util = $util;
74
		$this->mountManager = $mountManager;
75
		$this->encryptionManager = $encryptionManager;
76
		$this->file = $file;
77
		$this->uid = $uid;
78
	}
79
80
	/**
81
	 * hook after file was shared
82
	 *
83
	 * @param array $params
84
	 */
85 View Code Duplication
	public function postShared($params) {
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...
86
		if ($this->encryptionManager->isEnabled()) {
87
			if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
88
				$path = Filesystem::getPath($params['fileSource']);
89
				list($owner, $ownerPath) = $this->getOwnerPath($path);
90
				$absPath = '/' . $owner . '/files/' . $ownerPath;
91
				$this->update($absPath);
92
			}
93
		}
94
	}
95
96
	/**
97
	 * hook after file was unshared
98
	 *
99
	 * @param array $params
100
	 */
101 View Code Duplication
	public function postUnshared($params) {
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...
102
		if ($this->encryptionManager->isEnabled()) {
103
			if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
104
				$path = Filesystem::getPath($params['fileSource']);
105
				list($owner, $ownerPath) = $this->getOwnerPath($path);
106
				$absPath = '/' . $owner . '/files/' . $ownerPath;
107
				$this->update($absPath);
108
			}
109
		}
110
	}
111
112
	/**
113
	 * inform encryption module that a file was restored from the trash bin,
114
	 * e.g. to update the encryption keys
115
	 *
116
	 * @param array $params
117
	 */
118
	public function postRestore($params) {
119
		if ($this->encryptionManager->isEnabled()) {
120
			$path = Filesystem::normalizePath('/' . $this->uid . '/files/' . $params['filePath']);
121
			$this->update($path);
122
		}
123
	}
124
125
	/**
126
	 * inform encryption module that a file was renamed,
127
	 * e.g. to update the encryption keys
128
	 *
129
	 * @param array $params
130
	 */
131
	public function postRename($params) {
132
		$source = $params['oldpath'];
133
		$target = $params['newpath'];
134
		if(
135
			$this->encryptionManager->isEnabled() &&
136
			dirname($source) !== dirname($target)
137
		) {
138
				list($owner, $ownerPath) = $this->getOwnerPath($target);
139
				$absPath = '/' . $owner . '/files/' . $ownerPath;
140
				$this->update($absPath);
141
		}
142
	}
143
144
	/**
145
	 * get owner and path relative to data/<owner>/files
146
	 *
147
	 * @param string $path path to file for current user
148
	 * @return array ['owner' => $owner, 'path' => $path]
149
	 * @throw \InvalidArgumentException
150
	 */
151
	protected function getOwnerPath($path) {
152
		$info = Filesystem::getFileInfo($path);
153
		$owner = Filesystem::getOwner($path);
154
		$view = new View('/' . $owner . '/files');
155
		$path = $view->getPath($info->getId());
156
		if ($path === null) {
157
			throw new \InvalidArgumentException('No file found for ' . $info->getId());
158
		}
159
160
		return array($owner, $path);
161
	}
162
163
	/**
164
	 * notify encryption module about added/removed users from a file/folder
165
	 *
166
	 * @param string $path relative to data/
167
	 * @throws Exceptions\ModuleDoesNotExistsException
168
	 */
169
	public function update($path) {
170
171
		$encryptionModule = $this->encryptionManager->getEncryptionModule();
172
173
		// if the encryption module doesn't encrypt the files on a per-user basis
174
		// we have nothing to do here.
175
		if ($encryptionModule->needDetailedAccessList() === false) {
176
			return;
177
		}
178
179
		// if a folder was shared, get a list of all (sub-)folders
180
		if ($this->view->is_dir($path)) {
181
			$allFiles = $this->util->getAllFiles($path);
182
		} else {
183
			$allFiles = array($path);
184
		}
185
186
187
188
		foreach ($allFiles as $file) {
189
			$usersSharing = $this->file->getAccessList($file);
190
			$encryptionModule->update($file, $this->uid, $usersSharing);
191
		}
192
	}
193
194
}
195