Passed
Push — master ( 222d9e...5d29a9 )
by Christoph
23:13 queued 05:51
created

AppCalendar::getSupportedPrivilegeSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @copyright 2023 Ferdinand Thiessen <[email protected]>
6
 *
7
 * @author Ferdinand Thiessen <[email protected]>
8
 *
9
 * @license AGPL-3.0-or-later
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\DAV\CalDAV\AppCalendar;
27
28
use OCA\DAV\CalDAV\Plugin;
29
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
30
use OCP\Calendar\ICalendar;
31
use OCP\Calendar\ICreateFromString;
32
use OCP\Constants;
33
use Sabre\CalDAV\CalendarQueryValidator;
34
use Sabre\CalDAV\ICalendarObject;
35
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
36
use Sabre\DAV\Exception\Forbidden;
37
use Sabre\DAV\Exception\NotFound;
38
use Sabre\DAV\PropPatch;
39
use Sabre\VObject\Component\VCalendar;
40
use Sabre\VObject\Reader;
41
42
class AppCalendar extends ExternalCalendar {
43
	protected string $principal;
44
	protected ICalendar $calendar;
45
46
	public function __construct(string $appId, ICalendar $calendar, string $principal) {
47
		parent::__construct($appId, $calendar->getUri());
48
		$this->principal = $principal;
49
		$this->calendar = $calendar;
50
	}
51
52
	/**
53
	 * Return permissions supported by the backend calendar
54
	 * @return int Permissions based on \OCP\Constants
55
	 */
56
	public function getPermissions(): int {
57
		// Make sure to only promote write support if the backend implement the correct interface
58
		if ($this->calendar instanceof ICreateFromString) {
59
			return $this->calendar->getPermissions();
60
		}
61
		return Constants::PERMISSION_READ;
62
	}
63
64
	public function getOwner(): ?string {
65
		return $this->principal;
66
	}
67
68
	public function getGroup(): ?string {
69
		return null;
70
	}
71
72
	public function getACL(): array {
73
		$acl = [
74
			[
75
				'privilege' => '{DAV:}read',
76
				'principal' => $this->getOwner(),
77
				'protected' => true,
78
			],
79
			[
80
				'privilege' => '{DAV:}write-properties',
81
				'principal' => $this->getOwner(),
82
				'protected' => true,
83
			]
84
		];
85
		if ($this->getPermissions() & Constants::PERMISSION_CREATE) {
86
			$acl[] = [
87
				'privilege' => '{DAV:}write',
88
				'principal' => $this->getOwner(),
89
				'protected' => true,
90
			];
91
		}
92
		return $acl;
93
	}
94
95
	public function setACL(array $acl): void {
96
		throw new Forbidden('Setting ACL is not supported on this node');
97
	}
98
99
	public function getSupportedPrivilegeSet(): ?array {
100
		// Use the default one
101
		return null;
102
	}
103
104
	public function getLastModified(): ?int {
105
		// unknown
106
		return null;
107
	}
108
109
	public function delete(): void {
110
		// No method for deleting a calendar in OCP\Calendar\ICalendar
111
		throw new Forbidden('Deleting an entry is not implemented');
112
	}
113
114
	public function createFile($name, $data = null) {
115
		if ($this->calendar instanceof ICreateFromString) {
116
			if (is_resource($data)) {
117
				$data = stream_get_contents($data) ?: null;
118
			}
119
			$this->calendar->createFromString($name, is_null($data) ? '' : $data);
0 ignored issues
show
Bug introduced by
The method createFromString() does not exist on OCP\Calendar\ICalendar. It seems like you code against a sub-type of said class. However, the method does not exist in OCP\Calendar\IHandleImipMessage. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
			$this->calendar->/** @scrutinizer ignore-call */ 
120
                    createFromString($name, is_null($data) ? '' : $data);
Loading history...
120
			return null;
121
		} else {
122
			throw new Forbidden('Creating a new entry is not allowed');
123
		}
124
	}
125
126
	public function getProperties($properties) {
127
		return [
128
			'{DAV:}displayname' => $this->calendar->getDisplayName() ?: $this->calendar->getKey(),
129
			'{http://apple.com/ns/ical/}calendar-color' => $this->calendar->getDisplayColor() ?: '#0082c9',
130
			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VEVENT', 'VJOURNAL', 'VTODO']),
131
		];
132
	}
133
134
	public function calendarQuery(array $filters) {
135
		$result = [];
136
		$objects = $this->getChildren();
137
138
		foreach ($objects as $object) {
139
			if ($this->validateFilterForObject($object, $filters)) {
140
				$result[] = $object->getName();
141
			}
142
		}
143
144
		return $result;
145
	}
146
147
	protected function validateFilterForObject(ICalendarObject $object, array $filters): bool {
148
		/** @var \Sabre\VObject\Component\VCalendar */
149
		$vObject = Reader::read($object->get());
150
151
		$validator = new CalendarQueryValidator();
152
		$result = $validator->validate($vObject, $filters);
153
154
		// Destroy circular references so PHP will GC the object.
155
		$vObject->destroy();
156
157
		return $result;
158
	}
159
160
	public function childExists($name): bool {
161
		try {
162
			$this->getChild($name);
163
			return true;
164
		} catch (NotFound $error) {
165
			return false;
166
		}
167
	}
168
169
	public function getChild($name) {
170
		// Try to get calendar by filename
171
		$children = $this->calendar->search($name, ['X-FILENAME']);
172
		if (count($children) === 0) {
173
			// If nothing found try to get by UID from filename
174
			$pos = strrpos($name, '.ics');
175
			$children = $this->calendar->search(substr($name, 0, $pos ?: null), ['UID']);
176
		}
177
178
		if (count($children) > 0) {
179
			return new CalendarObject($this, $this->calendar, new VCalendar($children));
180
		}
181
182
		throw new NotFound('Node not found');
183
	}
184
185
	/**
186
	 * @return ICalendarObject[]
187
	 */
188
	public function getChildren(): array {
189
		$objects = $this->calendar->search('');
190
		// We need to group by UID (actually by filename but we do not have that information)
191
		$result = [];
192
		foreach ($objects as $object) {
193
			$uid = (string)$object['UID'] ?: uniqid();
194
			if (!isset($result[$uid])) {
195
				$result[$uid] = [];
196
			}
197
			$result[$uid][] = $object;
198
		}
199
200
		return array_map(function (array $children) {
201
			return new CalendarObject($this, $this->calendar, new VCalendar($children));
202
		}, $result);
203
	}
204
205
	public function propPatch(PropPatch $propPatch): void {
206
		// no setDisplayColor or setDisplayName in \OCP\Calendar\ICalendar
207
	}
208
}
209