|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Björn Schießle <[email protected]> |
|
4
|
|
|
* @author Joas Schilling <[email protected]> |
|
5
|
|
|
* @author Michael Gapczynski <[email protected]> |
|
6
|
|
|
* @author Morris Jobke <[email protected]> |
|
7
|
|
|
* @author Robin Appelman <[email protected]> |
|
8
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
9
|
|
|
* @author Vincent Petry <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
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
|
|
|
|
|
28
|
|
|
namespace OCA\Files_Sharing; |
|
29
|
|
|
|
|
30
|
|
|
class Updater { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $params |
|
34
|
|
|
*/ |
|
35
|
|
|
static public function renameHook($params) { |
|
36
|
|
|
self::renameChildren($params['oldpath'], $params['newpath']); |
|
37
|
|
|
self::moveShareToShare($params['newpath']); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Fix for https://github.com/owncloud/core/issues/20769 |
|
42
|
|
|
* |
|
43
|
|
|
* The owner is allowed to move their files (if they are shared) into a receiving folder |
|
44
|
|
|
* In this case we need to update the parent of the moved share. Since they are |
|
45
|
|
|
* effectively handing over ownership of the file the rest of the code needs to know |
|
46
|
|
|
* they need to build up the reshare tree. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $path |
|
49
|
|
|
*/ |
|
50
|
|
|
static private function moveShareToShare($path) { |
|
51
|
|
|
$userFolder = \OC::$server->getUserFolder(); |
|
52
|
|
|
|
|
53
|
|
|
// If the user folder can't be constructed (e.g. link share) just return. |
|
54
|
|
|
if ($userFolder === null) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$src = $userFolder->get($path); |
|
59
|
|
|
|
|
60
|
|
|
$shareManager = \OC::$server->getShareManager(); |
|
61
|
|
|
|
|
62
|
|
|
$shares = $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_USER, $src, false, -1); |
|
63
|
|
|
$shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $src, false, -1)); |
|
64
|
|
|
|
|
65
|
|
|
// If the path we move is not a share we don't care |
|
66
|
|
|
if (empty($shares)) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Check if the destination is inside a share |
|
71
|
|
|
$mountManager = \OC::$server->getMountManager(); |
|
72
|
|
|
$dstMount = $mountManager->find($src->getPath()); |
|
73
|
|
|
if (!($dstMount instanceof \OCA\Files_Sharing\SharedMount)) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$newOwner = $dstMount->getShare()->getShareOwner(); |
|
78
|
|
|
|
|
79
|
|
|
//Ownership is moved over |
|
80
|
|
|
foreach ($shares as $share) { |
|
81
|
|
|
/** @var \OCP\Share\IShare $share */ |
|
82
|
|
|
$share->setShareOwner($newOwner); |
|
83
|
|
|
$shareManager->updateShare($share); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* rename mount point from the children if the parent was renamed |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $oldPath old path relative to data/user/files |
|
91
|
|
|
* @param string $newPath new path relative to data/user/files |
|
92
|
|
|
*/ |
|
93
|
|
|
static private function renameChildren($oldPath, $newPath) { |
|
94
|
|
|
|
|
95
|
|
|
$absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $newPath); |
|
96
|
|
|
$absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $oldPath); |
|
97
|
|
|
|
|
98
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager(); |
|
99
|
|
|
$mountedShares = $mountManager->findIn('/' . \OCP\User::getUser() . '/files/' . $oldPath); |
|
100
|
|
|
foreach ($mountedShares as $mount) { |
|
101
|
|
|
if ($mount->getStorage()->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')) { |
|
102
|
|
|
$mountPoint = $mount->getMountPoint(); |
|
103
|
|
|
$target = str_replace($absOldPath, $absNewPath, $mountPoint); |
|
104
|
|
|
$mount->moveMount($target); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: