Skip to content

Commit

Permalink
for 129
Browse files Browse the repository at this point in the history
  • Loading branch information
eldertek committed Jan 23, 2025
1 parent 169f444 commit 48f65a3
Showing 1 changed file with 37 additions and 44 deletions.
81 changes: 37 additions & 44 deletions lib/Service/ExcludedFolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public function __construct(

private function validateUserContext(): void {
if (empty($this->userId)) {
$this->logger->debug('No user context available');
throw new \RuntimeException('User context required for this operation');
$this->logger->debug('No user context available, skipping excluded folder check');
// Instead of throwing an exception, we'll just skip the check
return;
}
$this->logger->debug('User context validated: {userId}', ['userId' => $this->userId]);
}
Expand Down Expand Up @@ -141,58 +142,50 @@ public function delete(int $id): void {
}
}

public function isPathExcluded(string $path): bool {
$this->validateUserContext();

$this->logger->debug('Checking if path is in excluded folder', [
'path' => $path,
'userId' => $this->userId
]);

// Normalize path for comparison - remove /admin/files/ prefix
private function isPathInExcludedFolder(string $path, string $excludedPath): bool {
// Normalize paths for comparison - remove /admin/files/ prefix
$normalizedPath = preg_replace('#^/[^/]+/files/#', '/', $path);
$normalizedPath = '/' . trim($normalizedPath, '/');

$this->logger->debug('Normalized path for exclusion check', [
'originalPath' => $path,
'normalizedPath' => $normalizedPath,
'strippedPrefix' => preg_match('#^/[^/]+/files/#', $path) ? 'true' : 'false'
]);
$excludedPath = '/' . trim($excludedPath, '/');

// Get all excluded folders
$excludedFolders = $this->findAll();
$this->logger->debug('Found excluded folders', [
'count' => count($excludedFolders),
'paths' => array_map(fn($f) => $f->getFolderPath(), $excludedFolders)
$this->logger->debug('Comparing paths for exclusion', [
'filePath' => $normalizedPath,
'excludedPath' => $excludedPath,
'isSubPath' => str_starts_with($normalizedPath, $excludedPath),
'exactMatch' => $normalizedPath === $excludedPath
]);

foreach ($excludedFolders as $folder) {
$excludedPath = '/' . trim($folder->getFolderPath(), '/');
$this->logger->debug('Comparing paths for exclusion', [
'filePath' => $normalizedPath,
'excludedPath' => $excludedPath,
'isSubPath' => str_starts_with($normalizedPath, $excludedPath),
'exactMatch' => $normalizedPath === $excludedPath
// Check if the path is either exactly the excluded path or starts with it followed by a slash
return $normalizedPath === $excludedPath || str_starts_with($normalizedPath, $excludedPath . '/');
}

public function isPathExcluded(string $path): bool {
if (empty($this->userId)) {
$this->logger->debug('No user context available for path exclusion check, assuming not excluded', [
'path' => $path
]);
return false;
}

// Check if the path is either exactly the excluded path or starts with it followed by a slash
if ($normalizedPath === $excludedPath || str_starts_with($normalizedPath, $excludedPath . '/')) {
$this->logger->debug('Path is in excluded folder', [
'path' => $path,
'normalizedPath' => $normalizedPath,
'excludedFolder' => $excludedPath,
'matchType' => $normalizedPath === $excludedPath ? 'exact' : 'subpath'
]);
return true;
try {
$excludedFolders = $this->findAll();
foreach ($excludedFolders as $excludedFolder) {
if ($this->isPathInExcludedFolder($path, $excludedFolder->getFolderPath())) {
$this->logger->debug('Path is in excluded folder', [
'path' => $path,
'excludedFolder' => $excludedFolder->getFolderPath()
]);
return true;
}
}
} catch (\Exception $e) {
$this->logger->warning('Error checking excluded folders, assuming not excluded', [
'path' => $path,
'error' => $e->getMessage()
]);
return false;
}

$this->logger->debug('Path is not in any excluded folder', [
'path' => $path,
'normalizedPath' => $normalizedPath,
'checkedFolders' => count($excludedFolders)
]);

return false;
}

Expand Down

0 comments on commit 48f65a3

Please sign in to comment.