Completed
Push — master ( e1740c...d98dea )
by Morris
14:21
created

Calendar::delete()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 4
nop 0
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Georg Ehrke <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 * @author Thomas Citharel <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
namespace OCA\DAV\CalDAV;
28
29
use OCA\DAV\DAV\Sharing\IShareable;
30
use OCP\IConfig;
31
use OCP\IL10N;
32
use Sabre\CalDAV\Backend\BackendInterface;
33
use Sabre\DAV\Exception\Forbidden;
34
use Sabre\DAV\Exception\NotFound;
35
use Sabre\DAV\PropPatch;
36
37
/**
38
 * Class Calendar
39
 *
40
 * @package OCA\DAV\CalDAV
41
 * @property BackendInterface|CalDavBackend $caldavBackend
42
 */
43
class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
44
45
	/** @var IConfig */
46
	private $config;
47
48
	public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n, IConfig $config) {
49
		parent::__construct($caldavBackend, $calendarInfo);
50
51
		if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
52
			$this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays');
53
		}
54
		if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI &&
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
55
			$this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
56
			$this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal');
57
		}
58
59
		$this->config = $config;
60
	}
61
62
	/**
63
	 * Updates the list of shares.
64
	 *
65
	 * The first array is a list of people that are to be added to the
66
	 * resource.
67
	 *
68
	 * Every element in the add array has the following properties:
69
	 *   * href - A url. Usually a mailto: address
70
	 *   * commonName - Usually a first and last name, or false
71
	 *   * summary - A description of the share, can also be false
72
	 *   * readOnly - A boolean value
73
	 *
74
	 * Every element in the remove array is just the address string.
75
	 *
76
	 * @param array $add
77
	 * @param array $remove
78
	 * @return void
79
	 * @throws Forbidden
80
	 */
81
	public function updateShares(array $add, array $remove) {
82
		if ($this->isShared()) {
83
			throw new Forbidden();
84
		}
85
		$this->caldavBackend->updateShares($this, $add, $remove);
86
	}
87
88
	/**
89
	 * Returns the list of people whom this resource is shared with.
90
	 *
91
	 * Every element in this array should have the following properties:
92
	 *   * href - Often a mailto: address
93
	 *   * commonName - Optional, for example a first + last name
94
	 *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
95
	 *   * readOnly - boolean
96
	 *   * summary - Optional, a description for the share
97
	 *
98
	 * @return array
99
	 */
100
	public function getShares() {
101
		if ($this->isShared()) {
102
			return [];
103
		}
104
		return $this->caldavBackend->getShares($this->getResourceId());
105
	}
106
107
	/**
108
	 * @return int
109
	 */
110
	public function getResourceId() {
111
		return $this->calendarInfo['id'];
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117
	public function getPrincipalURI() {
118
		return $this->calendarInfo['principaluri'];
119
	}
120
121
	public function getACL() {
122
		$acl =  [
123
			[
124
				'privilege' => '{DAV:}read',
125
				'principal' => $this->getOwner(),
126
				'protected' => true,
127
			]];
128
		if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
129
			$acl[] = [
130
				'privilege' => '{DAV:}write',
131
				'principal' => $this->getOwner(),
132
				'protected' => true,
133
			];
134
		} else {
135
			$acl[] = [
136
				'privilege' => '{DAV:}write-properties',
137
				'principal' => $this->getOwner(),
138
				'protected' => true,
139
			];
140
		}
141
142
		if ($this->getOwner() !== parent::getOwner()) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getOwner() instead of getACL()). Are you sure this is correct? If so, you might want to change this to $this->getOwner().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
143
			$acl[] =  [
144
					'privilege' => '{DAV:}read',
145
					'principal' => parent::getOwner(),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getOwner() instead of getACL()). Are you sure this is correct? If so, you might want to change this to $this->getOwner().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
146
					'protected' => true,
147
				];
148 View Code Duplication
			if ($this->canWrite()) {
149
				$acl[] = [
150
					'privilege' => '{DAV:}write',
151
					'principal' => parent::getOwner(),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getOwner() instead of getACL()). Are you sure this is correct? If so, you might want to change this to $this->getOwner().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
152
					'protected' => true,
153
				];
154
			} else {
155
				$acl[] = [
156
					'privilege' => '{DAV:}write-properties',
157
					'principal' => parent::getOwner(),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getOwner() instead of getACL()). Are you sure this is correct? If so, you might want to change this to $this->getOwner().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
158
					'protected' => true,
159
				];
160
			}
161
		}
162
		if ($this->isPublic()) {
163
			$acl[] = [
164
				'privilege' => '{DAV:}read',
165
				'principal' => 'principals/system/public',
166
				'protected' => true,
167
			];
168
		}
169
170
		$acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl);
171
172
		if (!$this->isShared()) {
173
			return $acl;
174
		}
175
176
		$allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/public'];
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getOwner() instead of getACL()). Are you sure this is correct? If so, you might want to change this to $this->getOwner().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
177
		return array_filter($acl, function($rule) use ($allowedPrincipals) {
178
			return in_array($rule['principal'], $allowedPrincipals);
179
		});
180
	}
181
182
	public function getChildACL() {
183
		return $this->getACL();
184
	}
185
186
	public function getOwner() {
187
		if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
188
			return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
189
		}
190
		return parent::getOwner();
191
	}
192
193
	public function delete() {
194
		if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) &&
195
			$this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) {
196
			$principal = 'principal:' . parent::getOwner();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getOwner() instead of delete()). Are you sure this is correct? If so, you might want to change this to $this->getOwner().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
197
			$shares = $this->caldavBackend->getShares($this->getResourceId());
198
			$shares = array_filter($shares, function($share) use ($principal){
199
				return $share['href'] === $principal;
200
			});
201
			if (empty($shares)) {
202
				throw new Forbidden();
203
			}
204
205
			$this->caldavBackend->updateShares($this, [], [
206
				'href' => $principal
207
			]);
208
			return;
209
		}
210
211
		// Remember when a user deleted their birthday calendar
212
		// in order to not regenerate it on the next contacts change
213
		if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
214
			$principalURI = $this->getPrincipalURI();
215
			$userId = substr($principalURI, 17);
216
217
			$this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'no');
218
		}
219
220
		parent::delete();
221
	}
222
223
	public function propPatch(PropPatch $propPatch) {
224
		// parent::propPatch will only update calendars table
225
		// if calendar is shared, changes have to be made to the properties table
226
		if (!$this->isShared()) {
227
			parent::propPatch($propPatch);
228
		}
229
	}
230
231 View Code Duplication
	public function getChild($name) {
232
233
		$obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
234
235
		if (!$obj) {
236
			throw new NotFound('Calendar object not found');
237
		}
238
239
		if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
240
			throw new NotFound('Calendar object not found');
241
		}
242
243
		$obj['acl'] = $this->getChildACL();
244
245
		return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
246
247
	}
248
249 View Code Duplication
	public function getChildren() {
250
251
		$objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
252
		$children = [];
253
		foreach ($objs as $obj) {
254
			if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
255
				continue;
256
			}
257
			$obj['acl'] = $this->getChildACL();
258
			$children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
259
		}
260
		return $children;
261
262
	}
263
264 View Code Duplication
	public function getMultipleChildren(array $paths) {
265
266
		$objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
267
		$children = [];
268
		foreach ($objs as $obj) {
269
			if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
270
				continue;
271
			}
272
			$obj['acl'] = $this->getChildACL();
273
			$children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
274
		}
275
		return $children;
276
277
	}
278
279
	public function childExists($name) {
280
		$obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
281
		if (!$obj) {
282
			return false;
283
		}
284
		if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
285
			return false;
286
		}
287
288
		return true;
289
	}
290
291
	public function calendarQuery(array $filters) {
292
293
		$uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
294
		if ($this->isShared()) {
295
			return array_filter($uris, function ($uri) {
296
				return $this->childExists($uri);
297
			});
298
		}
299
300
		return $uris;
301
	}
302
303
	/**
304
	 * @param boolean $value
305
	 * @return string|null
306
	 */
307
	public function setPublishStatus($value) {
308
		$publicUri = $this->caldavBackend->setPublishStatus($value, $this);
309
		$this->calendarInfo['publicuri'] = $publicUri;
310
		return $publicUri;
311
	}
312
313
	/**
314
	 * @return mixed $value
315
	 */
316
	public function getPublishStatus() {
317
		return $this->caldavBackend->getPublishStatus($this);
318
	}
319
320
	private function canWrite() {
321
		if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
322
			return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
323
		}
324
		return true;
325
	}
326
327
	private function isPublic() {
328
		return isset($this->calendarInfo['{http://owncloud.org/ns}public']);
329
	}
330
331 View Code Duplication
	protected function isShared() {
332
		if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
333
			return false;
334
		}
335
336
		return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
337
	}
338
339
	public function isSubscription() {
340
		return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']);
341
	}
342
343
}
344