Passed
Pull Request — master (#1480)
by Julius
03:11
created

ResourceProviderCard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Collaboration\Resources;
25
26
27
use OCA\Deck\Db\Acl;
28
use OCA\Deck\Db\Board;
29
use OCA\Deck\Db\BoardMapper;
30
use OCA\Deck\Db\CardMapper;
31
use OCA\Deck\Service\PermissionService;
32
use OCP\AppFramework\Db\DoesNotExistException;
33
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
34
use OCP\AppFramework\QueryException;
35
use OCP\Collaboration\Resources\IManager;
36
use OCP\Collaboration\Resources\IProvider;
37
use OCP\Collaboration\Resources\IResource;
38
use OCP\Collaboration\Resources\ResourceException;
39
use OCP\IURLGenerator;
40
use OCP\IUser;
41
42
class ResourceProviderCard implements IProvider {
43
44
	const RESOURCE_TYPE = 'deck-card';
45
46
	/** @var CardMapper */
47
	private $cardMapper;
48
49
	/** @var BoardMapper */
50
	private $boardMapper;
51
52
	/** @var PermissionService */
53
	private $permissionService;
54
55
	/** @var IURLGenerator */
56
	private $urlGenerator;
57
58
	/** @var array */
59
	protected $nodes = [];
60
61
	public function __construct(CardMapper $cardMapper, BoardMapper $boardMapper, PermissionService $permissionService, IURLGenerator $urlGenerator) {
62
		$this->cardMapper = $cardMapper;
63
		$this->boardMapper = $boardMapper;
64
		$this->permissionService = $permissionService;
65
		$this->urlGenerator = $urlGenerator;
66
	}
67
68
	/**
69
	 * Get the type of a resource
70
	 *
71
	 * @return string
72
	 * @since 15.0.0
73
	 */
74
	public function getType(): string {
75
		return self::RESOURCE_TYPE;
76
	}
77
78
	/**
79
	 * Get the rich object data of a resource
80
	 *
81
	 * @param IResource $resource
82
	 * @return array
83
	 * @since 16.0.0
84
	 */
85
	public function getResourceRichObject(IResource $resource): array {
86
		try {
87
			$card = $this->cardMapper->find($resource->getId());
88
			$board = $this->getBoard($resource->getId());
89
		} catch (DoesNotExistException $e) {
90
			throw new ResourceException('No card found for resource');
91
		} catch (MultipleObjectsReturnedException $e) {
92
			throw new ResourceException('No unique card found for resource, this should never happen');
93
		}
94
95
		$link = $this->urlGenerator->linkToRoute('deck.page.index') . '#/board/' . $board->getId() . '/cards/' . $resource->getId();
96
97
		return [
98
			'type' => self::RESOURCE_TYPE,
99
			'id' => $resource->getId(),
100
			'name' => $board->getTitle() . ': ' . $card->getTitle(),
101
			'link' => $link,
102
			'iconUrl' => $this->urlGenerator->imagePath('core', 'actions/toggle-pictures.svg')
103
		];
104
105
	}
106
107
	/**
108
	 * Can a user/guest access the collection
109
	 *
110
	 * @param IResource $resource
111
	 * @param IUser|null $user
112
	 * @return bool
113
	 * @since 16.0.0
114
	 */
115
	public function canAccessResource(IResource $resource, ?IUser $user): bool {
116
		if (!$user instanceof IUser || $resource->getType() !== self::RESOURCE_TYPE) {
117
			return false;
118
		}
119
		try {
120
			$board = $this->getBoard($resource->getId());
121
		} catch (DoesNotExistException $e) {
122
			return false;
123
		} catch (MultipleObjectsReturnedException $e) {
124
			return false;
125
		}
126
127
		if ($board === null) {
128
			return false;
129
		}
130
		if ($board->getOwner() === $user->getUID()) {
131
			return true;
132
		}
133
		return $this->permissionService->userCan($board->getAcl(), Acl::PERMISSION_READ, $user->getUID());
134
	}
135
136
	/**
137
	 * @param $cardId
138
	 * @return Board
139
	 * @throws DoesNotExistException
140
	 * @throws MultipleObjectsReturnedException
141
	 */
142
	private function getBoard($cardId) {
143
		$boardId = $this->cardMapper->findBoardId($cardId);
144
		return $this->boardMapper->find($boardId, false, true);
145
	}
146
147
	public function invalidateAccessCache($cardId = null) {
148
		try {
149
			/** @var IManager $resourceManager */
150
			$resourceManager = \OC::$server->query(IManager::class);
151
		} catch (QueryException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
152
		}
153
		if ($cardId !== null) {
154
			$resource = $resourceManager->getResourceForUser(self::RESOURCE_TYPE, (string)$cardId, null);
155
			$resourceManager->invalidateAccessCacheForResource($resource);
156
		} else {
157
			$resourceManager->invalidateAccessCacheForProvider($this);
158
		}
159
	}
160
}
161