diff --git a/lib/Sabre/Album/AlbumRoot.php b/lib/Sabre/Album/AlbumRoot.php index 5644a5da6..8ac26912c 100644 --- a/lib/Sabre/Album/AlbumRoot.php +++ b/lib/Sabre/Album/AlbumRoot.php @@ -32,6 +32,7 @@ use OCP\Files\InvalidDirectoryException; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\Conflict; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\NotFound; @@ -39,18 +40,21 @@ use Sabre\DAV\ICopyTarget; use Sabre\DAV\INode; -class AlbumRoot implements ICollection, ICopyTarget { +class AlbumRoot implements ICollection, ICopyTarget +{ protected AlbumMapper $albumMapper; protected AlbumWithFiles $album; protected IRootFolder $rootFolder; protected string $userId; + private UserConfigService $userConfigService; public function __construct( AlbumMapper $albumMapper, AlbumWithFiles $album, IRootFolder $rootFolder, string $userId, - UserConfigService $userConfigService + UserConfigService $userConfigService, + protected LoggerInterface $logger, ) { $this->albumMapper = $albumMapper; $this->album = $album; @@ -66,7 +70,8 @@ public function delete() { $this->albumMapper->delete($this->album->getAlbum()->getId()); } - public function getName(): string { + public function getName(): string + { return basename($this->album->getAlbum()->getTitle()); } @@ -121,6 +126,7 @@ public function createFile($name, $data = null) { \header('OC-FileId: ' . $node->getId()); return '"' . $node->getEtag() . '"'; } catch (\Exception $e) { + $this->logger->error('Could not create file', ['exception' => $e]); throw new Forbidden('Could not create file'); } } @@ -132,13 +138,15 @@ public function createDirectory($name) { throw new Forbidden('Not allowed to create directories in this folder'); } - public function getChildren(): array { + public function getChildren(): array + { return array_map(function (AlbumFile $file) { return new AlbumPhoto($this->albumMapper, $this->album->getAlbum(), $file, $this->rootFolder, $this->rootFolder->getUserFolder($this->userId)); }, $this->album->getFiles()); } - public function getChild($name): AlbumPhoto { + public function getChild($name): AlbumPhoto + { foreach ($this->album->getFiles() as $file) { if ($file->getFileId() . "-" . $file->getName() === $name) { return new AlbumPhoto($this->albumMapper, $this->album->getAlbum(), $file, $this->rootFolder, $this->rootFolder->getUserFolder($this->userId)); @@ -147,7 +155,8 @@ public function getChild($name): AlbumPhoto { throw new NotFound("$name not found"); } - public function childExists($name): bool { + public function childExists($name): bool + { try { $this->getChild($name); return true; @@ -156,11 +165,13 @@ public function childExists($name): bool { } } - public function getLastModified(): int { + public function getLastModified(): int + { return 0; } - public function copyInto($targetName, $sourcePath, INode $sourceNode): bool { + public function copyInto($targetName, $sourcePath, INode $sourceNode): bool + { if (!$sourceNode instanceof File) { throw new Forbidden("The source is not a file"); } @@ -175,7 +186,8 @@ public function copyInto($targetName, $sourcePath, INode $sourceNode): bool { return $this->addFile($sourceId, $ownerUID); } - protected function addFile(int $sourceId, string $ownerUID): bool { + protected function addFile(int $sourceId, string $ownerUID): bool + { if (in_array($sourceId, $this->album->getFileIds())) { throw new Conflict("File $sourceId is already in the folder"); } @@ -188,11 +200,13 @@ protected function addFile(int $sourceId, string $ownerUID): bool { return false; } - public function getAlbum(): AlbumWithFiles { + public function getAlbum(): AlbumWithFiles + { return $this->album; } - public function getDateRange(): array { + public function getDateRange(): array + { $earliestDate = null; $latestDate = null; @@ -231,7 +245,8 @@ public function getCover() { /** * @return array{array{'nc:collaborator': array{'id': string, 'label': string, 'type': int}}} */ - public function getCollaborators(): array { + public function getCollaborators(): array + { return array_map( fn (array $collaborator) => [ 'nc:collaborator' => $collaborator ], $this->albumMapper->getCollaborators($this->album->getAlbum()->getId()), @@ -242,7 +257,8 @@ public function getCollaborators(): array { * @param array{'id': string, 'type': int} $collaborators * @return array{array{'nc:collaborator': array{'id': string, 'label': string, 'type': int}}} */ - public function setCollaborators($collaborators): array { + public function setCollaborators($collaborators): array + { $this->albumMapper->setCollaborators($this->getAlbum()->getAlbum()->getId(), $collaborators); return $this->getCollaborators(); } diff --git a/lib/Sabre/Album/AlbumsHome.php b/lib/Sabre/Album/AlbumsHome.php index d73e6da31..7b0e308ae 100644 --- a/lib/Sabre/Album/AlbumsHome.php +++ b/lib/Sabre/Album/AlbumsHome.php @@ -28,17 +28,12 @@ use OCA\Photos\Album\AlbumWithFiles; use OCA\Photos\Service\UserConfigService; use OCP\Files\IRootFolder; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\NotFound; use Sabre\DAV\ICollection; class AlbumsHome implements ICollection { - protected AlbumMapper $albumMapper; - protected array $principalInfo; - protected string $userId; - protected IRootFolder $rootFolder; - protected UserConfigService $userConfigService; - public const NAME = 'albums'; /** @@ -47,17 +42,13 @@ class AlbumsHome implements ICollection { protected ?array $children = null; public function __construct( - array $principalInfo, - AlbumMapper $albumMapper, - string $userId, - IRootFolder $rootFolder, - UserConfigService $userConfigService + protected array $principalInfo, + protected AlbumMapper $albumMapper, + protected string $userId, + protected IRootFolder $rootFolder, + protected UserConfigService $userConfigService, + protected LoggerInterface $logger, ) { - $this->principalInfo = $principalInfo; - $this->albumMapper = $albumMapper; - $this->userId = $userId; - $this->rootFolder = $rootFolder; - $this->userConfigService = $userConfigService; } /** @@ -106,7 +97,14 @@ public function getChildren(): array { if ($this->children === null) { $albumInfos = $this->albumMapper->getForUser($this->userId); $this->children = array_map(function (AlbumInfo $albumInfo) { - return new AlbumRoot($this->albumMapper, new AlbumWithFiles($albumInfo, $this->albumMapper), $this->rootFolder, $this->userId, $this->userConfigService); + return new AlbumRoot( + $this->albumMapper, + new AlbumWithFiles($albumInfo, $this->albumMapper), + $this->rootFolder, + $this->userId, + $this->userConfigService, + $this->logger, + ); }, $albumInfos); } diff --git a/lib/Sabre/Album/SharedAlbumRoot.php b/lib/Sabre/Album/SharedAlbumRoot.php index f0c367269..c6bc1f04a 100644 --- a/lib/Sabre/Album/SharedAlbumRoot.php +++ b/lib/Sabre/Album/SharedAlbumRoot.php @@ -27,20 +27,19 @@ use OCA\Photos\Album\AlbumWithFiles; use OCA\Photos\Service\UserConfigService; use OCP\Files\IRootFolder; -use OCP\IUserManager; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\Conflict; use Sabre\DAV\Exception\Forbidden; -class SharedAlbumRoot extends AlbumRoot { - private IUserManager $userManager; - +class SharedAlbumRoot extends AlbumRoot +{ public function __construct( AlbumMapper $albumMapper, AlbumWithFiles $album, IRootFolder $rootFolder, string $userId, UserConfigService $userConfigService, - IUserManager $userManager + LoggerInterface $logger, ) { parent::__construct( $albumMapper, @@ -48,27 +47,28 @@ public function __construct( $rootFolder, $userId, $userConfigService, - $userManager + $logger ); - - $this->userManager = $userManager; } /** * @return void */ - public function delete() { + public function delete() + { $this->albumMapper->deleteUserFromAlbumCollaboratorsList($this->userId, $this->album->getAlbum()->getId()); } /** * @return void */ - public function setName($name) { + public function setName($name) + { throw new Forbidden('Not allowed to rename a shared album'); } - protected function addFile(int $sourceId, string $ownerUID): bool { + protected function addFile(int $sourceId, string $ownerUID): bool + { if (in_array($sourceId, $this->album->getFileIds())) { throw new Conflict("File $sourceId is already in the folder"); } @@ -84,7 +84,8 @@ protected function addFile(int $sourceId, string $ownerUID): bool { /** * Return only the owner, and do not reveal other collaborators. */ - public function getCollaborators(): array { + public function getCollaborators(): array + { return [[ 'nc:collaborator' => [ 'id' => $this->album->getAlbum()->getUserId(), @@ -94,7 +95,8 @@ public function getCollaborators(): array { ]]; } - public function setCollaborators($collaborators): array { + public function setCollaborators($collaborators): array + { throw new Forbidden('Not allowed to collaborators to a public album'); } } diff --git a/lib/Sabre/Album/SharedAlbumsHome.php b/lib/Sabre/Album/SharedAlbumsHome.php index 44f96c46f..8df10ece1 100644 --- a/lib/Sabre/Album/SharedAlbumsHome.php +++ b/lib/Sabre/Album/SharedAlbumsHome.php @@ -29,12 +29,10 @@ use OCP\Files\IRootFolder; use OCP\IGroupManager; use OCP\IUserManager; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\Forbidden; class SharedAlbumsHome extends AlbumsHome { - private IUserManager $userManager; - private IGroupManager $groupManager; - public const NAME = 'sharedalbums'; public function __construct( @@ -42,20 +40,20 @@ public function __construct( AlbumMapper $albumMapper, string $userId, IRootFolder $rootFolder, - IUserManager $userManager, - IGroupManager $groupManager, - UserConfigService $userConfigService + private IUserManager $userManager, + private IGroupManager $groupManager, + UserConfigService $userConfigService, + LoggerInterface $logger, + ) { parent::__construct( $principalInfo, $albumMapper, $userId, $rootFolder, - $userConfigService + $userConfigService, + $logger, ); - - $this->userManager = $userManager; - $this->groupManager = $groupManager; } /** @@ -81,7 +79,15 @@ public function getChildren(): array { } $this->children = array_map(function (AlbumWithFiles $album) { - return new SharedAlbumRoot($this->albumMapper, $album, $this->rootFolder, $this->userId, $this->userConfigService, $this->userManager); + return new SharedAlbumRoot( + $this->albumMapper, + $album, + $this->rootFolder, + $this->userId, + $this->userConfigService, + $this->logger, + $this->userManager, + ); }, $albums); } diff --git a/lib/Sabre/PhotosHome.php b/lib/Sabre/PhotosHome.php index 40c24849f..afe079ee7 100644 --- a/lib/Sabre/PhotosHome.php +++ b/lib/Sabre/PhotosHome.php @@ -33,6 +33,7 @@ use OCP\Files\IRootFolder; use OCP\IGroupManager; use OCP\IUserManager; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\NotFound; use Sabre\DAV\ICollection; @@ -48,6 +49,7 @@ public function __construct( private IUserManager $userManager, private IGroupManager $groupManager, private UserConfigService $userConfigService, + private LoggerInterface $logger, ) { } @@ -84,9 +86,9 @@ public function createDirectory($name) { public function getChild($name) { switch ($name) { case AlbumsHome::NAME: - return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService); + return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService, $this->logger); case SharedAlbumsHome::NAME: - return new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService); + return new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService, $this->logger); case PlacesHome::NAME: return new PlacesHome($this->userId, $this->rootFolder, $this->reverseGeoCoderService, $this->placeMapper); } @@ -99,8 +101,8 @@ public function getChild($name) { */ public function getChildren(): array { return [ - new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService), - new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService), + new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService, $this->logger), + new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService, $this->logger), new PlacesHome($this->userId, $this->rootFolder, $this->reverseGeoCoderService, $this->placeMapper), ]; } diff --git a/lib/Sabre/PublicRootCollection.php b/lib/Sabre/PublicRootCollection.php index 9414b24d9..2c362fd5c 100644 --- a/lib/Sabre/PublicRootCollection.php +++ b/lib/Sabre/PublicRootCollection.php @@ -29,33 +29,24 @@ use OCP\Files\IRootFolder; use OCP\IRequest; use OCP\Security\Bruteforce\IThrottler; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\NotFound; use Sabre\DAVACL\AbstractPrincipalCollection; use Sabre\DAVACL\PrincipalBackend; class PublicRootCollection extends AbstractPrincipalCollection { private const BRUTEFORCE_ACTION = 'publicphotos_webdav_auth'; - private AlbumMapper $albumMapper; - private IRootFolder $rootFolder; - private UserConfigService $userConfigService; - private IRequest $request; - private IThrottler $throttler; public function __construct( - AlbumMapper $albumMapper, - IRootFolder $rootFolder, + private AlbumMapper $albumMapper, + private IRootFolder $rootFolder, PrincipalBackend\BackendInterface $principalBackend, - UserConfigService $userConfigService, - IRequest $request, - IThrottler $throttler + private UserConfigService $userConfigService, + private IRequest $request, + private IThrottler $throttler, + private LoggerInterface $logger, ) { parent::__construct($principalBackend, 'principals/token'); - - $this->albumMapper = $albumMapper; - $this->rootFolder = $rootFolder; - $this->userConfigService = $userConfigService; - $this->request = $request; - $this->throttler = $throttler; } public function getName(): string { @@ -94,6 +85,13 @@ public function getChild($token) { throw new NotFound("Unable to find public album"); } - return new PublicAlbumRoot($this->albumMapper, $albums[0], $this->rootFolder, $albums[0]->getAlbum()->getUserId(), $this->userConfigService); + return new PublicAlbumRoot( + $this->albumMapper, + $albums[0], + $this->rootFolder, + $albums[0]->getAlbum()->getUserId(), + $this->userConfigService, + $this->logger, + ); } } diff --git a/lib/Sabre/RootCollection.php b/lib/Sabre/RootCollection.php index 348b6eaaa..9127601ea 100644 --- a/lib/Sabre/RootCollection.php +++ b/lib/Sabre/RootCollection.php @@ -31,6 +31,7 @@ use OCP\IGroupManager; use OCP\IUserManager; use OCP\IUserSession; +use Psr\Log\LoggerInterface; use Sabre\DAVACL\AbstractPrincipalCollection; use Sabre\DAVACL\PrincipalBackend; @@ -45,6 +46,7 @@ public function __construct( private IUserManager $userManager, private IGroupManager $groupManager, private UserConfigService $userConfigService, + private LoggerInterface $logger, ) { parent::__construct($principalBackend, 'principals/users'); } @@ -64,7 +66,7 @@ public function getChildForPrincipal(array $principalInfo): PhotosHome { if (is_null($user) || $name !== $user->getUID()) { throw new \Sabre\DAV\Exception\Forbidden(); } - return new PhotosHome($principalInfo, $this->albumMapper, $this->placeMapper, $this->reverseGeoCoderService, $name, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService); + return new PhotosHome($principalInfo, $this->albumMapper, $this->placeMapper, $this->reverseGeoCoderService, $name, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService, $this->logger); } public function getName(): string {