1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018 Joas Schilling <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @license GNU AGPL version 3 or any later version |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OC\Collaboration\Resources; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
use OCP\Collaboration\Resources\ICollection; |
27
|
|
|
use OCP\Collaboration\Resources\IManager; |
28
|
|
|
use OCP\Collaboration\Resources\IResource; |
29
|
|
|
use OCP\IDBConnection; |
30
|
|
|
use OCP\IUser; |
31
|
|
|
|
32
|
|
|
class Resource implements IResource { |
33
|
|
|
|
34
|
|
|
/** @var IManager */ |
35
|
|
|
protected $manager; |
36
|
|
|
|
37
|
|
|
/** @var IDBConnection */ |
38
|
|
|
protected $connection; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $type; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
protected $id; |
45
|
|
|
|
46
|
|
|
/** @var IUser|null */ |
47
|
|
|
protected $userForAccess; |
48
|
|
|
|
49
|
|
|
/** @var bool|null */ |
50
|
|
|
protected $access; |
51
|
|
|
|
52
|
|
|
/** @var array|null */ |
53
|
|
|
protected $data; |
54
|
|
|
|
55
|
|
|
public function __construct( |
56
|
|
|
IManager $manager, |
57
|
|
|
IDBConnection $connection, |
58
|
|
|
string $type, |
59
|
|
|
string $id, |
60
|
|
|
?IUser $userForAccess = null, |
61
|
|
|
?bool $access = null |
62
|
|
|
) { |
63
|
|
|
$this->manager = $manager; |
64
|
|
|
$this->connection = $connection; |
65
|
|
|
$this->type = $type; |
66
|
|
|
$this->id = $id; |
67
|
|
|
$this->userForAccess = $userForAccess; |
68
|
|
|
$this->access = $access; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
* @since 16.0.0 |
74
|
|
|
*/ |
75
|
|
|
public function getType(): string { |
76
|
|
|
return $this->type; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
* @since 16.0.0 |
82
|
|
|
*/ |
83
|
|
|
public function getId(): string { |
84
|
|
|
return $this->id; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
* @since 16.0.0 |
90
|
|
|
*/ |
91
|
|
|
public function getRichObject(): array { |
92
|
|
|
if ($this->data === null) { |
93
|
|
|
$this->data = $this->manager->getResourceRichObject($this); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->data; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Can a user/guest access the resource |
101
|
|
|
* |
102
|
|
|
* @param IUser|null $user |
103
|
|
|
* @return bool |
104
|
|
|
* @since 16.0.0 |
105
|
|
|
*/ |
106
|
|
|
public function canAccess(?IUser $user): bool { |
107
|
|
|
if ($user instanceof IUser) { |
108
|
|
|
return $this->canUserAccess($user); |
109
|
|
|
} |
110
|
|
|
return $this->canGuestAccess(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function canUserAccess(IUser $user): bool { |
114
|
|
|
if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
115
|
|
|
return $this->access; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$access = $this->manager->canAccessResource($this, $user); |
119
|
|
|
if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
120
|
|
|
$this->access = $access; |
121
|
|
|
} |
122
|
|
|
return $access; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function canGuestAccess(): bool { |
126
|
|
|
if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { |
127
|
|
|
return $this->access; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$access = $this->manager->canAccessResource($this, null); |
131
|
|
|
if (!$this->userForAccess instanceof IUser) { |
132
|
|
|
$this->access = $access; |
133
|
|
|
} |
134
|
|
|
return $access; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return ICollection[] |
139
|
|
|
* @since 16.0.0 |
140
|
|
|
*/ |
141
|
|
|
public function getCollections(): array { |
142
|
|
|
$collections = []; |
143
|
|
|
|
144
|
|
|
$query = $this->connection->getQueryBuilder(); |
145
|
|
|
|
146
|
|
|
$query->select('collection_id') |
147
|
|
|
->from('collres_resources') |
148
|
|
|
->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType()))) |
149
|
|
|
->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId()))); |
150
|
|
|
|
151
|
|
|
$result = $query->execute(); |
152
|
|
|
while ($row = $result->fetch()) { |
153
|
|
|
$collections[] = $this->manager->getCollection((int) $row['collection_id']); |
154
|
|
|
} |
155
|
|
|
$result->closeCursor(); |
156
|
|
|
|
157
|
|
|
return $collections; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|