Passed
Push — master ( a842fd...195d63 )
by Julius
03:43 queued 51s
created

ResourceProvider::invalidateAccessCache()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 4
nop 1
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\Service\PermissionService;
31
use OCP\AppFramework\Db\DoesNotExistException;
32
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
33
use OCP\AppFramework\QueryException;
34
use OCP\Collaboration\Resources\IManager;
35
use OCP\Collaboration\Resources\IProvider;
36
use OCP\Collaboration\Resources\IResource;
37
use OCP\Collaboration\Resources\ResourceException;
38
use OCP\IUser;
39
40
class ResourceProvider implements IProvider {
41
42
	const RESOURCE_TYPE = 'deck';
43
44
	private $boardMapper;
45
	private $permissionService;
46
47
	/** @var array */
48
	protected $nodes = [];
49
50
	public function __construct(BoardMapper $boardMapper, PermissionService $permissionService) {
51
		$this->boardMapper = $boardMapper;
52
		$this->permissionService = $permissionService;
53
	}
54
55
	/**
56
	 * Get the type of a resource
57
	 *
58
	 * @param IResource $resource
0 ignored issues
show
Bug introduced by
There is no parameter named $resource. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
59
	 * @return string
60
	 * @since 15.0.0
61
	 */
62
	public function getType(): string {
63
		return self::RESOURCE_TYPE;
64
	}
65
66
	/**
67
	 * Get the rich object data of a resource
68
	 *
69
	 * @param IResource $resource
70
	 * @return array
71
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
72
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
73
	 * @since 16.0.0
74
	 */
75
	public function getResourceRichObject(IResource $resource): array {
76
		$board = $this->getBoard($resource);
77
		$link = \OC::$server->getURLGenerator()->linkToRoute('deck.page.index') . '#!/board/' . $resource->getId();
78
79
		return [
80
			'type' => self::RESOURCE_TYPE,
81
			'id' => $resource->getId(),
82
			'name' => $board->getTitle(),
83
			'link' => $link,
84
			'iconUrl' => \OC::$server->getURLGenerator()->imagePath('deck', 'deck-dark.svg')
85
		];
86
87
	}
88
89
	/**
90
	 * Can a user/guest access the collection
91
	 *
92
	 * @param IResource $resource
93
	 * @param IUser|null $user
94
	 * @return bool
95
	 * @since 16.0.0
96
	 */
97
	public function canAccessResource(IResource $resource, ?IUser $user): bool {
98
		if ($resource->getType() !== self::RESOURCE_TYPE || !$user instanceof IUser) {
99
			return false;
100
		}
101
		$board = $this->getBoard($resource);
102
		if ($board === null) {
103
			return false;
104
		}
105
		if ($board->getOwner() === $user->getUID()) {
106
			return true;
107
		}
108
		return $this->permissionService->userCan($board->getAcl(), Acl::PERMISSION_READ, $user->getUID());
109
	}
110
111
	private function getBoard(IResource $resource) {
112
		try {
113
			return $this->boardMapper->find($resource->getId(), false, true);
114
		} catch (DoesNotExistException $e) {
115
		} catch (MultipleObjectsReturnedException $e) {
116
			return null;
117
		}
118
	}
119
120
	public function invalidateAccessCache($boardId = null) {
121
		try {
122
			/** @var IManager $resourceManager */
123
			$resourceManager = \OC::$server->query(IManager::class);
124
		} catch (QueryException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
125
		}
126
		if ($boardId !== null) {
127
			$resource = $resourceManager->getResourceForUser(self::RESOURCE_TYPE, (string)$boardId, null);
128
			$resourceManager->invalidateAccessCacheForResource($resource);
129
		} else {
130
			$resourceManager->invalidateAccessCacheForProvider($this);
131
		}
132
	}
133
}
134