* Update permissions (encrypted on disk). Skips admins. */
($permissions)
| 480 | * Update permissions (encrypted on disk). Skips admins. |
| 481 | */ |
| 482 | public static function updateUserPermissions($permissions) |
| 483 | { |
| 484 | self::ensureUserCaseMigration(); |
| 485 | global $encryptionKey; |
| 486 | $permissionsFile = USERS_DIR . "userPermissions.json"; |
| 487 | $existingPermissions = []; |
| 488 | |
| 489 | // Load existing (decrypt if needed) |
| 490 | if (file_exists($permissionsFile)) { |
| 491 | $encryptedContent = file_get_contents($permissionsFile); |
| 492 | $json = decryptData($encryptedContent, $encryptionKey); |
| 493 | if ($json === false) { |
| 494 | $json = $encryptedContent; // legacy plaintext |
| 495 | } |
| 496 | $existingPermissions = json_decode($json, true) ?: []; |
| 497 | } |
| 498 | |
| 499 | // Load roles to skip admins |
| 500 | $usersFile = USERS_DIR . USERS_FILE; |
| 501 | $userRoles = []; |
| 502 | if (file_exists($usersFile)) { |
| 503 | foreach (file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) { |
| 504 | $parts = explode(':', trim($line)); |
| 505 | if (count($parts) >= 3 && preg_match(REGEX_USER, $parts[0])) { |
| 506 | $userRoles[strtolower($parts[0])] = trim($parts[2]); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | $knownKeys = [ |
| 512 | 'folderOnly','readOnly','disableUpload', |
| 513 | 'bypassOwnership','canShare','canZip','viewOwnOnly' |
| 514 | ]; |
| 515 | |
| 516 | // Build a map of lowercase->actual key to update existing entries case-insensitively |
| 517 | $lcIndex = []; |
| 518 | foreach ($existingPermissions as $k => $_) { |
| 519 | $lcIndex[strtolower($k)] = $k; |
| 520 | } |
| 521 | |
| 522 | foreach ($permissions as $perm) { |
| 523 | if (empty($perm['username'])) { |
| 524 | continue; |
| 525 | } |
| 526 | |
| 527 | $unameOrig = (string)$perm['username']; // preserve original case |
| 528 | $unameLc = strtolower($unameOrig); |
| 529 | $role = $userRoles[$unameLc] ?? null; |
| 530 | if ($role === "1") { |
| 531 | continue; // skip admins |
| 532 | } |
| 533 | |
| 534 | // Find existing key case-insensitively; otherwise use original case as canonical |
| 535 | $storeKey = $lcIndex[$unameLc] ?? $unameOrig; |
| 536 | |
| 537 | $current = $existingPermissions[$storeKey] ?? []; |
| 538 | foreach ($knownKeys as $k) { |
| 539 | if (array_key_exists($k, $perm)) { |
no test coverage detected