Passed
Push — master ( 7917f4...818fc9 )
by Blizzz
14:01 queued 13s
created

Updater::moveShareToShare()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 40
rs 8.9457
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Michael Gapczynski <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
namespace OCA\Files_Sharing;
28
29
use OCP\Constants;
30
use OCP\Share\IShare;
31
32
class Updater {
33
34
	/**
35
	 * @param array $params
36
	 */
37
	public static function renameHook($params) {
38
		self::renameChildren($params['oldpath'], $params['newpath']);
39
		self::moveShareToShare($params['newpath']);
40
	}
41
42
	/**
43
	 * Fix for https://github.com/owncloud/core/issues/20769
44
	 *
45
	 * The owner is allowed to move their files (if they are shared) into a receiving folder
46
	 * In this case we need to update the parent of the moved share. Since they are
47
	 * effectively handing over ownership of the file the rest of the code needs to know
48
	 * they need to build up the reshare tree.
49
	 *
50
	 * @param string $path
51
	 */
52
	private static function moveShareToShare($path) {
53
		$userFolder = \OC::$server->getUserFolder();
54
55
		// If the user folder can't be constructed (e.g. link share) just return.
56
		if ($userFolder === null) {
57
			return;
58
		}
59
60
		$src = $userFolder->get($path);
61
62
		$shareManager = \OC::$server->getShareManager();
63
64
		$shares = $shareManager->getSharesBy($userFolder->getOwner()->getUID(), IShare::TYPE_USER, $src, false, -1);
65
		$shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), IShare::TYPE_GROUP, $src, false, -1));
66
		$shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), IShare::TYPE_ROOM, $src, false, -1));
67
68
		// If the path we move is not a share we don't care
69
		if (empty($shares)) {
70
			return;
71
		}
72
73
		// Check if the destination is inside a share
74
		$mountManager = \OC::$server->getMountManager();
75
		$dstMount = $mountManager->find($src->getPath());
76
		if (!($dstMount instanceof \OCA\Files_Sharing\SharedMount)) {
77
			return;
78
		}
79
80
		$newOwner = $dstMount->getShare()->getShareOwner();
81
82
		//Ownership is moved over
83
		foreach ($shares as $share) {
84
			/** @var IShare $share */
85
			if (!($dstMount->getShare()->getPermissions() & Constants::PERMISSION_SHARE)) {
86
				$shareManager->deleteShare($share);
87
				continue;
88
			}
89
			$share->setShareOwner($newOwner);
90
			$share->setPermissions($share->getPermissions() & $dstMount->getShare()->getPermissions());
91
			$shareManager->updateShare($share);
92
		}
93
	}
94
95
	/**
96
	 * rename mount point from the children if the parent was renamed
97
	 *
98
	 * @param string $oldPath old path relative to data/user/files
99
	 * @param string $newPath new path relative to data/user/files
100
	 */
101
	private static function renameChildren($oldPath, $newPath) {
102
		$absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $newPath);
103
		$absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $oldPath);
104
105
		$mountManager = \OC\Files\Filesystem::getMountManager();
106
		$mountedShares = $mountManager->findIn('/' . \OC_User::getUser() . '/files/' . $oldPath);
107
		foreach ($mountedShares as $mount) {
108
			if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) {
109
				$mountPoint = $mount->getMountPoint();
110
				$target = str_replace($absOldPath, $absNewPath, $mountPoint);
111
				$mount->moveMount($target);
112
			}
113
		}
114
	}
115
}
116