-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupFolderACL_Fill.php
More file actions
113 lines (101 loc) · 3.47 KB
/
Copy pathGroupFolderACL_Fill.php
File metadata and controls
113 lines (101 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
require_once './migrator-config.php';
require_once $nextcloudBinDir.'/lib/versioncheck.php';
require_once $nextcloudBinDir.'/lib/base.php';
use OCA\GroupFolders\ACL\RuleManager;
use OCA\GroupFolders\ACL\Rule;
use OCA\GroupFolders\Mount\MountProvider;
use OCP\Files\IRootFolder;
use OCP\Server;
use OCP\Constants;
OC_App::loadApps();
// echo "All app loaded\n";
$rm = Server::get(RuleManager::class);
// echo "Get rulemanager\n";
$mountProvider = Server::get(MountProvider::class);
// echo "Get mountprovider\n";
$rootFolder = Server::get(IRootFolder::class);
// echo "Get root folder id:" . $rootFolder->getMountPoint()->getNumericStorageId() . "\n";
$filecache = $rootFolder->getMountPoint()->getStorage()->getCache();
// echo "Get filecache\n";
$rulesbypath = $rm->getAllRulesForPrefix($rootFolder->getMountPoint()->getNumericStorageId(),'__groupfolders');
// echo "all rules loaded\n";
# Copy from https://github.com/nextcloud/groupfolders/blob/master/lib/Command/ACL.php#L247C1-L256C3
function formatRulePermissions(int $mask, int $permissions): string {
$PERMISSIONS_MAP = [
'read' => Constants::PERMISSION_READ,
'write' => Constants::PERMISSION_UPDATE,
'create' => Constants::PERMISSION_CREATE,
'delete' => Constants::PERMISSION_DELETE,
'share' => Constants::PERMISSION_SHARE,
];
$result = [];
foreach ($PERMISSIONS_MAP as $name => $value) {
if (($mask & $value) === $value) {
$type = ($permissions & $value) === $value ? '+' : '-';
$result[] = $type . $name;
}
}
return implode(', ', $result);
}
function findRule($path, $type, $id) {
global $rulesbypath;
if (array_key_exists($path,$rulesbypath)) {
foreach($rulesbypath[$path] as $r){
if ($r->getUserMapping()->getType() == $type && $r->getUserMapping()->getId() == $id){
return $r;
}
}
}
return;
}
function findParentRule($rule) {
global $filecache;
$currentfilestr = $filecache->getPathById($rule->getFileId());
while (true) {
//echo "Current : " . $currentfilestr . "\n";
// Find parent
$parentfileid = $filecache->getParentId($currentfilestr);
if ($parentfileid < 0){
// No parent
//echo "No parent\n";
return;
}
$parentfilestr = $filecache->getPathById($parentfileid);
//echo "Parent : ". $parentfilestr . "\n";
// Existing rule ?
$r = findRule($parentfilestr, $rule->getUserMapping()->getType(), $rule->getUserMapping()->getId());
if ($r) {
return $r;
}
$currentfilestr = $parentfilestr;
}
}
$nbtot = 0;
$nb = 0;
foreach($rulesbypath as $path => $rules){
//echo "==> " . $path . "\n";
foreach($rules as $r) {
$nbtot++;
if ($r->getMask() < 31) {
$nb++;
$rulesarr = array();
$rulesarr[] = $r;
$pr = $r;
while ($pr = findParentRule($pr)) {
$rulesarr[] = $pr;
}
$nr = new Rule($r->getUserMapping(), $r->getFileId(), 31, Rule::mergeRules($rulesarr)->applyPermissions(31));
echo "Completed rules for \e[96m" . $r->getUserMapping()->getType() . '/' . $r->getUserMapping()->getId() . "\e[39m on \e[96m" . $path . "\e[39m before: \e[93m[" . formatRulePermissions($r->getMask(), $r->getPermissions()) . "]\e[39m, after: \e[92m[" . formatRulePermissions($nr->getMask(), $nr->getPermissions()) . "]\e[39m" . "\n";
if(!$dryRun) {
$rm->saveRule($nr);
}
}
}
}
if($dryRun) {
echo "\e[91mDry run mode enabled, nothing has been saved.\e[39m\n";
} else {
echo $nb . " rules has been updated to include implicit permissions.\n";
}
echo "END\n";