2018-09-14 06:34:24 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-09-14 06:34:24 -04:00
|
|
|
declare(strict_types=1);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-09-14 06:34:24 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-09-14 06:34:24 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Collaboration\Resources;
|
|
|
|
|
|
|
|
|
|
use OCP\Collaboration\Resources\ICollection;
|
|
|
|
|
use OCP\Collaboration\Resources\IManager;
|
|
|
|
|
use OCP\Collaboration\Resources\IResource;
|
|
|
|
|
use OCP\IDBConnection;
|
2018-10-17 14:03:44 -04:00
|
|
|
use OCP\IUser;
|
2018-09-14 06:34:24 -04:00
|
|
|
|
|
|
|
|
class Resource implements IResource {
|
2023-07-03 03:37:33 -04:00
|
|
|
protected ?array $data = null;
|
2019-01-24 06:03:52 -05:00
|
|
|
|
2018-10-24 08:25:35 -04:00
|
|
|
public function __construct(
|
2023-07-03 03:37:33 -04:00
|
|
|
protected IManager $manager,
|
|
|
|
|
protected IDBConnection $connection,
|
|
|
|
|
protected string $type,
|
|
|
|
|
protected string $id,
|
|
|
|
|
protected ?IUser $userForAccess = null,
|
|
|
|
|
protected ?bool $access = null,
|
2018-10-24 08:25:35 -04:00
|
|
|
) {
|
2018-09-14 06:34:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-07 10:04:15 -05:00
|
|
|
* @since 16.0.0
|
2018-09-14 06:34:24 -04:00
|
|
|
*/
|
|
|
|
|
public function getType(): string {
|
|
|
|
|
return $this->type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-07 10:04:15 -05:00
|
|
|
* @since 16.0.0
|
2018-09-14 06:34:24 -04:00
|
|
|
*/
|
|
|
|
|
public function getId(): string {
|
|
|
|
|
return $this->id;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-17 14:03:44 -04:00
|
|
|
/**
|
2019-02-07 10:04:15 -05:00
|
|
|
* @since 16.0.0
|
2018-10-17 14:03:44 -04:00
|
|
|
*/
|
2019-03-14 07:30:34 -04:00
|
|
|
public function getRichObject(): array {
|
|
|
|
|
if ($this->data === null) {
|
|
|
|
|
$this->data = $this->manager->getResourceRichObject($this);
|
2019-01-24 06:03:52 -05:00
|
|
|
}
|
|
|
|
|
|
2019-03-14 07:30:34 -04:00
|
|
|
return $this->data;
|
2019-01-24 06:03:52 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-17 14:03:44 -04:00
|
|
|
/**
|
|
|
|
|
* Can a user/guest access the resource
|
|
|
|
|
*
|
2019-02-07 10:04:15 -05:00
|
|
|
* @since 16.0.0
|
2018-10-17 14:03:44 -04:00
|
|
|
*/
|
2019-02-11 06:57:38 -05:00
|
|
|
public function canAccess(?IUser $user): bool {
|
|
|
|
|
if ($user instanceof IUser) {
|
|
|
|
|
return $this->canUserAccess($user);
|
|
|
|
|
}
|
|
|
|
|
return $this->canGuestAccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function canUserAccess(IUser $user): bool {
|
|
|
|
|
if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
|
|
|
|
|
return $this->access;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$access = $this->manager->canAccessResource($this, $user);
|
|
|
|
|
if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
|
|
|
|
|
$this->access = $access;
|
|
|
|
|
}
|
|
|
|
|
return $access;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function canGuestAccess(): bool {
|
|
|
|
|
if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) {
|
|
|
|
|
return $this->access;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$access = $this->manager->canAccessResource($this, null);
|
|
|
|
|
if (!$this->userForAccess instanceof IUser) {
|
|
|
|
|
$this->access = $access;
|
2019-02-07 09:43:20 -05:00
|
|
|
}
|
2019-02-11 06:57:38 -05:00
|
|
|
return $access;
|
2018-10-17 14:03:44 -04:00
|
|
|
}
|
|
|
|
|
|
2018-09-14 06:34:24 -04:00
|
|
|
/**
|
|
|
|
|
* @return ICollection[]
|
2019-02-07 10:04:15 -05:00
|
|
|
* @since 16.0.0
|
2018-09-14 06:34:24 -04:00
|
|
|
*/
|
2018-10-16 11:48:16 -04:00
|
|
|
public function getCollections(): array {
|
2018-09-14 06:34:24 -04:00
|
|
|
$collections = [];
|
|
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
|
|
$query->select('collection_id')
|
|
|
|
|
->from('collres_resources')
|
2018-10-16 11:48:16 -04:00
|
|
|
->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType())))
|
|
|
|
|
->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId())));
|
2018-09-14 06:34:24 -04:00
|
|
|
|
2025-08-26 08:24:48 -04:00
|
|
|
$result = $query->executeQuery();
|
2018-09-14 06:34:24 -04:00
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
|
$collections[] = $this->manager->getCollection((int)$row['collection_id']);
|
|
|
|
|
}
|
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
|
|
return $collections;
|
|
|
|
|
}
|
|
|
|
|
}
|