1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Arthur Schiwon <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Files_Sharing\Collaboration; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use OCP\Collaboration\AutoComplete\ISorter; |
28
|
|
|
use OCP\Files\Folder; |
29
|
|
|
use OCP\Files\IRootFolder; |
30
|
|
|
use OCP\Files\Node; |
31
|
|
|
use OCP\IUserSession; |
32
|
|
|
use OCP\Share\IManager; |
33
|
|
|
|
34
|
|
|
class ShareRecipientSorter implements ISorter { |
35
|
|
|
|
36
|
|
|
/** @var IManager */ |
37
|
|
|
private $shareManager; |
38
|
|
|
/** @var Folder */ |
39
|
|
|
private $rootFolder; |
40
|
|
|
/** @var IUserSession */ |
41
|
|
|
private $userSession; |
42
|
|
|
|
43
|
|
|
public function __construct(IManager $shareManager, IRootFolder $rootFolder, IUserSession $userSession) { |
44
|
|
|
$this->shareManager = $shareManager; |
45
|
|
|
$this->rootFolder = $rootFolder; |
46
|
|
|
$this->userSession = $userSession; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getId() { |
50
|
|
|
return 'share-recipients'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function sort(array &$sortArray, array $context) { |
54
|
|
|
// let's be tolerant. Comments uses "files" by default, other usages are often singular |
55
|
|
|
if($context['itemType'] !== 'files' && $context['itemType'] !== 'file') { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
$user = $this->userSession->getUser(); |
59
|
|
|
if($user === null) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
|
|
|
|
63
|
|
|
/** @var Node[] $nodes */ |
64
|
|
|
$nodes = $userFolder->getById((int)$context['itemId']); |
65
|
|
|
if(count($nodes) === 0) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
$al = $this->shareManager->getAccessList($nodes[0]); |
69
|
|
|
|
70
|
|
View Code Duplication |
foreach ($sortArray as $type => &$byType) { |
71
|
|
|
if(!isset($al[$type]) || !is_array($al[$type])) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// at least on PHP 5.6 usort turned out to be not stable. So we add |
76
|
|
|
// the current index to the value and compare it on a draw |
77
|
|
|
$i = 0; |
78
|
|
|
$workArray = array_map(function($element) use (&$i) { |
79
|
|
|
return [$i++, $element]; |
80
|
|
|
}, $byType); |
81
|
|
|
|
82
|
|
|
usort($workArray, function ($a, $b) use ($al, $type) { |
83
|
|
|
$result = $this->compare($a[1], $b[1], $al[$type]); |
84
|
|
|
if($result === 0) { |
85
|
|
|
$result = $a[0] - $b[0]; |
86
|
|
|
} |
87
|
|
|
return $result; |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
// and remove the index values again |
91
|
|
|
$byType = array_column($workArray, 1); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $a |
97
|
|
|
* @param array $b |
98
|
|
|
* @param array $al |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
protected function compare(array $a, array $b, array $al) { |
102
|
|
|
$a = $a['value']['shareWith']; |
103
|
|
|
$b = $b['value']['shareWith']; |
104
|
|
|
|
105
|
|
|
$valueA = (int)in_array($a, $al, true); |
106
|
|
|
$valueB = (int)in_array($b, $al, true); |
107
|
|
|
|
108
|
|
|
return $valueB - $valueA; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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 implementation 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 interface: